Class: Cisco::Vlan

Inherits:
VlanDeprecated show all
Defined in:
lib/cisco_node_utils/vlan.rb

Overview

Vlan - node utility class for VLAN configuration management

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VlanDeprecated

#default_private_vlan_type, #deprecation_warning, #private_vlan_association, #private_vlan_association=, #private_vlan_type, #private_vlan_type=

Methods inherited from NodeUtil

#client, client, #config_get, config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, node, #node, platform, #platform, supports?, #supports?

Constructor Details

#initialize(vlan_id, instantiate = true) ⇒ Vlan


32
33
34
35
36
37
38
# File 'lib/cisco_node_utils/vlan.rb', line 32

def initialize(vlan_id, instantiate=true)
  @vlan_id = vlan_id.to_s
  fail ArgumentError,
       'Invalid value(non-numeric Vlan id)' unless @vlan_id[/^\d+$/]

  create if instantiate
end

Instance Attribute Details

#vlan_idObject (readonly)

Returns the value of attribute vlan_id


30
31
32
# File 'lib/cisco_node_utils/vlan.rb', line 30

def vlan_id
  @vlan_id
end

Class Method Details

.vlansObject


44
45
46
47
48
49
50
51
52
53
# File 'lib/cisco_node_utils/vlan.rb', line 44

def self.vlans
  hash = {}
  vlan_list = config_get('vlan', 'all_vlans')
  return hash if vlan_list.nil?

  vlan_list.each do |id|
    hash[id.to_s] = Vlan.new(id, false)
  end
  hash
end

Instance Method Details

#add_interface(interface) ⇒ Object


199
200
201
# File 'lib/cisco_node_utils/vlan.rb', line 199

def add_interface(interface)
  interface.access_vlan = @vlan_id
end

#association_delta(is, should) ⇒ Object


association_delta is a helper function for the pvlan_association property. It walks the delta hash and adds/removes each target private vlan.


289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/cisco_node_utils/vlan.rb', line 289

def association_delta(is, should)
  delta_hash = Utils.delta_add_remove(should, is)
  Cisco::Logger.debug("association_delta: #{@vlan_id}: #{delta_hash}")
  [:add, :remove].each do |action|
    delta_hash[action].each do |vlans|
      state = (action == :add) ? '' : 'no'
      set_args_keys(state: state, vlans: vlans)
      result = config_set('vlan', 'pvlan_association', @set_args)
      cli_error_check(result)
    end
  end
end

#cli_error_check(result, ignore_message = nil) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cisco_node_utils/vlan.rb', line 63

def cli_error_check(result, ignore_message=nil)
  # The NXOS vlan cli does not raise an exception in some conditions and
  # instead just displays a STDOUT error message; thus NXAPI does not detect
  # the failure and we must catch it by inspecting the "body" hash entry
  # returned by NXAPI. This vlan cli behavior is unlikely to change.
  # Check for messages that can be safely ignored.

  errors = /(ERROR:|VLAN:|Warning:)/

  return unless
    result[2].is_a?(Hash) && errors.match(result[2]['body'].to_s)
  # Split errors into a list, but keep the delimiter as part of the message.
  error_list =
    (result[2]['body'].split(errors) - ['']).each_slice(2).map(&:join)
  error_list.each do |msg|
    next if ignore_message && msg.to_s.include?(ignore_message)
    fail Cisco::CliError, result[2]['body']
  end
end

#createObject


55
56
57
# File 'lib/cisco_node_utils/vlan.rb', line 55

def create
  config_set('vlan', 'create', @vlan_id)
end

#default_fabric_controlObject


104
105
106
# File 'lib/cisco_node_utils/vlan.rb', line 104

def default_fabric_control
  config_get_default('vlan', 'fabric_control')
end

#default_mapped_vniObject


239
240
241
# File 'lib/cisco_node_utils/vlan.rb', line 239

def default_mapped_vni
  config_get_default('vlan', 'mapped_vni')
end

#default_modeObject


139
140
141
# File 'lib/cisco_node_utils/vlan.rb', line 139

def default_mode
  config_get_default('vlan', 'mode')
end

#default_private_vlan_associationObject


281
282
283
# File 'lib/cisco_node_utils/vlan.rb', line 281

def default_private_vlan_association
  config_get_default('vlan', 'pvlan_association')
end

#default_pvlan_typeObject


264
265
266
# File 'lib/cisco_node_utils/vlan.rb', line 264

