Class: Puppet::Provider::Junos::L2Interface

Inherits:
Puppet::Provider::Junos
  • Object
show all
Defined in:
lib/puppet/provider/junos/junos_l2_interface.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ac_ac_nountg(this, xml) ⇒ Object


The following are all the change transition functions for each of the use-cases




287
288
289
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 287

def ac_ac_nountg( this, xml )
  xml.vlan Netconf::JunosConfig::DELETE
end

.ac_ac_untg(this, xml) ⇒ Object



308
309
310
311
312
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 308

def ac_ac_untg( this, xml )
  xml.vlan( Netconf::JunosConfig::REPLACE ) {
    xml.members this.resource[:untagged_vlan]
  }            
end

.ac_tr_nountg(this, xml) ⇒ Object



291
292
293
294
295
296
297
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 291

def ac_tr_nountg( this, xml )      
  unless (untg_vlan = this.ndev_res[:tagged_vlans]).empty?
    xml.vlan {
      xml.members untg_vlan, Netconf::JunosConfig::DELETE
    }              
  end
end

.ac_tr_untg(this, xml) ⇒ Object



314
315
316
317
318
319
320
321
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 314

def ac_tr_untg( this, xml )      
  was_untg_vlan = this.ndev_res[:untagged_vlan]
  
  xml.vlan( Netconf::JunosConfig::REPLACE ) { 
    xml.members was_untg_vlan, Netconf::JunosConfig::DELETE if was_untg_vlan
  }
  xml.send :'native-vlan-id', this.resource[:untagged_vlan]              
end

.change_untagged_vlan(this, xml) ⇒ Object

invoke the correct method from the jump table based on the three criteria to select the action



277
278
279
280
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 277

def change_untagged_vlan( this, xml )
  proc = @@untgv_jmptbl[this.is_trunk?][this.should_trunk?][this.resource[:untagged_vlan].empty?]
  proc.call( this, xml )
end

.init_class_varsObject

initialize the jump table once as a class variable this is called from #init_resource



270
271
272
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 270

def init_class_vars                      
  @@untgv_jmptbl ||= initcvar_jmptbl_untagged_vlan      
end

.initcvar_jmptbl_untagged_vlanObject

creating some class definitions … this is a bit complicated because we need to handle port-mode change transitions; basically dealing with the fact that trunk ports use ‘native-vlan-id’ and access ports have a vlan member definition; i.e. they don’t use native-vlan-id, ugh. Rather than doing all this logic as if/then/else statements, I’ve opted to using a proc jump-table technique. Lessons learned from lots of embedded systems programming :-)



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 242

def initcvar_jmptbl_untagged_vlan
  
  # auto-hash table
  hash = Hash.new(&(p=lambda{|h,k| h[k] = Hash.new(&p)}))
  
  # ------------------------------------------------------------------
  # -   jump table for handling various untagged vlan change use-cases      
  # ------------------------------------------------------------------      
  # There are three criteria for selection:  
  # | is_trunk | will_trunk | no_untg |
  # ------------------------------------------------------------------
  # - will not have untagged vlan 
  hash[false][false][true] = self.method(:ac_ac_nountg)
  hash[false][true][true] = self.method(:ac_tr_nountg)
  hash[true][false][true] = self.method(:tr_ac_nountg)
  hash[true][true][true] = self.method(:tr_tr_nountg)
  # - will have untagged vlan 
  hash[false][false][false] = self.method(:ac_ac_untg)
  hash[false][true][false] = self.method(:ac_tr_untg)
  hash[true][false][false] = self.method(:tr_ac_untg)
  hash[true][true][false] = self.method(:tr_tr_untg)
  
  hash
end

.tr_ac_nountg(this, xml) ⇒ Object



299
300
301
302
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 299

def tr_ac_nountg( this, xml )
  xml.send :'native-vlan-id', Netconf::JunosConfig::DELETE              
  xml.vlan( Netconf::JunosConfig::DELETE ) if this.ndev_res[:tagged_vlans]
end

.tr_ac_untg(this, xml) ⇒ Object



323
324
325
326
327
328
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 323

def tr_ac_untg( this, xml )
  xml.send :'native-vlan-id', Netconf::JunosConfig::DELETE              
  xml.vlan( Netconf::JunosConfig::REPLACE ) {
    xml.members this.resource[:untagged_vlan]
  }            
end

.tr_tr_nountg(this, xml) ⇒ Object



304
305
306
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 304

def tr_tr_nountg( this, xml )
  xml.send :'native-vlan-id', Netconf::JunosConfig::DELETE              
end

.tr_tr_untg(this, xml) ⇒ Object



330
331
332
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 330

def tr_tr_untg( this, xml )
  xml.send :'native-vlan-id', this.resource[:untagged_vlan]              
end

Instance Method Details

#default_descriptionObject



90
91
92
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 90

def default_description
  "Puppet created netdev_l2_interface: #{resource[:name]}"
end

#init_resourceObject


called from #netdev_exists?




67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 67

