Class: Puppet::NetDev::CE::L3InterfaceApi

Inherits:
ApiBase
  • Object
show all
Defined in:
lib/puppet/provider/ce/api/l3_interface/l3_interface_api.rb

Instance Method Summary collapse

Constructor Details

#initializeL3InterfaceApi

Returns a new instance of L3InterfaceApi.



22
23
24
# File 'lib/puppet/provider/ce/api/l3_interface/l3_interface_api.rb', line 22

def initialize
  super()
end

Instance Method Details

#create_l3_interface(resource) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/puppet/provider/ce/api/l3_interface/l3_interface_api.rb', line 147

def create_l3_interface(resource)
  session = Puppet::NetDev::CE::Device.session

  create_l3_interface_xml = '<rpc><edit-config><target><running/></target><default-operation>merge</default-operation><error-option>rollback-on-error</error-option><config><ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"><interfaces><interface operation="create"><ifName>' + (resource[:name]).to_s + '</ifName></interface></interfaces></ifm></config></edit-config></rpc>'
  
  session.rpc.do_config(create_l3_interface_xml)
  
end

#delete_l3_interface(resource) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/puppet/provider/ce/api/l3_interface/l3_interface_api.rb', line 156

def delete_l3_interface(resource)
  session = Puppet::NetDev::CE::Device.session

  delete_l3_interface_xml = '<rpc><edit-config><target><running/></target><default-operation>merge</default-operation><error-option>rollback-on-error</error-option><config><ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"><interfaces><interface operation="delete"><ifName>' + (resource[:name]).to_s + '</ifName></interface></interfaces></ifm></config></edit-config></rpc>'
  
  session.rpc.do_config(delete_l3_interface_xml)
  
end

#get_l3_interfaceObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/puppet/provider/ce/api/l3_interface/l3_interface_api.rb', line 26

def get_l3_interface
  l3_interface_array = []
  session = Puppet::NetDev::CE::Device.session

  get_l3_interface_xml = '<rpc><get><filter type="subtree"><ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"><interfaces><interface><ifName></ifName><ifDescr></ifDescr><ifAdminStatus></ifAdminStatus><isL2SwitchPort></isL2SwitchPort><ifmAm4></ifmAm4></interface></interfaces></ifm></filter></get></rpc>'

  interface_all = session.rpc.do_config(get_l3_interface_xml)
  interface_elements = interface_all.first_element_child.first_element_child

  interface_elements.element_children.each do |interface_elem|
    interface_doc = Nokogiri::XML(interface_elem.to_s)

    interface_mode = interface_doc.xpath('/interface/isL2SwitchPort').text
    next if interface_mode == 'true'

    interface_name = interface_doc.xpath('/interface/ifName').text
    interface_des = interface_doc.xpath('/interface/ifDescr').text
    interface_enable = interface_doc.xpath('/interface/ifAdminStatus').text
    interface_ipaddress = nil

    ip_info = interface_doc.xpath('/interface/ifmAm4/am4CfgAddrs/am4CfgAddr')
    ip_info.each do |node|
      ip_node = Nokogiri::XML(node.to_s)
      add_type = ip_node.xpath('/am4CfgAddr/addrType').text

      next unless add_type == 'main'
      ipaddress = ip_node.xpath('/am4CfgAddr/ifIpAddr').text
      netmask = ip_node.xpath('/am4CfgAddr/subnetMask').text
      interface_ipaddress = ipaddress + ' ' + netmask
      break
    end

    property_hash = { ensure: :present }
    property_hash[:name] = interface_name

    property_hash[:description] = interface_des if interface_des

    if interface_enable == 'up'
      property_hash[:enable] = :true
    elsif interface_enable == 'down'
      property_hash[:enable] = :false
    end

    property_hash[:ipaddress] = if !interface_ipaddress.nil?
                                  interface_ipaddress
                                else
                                  'null'
                                end

    l3_interface_array << property_hash
  end

  l3_interface_array
end

#set_l3_interface(resource) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/puppet/provider/ce/api/l3_interface/l3_interface_api.rb', line 81

def set_l3_interface(resource)
  session = Puppet::NetDev::CE::Device.session

  set_interface_xml = '<rpc><edit-config><target><running/></target><default-operation>merge</default-operation><error-option>rollback-on-error</error-option><config><ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"><interfaces><interface operation="merge"><ifName>' + (resource[:name]).to_s + '</ifName>'

  if resource[:description]
    set_interface_xml += '<ifDescr>' + (resource[:description]).to_s + '</ifDescr>'
  end

  if resource[:enable] == :true
    set_interface_xml += '<ifAdminStatus>up</ifAdminStatus>'
  end

  if resource[:enable] == :false
    set_interface_xml += '<ifAdminStatus>down</ifAdminStatus>'
  end

  # e.g. '192.168.1.1 255.255.255.0'
  if resource[:ipaddress] && resource[:ipaddress] != 'null'
    ipadd_and_mask = resource[:ipaddress].split
    # ipadd_and_mask include IP address and netmask
    if ipadd_and_mask.count == 2
      ip_address = ipadd_and_mask[0]
      net_mask = ipadd_and_mask[1]
      set_interface_xml += '<ifmAm4><am4CfgAddrs><am4CfgAddr operation="merge"><ifIpAddr>' + ip_address.to_s + '</ifIpAddr><subnetMask>' + net_mask.to_s + '</subnetMask><addrType>main</addrType></am4CfgAddr></am4CfgAddrs></ifmAm4>'
    end
  end

  if resource[:ipaddress] == 'null'
    # check whether main IP address exists, if true, delete it.
    get_l3_interface_xml = '<rpc><get><filter type="subtree"><ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"><interfaces><interface><ifName>' + (resource[:name]).to_s + '</ifName><ifmAm4></ifmAm4></interface></interfaces></ifm></filter></get></rpc>'

    main_ipaddress_exist = false
    main_ipaddress = '0.0.0.0'

    interface_all = session.rpc.do_config(get_l3_interface_xml)
    interface_elements = interface_all.first_element_child.first_element_child

    interface_elements.element_children.each do |interface_elem|
      interface_doc = Nokogiri::XML(interface_elem.to_s)

      ip_info = interface_doc.xpath('/interface/ifmAm4/am4CfgAddrs/am4CfgAddr')
      ip_info.each do |node|
        ip_node = Nokogiri::XML(node.to_s)
        add_type = ip_node.xpath('/am4CfgAddr/addrType').text

        next unless add_type == 'main'
        ipaddress = ip_node.xpath('/am4CfgAddr/ifIpAddr').text
        netmask = ip_node.xpath('/am4CfgAddr/subnetMask').text
        main_ipaddress_exist = true
        main_ipaddress = ipaddress
        break
      end
    end

    if main_ipaddress_exist == true
      set_interface_xml += '<ifmAm4><am4CfgAddrs><am4CfgAddr operation="delete"><ifIpAddr>' + main_ipaddress + '</ifIpAddr></am4CfgAddr></am4CfgAddrs></ifmAm4>'
    end

  end

  set_interface_xml += '</interface></interfaces></ifm></config></edit-config></rpc>'

  session.rpc.do_config(set_interface_xml)
end