def default_pvlan_type
  config_get_default('vlan', 'pvlan_type')
end

#default_shutdownObject


195
196
197
# File 'lib/cisco_node_utils/vlan.rb', line 195

def default_shutdown
  config_get_default('vlan', 'shutdown')
end

#default_stateObject


181
182
183
# File 'lib/cisco_node_utils/vlan.rb', line 181

def default_state
  config_get_default('vlan', 'state')
end

#default_vlan_nameObject


157
158
159
# File 'lib/cisco_node_utils/vlan.rb', line 157

def default_vlan_name
  sprintf('VLAN%04d', @vlan_id)
end

#destroyObject


59
60
61
# File 'lib/cisco_node_utils/vlan.rb', line 59

def destroy
  config_set('vlan', 'destroy', @vlan_id)
end

#fabric_controlObject


93
94
95
# File 'lib/cisco_node_utils/vlan.rb', line 93

def fabric_control
  config_get('vlan', 'fabric_control', vlan: @vlan_id)
end

#fabric_control=(val) ⇒ Object


97
98
99
100
101
102
# File 'lib/cisco_node_utils/vlan.rb', line 97

def fabric_control=(val)
  no_cmd = (val) ? '' : 'no'
  result = config_set('vlan', 'fabric_control', vlan:  @vlan_id,
                                                state: no_cmd)
  cli_error_check(result)
end

#fabricpath_featureObject


108
109
110
# File 'lib/cisco_node_utils/vlan.rb', line 108

def fabricpath_feature
  FabricpathGlobal.fabricpath_feature
end

#fabricpath_feature_set(fabricpath_set) ⇒ Object


112
113
114
# File 'lib/cisco_node_utils/vlan.rb', line 112

def fabricpath_feature_set(fabricpath_set)
  FabricpathGlobal.fabricpath_feature_set(fabricpath_set)
end

#interfacesObject


207
208
209
210
211
212
213
214
215
216
# File 'lib/cisco_node_utils/vlan.rb', line 207

def interfaces
  all_interfaces = Interface.interfaces
  interfaces = {}
  all_interfaces.each do |name, i|
    next unless i.switchport_mode == :access
    next unless i.access_vlan.to_i == @vlan_id.to_i
    interfaces[name] = i
  end
  interfaces
end

#mapped_vniObject


218
219
220
# File 'lib/cisco_node_utils/vlan.rb', line 218

def mapped_vni
  config_get('vlan', 'mapped_vni', vlan: @vlan_id)
end

#mapped_vni=(vni) ⇒ Object


226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/cisco_node_utils/vlan.rb', line 226

def mapped_vni=(vni)
  # Some platforms require feature nv_overlay to be enabled first.
  Feature.nv_overlay_enable if requires_nv_overlay?
  Feature.vn_segment_vlan_based_enable
  # Remove the existing mapping first as cli doesn't support overwriting.
  config_set('vlan', 'mapped_vni', vlan: @vlan_id,
                     state: 'no', vni: vni) unless mapped_vni.to_s.empty?
  # Configure the new mapping
  state = vni == default_mapped_vni ? 'no' : ''
  config_set('vlan', 'mapped_vni', vlan: @vlan_id,
                      state: state, vni: vni)
end

#modeObject


116
117
118
119
120
121
122
123
124
125
# File 'lib/cisco_node_utils/vlan.rb', line 116

def mode
  result = config_get('vlan', 'mode', @vlan_id)
  return default_mode if result.nil?
  # Note: The yaml definition for this property
  # uses 'multiple' as a workaround for a bug
  # in the N7k nxapi code which displays
  # the 'show vlan' output twice.
  result[0].downcase! if result[0][/FABRICPATH/]
  result[0]
end

#mode=(str) ⇒ Object


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cisco_node_utils/vlan.rb', line 127

def mode=(str)
  if str == default_mode
    config_set('vlan', 'mode', @vlan_id, 'no', '')
  else
    if 'fabricpath' == str
      fabricpath_feature_set(:enabled) unless
        :enabled == fabricpath_feature
    end
    config_set('vlan', 'mode', @vlan_id, '', str)
  end
end

#pvlan_associationObject


268
269
270
271
272
# File 'lib/cisco_node_utils/vlan.rb', line 268

def pvlan_association
  return nil unless Feature.private_vlan_enabled?
  range = config_get('vlan', 'pvlan_association', id: @vlan_id)
  Utils.normalize_range_array(range)
