def get_interface_mdn_exist_config(self): """Get lldp existed configure""" lldp_config = list() lldp_dict = dict() conf_enable_str = CE_NC_GET_GLOBAL_LLDPENABLE_CONFIG conf_enable_obj = get_nc_config(self.module, conf_enable_str) xml_enable_str = conf_enable_obj.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get lldp enable config info root_enable = ElementTree.fromstring(xml_enable_str) ntpsite_enable = root_enable.findall("lldp/lldpSys") for nexthop_enable in ntpsite_enable: for ele_enable in nexthop_enable: if ele_enable.tag in ["lldpEnable"]: lldp_dict[ele_enable.tag] = ele_enable.text if self.state == "present": if lldp_dict['lldpEnable'] == 'enabled': self.enable_flag = 1 lldp_config.append(dict(lldpenable=lldp_dict['lldpEnable'])) if self.enable_flag == 1: conf_str = CE_NC_GET_INTERFACE_MDNENABLE_CONFIG conf_obj = get_nc_config(self.module, conf_str) if "<data/>" in conf_obj: return lldp_config xml_str = conf_obj.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get all ntp config info root = ElementTree.fromstring(xml_str) ntpsite = root.findall("lldp/mdnInterfaces/mdnInterface") for nexthop in ntpsite: for ele in nexthop: if ele.tag in ["ifName", "mdnStatus"]: lldp_dict[ele.tag] = ele.text if self.state == "present": cur_interface_mdn_cfg = dict( ifname=lldp_dict['ifName'], mdnstatus=lldp_dict['mdnStatus']) exp_interface_mdn_cfg = dict(ifname=self.ifname, mdnstatus=self.mdnstatus) if self.ifname == lldp_dict['ifName']: if cur_interface_mdn_cfg != exp_interface_mdn_cfg: self.conf_exsit = True lldp_config.append( dict(ifname=lldp_dict['ifName'], mdnstatus=lldp_dict['mdnStatus'])) return lldp_config lldp_config.append( dict(ifname=lldp_dict['ifName'], mdnstatus=lldp_dict['mdnStatus'])) return lldp_config
def get_scp_enable(self): """Get scp enable state""" ret_xml = '' try: ret_xml = get_nc_config(self.module, CE_NC_GET_SCP_ENABLE) except ConnectionError: self.module.fail_json( msg='Error: The NETCONF API of scp_enable is not supported.') if "<data/>" in ret_xml: return False xml_str = ret_xml.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get file info root = ElementTree.fromstring(xml_str) topo1 = root.find("sshs/sshServer/scpEnable") topo2 = root.find("sshs/sshServerEnable/scpIpv4Enable") topo3 = root.find("sshs/sshServerEnable/scpIpv6Enable") if topo1 is not None: return str(topo1.text).strip().lower() == 'enable' elif self.host_is_ipv6 and topo3 is not None: return str(topo3.text).strip().lower() == 'enable' elif topo2 is not None: return str(topo2.text).strip().lower() == 'enable' return False
def remote_file_exists(self, dst, file_system='flash:'): """Remote file whether exists""" full_path = file_system + dst file_name = os.path.basename(full_path) file_path = os.path.dirname(full_path) file_path = file_path + '/' xml_str = CE_NC_GET_FILE_INFO % (file_name, file_path) ret_xml = get_nc_config(self.module, xml_str) if "<data/>" in ret_xml: return False, 0 xml_str = ret_xml.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get file info root = ElementTree.fromstring(xml_str) topo = root.find("vfm/dirs/dir") if topo is None: return False, 0 for eles in topo: if eles.tag in ["DirSize"]: return True, int(eles.text.replace(',', '')) return False, 0
def get_mlag_error_down_info(self): """ get error down info.""" mlag_error_down_info = dict() conf_str = CE_NC_GET_MLAG_ERROR_DOWN_INFO xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return mlag_error_down_info else: xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") mlag_error_down_info["mlagErrorDownInfos"] = list() root = ElementTree.fromstring(xml_str) mlag_error_infos = root.findall( "./mlag/errordowns/errordown") if mlag_error_infos: for mlag_error_info in mlag_error_infos: mlag_error_dict = dict() for ele in mlag_error_info: if ele.tag in ["dfsgroupId", "portName"]: mlag_error_dict[ele.tag] = ele.text mlag_error_down_info[ "mlagErrorDownInfos"].append(mlag_error_dict) return mlag_error_down_info
def get_bd_vap_dict(self): """get virtual access point info""" vap_info = dict() conf_str = CE_NC_GET_BD_VAP % self.bridge_domain_id xml_str = get_nc_config(self.module, conf_str) xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get vap: VLAN vap_info["bdId"] = self.bridge_domain_id root = ElementTree.fromstring(xml_str) vap_info["vlanList"] = "" vap_vlan = root.find("evc/bds/bd/bdBindVlan") if vap_vlan: for ele in vap_vlan: if ele.tag == "vlanList": vap_info["vlanList"] = ele.text # get vap: l2 su-interface vap_ifs = root.findall( "evc/bds/bd/servicePoints/servicePoint/ifName") if_list = list() if vap_ifs: for vap_if in vap_ifs: if vap_if.tag == "ifName": if_list.append(vap_if.text) vap_info["intfList"] = if_list return vap_info
def get_vlan_attr(self, vlan_id): """ get vlan attributes.""" conf_str = CE_NC_GET_VLAN % vlan_id xml_str = get_nc_config(self.module, conf_str) attr = dict() if "<data/>" in xml_str: return attr else: re_find_id = re.findall(r'.*<vlanId>(.*)</vlanId>.*\s*', xml_str) re_find_name = re.findall(r'.*<vlanName>(.*)</vlanName>.*\s*', xml_str) re_find_desc = re.findall(r'.*<vlanDesc>(.*)</vlanDesc>.*\s*', xml_str) if re_find_id: if re_find_name: attr = dict(vlan_id=re_find_id[0], name=re_find_name[0], description=re_find_desc[0]) else: attr = dict(vlan_id=re_find_id[0], name=None, description=re_find_desc[0]) return attr
def get_dfs_group_info(self): """ get dfs group attributes info.""" dfs_group_info = dict() conf_str = CE_NC_GET_DFS_GROUP_INFO xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return dfs_group_info else: xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") root = ElementTree.fromstring(xml_str) dfs_info = root.findall("dfs/groupInstances/groupInstance") if dfs_info: for tmp in dfs_info: for site in tmp: if site.tag in [ "groupId", "priority", "ipAddress", "srcVpnName" ]: dfs_group_info[site.tag] = site.text dfs_nick_info = root.findall( "dfs/groupInstances/groupInstance/trillType") if dfs_nick_info: for tmp in dfs_nick_info: for site in tmp: if site.tag in [ "localNickname", "pseudoNickname", "pseudoPriority" ]: dfs_group_info[site.tag] = site.text return dfs_group_info
def get_interface_info(self): """Get interface information""" xml_str = CE_NC_GET_INT_STATISTICS % self.interface.upper() con_obj = get_nc_config(self.module, xml_str) if "<data/>" in con_obj: self.module.fail_json( msg='Error: %s interface does not exist.' % self.interface.upper()) return xml_str = con_obj.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get link status information root = ElementTree.fromstring(xml_str) intf_info = root.find("ifm/interfaces/interface") if intf_info: for eles in intf_info: if eles.tag in ["ifDynamicInfo", "ifStatistics", "ifClearedStat"]: if eles.tag == "ifDynamicInfo": self.get_intf_dynamic_info(eles, self.interface) elif eles.tag == "ifStatistics": self.get_intf_statistics_info(eles, self.interface) elif eles.tag == "ifClearedStat": self.get_intf_cleared_stat(eles, self.interface)
def get_mlag_info(self): """ get mlag info.""" mlag_info = dict() conf_str = CE_NC_GET_MLAG_INFO % ("<localMlagPort>Eth-Trunk%s</localMlagPort>" % self.eth_trunk_id) xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return mlag_info else: xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") mlag_info["mlagInfos"] = list() root = ElementTree.fromstring(xml_str) dfs_mlag_infos = root.findall( "./mlag/mlagInstances/mlagInstance") if dfs_mlag_infos: for dfs_mlag_info in dfs_mlag_infos: mlag_dict = dict() for ele in dfs_mlag_info: if ele.tag in ["dfsgroupId", "mlagId", "localMlagPort"]: mlag_dict[ele.tag] = ele.text mlag_info["mlagInfos"].append(mlag_dict) return mlag_info
def get_vni2bd_dict(self): """ get vni2bd attributes dict.""" vni2bd_info = dict() # get vni bd info conf_str = CE_NC_GET_VNI_BD_INFO xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return vni2bd_info xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get vni to bridge domain id info root = ElementTree.fromstring(xml_str) vni2bd_info["vni2BdInfos"] = list() vni2bds = root.findall("nvo3/nvo3Vni2Bds/nvo3Vni2Bd") if vni2bds: for vni2bd in vni2bds: vni_dict = dict() for ele in vni2bd: if ele.tag in ["vniId", "bdId"]: vni_dict[ele.tag] = ele.text vni2bd_info["vni2BdInfos"].append(vni_dict) return vni2bd_info
def get_dldp_exist_config(self): """Get current dldp existed configuration""" dldp_conf = dict() xml_str = CE_NC_GET_GLOBAL_DLDP_CONFIG con_obj = get_nc_config(self.module, xml_str) if "<data/>" in con_obj: return dldp_conf xml_str = con_obj.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get global DLDP info root = ElementTree.fromstring(xml_str) topo = root.find("dldp/dldpSys") if not topo: self.module.fail_json( msg="Error: Get current DLDP configuration failed.") for eles in topo: if eles.tag in [ "dldpEnable", "dldpInterval", "dldpWorkMode", "dldpAuthMode" ]: if eles.tag == 'dldpEnable': if eles.text == 'true': value = 'enable' else: value = 'disable' else: value = eles.text dldp_conf[eles.tag] = value return dldp_conf
def get_vrf(self): """ check if vrf is need to change""" getxmlstr = CE_NC_GET_VRF xml_str = get_nc_config(self.module, getxmlstr) xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") root = ElementTree.fromstring(xml_str) vpn_instances = root.findall( "l3vpn/l3vpncomm/l3vpnInstances/l3vpnInstance") if vpn_instances: for vpn_instance in vpn_instances: if vpn_instance.find('vrfName').text == self.vrf: if vpn_instance.find('vrfDescription').text == self.description: if self.state == "present": return False else: return True else: return True return self.state == "present" else: return self.state == "present"
def get_interfaces_dict(self): """ get interfaces attributes dict.""" intfs_info = dict() conf_str = CE_NC_GET_INTFS % self.interface_type recv_xml = get_nc_config(self.module, conf_str) if "<data/>" in recv_xml: return intfs_info xml_str = recv_xml.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") root = ElementTree.fromstring(xml_str) intfs = root.findall("ifm/interfaces/") if intfs: for intf in intfs: intf_type = intf.find("ifPhyType").text.lower() if intf_type: if not intfs_info.get(intf_type): intfs_info[intf_type] = list() intf_info = dict() for tmp in intf: intf_info[tmp.tag] = tmp.text intfs_info[intf_type].append(intf_info) return intfs_info
def get_interface_vpn(self): """ get the VPN instance associated with the interface""" xml_str = CE_NC_GET_VRF_INTERFACE con_obj = get_nc_config(self.module, xml_str) if "<data/>" in con_obj: return xml_str = con_obj.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get global vrf interface info root = ElementTree.fromstring(xml_str) vpns = root.findall( "l3vpn/l3vpncomm/l3vpnInstances/l3vpnInstance") if vpns: for vpnele in vpns: vpn_name = None for vpninfo in vpnele: if vpninfo.tag == 'vrfName': vpn_name = vpninfo.text if vpninfo.tag == 'l3vpnIfs': self.get_interface_vpn_name(vpninfo, vpn_name) return
def get_mlag_trunk_attribute_info(self): """ get mlag global info.""" mlag_trunk_attribute_info = dict() eth_trunk = "Eth-Trunk" eth_trunk += self.eth_trunk_id conf_str = CE_NC_GET_LACP_MLAG_INFO % eth_trunk xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return mlag_trunk_attribute_info else: xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") root = ElementTree.fromstring(xml_str) global_info = root.findall( "./ifmtrunk/TrunkIfs/TrunkIf/lacpMlagIf") if global_info: for tmp in global_info: for site in tmp: if site.tag in ["lacpMlagSysId", "lacpMlagPriority"]: mlag_trunk_attribute_info[site.tag] = site.text return mlag_trunk_attribute_info
def get_bfd_dict(self): """bfd config dict""" bfd_dict = dict() bfd_dict["global"] = dict() bfd_dict["session"] = dict() conf_str = CE_NC_GET_BFD % ( CE_NC_GET_BFD_GLB + (CE_NC_GET_BFD_SESSION % self.session_name)) xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return bfd_dict xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") root = ElementTree.fromstring(xml_str) # get bfd global info glb = root.find("bfd/bfdSchGlobal") if glb: for attr in glb: bfd_dict["global"][attr.tag] = attr.text # get bfd session info sess = root.find("bfd/bfdCfgSessions/bfdCfgSession") if sess: for attr in sess: bfd_dict["session"][attr.tag] = attr.text return bfd_dict
def get_isis_dict(self): """bfd config dict""" isis_dict = dict() isis_dict["instance"] = dict() conf_str = CE_NC_GET_ISIS % ( (CE_NC_GET_ISIS_INTERFACE % self.instance_id)) if self.bfdstaticen or self.bfdblocken: conf_str = CE_NC_GET_ISIS % ( (CE_NC_GET_ISIS_BFDINTERFACE % self.instance_id)) xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return isis_dict xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") root = ElementTree.fromstring(xml_str) # glb = root.find("isiscomm/isSites/isSite/isCircuits/isCircuit") if self.bfdstaticen or self.bfdblocken: glb = root.find( "isiscomm/isSites/isSite/isSiteMTs/isSiteMT/isCircMts/isCircMt" ) if glb: for attr in glb: isis_dict["instance"][attr.tag] = attr.text return isis_dict
def get_port_info(self, interface): """Get port information""" if_type = get_interface_type(interface) if if_type == 'meth': xml_str = CE_NC_GET_PORT_SPEED % interface.lower().replace('meth', 'MEth') else: xml_str = CE_NC_GET_PORT_SPEED % interface.upper() con_obj = get_nc_config(self.module, xml_str) if "<data/>" in con_obj: return xml_str = con_obj.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get link status information root = ElementTree.fromstring(xml_str) port_info = root.find("devm/ports/port") if port_info: for eles in port_info: if eles.tag == "ethernetPort": for ele in eles: if ele.tag == 'speed': self.result[interface]['Speed'] = ele.text
def is_vrf_exist(self): """ judge whether the VPN instance is existed""" conf_str = CE_NC_GET_VRF % self.vrf con_obj = get_nc_config(self.module, conf_str) if "<data/>" in con_obj: return False return True
def netconf_get_config(self, **kwargs): """ Get configure through netconf """ module = kwargs["module"] conf_str = kwargs["conf_str"] xml_str = get_nc_config(module, conf_str) return xml_str
def get_nve_dict(self, nve_name): """ get nve interface attributes dict.""" nve_info = dict() # get nve info conf_str = CE_NC_GET_NVE_INFO % nve_name xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return nve_info xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get nve info root = ElementTree.fromstring(xml_str) nvo3 = root.find("nvo3/nvo3Nves/nvo3Nve") if nvo3: for nve in nvo3: if nve.tag in ["srcAddr", "ifName", "nveType"]: nve_info[nve.tag] = nve.text # get nve vni info nve_info["vni_peer_protocols"] = list() vni_members = root.findall( "nvo3/nvo3Nves/nvo3Nve/vniMembers/vniMember") if vni_members: for member in vni_members: vni_dict = dict() for ele in member: if ele.tag in ["vniId", "protocol"]: vni_dict[ele.tag] = ele.text nve_info["vni_peer_protocols"].append(vni_dict) # get vni peer address ip info nve_info["vni_peer_ips"] = list() re_find = re.findall(r'<vniId>(.*?)</vniId>\s*' r'<protocol>(.*?)</protocol>\s*' r'<nvo3VniPeers>(.*?)</nvo3VniPeers>', xml_str) if re_find: for vni_peers in re_find: vni_info = dict() vni_peer = re.findall(r'<peerAddr>(.*?)</peerAddr>', vni_peers[2]) if vni_peer: vni_info["vniId"] = vni_peers[0] vni_peer_list = list() for peer in vni_peer: vni_peer_list.append(peer) vni_info["peerAddr"] = vni_peer_list nve_info["vni_peer_ips"].append(vni_info) return nve_info
def check_vni_bd(self): """Check whether vxlan vni is configured in BD view""" xml_str = CE_NC_GET_VNI_BD xml_str = get_nc_config(self.module, xml_str) if "<data/>" in xml_str or not re.findall( r'<vniId>\S+</vniId>\s+<bdId>%s</bdId>' % self.bridge_domain_id, xml_str): self.module.fail_json( msg= 'Error: The vxlan vni is not configured or the bridge domain id is invalid.' )
def get_vlans_list(self): """ get all vlan vid list, sample: [ "20", "30", "31" ]""" conf_str = CE_NC_GET_VLANS xml_str = get_nc_config(self.module, conf_str) vlan_list = list() if "<data/>" in xml_str: return vlan_list else: vlan_list = re.findall(r'.*<vlanId>(.*)</vlanId>.*', xml_str) return vlan_list
def check_global_args(self): """ Check global args """ need_cfg = False find_flag = False self.cur_global_cfg["global_cfg"] = [] if self.debug_time_stamp: conf_str = CE_GET_DEBUG_GLOBAL_HEADER conf_str += "<debugTimeStamp></debugTimeStamp>" conf_str += CE_GET_DEBUG_GLOBAL_TAIL xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: find_flag = False else: xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") root = ElementTree.fromstring(xml_str) global_cfg = root.findall("syslog/globalParam") if global_cfg: for tmp in global_cfg: tmp_dict = dict() for site in tmp: if site.tag in ["debugTimeStamp"]: tmp_dict[site.tag] = site.text self.cur_global_cfg["global_cfg"].append(tmp_dict) if self.cur_global_cfg["global_cfg"]: for tmp in self.cur_global_cfg["global_cfg"]: find_flag = True if tmp.get("debugTimeStamp").lower( ) != self.debug_time_stamp: find_flag = False if find_flag: break else: find_flag = False if self.state == "present": need_cfg = bool(not find_flag) else: need_cfg = bool(find_flag) self.cur_global_cfg["need_cfg"] = need_cfg
def get_vrf(self): """ check if vrf is need to change""" getxmlstr = CE_NC_GET_VRF xmlstr_new_1 = (self.vrf.lower()) xml_str = get_nc_config(self.module, getxmlstr) re_find_1 = re.findall( r'.*<vrfname>(.*)</vrfname>.*', xml_str.lower()) if re_find_1 is None: return False return xmlstr_new_1 in re_find_1
def get_ospf_dict(self): """ get one ospf attributes dict.""" ospf_info = dict() conf_str = CE_NC_GET_OSPF % (self.process_id, self.get_area_ip(), self.interface) rcv_xml = get_nc_config(self.module, conf_str) if "<data/>" in rcv_xml: return ospf_info xml_str = rcv_xml.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get process base info root = ElementTree.fromstring(xml_str) ospfsite = root.find("ospfv2/ospfv2comm/ospfSites/ospfSite") if not ospfsite: self.module.fail_json(msg="Error: ospf process does not exist.") for site in ospfsite: if site.tag in ["processId", "routerId", "vrfName"]: ospf_info[site.tag] = site.text # get areas info ospf_info["areaId"] = "" areas = root.find("ospfv2/ospfv2comm/ospfSites/ospfSite/areas/area") if areas: for area in areas: if area.tag == "areaId": ospf_info["areaId"] = area.text break # get interface info ospf_info["interface"] = dict() intf = root.find( "ospfv2/ospfv2comm/ospfSites/ospfSite/areas/area/interfaces/interface" ) if intf: for attr in intf: if attr.tag in [ "ifName", "networkType", "helloInterval", "deadInterval", "silentEnable", "configCost", "authenticationMode", "authTextSimple", "keyId", "authTextMd5" ]: ospf_info["interface"][attr.tag] = attr.text return ospf_info
def get_vlans_name(self): """ get all vlan vid and its name list, sample: [ ("20", "VLAN_NAME_20"), ("30", "VLAN_NAME_30") ]""" conf_str = CE_NC_GET_VLANS xml_str = get_nc_config(self.module, conf_str) vlan_list = list() if "<data/>" in xml_str: return vlan_list else: vlan_list = re.findall( r'.*<vlanId>(.*)</vlanId>.*\s*<vlanName>(.*)</vlanName>.*', xml_str) return vlan_list
def get_interface_dict(self, ifname): """ get one interface attributes dict.""" intf_info = dict() conf_str = CE_NC_GET_INTF % ifname rcv_xml = get_nc_config(self.module, conf_str) if "<data/>" in rcv_xml: return intf_info # get interface base info intf = re.findall( r'.*<ifName>(.*)</ifName>.*\s*' r'<isL2SwitchPort>(.*)</isL2SwitchPort>.*', rcv_xml) if intf: intf_info = dict(ifName=intf[0][0], isL2SwitchPort=intf[0][1]) # get interface ipv4 address info ipv4_info = re.findall( r'.*<ifIpAddr>(.*)</ifIpAddr>.*\s*<subnetMask>(.*)' r'</subnetMask>.*\s*<addrType>(.*)</addrType>.*', rcv_xml) intf_info["am4CfgAddr"] = list() for info in ipv4_info: intf_info["am4CfgAddr"].append( dict(ifIpAddr=info[0], subnetMask=info[1], addrType=info[2])) # get interface ipv6 address info ipv6_info = re.findall( r'.*<ifmAm6>.*\s*<enableFlag>(.*)</enableFlag>.*', rcv_xml) if not ipv6_info: self.module.fail_json(msg='Error: Fail to get interface %s IPv6 state.' % self.interface) else: intf_info["enableFlag"] = ipv6_info[0] # get interface ipv6 enable info ipv6_info = re.findall( r'.*<ifIp6Addr>(.*)</ifIp6Addr>.*\s*<addrPrefixLen>(.*)' r'</addrPrefixLen>.*\s*<addrType6>(.*)</addrType6>.*', rcv_xml) intf_info["am6CfgAddr"] = list() for info in ipv6_info: intf_info["am6CfgAddr"].append( dict(ifIp6Addr=info[0], addrPrefixLen=info[1], addrType6=info[2])) return intf_info
def get_vrf_af(self): """ check if vrf is need to change""" self.vrf_af_info["vpnInstAF"] = list() if self.evpn is True: getxmlstr = CE_NC_GET_VRF_AF % ( self.vrf, CE_NC_GET_EXTEND_VRF_TARGET) else: getxmlstr = CE_NC_GET_VRF_AF % (self.vrf, CE_NC_GET_VRF_TARGET) xml_str = get_nc_config(self.module, getxmlstr) if 'data/' in xml_str: return self.state == 'present' xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") root = ElementTree.fromstring(xml_str) # get the vpn address family and RD text vrf_addr_types = root.findall( "l3vpn/l3vpncomm/l3vpnInstances/l3vpnInstance/vpnInstAFs/vpnInstAF") if vrf_addr_types: for vrf_addr_type in vrf_addr_types: vrf_af_info = dict() for vrf_addr_type_ele in vrf_addr_type: if vrf_addr_type_ele.tag in ["vrfName", "afType", "vrfRD"]: vrf_af_info[vrf_addr_type_ele.tag] = vrf_addr_type_ele.text if vrf_addr_type_ele.tag == 'vpnTargets': vrf_af_info["vpnTargets"] = list() for rtargets in vrf_addr_type_ele: rt_dict = dict() for rtarget in rtargets: if rtarget.tag in ["vrfRTValue", "vrfRTType"]: rt_dict[rtarget.tag] = rtarget.text vrf_af_info["vpnTargets"].append(rt_dict) if vrf_addr_type_ele.tag == 'exVpnTargets': vrf_af_info["evpnTargets"] = list() for rtargets in vrf_addr_type_ele: rt_dict = dict() for rtarget in rtargets: if rtarget.tag in ["vrfRTValue", "vrfRTType"]: rt_dict[rtarget.tag] = rtarget.text vrf_af_info["evpnTargets"].append(rt_dict) self.vrf_af_info["vpnInstAF"].append(vrf_af_info)
def get_l2_sub_intf_dict(self, ifname): """get l2 sub-interface info""" intf_info = dict() if not ifname: return intf_info conf_str = CE_NC_GET_ENCAP % ifname xml_str = get_nc_config(self.module, conf_str) if "<data/>" in xml_str: return intf_info xml_str = xml_str.replace('\r', '').replace('\n', '').\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="http://www.huawei.com/netconf/vrp"', "") # get l2 sub interface encapsulation info root = ElementTree.fromstring(xml_str) bds = root.find("ethernet/servicePoints/servicePoint") if not bds: return intf_info for ele in bds: if ele.tag in ["ifName", "flowType"]: intf_info[ele.tag] = ele.text.lower() if intf_info.get("flowType") == "dot1q": ce_vid = root.find( "ethernet/servicePoints/servicePoint/flowDot1qs") intf_info["dot1qVids"] = "" if ce_vid: for ele in ce_vid: if ele.tag == "dot1qVids": intf_info["dot1qVids"] = ele.text elif intf_info.get("flowType") == "qinq": vids = root.find( "ethernet/servicePoints/servicePoint/flowQinqs/flowQinq") if vids: for ele in vids: if ele.tag in ["peVlanId", "ceVids"]: intf_info[ele.tag] = ele.text return intf_info