def deviceStatus(): logger.info("*** VALIDATING DEVICE STATUS ***") DeviceState = {} devices = getDevices() for device in devices: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None swtch = FlexSwitch(DeviceIP, http_port, user=Username, passwd=Password) logger.info("Checking the status of %s" % (device)) try: logger.info("%s.getAllSystemStatusStates()" % device) response = swtch.getAllSystemStatusStates() #logger.info("RESULT: %s"%response) logger.info("DEVICE %s --> EXISTS" % device) DeviceState.update({device: "Exists"}) except Exception as ex: logger.debug("%s,--> %s" % (type(ex).__name__, device)) logger.info("DEVICE %s --> DOESN'T EXISTS" % device) DeviceState.update({device: "None"}) if re.search("None", str(DeviceState)): logger.info("*** DEVICE STATUS ***") logger.debug(DeviceState) return False else: return True
def getAllIPv4RouteStates(args=[]): logger.info("*** CHECKING IP ROUTE TABLE ***") StatusList = {} if args: for device in args: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None swtch = FlexSwitch(DeviceIP,http_port,Username,Password) logger.info("%s.getAllIPv4RouteStates()"%device) response = swtch.getAllIPv4RouteStates() logger.info("IPV4 ROUTE OBJECT DETAILS:%s"%(response)) if response: StatusList.update({"%s"%(device) : "PASS"}) else: StatusList.update({"%s"%(device) :"FAIL"}) if "FAIL" in StatusList.values(): logger.info("%s"%StatusList) return False else: return True else: logger.info("PLEASE PROVIDE DEVICE AND REQUIRED PARAMETERS TO VERIFY ROUTES IN IPV4 TABLE") sys.exit(1)
def getBGPv4NeighborState(args = {}): logger.info("*** CHECKING BGP NEIGHBOR STATUS ***") StatusList = [] if args: for device in args.keys(): DeviceIP = getMgmtIP(device) Username = getUsername(device) Password = getPassword(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for peer in args[device].keys(): intfref = args[device][peer]["IntfRef"] neighboraddress = args[device][peer]["NeighborAddress"] Parameter = args[device][peer]["CheckParameter"] logger.info("Checking BGP Neighbor Status on device %s"%device) swtch = FlexSwitch(DeviceIP,http_port,Username,Password) logger.info ("getBGPv4NeighborState(%s,%s)"%(intfref,neighboraddress)) response = swtch.getBGPv4NeighborState(IntfRef = intfref, NeighborAddress = neighboraddress) logger.info(response) jData = json.loads(response.content) logger.info(jData) if Parameter == "SessionState": if str(response) == "<Response [200]>": logger.info("BGP NEIGHBOR STATUS ON %s"%device) if int(jData["Object"][Parameter]) == 1: check = "IDLE" elif int(jData["Object"][Parameter]) == 2: check = "CONNECT" elif int(jData["Object"][Parameter]) == 3: check = "ACTIVE" elif int(jData["Object"][Parameter]) == 4: check = "OPENSENT" elif int(jData["Object"][Parameter]) == 5: check = "OPENCONFIRM" elif int(jData["Object"][Parameter]) == 6: check = "ESTABLISHED" else: check = "Unknown State" logger.info("The BGP session state of peer %s is %s"%(peer,check)) if check == args[device][peer]["ExpectedState"]: StatusList.append("EXPECTED") else: StatusList.append("NOT EXPECTED") else: StatusList.append("Neighbor Does Not Exist") logger.info(StatusList) if (("NOT EXPECTED" in StatusList) or ("Neighbor Does Not Exist" in StatusList)): return False else: return True else: logger.info("PLEASE PROVIDE DEVICE AND REQUIRED PARAMETERS TO GET BGP NEIGHBORS STATE") sys.exit(1)
def createPolicyDefinition(args = {}): matchtype, policytype, priority, statementlist = "all","ALL",0,None logger.info("*** CREATING POLICY DEFINITION ***") StatusList = {} if args: for device in args: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for parameter in args[device].keys(): if parameter == "Name": name = args[device][parameter] if parameter == "MatchType": matchtype = args[device][parameter] if parameter == "PolicyType": policytype = args[device][parameter] if parameter == "Priority": priority = args[device][parameter] if parameter == "StatementList": statementlist = args[device][parameter] swtch = FlexSwitch(DeviceIP,http_port,Username,Password) response = swtch.createPolicyDefinition(Name=name, MatchType=matchtype, PolicyType=policytype, Priority=priority, StatementList=statementlist) jData = json.loads(response.content) logger.info(jData) if jData['Result'] == "Success": logger.info("RESULT:%s"%jData['Result']) logger.info("POLICY DEFINITION OBJECT DETAILS") logger.info(swtch.getAllPolicyDefinitions()) logger.info("Policy Defined on %s"%device) StatusList.update({device:"PASS"}) elif jData['Result'] == "Error: Already configured. Delete and Update operations are allowed.": logger.info("RESULT:%s"%jData['Result']) logger.info("POLICY STATEMENT OBJECT DETAILS") logger.info(swtch.getAllPolicyStmts()) logger.info("Policy Statement Added on %s"%device) StatusList.update({device:"PASS"}) else: logger.debug("ERROR :%s"%jData['Result']) StatusList.update({device:"FAIL"}) if "FAIL" in StatusList.values(): logger.info("%s" %StatusList) return False else: return True else: logger.info("PLEASE PROVIDE ONE/MORE DEVICE WITH RELATED PARAMETERS TO CONFIGURE POLICY DEFINITION") sys.exit(1)
def createIPv4Intf(args={}): if args: logger.info("*** CONFIGURING IP ADDRESS ON INTERFACES OF DEVICES ***") StatusList = {} for device in args.keys(): DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for intf in args[device].keys(): IfaceIP = args[device][intf] logger.info( "Configuring IP Address on %s interface of %s with IP %s" % (intf, device, IfaceIP)) swtch = FlexSwitch(DeviceIP, http_port, user=Username, passwd=Password) logger.info("createIPv4Intf(%s) : %s" % (intf, device)) response = swtch.createIPv4Intf(intf, IfaceIP, AdminState='UP') jData = json.loads(response.content) if jData['Result'] == "Success": logger.info("RESULT : %s" % jData['Result']) response1 = swtch.getIPv4Intf(intf) logger.info("IP CONFIGURATION STATUS") logger.info("INTF OBJECT DETAILS:%s" % json.loads(response1.content)) logger.info("IP address configured on %s interface of %s" % (intf, device)) StatusList.update({"%s[%s]" % (device, intf): "PASS"}) else: logger.info("ERROR:%s" % jData['Result']) logger.info( "IP address could not be configured on %s interface of %s" % (intf, device)) StatusList.update({"%s[%s]" % (device, intf): "FAIL"}) if "FAIL" in StatusList.values(): logger.debug(StatusList) return False else: logger.info(StatusList) return True else: logger.info( "Please specify the list of devices and it's interface ip addresses in the arguments" ) sys.exit(1)
def adminStatus(): logger.info("***VALIDATING INTERFACE STATUS***") global LinkStartVal global LinkmaxVal global LinkStepVal portAdminState = {} linkStateList = {} links = getLinks() for device in getDevices(): for port in getPorts(device): portAdminState.update({port: ""}) DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None swtch = FlexSwitch(DeviceIP, http_port, user=Username, passwd=Password) logger.info("Checking admin state of %s port of %s" % (port, device)) try: logger.info("%s.getPort()" % device) response = swtch.getPort(port) logger.info("RESULT: %s" % response) jdata = json.loads(response.content) logger.info("PORT OBJECT DETAILS: %s" % jdata) AdminState = jdata['Object']["AdminState"] logger.info("The AdminState on interface %s is %s" % (port, AdminState)) portAdminState[port] = AdminState if AdminState == "DOWN": response = swtch.updatePort(port, "UP") logger.info("IN here %s" % response) while (LinkStartVal < LinkmaxVal): time.sleep(interval) LinkStartVal += LinkStepVal linkStatus() else: continue except Exception as ex: logger.info("%s" % (type(ex)), also_console=True) logger.info(portAdminState) if "DOWN" in portAdminState.values(): logger.info("*** LINK STATUS ***") logger.debug("The one/more links is DOWN. Please check the setup") logger.debug(linkStateList) return False else: return True
def linkStatus(): logger.info("***VALIDATING LINK STATUS***") operStateList = {} Links = getLinks() status = [] for link in Links: EndDevices = getConnectedDevices(link) for device in EndDevices: ConnectedPort = getConnectedPort(link, device) DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None swtch = FlexSwitch(DeviceIP, http_port, user=Username, passwd=Password) logger.info("Checking operational state of %s port of %s" % (ConnectedPort, device)) try: logger.info("%s.getPortState()" % device) response = swtch.getPortState(ConnectedPort) logger.info("RESULT: %s" % response) jdata = json.loads(response.content) logger.info("PORT STATE OBJECT DETAILS: %s" % jdata) operState = jdata['Object']["OperState"] logger.info("OperState on %s is %s" % (ConnectedPort, operState)) if operState == "UP": status.append(True) else: status.append(False) except Exception as ex: logger.info("%s" % (type(ex))) status.append(False) if False in status: logger.info("The operState of link %s is DOWN" % link) operStateList[link] = "DOWN" else: logger.info("The operState of link %s is UP" % link) operStateList[link] = "UP" if "DOWN" in operStateList.values(): logger.debug( "The operState of one/more links is DOWN. Please check the setup") logger.debug(operStateList) return False else: logger.info("The operState of all links are UP") return True
def createPolicyStmt(args = {}): name, conditions, action, matchconditions = None,None,'deny','all' logger.info("*** CREATING POLICY STATEMENT ***") StatusList = {} if args: for device in args: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for parameter in args[device].keys(): if parameter == "Name": name = args[device][parameter] if parameter == "MatchConditions": matchconditions = args[device][parameter] if parameter == "Action": action = args[device][parameter] if parameter == "Conditions": conditions = args[device][parameter] swtch = FlexSwitch(DeviceIP,http_port,Username,Password) response = swtch.createPolicyStmt(Name=name, MatchConditions=matchconditions, Action=action, Conditions=conditions) jData = json.loads(response.content) logger.info(jData) if jData['Result'] == "Success": logger.info("RESULT:%s"%jData['Result']) logger.info("POLICY STATEMENT OBJECT DETAILS") logger.info(swtch.getAllPolicyStmts()) logger.info("Policy Statement Added on %s"%device) StatusList.update({device:"PASS"}) elif jData['Result'] == "Error: Already configured. Delete and Update operations are allowed.": logger.info("RESULT:%s"%jData['Result']) logger.info("POLICY STATEMENT OBJECT DETAILS") logger.info(swtch.getAllPolicyStmts()) logger.info("Policy Statement Added on %s"%device) StatusList.update({device:"PASS"}) else: logger.debug("ERROR :%s"%jData['Result']) StatusList.update({device:"FAIL"}) if "FAIL" in StatusList.values(): logger.info("%s" %StatusList) return False else: return True else: logger.info("PLEASE PROVIDE ONE/MORE DEVICE WITH RELATED PARAMETERS TO CONFIGURE POLICY STATEMENT") sys.exit(1)
def deleteIPv4Intf(args={}): if args: logger.info("UNCONFIGURING IP ADDRESS ON DEVICE[S]") StatusList = {} for device in args.keys(): DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for intf in args[device]: logger.info("Unconfiguring IP Address on %s interface of %s " % (intf, device)) swtch = FlexSwitch(DeviceIP, http_port, user=Username, passwd=Password) logger.info("deleteIPv4(%s) : %s" % (intf, device)) response = swtch.deleteIPv4Intf(intf) jData = json.loads(response.content) if jData['Result'] == "Success": logger.info("RESULT : %s" % jData['Result']) logger.info("VALIDATING IP ADDRESS UNCONFIGURATION") response = swtch.getIPv4Intf(intf) logger.info("RESULT:%s" % response) logger.info( "Failed To Find Entry of the interface, thus ip address is unconfigured" ) StatusList.update({"%s[%s]" % (device, intf): "PASS"}) else: logger.info(jData['Result']) logger.info( "IP address could not be unconfigured on %s interface of %s" % (intf, device)) StatusList.update({"%s[%s]" % (device, intf): "FAIL"}) if "FAIL" in StatusList.values(): logger.info(StatusList) return False else: logger.info(StatusList) return True else: logger.info( "Please specify the list of devices and it's interfaces in the arguments" ) sys.exit(1)
def deleteBGPv4Neighbor(args={}): neighboraddress,intfref,description,peergroup,peeras,localas,updatesource,authpassword,adjribinfilter,adjriboutfilter,bfdenable, multihopttl, keepalivetime, addpathsrx, routereflectorclient, maxprefixesrestarttimer, multihopenable, routereflectorclusterid, maxprefixesdisconnect, addpathsmaxtx, maxprefixes, maxprefixesthresholdpct, bfdsessionparam,nexthopself,disabled,holdtime, connectretrytime = None,None,'','' ,'' ,'' ,'', '', '', '', False, 0, 0, False, False, 0, False, 0, False, 0, 0, 80, 'default', False, False, 0, 0 logger.info("*** UNCONFIGURING BGP NEIGHBORS ON DEVICES ***") StatusList = {} if args: for device in args: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for Peer in args[device].keys(): for parameter in args[device][Peer].keys(): if parameter == "IntfRef": intfref = args[device][Peer][parameter] if parameter == "NeighborAddress": neighboraddress = args[device][Peer][parameter] logger.info("Unconfiguring BGP Neighbors on device %s"%device) swtch = FlexSwitch(DeviceIP,http_port,Username,Password) logger.info("%s.deleteBGPv4Neighbor(%s,%s)"%(device,intfref,neighboraddress)) response = swtch.deleteBGPv4Neighbor(IntfRef=intfref,NeighborAddress=neighboraddress) jData = json.loads(response.content) if jData['Result'] == "Success": logger.info("RESULT:%s"%jData['Result']) response =swtch.getAllBGPv4Neighbors() logger.info("BGP NEIGHBOR OBJECT DETAILS:%s"%response) logger.info("BGP Neighbor unconfigured on %s"%device) StatusList.update({device:"PASS"}) else: logger.debug("ERROR :%s"%jData['Result']) StatusList.update({device:"FAIL"}) if "FAIL" in StatusList.values(): logger.info("%s"%StatusList) return False else: return True else: logger.info("PLEASE PROVIDE DEVICE AND REQUIRED PARAMETERS TO UNCONFIGURE BGP NEIGHBORS") sys.exit(1)
def getIPv4IntfState(args={}): if args: logger.info("*** CHECKING INTERFACE STATUS OF ALL DEVICES ***") StatusList = {} for device in args.keys(): DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for intf in args[device]: logger.info("Checking interface state on %s interface of %s " % (intf, device)) swtch = FlexSwitch(DeviceIP, http_port, user=Username, passwd=Password) logger.info("getIPv4IntfState(%s) : %s" % (intf, device)) response = swtch.getIPv4IntfState(intf) logger.info("RESULT: %s" % response) logger.info("INTF STATE OBJECT DETAILS:%s" % json.loads(response.content)) if str(response) == "<Response [200]>": logger.info("%s interface of %s UP" % (intf, device)) StatusList.update({"%s[%s]" % (device, intf): "UP"}) else: logger.info("%s interface of %s DOWN" % (intf, device)) StatusList.update({"%s[%s]" % (device, intf): "DOWN"}) if "DOWN" in StatusList.values(): logger.info(StatusList) return False else: logger.debug(StatusList) return True else: logger.info("*** Please specify the list of devices ***") sys.exit(1)
def portStatus(): logger.info("***VALIDATING PORT EXISTENCE***") devices = getDevices() PortState = {} for device in devices: PortState.update({device: {}}) ports = getPorts(device) DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None swtch = FlexSwitch(DeviceIP, http_port, user=Username, passwd=Password) for port in ports: logger.info("Checking %s port of %s" % (port, device)) try: logger.info("getPort(%s): %s" % (port, device)) response = swtch.getPort(port) jdata = json.loads(response.content) logger.info("RESULT: %s" % response) logger.info("PORT OBJECT DETAILS:%s" % jdata) if (str(response)) != "<Response [200]>": logger.info("PORT %s of %s --> DOESN'T EXISTS" % (port, device)) PortState[device].update({port: "None"}) else: logger.info("PORT %s of %s --> EXISTS" % (port, device)) PortState[device].update({port: "Exists"}) except Exception as ex: logger.info("RESULT: %s" % response) logger.debug("%s,--> %s" % (type(ex).__name__, device)) logger.info("PORT %s of %s --> DOESN'T EXISTS" % (port, device)) PortState[device].update({port: "None"}) if re.search("None", str(PortState)): logger.info("*** PORT STATUS ***") logger.debug(PortState) return False else: return True
def getAllArpLinuxEntryStates(args = []): logger.info("*** CHECKING ARP TABLE ***") StatusList = {} if args: for device in args: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None logger.info("Checking ARP Table on %s"%device) ARPObj = FlexSwitch(DeviceIP, http_port, Username, Password) logger.info(ARPObj.getAllArpLinuxEntryStates()) else: logger.info("Please specify the list of devices") sys.exit(1) return True
def updateArpGlobal(args={}): logger.info("*** ENABLING ARP GLOBAL ON DEVICES ***") Vrf, Timeout = "default",600 StatusList = {} if args: for device in args: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for parameter in args[device].keys(): if parameter == "Vrf": Vrf = args[device][parameter] if parameter == "Timeout": Timeout = args[devce][parameter] logger.info("Enabling ARP on device %s"%device) ARPObj = FlexSwitch(DeviceIP, http_port, Username, Password) logger.info("updateArpGlobal():%s"%device) output = ARPObj.updateArpGlobal(Vrf,Timeout) jData = json.loads(output.content) logger.info(jData) if jData['Result'] == "Success": logger.info("RESULT:%s"%jData['Result']) logger.info("ARP Enabled on %s"%device) StatusList.update({device:"ARP Enabled"}) else: logger.debug("ERROR :%s"%jData['Result']) logger.info("ARP not enabled in %s"%device) StatusList.update({device:"None"}) if "None" in StatusList.values(): logger.info("ERROR : %s" %StatusList) return False else: return True else: logger.info("Please specify the dictionary of devices with ARP details to be configured") sys.exit(1)
def getArpLinuxEntryState(device, intf): logger.info("*** CHECKING ARP TABLE FOR ENTRY ***") StatusList = {} DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None logger.info("Checking ARP Table on %s"%device) ARPObj = FlexSwitch(DeviceIP, http_port, Username, Password) logger.info("getArpLinuxEntryState(%s)"%intf) response = ARPObj.getArpLinuxEntryState(intf) logger.info("RESULT:%s"%response) logger.info(json.loads(response.content)) if response == "<Response [200]>": return True else: return False
def updatePort(device, IntfRef, adminState): logger.info("UPDATING THE PORT STATUS") DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None logger.info("Updating port %s of %s to %s" % (IntfRef, device, adminState)) swtch = FlexSwitch(DeviceIP, http_port, user=Username, passwd=Password) logger.info("updatePort(%s,%s) : %s" % (IntfRef, adminState, device)) response = swtch.updatePort(IntfRef=IntfRef, AdminState=adminState) time.sleep(1) jData = json.loads(response.content) portState = json.loads( swtch.getPortState(IntfRef).content)["Object"]["OperState"] if jData['Result'] == "Success": logger.info("RESULT : %s" % jData['Result']) logger.info("PORT %s STATUS : %s" % (IntfRef, portState)) if portState == adminState: return True else: return False elif jData['Result'] == "Error: Nothing to be updated.": logger.info("RESULT : %s" % jData['Result']) logger.info("PORT %s STATUS : %s" % (IntfRef, portState)) if portState == adminState: return True else: return False else: logger.info("ERROR : %s" % jData['Result']) logger.info("PORT %s STATUS : %s" % (IntfRef, portState)) if portState == adminState: return True else: return False
def executeArpRefreshByIPv4Addr(device, IpAddr): logger.info("*** REFRESHING ARP TABLE ***") StatusList = {} DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None logger.info("Refreshing ARP Table on %s"%device) ARPObj = FlexSwitch(DeviceIP, http_port, Username, Password) logger.info("executeArpRefreshByIPv4Addr(%s)"%IpAddr) response = ARPObj.executeArpRefreshByIPv4Addr(IpAddr) logger.info("RESULT:%s"%response) #logger.info(json.loads(response.content)) if response == "<Response [200]>": return True else: return False
def getBGPv4Neighbor(args = {}): logger.info("*** VERIFYING BGP NEIGHBOR CONFIGURATION ***") StatusList = {} if args: for device in args: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for Peer in args[device].keys(): for parameter in args[device][Peer].keys(): if parameter == "IntfRef": intfref = args[device][Peer][parameter] if parameter == "NeighborAddress": neighboraddr = args[device][Peer][parameter] swtch = FlexSwitch(DeviceIP,http_port,Username,Password) logger.info("%s.getBGPv4Neighbor(%s,%s)"%(device,intfref,neighboraddr)) response = swtch.getBGPv4Neighbor(IntfRef = intfref, NeighborAddress = neighboraddr) logger.info("BGP NEIGHBOR OBJECT DETAILS:%s"%(json.loads(response.content))) if (str(response)) != "<Response [200]>": StatusList.update({"%s[%s][%s][%s]"%(device,Peer,intfref,neighboraddr) : "FAIL"}) else: StatusList.update({"%s[%s][%s][%s]"%(device,Peer,intfref,neighboraddr):"PASS"}) if "FAIL" in StatusList.values(): logger.info("%s"%StatusList) return False else: return True else: logger.info("PLEASE PROVIDE DEVICE AND REQUIRED PARAMETERS TO UNCONFIGURE BGP NEIGHBORS") sys.exit(1)
def bgpRedistribution(args={}): vrf,asnum,routerid,usemultiplepaths,ebgpmaxpaths,ebgpallowmultipleas,disabled,ibgpmaxpaths,redistribution = "default", "","0.0.0.0", False, False, 0, False, 0,[] logger.info("*** REDISTRIBUTING ROUTES INTO BGP ***") StatusList = {} if args: for device in args: DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for parameter in args[device].keys(): if parameter == "Vrf": vrf = args[device][parameter] if parameter == "Disabled": disabled = args[device][parameter] if parameter == "EBGPAllowMultipleAS": ebgpallowmultipleas = args[device][parameter] if parameter == "EBGPMaxPaths": ebgpmaxpaths = args[device][parameter] if parameter == "Redistribution": redistribution = args[device][parameter] if parameter == "RouterId": routerid = args[device][parameter] if parameter == "UseMultiplePaths": usemultiplepaths = args[device][parameter] if parameter == "ASNum": asnum = args[device][parameter] if parameter == "IBGPMaxPaths": ibgpmaxpaths = args[device][parameter] logger.info("Redistributing Routes into BGP on device %s"%device) swtch = FlexSwitch(DeviceIP,http_port,Username,Password) response = swtch.updateBGPGlobal(Vrf = vrf, Disabled = disabled, EBGPAllowMultipleAS = ebgpallowmultipleas, EBGPMaxPaths = ebgpmaxpaths, Redistribution = redistribution, RouterId = routerid, UseMultiplePaths = usemultiplepaths, ASNum = asnum, IBGPMaxPaths = ibgpmaxpaths) jData = json.loads(response.content) logger.info(jData) if jData['Result'] == "Success": logger.info("RESULT:%s"%jData['Result']) logger.info("BGP GLOBAL OBJECT(REDISTRIBUTION) DETAILS") logger.info(swtch.getAllBGPGlobals()) logger.info("BGP Redistribution enabled on %s"%device) StatusList.update({device:"PASS"}) else: logger.debug("ERROR :%s"%jData['Result']) StatusList.update({device:"FAIL"}) if "FAIL" in StatusList.values(): logger.info("%s"%StatusList) return False else: return True else: logger.info("PLEASE PROVIDE DEVICE AND REQUIRED PARAMETERS TO ENABLE BGP REDISTRIBUTION") sys.exit(1)
def createBGPv4Neighbor(args={}): description,peergroup,peeras,localas,updatesource,authpassword,adjribinfilter,adjriboutfilter,bfdenable, multihopttl, keepalivetime, addpathsrx, routereflectorclient, maxprefixesrestarttimer, multihopenable, routereflectorclusterid, maxprefixesdisconnect, addpathsmaxtx, maxprefixes, maxprefixesthresholdpct, bfdsessionparam,nexthopself,disabled,holdtime, connectretrytime ='','' ,'' ,'' ,'', '', '', '', False, 0, 0, False, False, 0, False, 0, False, 0, 0, 80, 'default', False, False, 0, 0 logger.info("*** CONFIGURING BGP NEIGHBORS ON DEVICES ***") StatusList = {} if args: for device in args.keys(): DeviceIP = getMgmtIP(device) Username = getUsername(device) if Username == "None" or Username == "": Username = None Password = getPassword(device) if Password == "None" or Password == "": Password = None for Peer in args[device].keys(): for parameter in args[device][Peer].keys(): if parameter == "NeighborAddress": neighboraddress = args[device][Peer][parameter] if parameter == "IntfRef": intfref = args[device][Peer][parameter] if parameter == "AdjRIBInFilter": adjribinfilter = args[device][Peer][parameter] if parameter == "AdjRIBOutFilter": adjriboutfilter = args[device][Peer][parameter] if parameter == "PeerAS": peeras = args[device][Peer][parameter] if parameter == "MaxPrefixesDisconnect": maxprefixesdisconnect = args[device][Peer][parameter] if parameter == "MaxPrefixesRestartTimer": maxprefixesrestarttimer = args[device][Peer][parameter] if parameter == "MultiHopTTL": multihopttl = args[device][Peer][parameter] if parameter == "LocalAS": localas = args[device][Peer][parameter] if parameter == "MaxPrefixesThresholdPct": maxprefixesthresholdpct = args[device][Peer][parameter] if parameter == "ConnectRetryTime": connectretrytime = args[device][Peer][parameter] if parameter == "Description": description = args[device][Peer][parameter] if parameter == "MaxPrefixes": maxprefixes = args[device][Peer][parameter] if parameter == "MultiHopEnable": multihopenable = args[device][Peer][parameter] if parameter == "RouteReflectorClient": routereflectorclient = args[device][Peer][parameter] if parameter == "AddPathsRx": addpathsrx = args[device][Peer][parameter] if parameter == "PeerGroup": peergroup = args[device][Peer][parameter] if parameter == "BfdSessionParam": bfdsessionparam = args[device][Peer][parameter] if parameter == "Disabled": disabled = args[device][Peer][parameter] if parameter == "NextHopSelf": nexthopself = args[device][Peer][parameter] if parameter == "RouteReflectorClusterId": routereflectorclusterid = args[device][Peer][parameter] if parameter == "AddPathsMaxTx": addpathsmaxtx = args[device][Peer][parameter] if parameter == "BfdEnable": bfdenable = args[device][Peer][parameter] if parameter == "KeepaliveTime": keepalivetime = args[device][Peer][parameter] if parameter == "UpdateSource": updatesource = args[device][Peer][parameter] if parameter == "AuthPassword": authpassword = args[device][Peer][parameter] if parameter == "HoldTime": holdtime = args[device][Peer][parameter] logger.info("Configuring BGP Neighbor on %s"%device) swtch = FlexSwitch(DeviceIP,http_port,Username,Password) response = swtch.createBGPv4Neighbor(NeighborAddress=neighboraddress, IntfRef=intfref, AdjRIBInFilter=adjribinfilter, AdjRIBOutFilter=adjriboutfilter, PeerAS=peeras, MaxPrefixesDisconnect=maxprefixesdisconnect, MaxPrefixesRestartTimer=maxprefixesrestarttimer, MultiHopTTL=multihopttl, LocalAS=localas, MaxPrefixesThresholdPct=maxprefixesthresholdpct, ConnectRetryTime=connectretrytime, Description=description, MaxPrefixes=maxprefixes, MultiHopEnable=multihopenable, RouteReflectorClient=routereflectorclient, AddPathsRx=addpathsrx, PeerGroup=peergroup, BfdSessionParam=bfdsessionparam, Disabled=disabled, NextHopSelf=nexthopself, RouteReflectorClusterId=routereflectorclusterid, AddPathsMaxTx=addpathsmaxtx, BfdEnable=bfdenable, KeepaliveTime=keepalivetime, UpdateSource=updatesource, AuthPassword=authpassword, HoldTime=holdtime) jData = json.loads(response.content) logger.info(jData) if jData['Result'] == "Success": logger.info("RESULT:%s"%jData['Result']) response = swtch.getAllBGPv4Neighbors() logger.info("BGP NEIGHBOR OBJECT DETAILS:%s"%response) logger.info("BGP Neighbor configured on %s"%device) StatusList.update({device:"PASS"}) elif jData['Result'] == "Error: Already configured. Delete and Update operations are allowed.": logger.info("RESULT:%s"%jData['Result']) logger.info("POLICY STATEMENT OBJECT DETAILS") logger.info(swtch.getAllPolicyStmts()) logger.info("Policy Statement Added on %s"%device) StatusList.update({device:"PASS"}) else: logger.info("BGP Neighbor not configured on %s"%device) StatusList.update({device:"FAIL"}) if "FAIL" in StatusList.values(): logger.info("%s" %StatusList) return False else: return True else: logger.info("PLEASE PROVIDE ONE/MORE DEVICE AND RELATED PARAMETERS INORDER TO CONFIGURE BGP NEIGHBORS ") sys.exit(1)