end

#pvlan_association=(range) ⇒ Object


274
275
276
277
278
279
# File 'lib/cisco_node_utils/vlan.rb', line 274

def pvlan_association=(range)
  Feature.private_vlan_enable
  is = Utils.dash_range_to_elements(pvlan_association)
  should = Utils.dash_range_to_elements(range)
  association_delta(is, should)
end

#pvlan_typeObject


243
244
245
246
# File 'lib/cisco_node_utils/vlan.rb', line 243

def pvlan_type
  return nil unless Feature.private_vlan_enabled?
  config_get('vlan', 'pvlan_type', id: @vlan_id)
end

#pvlan_type=(type) ⇒ Object


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/cisco_node_utils/vlan.rb', line 248

def pvlan_type=(type)
  Feature.private_vlan_enable
  fail TypeError unless type && type.is_a?(String)

  if type == default_pvlan_type
    return if pvlan_type.empty?
    set_args_keys(state: 'no', type: pvlan_type)
    ignore_msg = 'Warning: Private-VLAN CLI removed'
  else
    set_args_keys(state: '', type: type)
    ignore_msg = 'Warning: Private-VLAN CLI entered'
  end
  result = config_set('vlan', 'pvlan_type', @set_args)
  cli_error_check(result, ignore_msg)
end

#remove_interface(interface) ⇒ Object


203
204
205
# File 'lib/cisco_node_utils/vlan.rb', line 203

def remove_interface(interface)
  interface.access_vlan = interface.default_access_vlan
end

#requires_nv_overlay?Boolean


222
223
224
# File 'lib/cisco_node_utils/vlan.rb', line 222

def requires_nv_overlay?
  config_get('vlan', 'mapped_vni_requires_nv_overlay')
end

#set_args_keys(hash = {}) ⇒ Object

rubocop:disable Style/AccessorMethodName


88
89
90
91
# File 'lib/cisco_node_utils/vlan.rb', line 88

def set_args_keys(hash={}) # rubocop:disable Style/AccessorMethodName
  set_args_keys_default
  @set_args = @set_args.merge!(hash) unless hash.empty?
end

#set_args_keys_defaultObject


83
84
85
86
# File 'lib/cisco_node_utils/vlan.rb', line 83

def set_args_keys_default
  keys = { vlan: @vlan_id }
  @set_args = keys
end

#shutdownObject


185
186
187
# File 'lib/cisco_node_utils/vlan.rb', line 185

def shutdown
  config_get('vlan', 'shutdown', vlanid: @vlan_id)
end

#shutdown=(val) ⇒ Object


189
190
191
192
193
# File 'lib/cisco_node_utils/vlan.rb', line 189

def shutdown=(val)
  no_cmd = (val) ? '' : 'no'
  result = config_set('vlan', 'shutdown', @vlan_id, no_cmd)
  cli_error_check(result)
end

#stateObject


161
162
163
164
165
166
167
168
169
# File 'lib/cisco_node_utils/vlan.rb', line 161

def state
  result = config_get('vlan', 'state', @vlan_id)
  case result
  when /act/
    return 'active'
  when /sus/
    return 'suspend'
  end
end

#state=(str) ⇒ Object


171
172
173
174
175
176
177
178
179
# File 'lib/cisco_node_utils/vlan.rb', line 171

def state=(str)
  str = str.to_s
  if str.empty?
    result = config_set('vlan', 'state', @vlan_id, 'no', '')
  else
    result = config_set('vlan', 'state', @vlan_id, '', str)
  end
  cli_error_check(result)
end

#to_sObject


40
41
42
# File 'lib/cisco_node_utils/vlan.rb', line 40

def to_s
  "VLAN #{vlan_id}"
end

#vlan_nameObject


143
144
145
# File 'lib/cisco_node_utils/vlan.rb', line 143

def vlan_name
  config_get('vlan', 'name', vlanid: @vlan_id)
end

#vlan_name=(str) ⇒ Object


147
148
149
150
151
152
153
154
155
# File 'lib/cisco_node_utils/vlan.rb', line 147

def vlan_name=(str)
  fail TypeError unless str.is_a?(String)
  if str.empty?
    result = config_set('vlan', 'name', @vlan_id, 'no', '')
  else
    result = config_set('vlan', 'name', @vlan_id, '', str)
  end
  cli_error_check(result)
end