Class: Construqt::Flavour::Ciscian::DlinkDgs15xx::Dialect

Inherits:
Object
  • Object
show all
Defined in:
lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Dialect

Returns a new instance of Dialect.



172
173
174
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 172

def initialize(result)
  @result=result
end

Class Method Details

.nameObject



168
169
170
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 168

def self.name
  'dlink-dgs15xx'
end

Instance Method Details

#add_bond(bond) ⇒ Object



275
276
277
278
279
280
281
282
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 275

def add_bond(bond)
  bond.interfaces.each do |iface|
    @result.add("interface #{expand_device_name(iface)}", NestedSection) do |section|
      section.add("channel-group", ChannelGroupVerb).add({"{+channel}" => [bond.name[2..-1]]})
    end
  end
  self.add_device(bond, true)
end

#add_device(device, bond = false) ⇒ Object



264
265
266
267
268
269
270
271
272
273
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 264

def add_device(device, bond=false)
  @result.add("interface #{expand_device_name(device)}", NestedSection) do |section|
    section.add("switchport mode").add("trunk")
    unless bond
      section.add("flowcontrol").add("off")
      section.add("max-rcv-frame-size").add(device.delegate.mtu)
      section.add("snmp trap").add("link-status")
    end
  end
end

#add_host(host) ⇒ Object



180
181
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 180

def add_host(host)
end

#add_vlan(vlan) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 284

def add_vlan(vlan)
  @result.add("vlan #{vlan.delegate.vlan_id}", NestedSection) do |section|
    next unless vlan.delegate.description && !vlan.delegate.description.empty?
    throw "vlan name too long, max 32 chars" if vlan.delegate.description.length > 32
    section.add("name").add(vlan.delegate.description)
  end
  @result.add("interface vlan #{vlan.delegate.vlan_id}", NestedSection) do |section|
    if vlan.delegate.address
      if vlan.delegate.address.first_ipv4
        section.add("ip address").add(vlan.delegate.address.first_ipv4.to_string.upcase)
      elsif vlan.delegate.address.dhcpv4?
        section.add("ip address").add("dhcp-bootp")
      end
      if vlan.delegate.address.first_ipv6
        section.add("ipv6 address").add(vlan.delegate.address.first_ipv6.to_string.upcase)
      elsif vlan.delegate.address.dhcpv6?
        section.add("ipv6 address").add("dhcp-bootp")
      end
    end
  end

  vlan_id=vlan.delegate.vlan_id
  vlan.interfaces.each do |iface|
    @result.add("interface #{expand_device_name(iface)}", NestedSection) do |section|
      section.add("switchport trunk allowed vlan", Ciscian::RangeVerb).add(vlan_id)
      unless iface.template.is_tagged?(vlan_id)
        section.add("switchport trunk native vlan").add(vlan_id)
      end
    end
  end
end

#block_end?(line) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 176

def block_end?(line)
  ['end','exit'].include?(line.strip)
end

#clear_interface(line) ⇒ Object



183
184
185
186
187
188
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 183

def clear_interface(line)
  line.to_s.split(/\s+/).map do |i|
    split = /^([^0-9]+)([0-9].*)$/.match(i)
    split ? split[1..-1] : i
  end.flatten.join(' ')
end

#commitObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 243

def commit
  [
    "aaa",
    "service password-encryption",
    "no ip http server",
    "debug reboot on-error",
    "no debug enable"
  ].each do |verb|
    @result.add(verb, Ciscian::SingleValueVerb)
  end

  @result.add("snmp-server name").add(@result.host.name)
  @result.host.interfaces.values.each do |iface|
    next unless iface.delegate.address
    iface.delegate.address.routes.each do |route|
      ip = route.dst.ipv6? ? "ipv6" : "ip"
      @result.add("#{ip} route #{route.dst.to_string.upcase} vlan#{iface.delegate.vlan_id} #{route.via.to_s.upcase}", Ciscian::SingleValueVerb)
    end
  end
end

#expand_device_name(device) ⇒ Object



232
233
234
235
236
237
238
239
240
241
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 232

def expand_device_name(device)
  return device.delegate.dev_name if device.delegate.dev_name
  pattern = (({
                "po" => "port-channel %s",
                "ge" => "ethernet 1/0/%s",
                "te" => "ethernet 1/0/%s"
  })[device.name[0..1]])
  throw "device not expandable #{device.name}" unless pattern
  pattern%device.name[2..-1]
end

#is_virtual?(line) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 190

def is_virtual?(line)
  line.start_with?("vlan") || line.include?("port-channel")
end

#parse_line(line, lines, section, result) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 194

def parse_line(line, lines, section, result)
  [
    WtfEnd,
    ConfigureTerminal,
    Line,
    Comment,
    HostNameVerb,
    MtuVerb,
    SwitchPortTrunkAllowedVlan,
    ChannelGroupVerb,
    Ipv4RouteVerb,
    IpHttpServerVerb
  ].find do |i|
    i.parse_line(line, lines, section, result)
  end
end

#rate_higher(prefix, a, b) ⇒ Object



228
229
230
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 228

def rate_higher(prefix, a, b)
  return a.start_with?(prefix) ^ b.start_with?(prefix) ? (a.start_with?(prefix) ? -1 : 1) : 0
end

#sort_section_keys(keys) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb', line 211

def sort_section_keys(keys)
  return keys.sort do |a,b|
    a = a.to_s
    b = b.to_s
    match_a=/^(.*[^\d])(\d+)$/.match(a)||[nil,a,1]
    match_b=/^(.*[^\d])(\d+)$/.match(b)||[nil,b,1]
    #puts match_a, match_b, a, b
    ret=0
    ret = rate_higher("vlan", match_a[1], match_b[1]) if ret==0
    ret = rate_higher("interface port-channel", match_a[1], match_b[1]) if ret==0
    ret = rate_higher("interface vlan", match_a[1], match_b[1]) if ret==0
    ret = match_a[1]<=>match_b[1] if ret==0
    ret = match_a[2].to_i<=>match_b[2].to_i if ret==0
    ret
  end
end