class Construqt::Flavour::Ciscian::DlinkDgs15xx::Dialect

Public Class Methods

name() click to toggle source
# File lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb, line 168
def self.name
  'dlink-dgs15xx'
end
new(result) click to toggle source
# File lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb, line 172
def initialize(result)
  @result=result
end

Public Instance Methods

add_bond(bond) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb, line 180
def add_host(host)
end
add_vlan(vlan) click to toggle source
# 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) click to toggle source
# File lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb, line 176
def block_end?(line)
  ['end','exit'].include?(line.strip)
end
clear_interface(line) click to toggle source
# 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
commit() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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