def loadYAML(nameFile): if os.path.isfile(nameFile) == True: with open(nameFile, 'r') as stream: try: globals().update(FactoryLoader().load(yaml.load(stream))) except yaml.YAMLError as exc: print(exc)
def test_cfgtable_set_inactive(self, mock_execute): yaml_auto_data = \ """--- UserConfigTable1: set: system/login key-field: - username view: UserConfigView1 UserConfigView1: groups: auth: authentication fields: user: user username: user/name classname: { user/class : { 'type' : { 'enum' : ['operator', 'read-only', 'super-user'] } } } uid: { user/uid : { 'type' : 'int', 'minValue' : 100, 'maxValue' : 64000 } } fields_auth: password: user/encrypted-password """ globals().update(FactoryLoader().load(yaml.load(yaml_auto_data, Loader=yaml.FullLoader))) at = UserConfigTable1(self.dev) at.rpc.lock_configuration = MagicMock() at.username = '******' at.user = {"inactive" : "inactive"} at.append() at.set() xml = at.get_table_xml() self.assertEqual( xml.xpath('system/login/user[@inactive="inactive"]/name')[0].text, 'user1' )
def get_bgp_routes(host,user,password,route,lsys): # Load the classes created by the FactoryLoader with the YAML to the global variables so they can be referenced globals().update(FactoryLoader().load(yaml.load(yml))) dev = Device(host=host, user=user, password=password, gather_facts=False) dev.open() dev.timeout = 60 if lsys: if(route.isspace() or route == ""): bt = bgpRoutes(dev).get(active_path=True, table="inet.0", logical_system=str(lsys)) else: bt = bgpRoutes(dev).get(active_path=True, destination=str(route), table="inet.0", logical_system=str(lsys)) else: if(route.isspace() or route == ""): bt = bgpRoutes(dev).get(active_path=True, table="inet.0") else: bt = bgpRoutes(dev).get(active_path=True, destination=str(route), table="inet.0") #Prepare the string with the information structured as we need it to be displayed output = "" for item in bt: output += "\n-\n" output += ("Route: "+item.rt_destination+"/"+item.rt_prefix_length+"\n") output += (item.as_path+" // Preference: "+item.preference+" // Community: "+str(item.community)+"\n") output += ("-\n") #output += ("---------------------------------------------\n") dev.close() return output
def getPrefixList(dev): #Open Connection Devices try: dev.open() except (ConnectError, ConnectAuthError, ConnectTimeoutError) as conErr: print("Cannot connect to device: {0}".format(conErr)) sys.exit(1) hostname = dev.facts['hostname'] yaml_data = \ """--- PolicyOption: get: policy-options/prefix-list view: PolicyOptionView PolicyOptionView: fields: name: name prefix: prefix-list-item/name """ globals().update(FactoryLoader().load(yaml.load(yaml_data))) po = PolicyOption(dev).get() for name, prefix in po.items(): prefixDetails = dict(prefix) pListName = prefixDetails['name'] pfix = prefixDetails['prefix'] print(hostname, pListName, pfix) dev.close()
def test_generate_sax_parser_item_with_many_slash(self): yaml_data = """ --- taskmallocdetail: rpc: get-task-memory-information args: level: detail item: task-memory-malloc-usage-report/task-malloc-list/task-malloc key: tm-name view: taskmallocview taskmallocview: fields: tmallocs: tm-allocs tmallocbytes: tm-alloc-bytes tmmaxallocs: tm-max-allocs tmmaxallocbytes: tm-max-alloc-bytes tmfunctioncalls: tm-function-calls """ globals().update(FactoryLoader().load( yaml.load(yaml_data, Loader=yaml.Loader))) tbl = taskmallocdetail(self.dev) data = generate_sax_parser_input(tbl) self.assertEqual(data.tag, 'task-memory-malloc-usage-report') self.assertEqual( len(etree.tostring(data)), len(b'<task-memory-malloc-usage-report><task-malloc-list><task-malloc><tm-name/><t' b'm-allocs/><tm-alloc-bytes/><tm-max-allocs/><tm-max-alloc-bytes/><tm-function' b'-calls/></task-malloc></task-malloc-list></task-memory-malloc-usage-report>' ))
def loadyaml(path): """ Load a YAML file at :path: that contains Table and View definitions. Returns a <dict> of item-name anditem-class definition. If you want to import these definitions directly into your namespace, (like a module) you would do the following: globals().update( loadyaml( <path-to-yaml-file> )) If you did not want to do this, you can access the items as the <dict>. For example, if your YAML file contained a Table called MyTable, then you could do something like: catalog = loadyaml( <path-to-yaml-file> ) MyTable = catalog['MyTable'] table = MyTable(dev) table.get() ... """ # if no extension is given, default to '.yml' if os.path.splitext(path)[1] == '': path += '.yml' return FactoryLoader().load( yaml.load(open(path, 'r'), Loader=yaml.FullLoader))
def test_key_pipe_delim_with_Null(self, mock_execute): mock_execute.side_effect = self._mock_manager yaml_data = """ --- UTMStatusTable: rpc: show-utmd-status item: //utmd-status view: UTMStatusView key: ../re-name | Null UTMStatusView: fields: running: { running: flag } """ globals().update(FactoryLoader().load( yaml.load(yaml_data, Loader=yaml.Loader))) tbl = UTMStatusTable(self.dev) data = tbl.get() self.assertEqual(json.loads(data.to_json()), { 'node0': { 'running': True }, 'node1': { 'running': True } })
def test_cfgtable_set_bool(self, mock_execute): yaml_auto_data = \ """--- UserConfigTable1: set: system/login key-field: - username view: UserConfigView1 UserConfigView1: groups: auth: authentication fields: user: user username: user/name classname: { user/class : { 'type' : { 'enum' : ['operator', 'read-only', 'super-user'] } } } uid: { user/uid : { 'type' : bool , 'default' :False} } fields_auth: password: user/encrypted-password """ globals().update(FactoryLoader().load(yaml.load(yaml_auto_data))) at = UserConfigTable1(self.dev) at.rpc.lock_configuration = MagicMock() at.username = True at.user = {'inactive': 'inactive'} at.append() at.set() xml = at.get_table_xml() self.assertNotEqual( xml.xpath('system/login/user/name')[0].text, 'user1')
def test_R2_bgp_peers_established(dev): globals().update(FactoryLoader().load(yaml.load(bgpYAML))) bgp_table = BgpSummaryTable(dev).get() for peer in bgp_table: if peer["peer_state"] != "Established": return False return True
def load_module(self, fullname): if fullname in sys.modules: return sys.modules[fullname] mod = fullname.split('.')[-1] modObj = types.ModuleType( mod, 'Module created to provide a context for %s' % mod) with open(os.path.join(os.path.dirname(__file__), mod + '.yml'), 'r')\ as stream: try: modules = FactoryLoader().load( yaml.load(stream, Loader=yamlordereddictloader.Loader)) except yaml.YAMLError as exc: raise ImportError("%s is not loaded" % mod) for k, v in modules.items(): setattr(modObj, k, v) sys.modules[fullname] = modObj return modObj
def test_cfgtable_invalid_view_not_defined_type_error(self, mock_execute): yaml_auto_data = \ """--- AutoSysTable: set: routing-options/autonomous-system """ globals().update(FactoryLoader().load(yaml.load(yaml_auto_data))) self.assertRaises(ValueError, AutoSysTable, self.dev)
def check_l2vpn(router_name, username, password): with open("checking/sensors/l2vpn_sensor.yml", 'r') as tvs: globals().update(FactoryLoader().load(yaml.load(tvs))) with Device(host=router_name, user=username, password=password, gather_facts=False) as dev: print(dev.facts['hostname']) l2vpn = L2vpnConnectionTable(dev) l2vpn.get() for item in l2vpn: print(item)
def main(): # Create table/view definition yaml_data = \ """--- ttyTable: rpc: get-system-users-information item: uptime-information/user-table/user-entry key: tty view: ttyView ttyView: fields: user: user from: from login_time: login-time idle_time: idle-time command: command """ # Load table/view into global namespace globals().update(FactoryLoader().load(yaml.load(yaml_data))) # Setup argument parsing for dynamic config parser = argparse.ArgumentParser() parser.add_argument('-tty', required=True) args = parser.parse_args() try: # create device object dev = Device(gather_facts=False) # open connection to device dev.open() try: # Create table tty = ttyTable(dev) # Fetch data for tty tty.get(args.tty) # Print some data from the view print "User: {0}, from: {1}".format(tty[0]['user'], tty[0]['from']) # Catch configuration RPC error except RpcError as err: jcs.emit_error("Unable to execute RPC: {0}".format(err)) dev.close() return dev.close() except Exception as err: jcs.emit_error("Uncaught exception: {0}".format(err))
def test_generate_sax_parser_same_parents_with_diff_fields(self): yaml_data = """ --- VtepTable: rpc: get-interface-information args: interface-name: "vtep" extensive: True item: physical-interface key: name view: VtepView VtepView: fields: admin-status: admin-status oper-status: oper-status link-level-type: link-level-type input-bytes: traffic-statistics/input-bytes output-bytes: traffic-statistics/output-bytes input-errors: input-error-list/input-errors output-errors: output-error-list/output-errors carrier-transitions: output-error-list/carrier-transitions vtep-tunnel-stats: VtepTunnelTable VtepTunnelTable: item: logical-interface key: name view: VtepTunnelView VtepTunnelView: fields: vtep-type: vtep-info/vtep-type vtep-address: vtep-info/vtep-address tunnel-input-bytes: traffic-statistics/input-bytes tunnel-output-bytes: traffic-statistics/output-bytes """ globals().update(FactoryLoader().load( yaml.load(yaml_data, Loader=yaml.Loader))) tbl = VtepTable(self.dev) data = generate_sax_parser_input(tbl) self.assertEqual(data.tag, "physical-interface") self.assertEqual( len(etree.tostring(data)), len(b"<physical-interface><name/><admin-status/><oper-status/>" b"<link-level-type/><traffic-statistics><input-bytes/>" b"<output-bytes/></traffic-statistics><input-error-list>" b"<input-errors/></input-error-list><output-error-list>" b"<output-errors/><carrier-transitions/></output-error-list>" b"<logical-interface><name/><vtep-info><vtep-type/><vtep-address/>" b"</vtep-info><traffic-statistics><input-bytes/><output-bytes/>" b"</traffic-statistics></logical-interface></physical-interface>" ), )
def test_cfgtable_invalid_key_field_not_defined_error(self, mock_execute): yaml_auto_data = \ """--- AutoSysTable: set: routing-options/autonomous-system view: AutoSysView AutoSysView: fields: as_num: as-number """ globals().update(FactoryLoader().load(yaml.load(yaml_auto_data))) self.assertRaises(ValueError, AutoSysTable, self.dev)
def main(): router = "<your-router-IP-here>" rtUser = "******" rtPassword = "******" print("") print("") yaml_data = """ --- BgpSummaryTable: rpc: get-bgp-summary-information item: bgp-peer key: peer-address view: BgpSummaryTableView BgpSummaryTableView: fields: peerAddress: peer-address peerAS: peer-as peerDescription: description peerState: peer-state """ # Let's connect to the router conn = connectToRouter(rtUser, rtPassword, router) if (conn == None): print("ERROR: could not connect to router " + router) print("") print("exiting ...") sys.exit(-1) globals().update(FactoryLoader().load(yaml.load(yaml_data))) bgpSummary = BgpSummaryTable(conn) bgpSummary.get() log.debug("printing the table") pprint.pprint(bgpSummary) print bgpSummary.keys() for bgpPeer in bgpSummary: print("BGP peer address: " + str(bgpPeer.peerAddress)) print("BGP peer ASN: " + str(bgpPeer.peerAS)) print("BGP peer description: " + str(bgpPeer.peerDescription)) print("BGP peer state: " + str(bgpPeer.peerState)) print("---")
def test_cfgtable_invalid_key_field_type_error(self, mock_execute): yaml_auto_data = """--- AutoSysTable: set: routing-options/autonomous-system key-field: as_num : as-number view: AutoSysView AutoSysView: fields: as_num: as-number """ globals().update(FactoryLoader().load( yaml.load(yaml_auto_data, Loader=yaml.FullLoader))) self.assertRaises(TypeError, AutoSysTable, self.dev)
def check_route(router_name, username, password): with open("sensors/route_sensor.yml", 'r') as tvs: globals().update(FactoryLoader().load(yaml.load(tvs))) with Device(host=router_name, user=username, password=password, gather_facts=False) as dev: print(dev.facts['hostname']) # route = RouteTable(dev) routes = RouteSummaryTable(dev) # route.get() routes.get() for item in routes: print(item)
def test_cfgtable_enum_value_str_error(self, mock_execute): yaml_auto_data = \ """--- AutoSysTable: set: routing-options/autonomous-system key-field: - as_num view: AutoSysView AutoSysView: fields: as_num: {'as-number' : {'type' : {'enum': '100'}}} """ globals().update(FactoryLoader().load(yaml.load(yaml_auto_data, Loader=yaml.FullLoader))) at = AutoSysTable(self.dev) at.as_num = 100 self.assertRaises(ValueError, at.append)
def test_cfgtable_invalid_type_error(self, mock_execute): yaml_auto_data = \ """--- AutoSysTable: set: routing-options/autonomous-system key-field: - as_num view: AutoSysView AutoSysView: fields: as_num: {'as-number': {'type': ['abc']}} """ globals().update(FactoryLoader().load(yaml.load(yaml_auto_data))) at = AutoSysTable(self.dev) at.as_num = 100 self.assertRaises(TypeError, at.append)
def get_cards_jnp(task): modules = [] hostname = task.host.name username = task.host.username password = task.host.password yml = ''' --- chassis: rpc: get-chassis-inventory args: extensive: True item: chassis/description key: chassis-module view: chassisView chassisView: fields: name: name model_number: model-number ''' globals().update(FactoryLoader().load(yaml.load(yml))) dev = Device(host=hostname, user=username, password=password, port='22', gather_facts=False) try: dev.open() dev.timeout = 10 module = ModuleTable(dev).get() #print('hostname:',hostname) for slot in module: module = slot.jname module_type = slot.type if re.search(r"FPC|Routing|PIC", module): modules.append((module, module_type)) #print (module,module_type) labels = ['card_slot', 'card_name'] df = pd.DataFrame(modules, columns=labels) df['router_name'] = hostname df['card_status'] = 'online' return (df) except Exception as e: print('Failed', e) raise Exception
def search_for_mac_and_tip(s_mac, s_ip, s_port, s_current_time,s_file, s_user,ss_server,ss_api): dev = Device(host=s_ip, user=s_user, ssh_private_key_file=s_file, port=s_port) s_location ="" s_name="" s_FLOOR="" s_DC="" s_VC="" s_Message="" yml = ''' EthernetSwitchingTable: rpc: get-ethernet-switching-table-information item: l2ng-l2ald-mac-entry-vlan/l2ng-mac-entry key: - l2ng-l2-mac-address - l2ng-l2-mac-logical-interface - l2ng-l2-vlan-id view: EtherSwView EtherSwView: fields: mac: l2ng-l2-mac-address port_id: l2ng-l2-mac-logical-interface id: l2ng-l2-vlan-id ''' globals().update(FactoryLoader().load(yaml.load(yml))) dev.open() table = EthernetSwitchingTable(dev) table.get() for i in table: if i.mac == s_mac: s_port = i.port_id s_name = dev.facts['hostname'] s_details = name_details(dev.facts['hostname']) s_location = s_details[0] s_FLOOR = s_details[1] s_DC = s_details[2] s_VC = s_details[3] s_Message = "found" dev.close() if Log_Stat == True: print('Data to be posted :',(s_mac, s_name, s_port, s_VC, s_DC,s_FLOOR, s_current_time,s_ip,ss_server, ss_api)) post_info(s_mac, s_name, s_port, s_VC, s_DC,s_FLOOR,s_current_time,s_ip,ss_server, ss_api)
def test_cfgtable_user_defined_type_error(self, mock_execute): yaml_auto_data = \ """--- AutoSysTable: set: routing-options/autonomous-system key-field: - as_num view: AutoSysView AutoSysView: fields: as_num: {'as-number': {'type': {'UserDefined': ''}}} """ globals().update(FactoryLoader().load(yaml.load(yaml_auto_data))) at = AutoSysTable(self.dev) at.rpc.lock_configuration = MagicMock() at.as_num = 100 self.assertRaises(TypeError, at.append)
def test_generate_sax_parser_fields_with_diff_child_xpaths(self): yaml_data = """ --- twampProbeTable: rpc: twamp-get-probe-results item: probe-test-results key: test-name view: probeResultsView probeResultsView: fields: min-delay: probe-test-global-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/min-delay max-delay: probe-test-global-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/max-delay avg-delay: probe-test-global-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/avg-delay positive-rtt-jitter: probe-test-global-results/probe-test-generic-results/probe-test-positive-round-trip-jitter/probe-summary-results/avg-delay loss-percentage: probe-test-global-results/probe-test-generic-results/loss-percentage current-min-delay: probe-last-test-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/min-delay current-max-delay: probe-last-test-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/max-delay current-avg-delay: probe-last-test-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/avg-delay current-positive-rtt-jitter: probe-last-test-results/probe-test-generic-results/probe-test-positive-round-trip-jitter/probe-summary-results/avg-delay current-loss-percentage: probe-last-test-results/probe-test-generic-results/loss-percentage """ globals().update(FactoryLoader().load( yaml.load(yaml_data, Loader=yaml.Loader))) tbl = twampProbeTable(self.dev) data = generate_sax_parser_input(tbl) self.assertEqual(data.tag, "probe-test-results") self.assertEqual( len(etree.tostring(data)), len(b"<probe-test-results><test-name/><probe-last-test-results>" b"<probe-test-generic-results><probe-test-rtt><probe-summary" b"-results><min-delay/><avg-delay/><max-delay/></probe-summary" b"-results></probe-test-rtt><loss-percentage/>" b"<probe-test-positive-round-trip-jitter><probe-summary-results>" b"<avg-delay/></probe-summary-results></probe-test-positive-round" b"-trip-jitter></probe-test-generic-results></probe-last-test-resu" b"lts><probe-test-global-results><probe-test-generic-results>" b"<probe-test-rtt><probe-summary-results><min-delay/><avg-delay/>" b"<max-delay/></probe-summary-results></probe-test-rtt>" b"<loss-percentage/><probe-test-positive-round-trip-jitter>" b"<probe-summary-results><avg-delay/></probe-summary-results>" b"</probe-test-positive-round-trip-jitter></probe-test-generic-" b"results></probe-test-global-results></probe-test-results>"), )
def bgp_adv(): globals().update(FactoryLoader().load(yaml.safe_load(yml))) with Device(varIP, port='22', user=varUser, passwd=varPassword) as dev: op = dev.rpc.get_bgp_summary_information() for i in op.xpath('bgp-peer/peer-address'): # print(i.text) load_bgp = bgpAdvertiseRoutes(dev).get(neighbor=i.text) print("\n-----------------BGP Advertising Routes For:", i.text, "-----------------") for item in load_bgp: print('----------------------------------------------') print("Advertising_Route:", item.rt_destination, "Prefix_Lengh:", item.rt_prefix_length, "MED:", item.rt_med, "LP:", item.rt_local_preference, "AS_Path:", item.rt_as_path, "Communities:", item.rt_communities) print("\n") print("\n")
def getMACtable(ipAddress, interface, junosUsername, junosPassword): dev = Device(host=ipAddress, user=junosUsername, password=junosPassword, port=22) try: dev.open(auto_probe=5) except: return {"Error": "Connection refused"} dev.timeout=120 yml = ''' --- EtherSwTable: rpc: get-interface-ethernet-switching-table item: ethernet-switching-table/mac-table-entry[mac-type='Learn'] key: mac-address view: EtherSwView EtherSwView: fields: vlan_name: mac-vlan mac: mac-address mac_type: mac-type mac_age: mac-age interface: mac-interfaces-list/mac-interfaces ''' globals().update(FactoryLoader().load(yaml.load(yml, Loader=yaml.FullLoader))) table = EtherSwTable(dev) try: table.get() except: dev.close() return {"Error": "Failed to get table"} response=[] if len(table)>0: for i in table: if i.interface==(interface+".0"): response.append({ "vlan_name":i.vlan_name, "mac": i.mac, "mac_type": i.mac_type, "mac_age": i.mac_age }) return response
def test_key_and_item_pipe_delim_with_Null_use_Null(self, mock_execute): mock_execute.side_effect = self._mock_manager yaml_data = """ --- UTMStatusTable: rpc: show-utmd-status_use_Null item: //multi-routing-engine-item/utmd-status | //utmd-status view: UTMStatusView key: - ../re-name | Null UTMStatusView: fields: running: { running: flag } """ globals().update(FactoryLoader().load( yaml.load(yaml_data, Loader=yaml.Loader))) tbl = UTMStatusTable(self.dev) data = tbl.get() self.assertEqual(json.loads(data.to_json()), {"running": True})
def test_cfgtable_field_value_xpath(self, mock_execute): yaml_auto_data = \ """--- AutoSysTable: set: routing-options key-field: as_num view: AutoSysView AutoSysView: fields: as_num: autonomous-system/as-number """ globals().update(FactoryLoader().load(yaml.load(yaml_auto_data))) at = AutoSysTable(self.dev) at.rpc.lock_configuration = MagicMock() at.as_num = 150 at.append() at.set() xml = at.get_table_xml() self.assertEqual( xml.xpath('routing-options/autonomous-system/as-number')[0].text, '150')
def test_cfgtable_str_key_field(self, mock_execute): yaml_auto_data = """--- AutoSysTable: set: routing-options/autonomous-system key-field: as_num view: AutoSysView AutoSysView: fields: as_num: as-number """ globals().update(FactoryLoader().load( yaml.load(yaml_auto_data, Loader=yaml.FullLoader))) at = AutoSysTable(self.dev) at.rpc.lock_configuration = MagicMock() at.as_num = 100 at.append() at.set() xml = at.get_table_xml() self.assertEqual( xml.xpath("routing-options/autonomous-system/as-number")[0].text, "100")
def test_generate_sax_parser_fields_with_many_slash(self): yaml_data = """ --- bgpNeighborTable: rpc: get-bgp-neighbor-information item: bgp-peer key: peer-address view: bgpNeighborView bgpNeighborView: fields: prefix-count: bgp-option-information/prefix-limit/prefix-count prefix-dummy: bgp-option-information/prefix-limit/prefix-dummy """ globals().update(FactoryLoader().load( yaml.load(yaml_data, Loader=yaml.Loader))) tbl = bgpNeighborTable(self.dev) data = generate_sax_parser_input(tbl) self.assertEqual(data.tag, 'bgp-peer') self.assertEqual( len(etree.tostring(data)), len(b'<bgp-peer><peer-address/><bgp-option-information><prefix-limit>' b'<prefix-count/><prefix-dummy/></prefix-limit>' b'</bgp-option-information></bgp-peer>'))