def init_resource
  
  @ndev_res ||= NetdevJunos::Resource.new( self, "interfaces" )
  
  @ndev_res[:description] = ''
  @ndev_res[:vlan_tagging] = :disable
  @ndev_res[:untagged_vlan] = ''
  @ndev_res[:tagged_vlans] = []        
  
  resource[:description] ||= default_description
  resource[:tagged_vlans] = resource[:tagged_vlans].to_a || []     
  resource[:untagged_vlan] ||= ''     # if not set in manifest, it is nil   
  resource[:vlan_tagging] = :enable unless resource[:tagged_vlans].empty?    
  
  ndev_config = @ndev_res.getconfig
  
  return false unless (ifl_config = ndev_config.xpath('//interface/unit')[0])
  
  @ndev_res.set_active_state( ifl_config )  
  
  return ifl_config
end

#is_trunk?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 111

def is_trunk?
  @ndev_res[:vlan_tagging] == :enable
end

#mode_changed?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 119

def mode_changed?
  @ndev_res[:name].nil? or (resource[:vlan_tagging] != @ndev_res[:vlan_tagging])
end

#netdev_res_exists?Boolean


triggered from Provider #exists?


Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 47

def netdev_res_exists?
  
  self.class.init_class_vars    
      
  return false unless (ifl_config = init_resource)
  
  @ndev_res[:description] = ifl_config.xpath('description').text.chomp    
  fam_eth_cfg = ifl_config.xpath('family/ethernet-switching')      
  
  return false if fam_eth_cfg.empty?
  
  netdev_retrieve_fam_eth_info( fam_eth_cfg )
  
  return true
end

#netdev_resxml_edit(xml) ⇒ Object

override default ‘edit’ method to place ‘dot’ inside the family ethernet-switching stanza



142
143
144
145
146
147
148
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 142

def netdev_resxml_edit( xml )
  xml.family { 
    xml.send(:'ethernet-switching') {
      return xml
    }
  }
end

#netdev_resxml_top(xml) ⇒ Object

override default ‘top’ method to create the unit sub-interface



129
130
131
132
133
134
135
136
137
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 129

def netdev_resxml_top( xml ) 
  xml.interface {
    xml.name resource[:name]  
    xml.unit { 
      xml.name '0'  
      return xml
    }
  }
end

#netdev_retrieve_fam_eth_info(fam_eth_cfg) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 94

def netdev_retrieve_fam_eth_info( fam_eth_cfg )
  
  @ndev_res[:vlan_tagging] = fam_eth_cfg.xpath('port-mode').text.chomp == 'trunk' ? :enable : :disable
  
  # --- access port      
  
  if @ndev_res[:vlan_tagging] == :disable
    @ndev_res[:untagged_vlan] = fam_eth_cfg.xpath('vlan/members').text.chomp || ''
    return
  end
  
  # --- trunk port
  
  @ndev_res[:untagged_vlan] = fam_eth_cfg.xpath('native-vlan-id').text.chomp
  @ndev_res[:tagged_vlans] = fam_eth_cfg.xpath('vlan/members').collect { |v| v.text.chomp }    
end

#should_trunk?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 115

def should_trunk?
  resource[:vlan_tagging] == :enable
end

#upd_tagged_vlans(xml) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 190

def upd_tagged_vlans( xml )
      
  return unless should_trunk?
  
  should = resource[:tagged_vlans] || []
  
  if should.empty?
    xml.vlan Netconf::JunosConfig::DELETE
    return
 end
  
  has = @ndev_res[:tagged_vlans] || []    
  has = has.map(&:to_s)    
  should = should.map(&:to_s)    
  
  del = has - should
  add = should - has 
  
  if add or del
    Puppet.debug "#{resource[:name]}: Adding VLANS: [#{add.join(',')}]" unless add.empty?
    Puppet.debug "#{resource[:name]}: Deleting VLANS: [#{del.join(',')}]" unless del.empty?      
    xml.vlan {
      del.each { |v| xml.members v, Netconf::JunosConfig::DELETE }
      add.each { |v| xml.members v }
    }  
  end
end

#upd_untagged_vlan(xml) ⇒ Object



227
228
229
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 227

def upd_untagged_vlan( xml )
  self.class.change_untagged_vlan( self, xml )
end

#xml_change_description(xml) ⇒ Object

:description



154
155
156
157
158
159
160
161
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 154

def xml_change_description( xml )
  par = xml.instance_variable_get(:@parent)    
  
  Nokogiri::XML::Builder.with(par.at_xpath('ancestor::unit')) {
    |dot|        
    dot.description resource[:description]
  }
end

#xml_change_tagged_vlans(xml) ⇒ Object


XML:tagged_vlans




185
186
187
188
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 185

def xml_change_tagged_vlans( xml )  
  return if mode_changed?  
  upd_tagged_vlans( xml )
end

#xml_change_untagged_vlan(xml) ⇒ Object


XML:untagged_vlan




222
223
224
225
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 222

def xml_change_untagged_vlan( xml )           
  return if mode_changed?         
  upd_untagged_vlan( xml )
end

#xml_change_vlan_tagging(xml) ⇒ Object

:vlan_tagging



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/puppet/provider/junos/junos_l2_interface.rb', line 167

def xml_change_vlan_tagging( xml )
  
  port_mode = should_trunk? ? 'trunk' : 'access'
  xml.send(:'port-mode', port_mode )
  
  # when the vlan_tagging value changes then this method
  # will trigger updates to the untagged_vlan and tagged_vlans
  # resource values as well.
  
  upd_untagged_vlan( xml )
  upd_tagged_vlans( xml )
  
end