Class: Cisco::Vlan

Inherits:
NodeUtil 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 NodeUtil

config_get, #config_get, config_get_default, #config_get_default, #config_set, config_set, #node, node, #show

Constructor Details

#initialize(vlan_id, instantiate = true) ⇒ Vlan

Returns a new instance of Vlan.



29
30
31
32
33
34
35
# File 'lib/cisco_node_utils/vlan.rb', line 29

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

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/cisco_node_utils/vlan.rb', line 27

def name
  @name
end

#vlan_idObject (readonly)

Returns the value of attribute vlan_id.



27
28
29
# File 'lib/cisco_node_utils/vlan.rb', line 27

def vlan_id
  @vlan_id
end

Class Method Details

.vlansObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/cisco_node_utils/vlan.rb', line 37

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

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

Instance Method Details

#add_interface(interface) ⇒ Object



175
176
177
# File 'lib/cisco_node_utils/vlan.rb', line 175

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

#cli_error_check(result) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cisco_node_utils/vlan.rb', line 56

def cli_error_check(result)
  # 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.
  fail result[2]['body'] if
    result[2].is_a?(Hash) &&
    /(ERROR:|Warning:)/.match(result[2]['body'].to_s)

  # Some test environments get result[2] as a string instead of a hash
  fail result[2] if
    result[2].is_a?(String) &&
    /(ERROR:|Warning:)/.match(result[2])
end

#createObject



48
49
50
# File 'lib/cisco_node_utils/vlan.rb', line 48

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

#default_mapped_vniObject



209
210
211
# File 'lib/cisco_node_utils/vlan.rb', line 209

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

#default_modeObject



106
107
108
# File 'lib/cisco_node_utils/vlan.rb', line 106

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

#default_shutdownObject



171
172
173
# File 'lib/cisco_node_utils/vlan.rb', line 171

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

#default_stateObject



153
154
155
# File 'lib/cisco_node_utils/vlan.rb', line 153

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

#default_vlan_nameObject



127
128
129
# File 'lib/cisco_node_utils/vlan.rb', line 127

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

#destroyObject



52
53
54
# File 'lib/cisco_node_utils/vlan.rb', line 52

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

#fabricpath_featureObject



71
72
73
# File 'lib/cisco_node_utils/vlan.rb', line 71

def fabricpath_feature
  FabricpathGlobal.fabricpath_feature
end

#fabricpath_feature_set(fabricpath_set) ⇒ Object



75
76
77
# File 'lib/cisco_node_utils/vlan.rb', line 75

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

#interfacesObject



183
184
185
186
187
188
189
190
191
192
# File 'lib/cisco_node_utils/vlan.rb', line 183

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

#mapped_vniObject



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

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

#mapped_vni=(vni) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/cisco_node_utils/vlan.rb', line 198

def mapped_vni=(vni)
  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)
  # Configure the new mapping
  state = vni == default_mapped_vni ? 'no' : ''
  config_set('vlan', 'mapped_vni', vlan: @vlan_id,
                      state: state, vni: vni)
end

#modeObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/cisco_node_utils/vlan.rb', line 79

def mode
  result = config_get('vlan', 'mode', @vlan_id)
  return default_mode if result.nil?
  case result
  when /fabricpath/i
    return 'fabricpath'
  when /ce/i
    return 'ce'
  end
end

#mode=(str) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cisco_node_utils/vlan.rb', line 90

def mode=(str)
  str = str.to_s
  if str.empty?
    result = config_set('vlan', 'mode', @vlan_id, 'no', '')
  else
    if 'fabricpath' == str
      fabricpath_feature_set(:enabled) unless
        :enabled == fabricpath_feature
    end
    result = config_set('vlan', 'mode', @vlan_id, '', str)
  end
  cli_error_check(result)
rescue CliError => e
  raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
end

#remove_interface(interface) ⇒ Object



179
180
181
# File 'lib/cisco_node_utils/vlan.rb', line 179

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

#shutdownObject



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

def shutdown
  result = config_get('vlan', 'shutdown', @vlan_id)
  # Valid result is either: "active"(aka no shutdown) or "shutdown"
  result[/shut/] ? true : false
end

#shutdown=(val) ⇒ Object



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

def shutdown=(val)
  no_cmd = (val) ? '' : 'no'
  result = config_set('vlan', 'shutdown', @vlan_id, no_cmd)
  cli_error_check(result)
rescue CliError => e
  raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
end

#stateObject



131
132
133
134
135
136
137
138
139
# File 'lib/cisco_node_utils/vlan.rb', line 131

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

#state=(str) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cisco_node_utils/vlan.rb', line 141

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)
rescue CliError => e
  raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
end

#vlan_nameObject



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

def vlan_name
  result = config_get('vlan', 'name', @vlan_id)
  result.nil? ? default_vlan_name : result
end

#vlan_name=(str) ⇒ Object



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

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)
rescue CliError => e
  raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
end