def create(name): cfg = util.load_config_from_file(name) l=[ obj.Struct(**node.parsing(i.strip())) for i in cfg.split('\n')] cfg_new = node.gen_graph(l) node.show_node(cfg_new,debug=True) device = model.DEVICE() for i in cfg_new: if i.TYPE==ast.AST.hostname: device.hostname = i.RGX[1]; elif i.TYPE==ast.AST.ip_default_gateway: device.default_gateway = i.RGX[1] elif i.TYPE==ast.AST.svi: mng = i.Node[0] device.mng_ip, device.mng_mask = mng.RGX[2], mng.RGX[3] device.mng_int_vlan = i.RGX[1] elif i.TYPE==ast.AST.interface: port = device.getport(int(i.RGX[1])) switchport_allowed_tagged, switchport_allowed_untagged, switchport_native, description = None, None, None, None for j in i.Node: if j.TYPE ==ast.AST.description: port.setdescription(j.RGX[1]) elif j.TYPE==ast.AST.switchport_allowed_tagged: port.addtagged( j.VLAN ) elif j.TYPE==ast.AST.switchport_allowed_untagged: port.adduntagged( j.VLAN ) elif j.TYPE==ast.AST.description: port.setdescription( j.RGX[1] ) elif j.TYPE==ast.AST.shutdown: port.setdown(); elif i.TYPE==ast.AST.vlan: if i.RGX[1] not in ['1','4093']: device.addvlan(model.VLAN(i.RGX[4],i.RGX[1])) elif i.TYPE==ast.AST.ip_igmp_snooping: if i.RGX[4] not in device.igmp_snooping: device.igmp_snooping.append(i.RGX[4]); elif i.TYPE==ast.AST.ip_dhcp_snooping: if i.RGX[4] not in device.dhcp_snooping: device.dhcp_snooping.append(i.RGX[4]);
def create(name): device = model.DEVICE() cfg = util.load_config_from_file(name) l = [obj.Struct(**node.parsing(i.strip())) for i in cfg.split('\n')] cfg_new = node.gen_graph(l) node.show_node(cfg_new) for i in cfg_new: if i.TYPE == ast.AST.hostname: device.hostname = i.RGX[1] elif i.TYPE == ast.AST.interface: for j in i.Node: if j.TYPE == ast.AST.name: port = device.getport(int(i.RGX[2])) port.setdescription(j.RGX[1]) if j.TYPE == ast.AST.inactive: port = device.getport(int(i.RGX[2])) port.setdown() elif i.TYPE == ast.AST.mvr: source_port, receiver_port, name, tagged = None, None, None, None vlan = model.VLAN(name, i.RGX[1]) device.addvlan(vlan) for j in i.Node: if j.TYPE == ast.AST.source_port: source_port = j.PORT elif j.TYPE == ast.AST.receiver_port: receiver_port = j.PORT elif j.TYPE == ast.AST.name: name = j.RGX[1] elif j.TYPE == ast.AST.tagged: tagged = j.PORT device.setmvr(model.MVR(i.RGX[1], name)) for v in source_port: port = device.getport(v) port.mvr.tag = i.RGX[1] port.mvr.receiver_port = True for v in receiver_port: port = device.getport(v) port.mvr.tag = i.RGX[1] port.mvr.source_port = True for v in tagged: port = device.getport(v) port.addtagged(i.RGX[1]) elif i.TYPE == ast.AST.vlan: fixed, forbidden, untagged, name = None, None, None, None for j in i.Node: if j.TYPE == ast.AST.fixed: fixed = j.PORT elif j.TYPE == ast.AST.forbidden: forbidden = j.PORT elif j.TYPE == ast.AST.untagged: untagged = j.PORT elif j.TYPE == ast.AST.name: name = j.RGX[1] elif j.TYPE == ast.AST.ip: if ast.default_management in j.RGX: device.mng_ip, device.mng_mask = j.RGX[3], j.RGX[4] device.mng_int_vlan = i.RGX[1] elif ast.default_gateway in j.RGX: device.default_gateway = j.RGX[3] device.addvlan(model.VLAN(name, i.RGX[1])) for k, _ in fixed.items(): port = device.getport(k) if k in untagged: port.adduntagged(i.RGX[1]) else: port.addtagged(i.RGX[1]) print("*" * 45) return device
def create(name): device = model.DEVICE() cfg = util.load_config_from_file(name) l = [obj.Struct(**node.parsing(i.strip())) for i in cfg.split('\n')] cfg = node.gen_graph(l) # node.show_node(cfg,debug=True); for i in cfg: if i.TYPE == ast.AST.hostname: device.hostname = i.RGX[1] elif i.TYPE == ast.AST.interface_range_ethernet: ports = node.parsing_interface(i.RGX[1]).PORT if len(i.Node) > 0: for j in i.Node: if j.TYPE != None: if j.TYPE == ast.AST.description: for h in ports: port = device.getport(int(h)) port.setdescription(j.RGX[1]) elif j.TYPE == ast.AST.switchport_trunk_allowed_vlan_add: for h in ports: port = device.getport(int(h)) port.addtagged(j.RGX[1]) elif j.TYPE == ast.AST.switchport_access: for h in ports: port = device.getport(int(h)) port.adduntagged(j.RGX[2]) elif j.TYPE == ast.AST.switchport_trunk_native_vlan: for h in ports: port = device.getport(int(h)) port.adduntagged(j.RGX[1]) elif j.TYPE == ast.AST.switchport_general_pvid: for h in ports: # print(" port",h," switchport_general_pvid",j.RGX); port = device.getport(int(h)) port.adduntagged(j.RGX[1]) port.addgeneral_pid(j.RGX[1]) elif j.TYPE == ast.AST.switchport_general_allowed_vlan_add: for h in ports: # print(" port",h," ast.AST.switchport_general",j.RGX); port = device.getport(int(h)) if len(j.RGX) == 2: port.addtagged(j.RGX[1]) port.addgeneral_tag(j.RGX[1]) elif len(j.RGX) == 3: port.adduntagged(j.RGX[1]) port.addgeneral_untag(j.RGX[1]) else: print( " port range", h, " ast.AST.switchport_general_allowed_vlan_add", j.RGX) elif i.TYPE == ast.AST.interface_ethernet: if len(i.Node) > 0: for j in i.Node: if j.TYPE != None: port = device.getport(int(i.RGX[2])) if j.TYPE == ast.AST.description: port.setdescription(j.RGX[1]) elif j.TYPE == ast.AST.switchport_general_allowed_vlan_add: if len(j.RGX) == 2: port = device.getport(int(i.RGX[2])) port.addtagged(j.RGX[1]) else: port = device.getport(int(i.RGX[2])) port.addtagged(j.RGX[2]) elif j.TYPE == ast.AST.switchport_general_pvid: port.adduntagged(j.RGX[1]) port.addgeneral_pid(j.RGX[1]) # print(" port",i.RGX[2]," switchport_general_pvid",j.RGX); elif j.TYPE == ast.AST.switchport_general: port = device.getport(int(i.RGX[2])) # print(" port",i.RGX[2]," ast.AST.switchport_general",j.RGX); if len(j.RGX) == 2: port = device.getport(int(i.RGX[2])) port.addtagged(j.RGX[1]) port.addgeneral_tag(j.RGX[1]) elif len(j.RGX) == 3: port.adduntagged(j.RGX[1]) port.addgeneral_tag(j.RGX[1]) else: print(" port", i.RGX[2], " ast.AST.switchport_general", j.RGX) elif i.TYPE == ast.AST.vlan_database: if len(i.Node) > 0: for j in i.Node: if j.TYPE == ast.AST.vlan: p = node.parsing_port(j.RGX[1]).VLAN for _, i in p.items(): device.addvlan(model.VLAN("", i)) elif i.TYPE == ast.AST.interface_vlan: if len(i.Node) > 0: for j in i.Node: if j.TYPE != None: if j.TYPE == ast.AST.name: device.setvlanname(i.RGX[1], j.RGX[1]) elif j.TYPE == ast.AST.ip_address: device.mng_ip, device.mng_mask = j.RGX[1], j.RGX[2] device.mng_int_vlan = i.RGX[1] elif j.TYPE == ast.AST.ip_igmp_snooping: if i.RGX[1] not in device.igmp_snooping: device.igmp_snooping.append(i.RGX[1]) else: print(" ", j.__dict__) elif i.TYPE == ast.AST.ip_dhcp_snooping: if i.RGX[2] not in device.dhcp_snooping: device.dhcp_snooping.append(i.RGX[2]) elif i.TYPE == ast.AST.ip_default_gateway: device.default_gateway = i.RGX[1] return device