def netconf_load_config(self, xml_str): """load bfd config by netconf""" if not xml_str: return xml_cfg = """ <config> <bfd xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> %s </bfd> </config>""" % xml_str set_nc_config(self.module, xml_cfg) self.changed = True
def config_interface_vrf(self): """ configure VPN instance of the interface""" if not self.conf_exist and self.state == 'present': xml_str = CE_NC_MERGE_VRF_INTERFACE % ( self.vrf, self.vpn_interface) ret_xml = set_nc_config(self.module, xml_str) self.check_response(ret_xml, "VRF_INTERFACE_CONFIG") self.changed = True elif self.state == 'absent': xml_str = CE_NC_DEL_INTF_VPN % (self.vrf, self.vpn_interface) ret_xml = set_nc_config(self.module, xml_str) self.check_response(ret_xml, "DEL_VRF_INTERFACE_CONFIG") self.changed = True
def merge_debug_source(self): """ Merge debug source """ conf_str = CE_MERGE_DEBUG_SOURCE_HEADER if self.module_name: conf_str += "<moduleName>%s</moduleName>" % self.module_name if self.channel_id: conf_str += "<icChannelId>%s</icChannelId>" % self.channel_id if self.debug_enable != 'no_use': conf_str += "<dbgEnFlg>%s</dbgEnFlg>" % self.debug_enable if self.debug_level: conf_str += "<dbgEnLevel>%s</dbgEnLevel>" % self.debug_level conf_str += CE_MERGE_DEBUG_SOURCE_TAIL recv_xml = set_nc_config(self.module, conf_str) if "<ok/>" not in recv_xml: self.module.fail_json(msg='Error: Merge debug source failed.') cmd = "info-center source" if self.module_name: cmd += " %s" % self.module_name if self.channel_id: cmd += " channel %s" % self.channel_id if self.debug_enable != 'no_use': if self.debug_enable == "true": cmd += " debug state on" else: cmd += " debug state off" if self.debug_level: cmd += " level %s" % self.debug_level self.updates_cmd.append(cmd) self.changed = True
def config_vap_sub_intf(self): """configure a Layer 2 sub-interface as a service access point""" if not self.vap_info: self.module.fail_json(msg="Error: Bridge domain %s does not exist." % self.bridge_domain_id) xml_str = "" if self.state == "present": if self.l2_sub_interface not in self.vap_info["intfList"]: self.updates_cmd.append("interface %s" % self.l2_sub_interface) self.updates_cmd.append("bridge-domain %s" % self.bridge_domain_id) xml_str = CE_NC_MERGE_BD_INTF % ( self.bridge_domain_id, self.l2_sub_interface) else: if self.l2_sub_interface in self.vap_info["intfList"]: self.updates_cmd.append("interface %s" % self.l2_sub_interface) self.updates_cmd.append( "undo bridge-domain %s" % self.bridge_domain_id) xml_str = CE_NC_DELETE_BD_INTF % ( self.bridge_domain_id, self.l2_sub_interface) if not xml_str: return recv_xml = set_nc_config(self.module, xml_str) self.check_response(recv_xml, "CONFIG_VAP_SUB_INTERFACE") self.changed = True
def netconf_set_config(self, xml_str, xml_name): """ netconf set config """ recv_xml = set_nc_config(self.module, xml_str) if "<ok/>" not in recv_xml: self.module.fail_json(msg='Error: %s failed.' % xml_name)
def create_interface(self, ifname, description, admin_state, mode, l2sub): """Create interface.""" if l2sub: self.updates_cmd.append("interface %s mode l2" % ifname) else: self.updates_cmd.append("interface %s" % ifname) if not description: description = '' else: self.updates_cmd.append("description %s" % description) if l2sub: xmlstr = CE_NC_XML_CREATE_INTF_L2SUB % (ifname, description) else: xmlstr = CE_NC_XML_CREATE_INTF % (ifname, description) if admin_state and is_admin_state_enable(self.intf_type): xmlstr += CE_NC_XML_MERGE_INTF_STATUS % (ifname, admin_state) if admin_state == 'up': self.updates_cmd.append("undo shutdown") else: self.updates_cmd.append("shutdown") if mode and is_portswitch_enalbe(self.intf_type): if mode == "layer2": xmlstr += CE_NC_XML_MERGE_INTF_L2ENABLE % (ifname, 'enable') self.updates_cmd.append('portswitch') elif mode == "layer3": xmlstr += CE_NC_XML_MERGE_INTF_L2ENABLE % (ifname, 'disable') self.updates_cmd.append('undo portswitch') conf_str = '<config> ' + xmlstr + ' </config>' recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "CREATE_INTF") self.changed = True
def config_delete_vni_peer_ip(self, nve_name, vni_id, peer_ip_list): """remove vni peer ip""" for peer_ip in peer_ip_list: if not self.is_vni_peer_list_exist(nve_name, vni_id, peer_ip): self.module.fail_json(msg='Error: The %s does not exist' % peer_ip) config = self.get_current_config(vni_id, peer_ip_list) if not config: cfg_xml = CE_NC_DELETE_VNI_PEER_ADDRESS_IP_HEAD % ( nve_name, vni_id) for peer_ip in peer_ip_list: cfg_xml += CE_NC_DELETE_VNI_PEER_ADDRESS_IP_DELETE % peer_ip cfg_xml += CE_NC_DELETE_VNI_PEER_ADDRESS_IP_END else: cfg_xml = CE_NC_DELETE_PEER_ADDRESS_IP_HEAD % ( nve_name, vni_id) for peer_ip in peer_ip_list: cfg_xml += CE_NC_DELETE_VNI_PEER_ADDRESS_IP_DELETE % peer_ip cfg_xml += CE_NC_DELETE_PEER_ADDRESS_IP_END recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "DELETE_VNI_PEER_IP") self.updates_cmd.append("interface %s" % nve_name) for peer_ip in peer_ip_list: cmd_output = "undo vni %s head-end peer-list %s" % (vni_id, peer_ip) self.updates_cmd.append(cmd_output) self.changed = True
def config_global_dldp(self): """Config global dldp""" if self.same_conf: return enable = self.enable if not self.enable: enable = self.dldp_conf['dldpEnable'] if enable == 'enable': enable = 'true' else: enable = 'false' internal = self.internal if not self.internal: internal = self.dldp_conf['dldpInterval'] work_mode = self.work_mode if not self.work_mode: work_mode = self.dldp_conf['dldpWorkMode'] if work_mode == 'enhance' or work_mode == 'dldpEnhance': work_mode = 'dldpEnhance' else: work_mode = 'dldpNormal' auth_mode = self.auth_mode if not self.auth_mode: auth_mode = self.dldp_conf['dldpAuthMode'] if auth_mode == 'md5': auth_mode = 'dldpAuthMD5' elif auth_mode == 'simple': auth_mode = 'dldpAuthSimple' elif auth_mode == 'sha': auth_mode = 'dldpAuthSHA' elif auth_mode == 'hmac-sha256': auth_mode = 'dldpAuthHMAC-SHA256' elif auth_mode == 'none': auth_mode = 'dldpAuthNone' xml_str = CE_NC_MERGE_DLDP_GLOBAL_CONFIG_HEAD % ( enable, internal, work_mode) if self.auth_mode: if self.auth_mode == 'none': xml_str += "<dldpAuthMode>dldpAuthNone</dldpAuthMode>" else: xml_str += "<dldpAuthMode>%s</dldpAuthMode>" % auth_mode xml_str += "<dldpPasswords>%s</dldpPasswords>" % self.auth_pwd xml_str += CE_NC_MERGE_DLDP_GLOBAL_CONFIG_TAIL ret_xml = set_nc_config(self.module, xml_str) self.check_response(ret_xml, "MERGE_DLDP_GLOBAL_CONFIG") if self.reset == 'enable': xml_str = CE_NC_ACTION_RESET_DLDP ret_xml = execute_nc_action(self.module, xml_str) self.check_response(ret_xml, "ACTION_RESET_DLDP") self.changed = True
def config_vap_vlan(self): """configure a VLAN as a service access point""" if not self.vap_info: self.module.fail_json(msg="Error: Bridge domain %s does not exist." % self.bridge_domain_id) xml_str = "" if self.state == "present": if not is_vlan_in_bitmap(self.bind_vlan_id, self.vap_info["vlanList"]): self.updates_cmd.append("bridge-domain %s" % self.bridge_domain_id) self.updates_cmd.append( "l2 binding vlan %s" % self.bind_vlan_id) vlan_bitmap = vlan_vid_to_bitmap(self.bind_vlan_id) xml_str = CE_NC_MERGE_BD_VLAN % ( self.bridge_domain_id, vlan_bitmap, vlan_bitmap) else: if is_vlan_in_bitmap(self.bind_vlan_id, self.vap_info["vlanList"]): self.updates_cmd.append("bridge-domain %s" % self.bridge_domain_id) self.updates_cmd.append( "undo l2 binding vlan %s" % self.bind_vlan_id) vlan_bitmap = vlan_vid_to_bitmap(self.bind_vlan_id) xml_str = CE_NC_MERGE_BD_VLAN % ( self.bridge_domain_id, "0" * 1024, vlan_bitmap) if not xml_str: return recv_xml = set_nc_config(self.module, xml_str) self.check_response(recv_xml, "CONFIG_VAP_VLAN") self.changed = True
def default_interface(self, ifname): """default_interface""" change = False xmlstr = "" self.updates_cmd.append("interface %s" % ifname) # set description default if self.intf_info["ifDescr"]: xmlstr += CE_NC_XML_MERGE_INTF_DES % (ifname, '') self.updates_cmd.append("undo description") change = True # set admin_status default if is_admin_state_enable(self.intf_type) \ and self.intf_info["ifAdminStatus"] != 'up': xmlstr += CE_NC_XML_MERGE_INTF_STATUS % (ifname, 'up') self.updates_cmd.append("undo shutdown") change = True # set portswitch default if is_portswitch_enalbe(self.intf_type) \ and self.intf_info["isL2SwitchPort"] != "true": xmlstr += CE_NC_XML_MERGE_INTF_L2ENABLE % (ifname, 'enable') self.updates_cmd.append("portswitch") change = True if not change: return conf_str = '<config> ' + xmlstr + ' </config>' recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "SET_INTF_DEFAULT") self.changed = True
def undo_config_vlan(self, vlanid): """Delete vlan.""" conf_str = CE_NC_DELETE_VLAN % vlanid recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "DELETE_VLAN") self.changed = True self.updates_cmd.append('undo vlan %s' % self.vlan_id)
def netconf_set_config(self, **kwargs): """ Set configure by netconf """ module = kwargs["module"] conf_str = kwargs["conf_str"] xml_str = set_nc_config(module, conf_str) return xml_str
def delete_interface(self, ifname): """ Delete interface.""" xmlstr = CE_NC_XML_DELETE_INTF % ifname conf_str = '<config> ' + xmlstr + ' </config>' self.updates_cmd.append('undo interface %s' % ifname) recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "DELETE_INTF") self.changed = True
def config_traffic_encap_qinq(self): """configure traffic encapsulation type qinq""" xml_str = "" self.updates_cmd.append("interface %s" % self.l2_sub_interface) if self.state == "present": if self.encapsulation != self.l2sub_info.get("flowType"): if self.ce_vid: vlan_bitmap = vlan_vid_to_bitmap(self.ce_vid) xml_str = CE_NC_SET_ENCAP_QINQ % (self.l2_sub_interface, self.pe_vid, vlan_bitmap, vlan_bitmap) self.updates_cmd.append( "encapsulation %s vid %s ce-vid %s" % (self.encapsulation, self.pe_vid, self.ce_vid)) else: xml_str = CE_NC_SET_ENCAP % ( self.l2_sub_interface, self.encapsulation) self.updates_cmd.append( "encapsulation %s" % self.encapsulation) else: if self.ce_vid: if not is_vlan_in_bitmap(self.ce_vid, self.l2sub_info.get("ceVids")) \ or self.pe_vid != self.l2sub_info.get("peVlanId"): vlan_bitmap = vlan_vid_to_bitmap(self.ce_vid) xml_str = CE_NC_SET_ENCAP_QINQ % (self.l2_sub_interface, self.pe_vid, vlan_bitmap, vlan_bitmap) self.updates_cmd.append( "encapsulation %s vid %s ce-vid %s" % (self.encapsulation, self.pe_vid, self.ce_vid)) else: if self.encapsulation == self.l2sub_info.get("flowType"): if self.ce_vid: if is_vlan_in_bitmap(self.ce_vid, self.l2sub_info.get("ceVids")) \ and self.pe_vid == self.l2sub_info.get("peVlanId"): xml_str = CE_NC_UNSET_ENCAP % self.l2_sub_interface self.updates_cmd.append( "undo encapsulation %s vid %s ce-vid %s" % (self.encapsulation, self.pe_vid, self.ce_vid)) else: xml_str = CE_NC_UNSET_ENCAP % self.l2_sub_interface self.updates_cmd.append( "undo encapsulation %s" % self.encapsulation) if not xml_str: self.updates_cmd.pop() return recv_xml = set_nc_config(self.module, xml_str) self.check_response(recv_xml, "CONFIG_INTF_ENCAP_QINQ") self.changed = True
def config_merge_mode(self, nve_name, mode): """config nve mode""" if self.is_nve_mode_change(nve_name, mode): cfg_xml = CE_NC_MERGE_NVE_MODE % (nve_name, mode) recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "MERGE_MODE") self.updates_cmd.append("interface %s" % nve_name) self.updates_cmd.append("mode l3") self.changed = True
def config_merge_vni2bd(self, bd_id, vni_id): """config vni to bd id""" if self.is_vni_bd_change(vni_id, bd_id): cfg_xml = CE_NC_MERGE_VNI_BD_ID % (vni_id, bd_id) recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "MERGE_VNI_BD") self.updates_cmd.append("bridge-domain %s" % bd_id) self.updates_cmd.append("vxlan vni %s" % vni_id) self.changed = True
def config_ntp_auth_keyid(self): """Config ntp authentication keyid""" if self.trusted_key == 'enable': trusted_key = 'true' else: trusted_key = 'false' xml_str = CE_NC_MERGE_NTP_AUTH_CONFIG % ( self.key_id, self.auth_mode.upper(), self.password, trusted_key) ret_xml = set_nc_config(self.module, xml_str) self.check_response(ret_xml, "NTP_AUTH_KEYID_CONFIG")
def config_merge_source_ip(self, nve_name, source_ip): """config nve source ip""" if self.is_nve_source_ip_change(nve_name, source_ip): cfg_xml = CE_NC_MERGE_NVE_SOURCE_IP_PROTOCOL % ( nve_name, source_ip) recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "MERGE_SOURCE_IP") self.updates_cmd.append("interface %s" % nve_name) self.updates_cmd.append("source %s" % source_ip) self.changed = True
def config_ntp_auth_enable(self): """Config ntp authentication enable""" if self.ntp_auth_conf['authentication'] != self.authentication: if self.authentication == 'enable': state = 'true' else: state = 'false' xml_str = CE_NC_MERGE_NTP_AUTH_ENABLE % state ret_xml = set_nc_config(self.module, xml_str) self.check_response(ret_xml, "NTP_AUTH_ENABLE")
def config_vlan(self, vlan_id, name='', description=''): """Create vlan.""" if name is None: name = '' if description is None: description = '' conf_str = CE_NC_CREATE_VLAN % (vlan_id, name, description) recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "CREATE_VLAN") self.changed = True
def set_ntp(self, *args): """Configure ntp parameters""" if self.state == 'present': if self.ip_ver == 'IPv4': xml_str = CE_NC_MERGE_NTP_CONFIG % ( args[0], args[1], '::', args[2], args[3], args[4], args[5], args[6]) elif self.ip_ver == 'IPv6': xml_str = CE_NC_MERGE_NTP_CONFIG % ( args[0], '0.0.0.0', args[1], args[2], args[3], args[4], args[5], args[6]) ret_xml = set_nc_config(self.module, xml_str) self.check_response(ret_xml, "NTP_CORE_CONFIG") else: if self.ip_ver == 'IPv4': xml_str = CE_NC_DELETE_NTP_CONFIG % ( args[0], args[1], '::', args[2], args[3]) elif self.ip_ver == 'IPv6': xml_str = CE_NC_DELETE_NTP_CONFIG % ( args[0], '0.0.0.0', args[1], args[2], args[3]) ret_xml = set_nc_config(self.module, xml_str) self.check_response(ret_xml, "UNDO_NTP_CORE_CONFIG")
def merge_interfaces(self, iftype, description, admin_state, mode): """ Merge interface attributes by type.""" xmlstr = '' change = False intfs_list = self.intfs_info.get(iftype.lower()) if not intfs_list: return for intf in intfs_list: if_change = False self.updates_cmd.append("interface %s" % intf['ifName']) if description and intf["ifDescr"] != description: xmlstr += CE_NC_XML_MERGE_INTF_DES % ( intf['ifName'], description) self.updates_cmd.append("description %s" % description) if_change = True if admin_state and is_admin_state_enable(self.intf_type)\ and intf["ifAdminStatus"] != admin_state: xmlstr += CE_NC_XML_MERGE_INTF_STATUS % ( intf['ifName'], admin_state) if_change = True if admin_state == "up": self.updates_cmd.append("undo shutdown") else: self.updates_cmd.append("shutdown") if is_portswitch_enalbe(self.intf_type): if mode == "layer2" \ and intf["isL2SwitchPort"] != "true": xmlstr += CE_NC_XML_MERGE_INTF_L2ENABLE % ( intf['ifName'], 'enable') self.updates_cmd.append("portswitch") if_change = True elif mode == "layer3" \ and intf["isL2SwitchPort"] != "false": xmlstr += CE_NC_XML_MERGE_INTF_L2ENABLE % ( intf['ifName'], 'disable') self.updates_cmd.append("undo portswitch") if_change = True if if_change: change = True else: self.updates_cmd.pop() if not change: return conf_str = '<config> ' + xmlstr + ' </config>' recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "MERGE_INTFS_ATTR") self.changed = True
def config_delete_source_ip(self, nve_name, source_ip): """nve source ip""" if not self.is_nve_source_ip_exist(nve_name, source_ip): return ipaddr = "0.0.0.0" cfg_xml = CE_NC_MERGE_NVE_SOURCE_IP_PROTOCOL % ( nve_name, ipaddr) recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "DELETE_SOURCE_IP") self.updates_cmd.append("interface %s" % nve_name) self.updates_cmd.append("undo source %s" % source_ip) self.changed = True
def config_delete_vni_protocol_type(self, nve_name, vni_id, protocol_type): """remove vni protocol type""" if not self.is_vni_protocol_exist(nve_name, vni_id, protocol_type): return cfg_xml = CE_NC_DELETE_VNI_PROTOCOL % (nve_name, vni_id, protocol_type) recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "DELETE_VNI_PEER_PROTOCOL") self.updates_cmd.append("interface %s" % nve_name) self.updates_cmd.append( "undo vni %s head-end peer-list protocol bgp " % vni_id) self.changed = True
def config_delete_vni2bd(self, bd_id, vni_id): """remove vni to bd id""" if not self.is_vni_bd_exist(vni_id, bd_id): return cfg_xml = CE_NC_DELETE_VNI_BD_ID % (vni_id, bd_id) recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "DELETE_VNI_BD") self.updates_cmd.append( "bridge-domain %s" % bd_id) self.updates_cmd.append( "undo vxlan vni %s" % vni_id) self.changed = True
def config_traffic_encap(self): """configure traffic encapsulation types""" if not self.l2sub_info: self.module.fail_json(msg="Error: Interface %s does not exist." % self.l2_sub_interface) if not self.encapsulation: return xml_str = "" if self.encapsulation in ["default", "untag"]: if self.state == "present": if self.encapsulation != self.l2sub_info.get("flowType"): xml_str = CE_NC_SET_ENCAP % ( self.l2_sub_interface, self.encapsulation) self.updates_cmd.append( "interface %s" % self.l2_sub_interface) self.updates_cmd.append( "encapsulation %s" % self.encapsulation) else: if self.encapsulation == self.l2sub_info.get("flowType"): xml_str = CE_NC_UNSET_ENCAP % self.l2_sub_interface self.updates_cmd.append( "interface %s" % self.l2_sub_interface) self.updates_cmd.append( "undo encapsulation %s" % self.encapsulation) elif self.encapsulation == "none": if self.state == "present": if self.encapsulation != self.l2sub_info.get("flowType"): xml_str = CE_NC_UNSET_ENCAP % self.l2_sub_interface self.updates_cmd.append( "interface %s" % self.l2_sub_interface) self.updates_cmd.append( "undo encapsulation %s" % self.l2sub_info.get("flowType")) elif self.encapsulation == "dot1q": self.config_traffic_encap_dot1q() return elif self.encapsulation == "qinq": self.config_traffic_encap_qinq() return else: pass if not xml_str: return recv_xml = set_nc_config(self.module, xml_str) self.check_response(recv_xml, "CONFIG_INTF_ENCAP") self.changed = True
def operate_vrf(self): """config/delete vrf""" if not self.changed: return if self.state == "present": if self.description is None: configxmlstr = CE_NC_CREATE_VRF % (self.vrf, '') else: configxmlstr = CE_NC_CREATE_VRF % (self.vrf, self.description) else: configxmlstr = CE_NC_DELETE_VRF % (self.vrf, self.description) conf_str = build_config_xml(configxmlstr) recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "OPERATE_VRF")
def config_merge_vni_peer_ip(self, nve_name, vni_id, peer_ip_list): """config vni peer ip""" if self.is_vni_peer_list_change(nve_name, vni_id, peer_ip_list): cfg_xml = CE_NC_MERGE_VNI_PEER_ADDRESS_IP_HEAD % (nve_name, vni_id) for peer_ip in peer_ip_list: cfg_xml += CE_NC_MERGE_VNI_PEER_ADDRESS_IP_MERGE % peer_ip cfg_xml += CE_NC_MERGE_VNI_PEER_ADDRESS_IP_END recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "MERGE_VNI_PEER_IP") self.updates_cmd.append("interface %s" % nve_name) for peer_ip in peer_ip_list: cmd_output = "vni %s head-end peer-list %s" % (vni_id, peer_ip) self.updates_cmd.append(cmd_output) self.changed = True
def delete_interfaces(self, iftype): """ Delete interfaces with type.""" xmlstr = '' intfs_list = self.intfs_info.get(iftype.lower()) if not intfs_list: return for intf in intfs_list: xmlstr += CE_NC_XML_DELETE_INTF % intf['ifName'] self.updates_cmd.append('undo interface %s' % intf['ifName']) conf_str = '<config> ' + xmlstr + ' </config>' recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "DELETE_INTFS") self.changed = True
def config_delete_mode(self, nve_name, mode): """nve mode""" if mode == "mode-l3": if not self.is_nve_mode_exist(nve_name, mode): return cfg_xml = CE_NC_MERGE_NVE_MODE % (nve_name, "mode-l2") recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "DELETE_MODE") self.updates_cmd.append("interface %s" % nve_name) self.updates_cmd.append("undo mode l3") self.changed = True else: self.module.fail_json( msg='Error: Can not configure undo mode l2.')
def delete_mlag(self): """delete mlag info""" if self.is_mlag_info_exist(): mlag_port = "Eth-Trunk" mlag_port += self.eth_trunk_id conf_str = CE_NC_DELETE_MLAG_INFO % (self.dfs_group_id, self.mlag_id, mlag_port) recv_xml = set_nc_config(self.module, conf_str) if "<ok/>" not in recv_xml: self.module.fail_json(msg='Error: delete mlag info failed.') self.updates_cmd.append("interface %s" % mlag_port) self.updates_cmd.append("undo dfs-group %s m-lag %s" % (self.dfs_group_id, self.mlag_id)) self.changed = True
def netconf_load_config(self, xml_str): """load log config by netconf""" if not xml_str: return xml_cfg = """ <config> <syslog xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> %s </syslog> </config>""" % xml_str recv_xml = set_nc_config(self.module, xml_cfg) self.check_response(recv_xml, "SET_LOG") self.changed = True
def delete_interfaces(self, iftype): """ Delete interfaces with type.""" xmlstr = '' intfs_list = self.intfs_info.get(iftype.lower()) if not intfs_list: return for intf in intfs_list: xmlstr += CE_NC_XML_DELETE_INTF % intf['ifName'] self.updates_cmd.append('undo interface %s' % intf['ifName']) conf_str = '<config> ' + xmlstr + ' </config>' recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "DELETE_INTFS") self.changed = True
def config_merge_vni_protocol_type(self, nve_name, vni_id, protocol_type): """config vni protocol type""" if self.is_vni_protocol_change(nve_name, vni_id, protocol_type): cfg_xml = CE_NC_MERGE_VNI_PROTOCOL % ( nve_name, vni_id, protocol_type) recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "MERGE_VNI_PEER_PROTOCOL") self.updates_cmd.append("interface %s" % nve_name) if protocol_type == "bgp": self.updates_cmd.append( "vni %s head-end peer-list protocol %s" % (vni_id, protocol_type)) else: self.updates_cmd.append( "undo vni %s head-end peer-list protocol bgp" % vni_id) self.changed = True
def remove_area_network(self): """remvoe ospf area network""" if not self.is_network_exist(): return xml_network = CE_NC_XML_DELETE_NETWORKS % (self.addr, self.get_wildcard_mask()) xml_area = CE_NC_XML_BUILD_AREA % (self.get_area_ip(), xml_network) xml_str = CE_NC_XML_BUILD_PROCESS % (self.process_id, xml_area) recv_xml = set_nc_config(self.module, xml_str) self.check_response(recv_xml, "DELETE_AREA_NETWORK") self.updates_cmd.append("ospf %s" % self.process_id) self.updates_cmd.append("area %s" % self.get_area_ip()) self.updates_cmd.append("undo network %s %s" % (self.addr, self.get_wildcard_mask())) self.changed = True
def config_merge_vni_peer_ip(self, nve_name, vni_id, peer_ip_list): """config vni peer ip""" if self.is_vni_peer_list_change(nve_name, vni_id, peer_ip_list): cfg_xml = CE_NC_MERGE_VNI_PEER_ADDRESS_IP_HEAD % ( nve_name, vni_id) for peer_ip in peer_ip_list: cfg_xml += CE_NC_MERGE_VNI_PEER_ADDRESS_IP_MERGE % peer_ip cfg_xml += CE_NC_MERGE_VNI_PEER_ADDRESS_IP_END recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "MERGE_VNI_PEER_IP") self.updates_cmd.append("interface %s" % nve_name) for peer_ip in peer_ip_list: cmd_output = "vni %s head-end peer-list %s" % (vni_id, peer_ip) self.updates_cmd.append(cmd_output) self.changed = True
def config_merge_vni_protocol_type(self, nve_name, vni_id, protocol_type): """config vni protocol type""" if self.is_vni_protocol_change(nve_name, vni_id, protocol_type): cfg_xml = CE_NC_MERGE_VNI_PROTOCOL % ( nve_name, vni_id, protocol_type) recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "MERGE_VNI_PEER_PROTOCOL") self.updates_cmd.append("interface %s" % nve_name) if protocol_type == "bgp": self.updates_cmd.append( "vni %s head-end peer-list protocol %s" % (vni_id, protocol_type)) else: self.updates_cmd.append( "undo vni %s head-end peer-list protocol bgp" % vni_id) self.changed = True
def config_traffic_encap_dot1q(self): """configure traffic encapsulation type dot1q""" xml_str = "" self.updates_cmd.append("interface %s" % self.l2_sub_interface) if self.state == "present": if self.encapsulation != self.l2sub_info.get("flowType"): if self.ce_vid: vlan_bitmap = vlan_vid_to_bitmap(self.ce_vid) xml_str = CE_NC_SET_ENCAP_DOT1Q % ( self.l2_sub_interface, vlan_bitmap, vlan_bitmap) self.updates_cmd.append("encapsulation %s vid %s" % (self.encapsulation, self.ce_vid)) else: xml_str = CE_NC_SET_ENCAP % (self.l2_sub_interface, self.encapsulation) self.updates_cmd.append("encapsulation %s" % self.encapsulation) else: if self.ce_vid and not is_vlan_in_bitmap( self.ce_vid, self.l2sub_info.get("dot1qVids")): vlan_bitmap = vlan_vid_to_bitmap(self.ce_vid) xml_str = CE_NC_SET_ENCAP_DOT1Q % ( self.l2_sub_interface, vlan_bitmap, vlan_bitmap) self.updates_cmd.append("encapsulation %s vid %s" % (self.encapsulation, self.ce_vid)) else: if self.encapsulation == self.l2sub_info.get("flowType"): if self.ce_vid: if is_vlan_in_bitmap(self.ce_vid, self.l2sub_info.get("dot1qVids")): xml_str = CE_NC_UNSET_ENCAP % self.l2_sub_interface self.updates_cmd.append( "undo encapsulation %s vid %s" % (self.encapsulation, self.ce_vid)) else: xml_str = CE_NC_UNSET_ENCAP % self.l2_sub_interface self.updates_cmd.append("undo encapsulation %s" % self.encapsulation) if not xml_str: self.updates_cmd.pop() return recv_xml = set_nc_config(self.module, xml_str) self.check_response(recv_xml, "CONFIG_INTF_ENCAP_DOT1Q") self.changed = True
def default_interfaces(self, iftype): """ Set interface config to default by type.""" change = False xmlstr = '' intfs_list = self.intfs_info.get(iftype.lower()) if not intfs_list: return for intf in intfs_list: if_change = False self.updates_cmd.append("interface %s" % intf['ifName']) # set description default if intf['ifDescr']: xmlstr += CE_NC_XML_MERGE_INTF_DES % (intf['ifName'], '') self.updates_cmd.append("undo description") if_change = True # set admin_status default if is_admin_state_enable( self.intf_type) and intf["ifAdminStatus"] != 'up': xmlstr += CE_NC_XML_MERGE_INTF_STATUS % (intf['ifName'], 'up') self.updates_cmd.append("undo shutdown") if_change = True # set portswitch default if is_portswitch_enalbe( self.intf_type) and intf["isL2SwitchPort"] != "true": xmlstr += CE_NC_XML_MERGE_INTF_L2ENABLE % (intf['ifName'], 'enable') self.updates_cmd.append("portswitch") if_change = True if if_change: change = True else: self.updates_cmd.pop() if not change: return conf_str = '<config> ' + xmlstr + ' </config>' recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "SET_INTFS_DEFAULT") self.changed = True
def delete_debug_global(self): """ Delete debug global """ conf_str = CE_MERGE_DEBUG_GLOBAL_HEADER if self.debug_time_stamp: conf_str += "<debugTimeStamp>DATE_MILLISECOND</debugTimeStamp>" conf_str += CE_MERGE_DEBUG_GLOBAL_TAIL recv_xml = set_nc_config(self.module, conf_str) if "<ok/>" not in recv_xml: self.module.fail_json(msg='Error: delete debug global failed.') if self.debug_time_stamp: cmd = "undo info-center timestamp debugging" self.updates_cmd.append(cmd) self.changed = True
def merge_debug_global(self): """ Merge debug global """ conf_str = CE_MERGE_DEBUG_GLOBAL_HEADER if self.debug_time_stamp: conf_str += "<debugTimeStamp>%s</debugTimeStamp>" % self.debug_time_stamp.upper( ) conf_str += CE_MERGE_DEBUG_GLOBAL_TAIL recv_xml = set_nc_config(self.module, conf_str) if "<ok/>" not in recv_xml: self.module.fail_json(msg='Error: Merge debug global failed.') if self.debug_time_stamp: cmd = "info-center timestamp debugging " + TIME_STAMP_DICT.get( self.debug_time_stamp) self.updates_cmd.append(cmd) self.changed = True
def config_delete_vni_peer_ip(self, nve_name, vni_id, peer_ip_list): """remove vni peer ip""" for peer_ip in peer_ip_list: if not self.is_vni_peer_list_exist(nve_name, vni_id, peer_ip): self.module.fail_json(msg='Error: The %s does not exist' % peer_ip) config = False nve_peer_info = list() for nve_peer in self.nve_info["vni_peer_ips"]: if nve_peer["vniId"] == vni_id: nve_peer_info = nve_peer.get("peerAddr") for peer in nve_peer_info: if peer not in peer_ip_list: config = True if not config: cfg_xml = CE_NC_DELETE_VNI_PEER_ADDRESS_IP_HEAD % (nve_name, vni_id) for peer_ip in peer_ip_list: cfg_xml += CE_NC_DELETE_VNI_PEER_ADDRESS_IP_DELETE % peer_ip cfg_xml += CE_NC_DELETE_VNI_PEER_ADDRESS_IP_END else: cfg_xml = CE_NC_DELETE_PEER_ADDRESS_IP_HEAD % (nve_name, vni_id) for peer_ip in peer_ip_list: cfg_xml += CE_NC_DELETE_VNI_PEER_ADDRESS_IP_DELETE % peer_ip cfg_xml += CE_NC_DELETE_PEER_ADDRESS_IP_END recv_xml = set_nc_config(self.module, cfg_xml) self.check_response(recv_xml, "DELETE_VNI_PEER_IP") self.updates_cmd.append("interface %s" % nve_name) for peer_ip in peer_ip_list: cmd_output = "undo vni %s head-end peer-list %s" % (vni_id, peer_ip) self.updates_cmd.append(cmd_output) self.changed = True
def delete_debug_source(self): """ Delete debug source """ if self.debug_enable == 'no_use' and not self.debug_level: conf_str = CE_DELETE_DEBUG_SOURCE_HEADER if self.module_name: conf_str += "<moduleName>%s</moduleName>" % self.module_name if self.channel_id: conf_str += "<icChannelId>%s</icChannelId>" % self.channel_id conf_str += CE_DELETE_DEBUG_SOURCE_TAIL else: conf_str = CE_MERGE_DEBUG_SOURCE_HEADER if self.module_name: conf_str += "<moduleName>%s</moduleName>" % self.module_name if self.channel_id: conf_str += "<icChannelId>%s</icChannelId>" % self.channel_id if self.debug_enable != 'no_use': conf_str += "<dbgEnFlg>%s</dbgEnFlg>" % CHANNEL_DEFAULT_DBG_STATE.get( self.channel_id) if self.debug_level: conf_str += "<dbgEnLevel>%s</dbgEnLevel>" % CHANNEL_DEFAULT_DBG_LEVEL.get( self.channel_id) conf_str += CE_MERGE_DEBUG_SOURCE_TAIL recv_xml = set_nc_config(self.module, conf_str) if "<ok/>" not in recv_xml: self.module.fail_json(msg='Error: Delete debug source failed.') cmd = "undo info-center source" if self.module_name: cmd += " %s" % self.module_name if self.channel_id: cmd += " channel %s" % self.channel_id if self.debug_enable != 'no_use': cmd += " debug state" if self.debug_level: cmd += " level" self.updates_cmd.append(cmd) self.changed = True
def delete_mlag_global(self): """delete mlag global attribute info""" xml_str = '' if self.is_mlag_global_info_exist(): if self.mlag_priority_id: cmd = "lacp m-lag priority %s" % self.mlag_priority_id xml_str += '<lacpMlagPriority></lacpMlagPriority>' self.cli_add_command(cmd, True) if self.mlag_system_id: cmd = "lacp m-lag system-id %s" % self.mlag_system_id xml_str += '<lacpMlagSysId></lacpMlagSysId>' self.cli_add_command(cmd, True) if xml_str != '': conf_str = CE_NC_SET_GLOBAL_LACP_MLAG_INFO_HEAD + xml_str + CE_NC_SET_GLOBAL_LACP_MLAG_INFO_TAIL recv_xml = set_nc_config(self.module, conf_str) if "<ok/>" not in recv_xml: self.module.fail_json( msg='Error: set mlag interface atrribute info failed.') self.changed = True
def merge_interface(self, ifname, description, admin_state, mode): """ Merge interface attributes.""" xmlstr = '' change = False self.updates_cmd.append("interface %s" % ifname) if description and self.intf_info["ifDescr"] != description: xmlstr += CE_NC_XML_MERGE_INTF_DES % (ifname, description) self.updates_cmd.append("description %s" % description) change = True if admin_state and is_admin_state_enable(self.intf_type) \ and self.intf_info["ifAdminStatus"] != admin_state: xmlstr += CE_NC_XML_MERGE_INTF_STATUS % (ifname, admin_state) change = True if admin_state == "up": self.updates_cmd.append("undo shutdown") else: self.updates_cmd.append("shutdown") if is_portswitch_enalbe(self.intf_type): if mode == "layer2" and self.intf_info["isL2SwitchPort"] != "true": xmlstr += CE_NC_XML_MERGE_INTF_L2ENABLE % (ifname, 'enable') self.updates_cmd.append("portswitch") change = True elif mode == "layer3" \ and self.intf_info["isL2SwitchPort"] != "false": xmlstr += CE_NC_XML_MERGE_INTF_L2ENABLE % (ifname, 'disable') self.updates_cmd.append("undo portswitch") change = True if not change: return conf_str = '<config> ' + xmlstr + ' </config>' recv_xml = set_nc_config(self.module, conf_str) self.check_response(recv_xml, "MERGE_INTF_ATTR") self.changed = True
def merge_process(self): """merge ospf process""" xml_area = "" xml_str = "" self.updates_cmd.append("ospf %s" % self.process_id) # nexthop weight xml_nh = "" if self.nexthop_addr and self.is_nexthop_change(): xml_nh = CE_NC_XML_MERGE_NEXTHOP % (self.nexthop_addr, self.nexthop_weight) self.updates_cmd.append("nexthop %s weight %s" % (self.nexthop_addr, self.nexthop_weight)) # max load balance xml_lb = "" if self.max_load_balance and self.ospf_info.get( "maxLoadBalancing") != self.max_load_balance: xml_lb = CE_NC_XML_SET_LB % self.max_load_balance self.updates_cmd.append("maximum load-balancing %s" % self.max_load_balance) xml_topo = "" if xml_lb or xml_nh: xml_topo = CE_NC_XML_BUILD_MERGE_TOPO % (xml_nh + xml_lb) if self.area: self.updates_cmd.append("area %s" % self.get_area_ip()) xml_network = "" xml_auth = "" if self.addr and self.mask: if not self.is_network_exist(): xml_network += CE_NC_XML_MERGE_NETWORKS % ( self.addr, self.get_wildcard_mask()) self.updates_cmd.append( "network %s %s" % (self.addr, self.get_wildcard_mask())) # NOTE: for security, authentication config will always be update if self.auth_mode: xml_auth += CE_NC_XML_SET_AUTH_MODE % self.auth_mode if self.auth_mode == "none": self.updates_cmd.append("undo authentication-mode") else: self.updates_cmd.append("authentication-mode %s" % self.auth_mode) if self.auth_mode == "simple" and self.auth_text_simple: xml_auth += CE_NC_XML_SET_AUTH_TEXT_SIMPLE % self.auth_text_simple self.updates_cmd.pop() self.updates_cmd.append( "authentication-mode %s %s" % (self.auth_mode, self.auth_text_simple)) if self.auth_mode in ["hmac-sha256", "hmac-sha256", "md5"]: if self.auth_key_id and self.auth_text_md5: xml_auth += CE_NC_XML_SET_AUTH_MD5 % ( self.auth_key_id, self.auth_text_md5) self.updates_cmd.pop() self.updates_cmd.append( "authentication-mode %s %s %s" % (self.auth_mode, self.auth_key_id, self.auth_text_md5)) if xml_network or xml_auth or not self.is_area_exist(): xml_area += CE_NC_XML_BUILD_MERGE_AREA % ( self.get_area_ip(), xml_network + xml_auth) elif self.is_area_exist(): self.updates_cmd.pop() # remove command: area else: pass if xml_area or xml_topo: xml_str = CE_NC_XML_BUILD_MERGE_PROCESS % (self.process_id, xml_topo + xml_area) recv_xml = set_nc_config(self.module, xml_str) self.check_response(recv_xml, "MERGE_PROCESS") self.changed = True