def get_fabric_switches(self): """ Returns all switches within the fabric :return: """ # Leafs class_query = ClassQuery('fabricNode') class_query.propFilter = 'eq(fabricNode.role, "leaf")' leafs = self.moDir.query(class_query) # Two lists are created, one for the distinguished names and other for the relative names dns = [] rns = [] for leaf in leafs: dns.append(str(leaf.dn)) rns.append(str(leaf.rn)) # Spines class_query = ClassQuery('fabricNode') class_query.propFilter = 'eq(fabricNode.role, "spine")' spines = self.moDir.query(class_query) for spine in spines: dns.append(str(spine.dn)) rns.append(str(spine.rn)) # Need to be human sorted (e.g 1,2,3,11 and not 1,11,2,3) dns.sort(key=natural_keys) rns.sort(key=natural_keys) return dns, rns
def delete_single_access(self, epg_dn, port_dn, if_policy_group_name, switch_p_name): """ Removes the static binding between a port and an end point group. If no other EPGs are using this port the system will remove the switch profiles, interface profiles and interface policy groups associated to the port :param epg_dn: :param port_dn: :param if_policy_group_name: :param switch_p_name: :return: """ fabric_path_dn = port_dn.replace('node', 'paths').replace( 'sys/phys', 'pathep') # Filters the EPG children in memory looking for the ones that belongs to the RsPathAtt class # and with an specific tDn rspathatt_list = filter( lambda x: type(x).__name__ == 'RsPathAtt' and str(x.tDn) == fabric_path_dn, self.query_child_objects(epg_dn)) if len(rspathatt_list) > 0: # Removes the static binding rspathatt_list[0].delete() self.commit(rspathatt_list[0]) # If there is not other assignment to this port, the switch profiles and policy groups are removed fabric_path_dn = port_dn.replace('node', 'paths').replace( 'sys/phys', 'pathep') class_query = ClassQuery('fvRsPathAtt') # Filters the all the fvRsPathAtt in memory looking for the ones that are using the port RsPathAtt_list = filter(lambda x: str(fabric_path_dn) in str(x.tDn), self.moDir.query(class_query)) if len(RsPathAtt_list) == 0: # Remove Policy group class_query = ClassQuery('infraAccPortGrp') class_query.propFilter = 'eq(infraAccPortGrp.name, "' + if_policy_group_name + '")' policy_groups = self.moDir.query(class_query) if len(policy_groups) > 0: policy_groups[0].delete() self.commit(policy_groups[0]) # Remove Interface profile port_mo = self.moDir.lookupByDn(port_dn) class_query = ClassQuery('infraAccPortP') class_query.propFilter = 'eq(infraAccPortP.name, "single_access_' + str( port_mo.id).split('/')[1] + '")' interface_profiles = self.moDir.query(class_query) if len(interface_profiles) > 0: interface_profiles[0].delete() self.commit(interface_profiles[0]) # RemoveSwitch profile class_query = ClassQuery('infraNodeP') class_query.propFilter = 'eq(infraNodeP.name, "' + switch_p_name + '")' switch_profiles = self.moDir.query(class_query) if len(switch_profiles) > 0: switch_profiles[0].delete() self.commit(switch_profiles[0])
def delete_single_access(self, epg_dn, port_dn, if_policy_group_name, switch_p_name): """ Removes the static binding between a port and an end point group. If no other EPGs are using this port the system will remove the switch profiles, interface profiles and interface policy groups associated to the port :param epg_dn: :param port_dn: :param if_policy_group_name: :param switch_p_name: :return: """ fabric_path_dn = port_dn.replace('node', 'paths').replace('sys/phys', 'pathep') # Filters the EPG children in memory looking for the ones that belongs to the RsPathAtt class # and with an specific tDn rspathatt_list = filter(lambda x: type(x).__name__ == 'RsPathAtt' and str(x.tDn) == fabric_path_dn, self.query_child_objects(epg_dn)) if len(rspathatt_list) > 0: # Removes the static binding rspathatt_list[0].delete() self.commit(rspathatt_list[0]) # If there is not other assignment to this port, the switch profiles and policy groups are removed fabric_path_dn = port_dn.replace('node', 'paths').replace('sys/phys', 'pathep') class_query = ClassQuery('fvRsPathAtt') # Filters the all the fvRsPathAtt in memory looking for the ones that are using the port RsPathAtt_list = filter(lambda x: str(fabric_path_dn) in str(x.tDn), self.moDir.query(class_query)) if len(RsPathAtt_list) == 0: # Remove Policy group class_query = ClassQuery('infraAccPortGrp') class_query.propFilter = 'eq(infraAccPortGrp.name, "' + if_policy_group_name + '")' policy_groups = self.moDir.query(class_query) if len(policy_groups) > 0: policy_groups[0].delete() self.commit(policy_groups[0]) # Remove Interface profile port_mo = self.moDir.lookupByDn(port_dn) class_query = ClassQuery('infraAccPortP') class_query.propFilter = 'eq(infraAccPortP.name, "single_access_' + str(port_mo.id).split('/')[1] + '")' interface_profiles = self.moDir.query(class_query) if len(interface_profiles) > 0: interface_profiles[0].delete() self.commit(interface_profiles[0]) # RemoveSwitch profile class_query = ClassQuery('infraNodeP') class_query.propFilter = 'eq(infraNodeP.name, "' + switch_p_name + '")' switch_profiles = self.moDir.query(class_query) if len(switch_profiles) > 0: switch_profiles[0].delete() self.commit(switch_profiles[0])
def add_vlan(self, vlan_number, vlan_pool_name): """ Add a vlan to a vlan pool. :param vlan_number: :param vlan_pool_name: Vlan pool name. If it does not exist the system will create one. :return: """ class_query = ClassQuery('fvnsVlanInstP') class_query.propFilter = 'eq(fvnsVlanInstP.name, "' + VLAN_POOL_PREFIX + vlan_pool_name + '")' vp_list = self.moDir.query(class_query) # If the vlan pool does not exists, create it with the physical domain and the attachable entity profile if len(vp_list) == 0: VlanInstP_mo = self.create_vlan_pool( VLAN_POOL_PREFIX + vlan_pool_name, 'static') DomP_mo = self.create_physical_domain(PD_PREFIX + vlan_pool_name, str(VlanInstP_mo.dn)) self.create_attachable_entity_profile(AEP_PREFIX + vlan_pool_name, str(DomP_mo.dn)) else: VlanInstP_mo = vp_list[0] encap_mo = EncapBlk(str(VlanInstP_mo.dn), VLAN_PREFIX + str(vlan_number), VLAN_PREFIX + str(vlan_number), allocMode='static') self.commit(encap_mo)
def create_vpc_if_policy_group(self, name, aep_name): """ Creates the virtual port channel interface policy groups :param name: :param aep_name: attachable entity profile name. If it does not exists the system will create a new one :return: """ policy_group_mo = AccBndlGrp('uni/infra/funcprof/', name, lagT='node') self.commit(policy_group_mo) # if attachable entity profile does not exists, creates a new one class_query = ClassQuery('infraAttEntityP') class_query.propFilter = 'eq(infraAttEntityP.name, "' + AEP_PREFIX + aep_name + '")' pd_list = self.moDir.query(class_query) if len(pd_list) == 0: vlan_pool_mo = self.create_vlan_pool(VLAN_POOL_PREFIX + aep_name, 'static') DomP_mo = self.create_physical_domain(PD_PREFIX + aep_name, str(vlan_pool_mo.dn)) AttEntityP_mo = self.create_attachable_entity_profile(AEP_PREFIX + aep_name, str(DomP_mo.dn)) else: AttEntityP_mo = pd_list[0] # Assign attached entity profile self.commit( RsAttEntP(policy_group_mo.dn, tDn=str(AttEntityP_mo.dn)) ) # Assign interface policies. For non-defaults, check if is already created. If not, the system will create them IfPolmo = self.moDir.lookupByDn('uni/infra/cdpIfP-CDP-ON') if not IfPolmo: IfPolmo = IfPol('uni/infra','CDP-ON',adminSt='enabled') self.commit(IfPolmo) self.commit( RsCdpIfPol(policy_group_mo.dn, tnCdpIfPolName=IfPolmo.name) ) self.commit( RsHIfPol(policy_group_mo.dn, tnFabricHIfPolName='default') ) self.commit( RsL2IfPol(policy_group_mo.dn, tnL2IfPolName='default') ) LagPolmo = self.moDir.lookupByDn('uni/infra/lacplagp-LACP') if not LagPolmo: LagPolmo = LagPol('uni/infra', 'LACP', mode='active') self.commit(LagPolmo) self.commit( RsLacpPol(policy_group_mo.dn, tnLacpLagPolName=LagPolmo.name) ) self.commit( RsLldpIfPol(policy_group_mo.dn, tnLldpIfPolName='default') ) self.commit( RsMcpIfPol(policy_group_mo.dn, tnMcpIfPolName='default') ) self.commit( RsMonIfInfraPol(policy_group_mo.dn, tnMonInfraPolName='default') ) self.commit( RsStormctrlIfPol(policy_group_mo.dn, tnStormctrlIfPolName='default') ) self.commit( RsStpIfPol(policy_group_mo.dn, tnStpIfPolName='default') ) return policy_group_mo
def lookupByClass(self, classNames, parentDn=None, propFilter=None): """Lookup MO's by class A short-form managed object (MO) query by class. Args: classNames (str or list): The class name list of class names. If parentDn is set, the classNames are used as a filter in a subtree query for the parentDn parentDn (cobra.mit.naming.Dn or str): The distinguished name of the parent object as a :class:`cobra.mit.naming.Dn` or string. propFilter (str): A property filter expression Returns: list: A list of the managed objects found in the query. """ if parentDn: dnQuery = DnQuery(parentDn) dnQuery.classFilter = classNames dnQuery.queryTarget = 'subtree' if propFilter: dnQuery.propFilter = propFilter mos = self.query(dnQuery) else: classQuery = ClassQuery(classNames) if propFilter: classQuery.propFilter = propFilter mos = self.query(classQuery) return mos
def create_if_policy_group(self, name, aep_name): """ Creates an interface policy group and associates it to an attachable entity profile :param name: interface policy group name :param aep_name: attachable entity profile. If does not exist the system will create it :return: """ # Creates policy group if_policy_group_mo = AccPortGrp('uni/infra/funcprof/', name) self.commit(if_policy_group_mo) # Query the AEP class_query = ClassQuery('infraAttEntityP') class_query.propFilter = 'eq(infraAttEntityP.name, "' + AEP_PREFIX + aep_name + '")' pd_list = self.moDir.query(class_query) if len(pd_list) == 0: # if attachable entity profile does not exists, creates a new one vlan_pool_mo = self.create_vlan_pool('vlan-pool-' + aep_name, 'static') DomP_mo = self.create_physical_domain('pd-' + aep_name, str(vlan_pool_mo.dn)) AttEntityP_mo = self.create_attachable_entity_profile('aep-' + aep_name, str(DomP_mo.dn)) else: AttEntityP_mo = pd_list[0] # Assign attached entity profile to the policy group self.commit( RsAttEntP(if_policy_group_mo.dn, tDn=str(AttEntityP_mo.dn)) ) # Assign interface policies. For non-defaults, check if is already created. If not, the system will create them IfPolmo = self.moDir.lookupByDn('uni/infra/cdpIfP-CDP-ON') if not IfPolmo: IfPolmo = IfPol('uni/infra','CDP-ON',adminSt='enabled') self.commit(IfPolmo) self.commit( RsCdpIfPol(if_policy_group_mo.dn, tnCdpIfPolName=IfPolmo.name) ) HIfPolmo = self.moDir.lookupByDn('uni/infra/hintfpol-1GB') if not HIfPolmo: HIfPolmo = HIfPol('uni/infra', '1GB', speed='1G') self.commit(HIfPolmo) self.commit( RsHIfPol(if_policy_group_mo.dn, tnFabricHIfPolName=HIfPolmo.name) ) self.commit( RsL2IfPol(if_policy_group_mo.dn, tnL2IfPolName='default') ) self.commit( RsLldpIfPol(if_policy_group_mo.dn, tnLldpIfPolName='default') ) self.commit( RsMcpIfPol(if_policy_group_mo.dn, tnMcpIfPolName='default') ) self.commit( RsMonIfInfraPol(if_policy_group_mo.dn, tnMonInfraPolName='default') ) self.commit( RsStormctrlIfPol(if_policy_group_mo.dn, tnStormctrlIfPolName='default') ) self.commit( RsStpIfPol(if_policy_group_mo.dn, tnStpIfPolName='default') ) return if_policy_group_mo
def get_faults_history(self, epg_dn): """ Retrieves a historic list of all faults associated to an EPG :param epg_dn: :return: """ class_query = ClassQuery('faultRecord') class_query.propFilter = 'eq(faultRecord.affected, "' + epg_dn + '")' return self.moDir.query(class_query)
def get_mo_for_interface(nodeid, portid): cq = ClassQuery('fabricPathEpCont') cq.propFilter = 'eq(fabricPathEpCont.nodeId, "{0}")'.format(nodeid) cq.subtree = 'children' cq.subtreeClassFilter = 'fabricPathEp' req = moDir.query(cq) for key in req[0].children: # children method applied to the query result will return a list of child objects # req[0] is fabricPathEpCont (leaf), children are fabricPathEp (ports) if key.name == portid: return format(key.dn)
def delete_group(self, group_o): """ Removes a group/tenant from the fabric :param group_o: :return: """ class_query = ClassQuery('fvTenant') class_query.propFilter = 'eq(fvTenant.name, "' + group_o.name + '")' tenant_list = self.moDir.query(class_query) if len(tenant_list) > 0: tenant_list[0].delete() self.commit(tenant_list[0])
def create_vpc_if_policy_group(self, name, aep_name): """ Creates the virtual port channel interface policy groups :param name: :param aep_name: attachable entity profile name. If it does not exists the system will create a new one :return: """ policy_group_mo = AccBndlGrp('uni/infra/funcprof/', name, lagT='node') self.commit(policy_group_mo) # if attachable entity profile does not exists, creates a new one class_query = ClassQuery('infraAttEntityP') class_query.propFilter = 'eq(infraAttEntityP.name, "' + AEP_PREFIX + aep_name + '")' pd_list = self.moDir.query(class_query) if len(pd_list) == 0: vlan_pool_mo = self.create_vlan_pool(VLAN_POOL_PREFIX + aep_name, 'static') DomP_mo = self.create_physical_domain(PD_PREFIX + aep_name, str(vlan_pool_mo.dn)) AttEntityP_mo = self.create_attachable_entity_profile( AEP_PREFIX + aep_name, str(DomP_mo.dn)) else: AttEntityP_mo = pd_list[0] # Assign attached entity profile self.commit(RsAttEntP(policy_group_mo.dn, tDn=str(AttEntityP_mo.dn))) # Assign interface policies. For non-defaults, check if is already created. If not, the system will create them IfPolmo = self.moDir.lookupByDn('uni/infra/cdpIfP-CDP-ON') if not IfPolmo: IfPolmo = IfPol('uni/infra', 'CDP-ON', adminSt='enabled') self.commit(IfPolmo) self.commit(RsCdpIfPol(policy_group_mo.dn, tnCdpIfPolName=IfPolmo.name)) self.commit(RsHIfPol(policy_group_mo.dn, tnFabricHIfPolName='default')) self.commit(RsL2IfPol(policy_group_mo.dn, tnL2IfPolName='default')) LagPolmo = self.moDir.lookupByDn('uni/infra/lacplagp-LACP') if not LagPolmo: LagPolmo = LagPol('uni/infra', 'LACP', mode='active') self.commit(LagPolmo) self.commit( RsLacpPol(policy_group_mo.dn, tnLacpLagPolName=LagPolmo.name)) self.commit(RsLldpIfPol(policy_group_mo.dn, tnLldpIfPolName='default')) self.commit(RsMcpIfPol(policy_group_mo.dn, tnMcpIfPolName='default')) self.commit( RsMonIfInfraPol(policy_group_mo.dn, tnMonInfraPolName='default')) self.commit( RsStormctrlIfPol(policy_group_mo.dn, tnStormctrlIfPolName='default')) self.commit(RsStpIfPol(policy_group_mo.dn, tnStpIfPolName='default')) return policy_group_mo
def remove_vlan(self, vlan_number, vlan_pool_name): """ Removes a VLAN from a vlan pool :param vlan_number: :param vlan_pool_name: :return: """ class_query = ClassQuery('fvnsVlanInstP') class_query.propFilter = 'eq(fvnsVlanInstP.name, "' + VLAN_POOL_PREFIX + vlan_pool_name + '")' vp_list = self.moDir.query(class_query) # Check if vlan pool exists if len(vp_list) == 0: vlan_pool_children = self.query_child_objects(str(vp_list[0].dn)) for vlan in vlan_pool_children: if vlan.to == 'vlan-' + str(vlan_number): vlan.delete() self.commit(vlan) break
def add_vlan(self, vlan_number, vlan_pool_name): """ Add a vlan to a vlan pool. :param vlan_number: :param vlan_pool_name: Vlan pool name. If it does not exist the system will create one. :return: """ class_query = ClassQuery('fvnsVlanInstP') class_query.propFilter = 'eq(fvnsVlanInstP.name, "' + VLAN_POOL_PREFIX + vlan_pool_name + '")' vp_list = self.moDir.query(class_query) # If the vlan pool does not exists, create it with the physical domain and the attachable entity profile if len(vp_list) == 0: VlanInstP_mo = self.create_vlan_pool(VLAN_POOL_PREFIX + vlan_pool_name, 'static') DomP_mo = self.create_physical_domain(PD_PREFIX + vlan_pool_name, str(VlanInstP_mo.dn)) self.create_attachable_entity_profile(AEP_PREFIX + vlan_pool_name, str(DomP_mo.dn)) else: VlanInstP_mo = vp_list[0] encap_mo = EncapBlk(str(VlanInstP_mo.dn), VLAN_PREFIX + str(vlan_number), VLAN_PREFIX + str(vlan_number), allocMode='static') self.commit(encap_mo)
def associate_epg_physical_domain(self, epg_dn, physical_domain_name): """ Associate a physical domain to an end point group :param epg_dn: :param physical_domain_name: :return: """ # Query the physical domain according to an specific name class_query = ClassQuery('physDomP') class_query.propFilter = 'eq(physDomP.name, "' + PD_PREFIX + physical_domain_name + '")' pd_list = self.moDir.query(class_query) # If the physical domain does not exists, create it with the vlan pool and the attachable entity profile if len(pd_list) == 0: vlan_pool_mo = self.create_vlan_pool(VLAN_POOL_PREFIX + physical_domain_name, 'static') DomP_mo = self.create_physical_domain(PD_PREFIX + physical_domain_name, str(vlan_pool_mo.dn)) self.create_attachable_entity_profile(AEP_PREFIX + physical_domain_name, str(DomP_mo.dn)) else: DomP_mo = pd_list[0] # Creates and commits the association rsdom = RsDomAtt(epg_dn, str(DomP_mo.dn)) self.commit(rsdom)
def get_leafs(self): """ Returns the leafs that are registered in the APIC :return: """ # Query leafs from the fabric class_query = ClassQuery('fabricNode') class_query.propFilter = 'eq(fabricNode.role, "leaf")' leafs = self.moDir.query(class_query) # creates two lists that will include the distinguished names and the relative names result = [] dns = [] rns = [] for leaf in leafs: dns.append(str(leaf.dn)) rns.append(str(leaf.rn)) # The following lines human sort the lists (e.g. 1,2,3,11 and not 1,11,2,3) dns.sort(key=natural_keys) rns.sort(key=natural_keys) result.append(dns) result.append(rns) # The result is a list with two lists inside. One list has distinguished names and the other the relative names return result
def get_epg_staticpaths_from_pg_name(pg_name): cq = ClassQuery('fvRsPathAtt') cq.propFilter = 'wcard(fvRsPathAtt.tDn, "{0}")'.format(pg_name) my_result = moDir.query(cq) return my_result # return list of objects
def get_class_mo(session, refClass, propFilter): #help(ClassQuery) classQuery = ClassQuery(refClass) classQuery.queryTarget = 'subtree' classQuery.propFilter = propFilter return session.query(classQuery)
def get_epg_staticpaths_from_int_mo(int_mo): cq = ClassQuery('fvRsPathAtt') cq.propFilter = 'eq(fvRsPathAtt.tDn, "{0}")'.format(int_mo) my_result = moDir.query(cq) return my_result