def xml_to_json(val): if isinstance(val, string_types): return jxmlease.parse(val) else: return jxmlease.parse_etree(val)
def get_system_time(device=None, node=None): """ To get current system time Example : get_system_time(device=dh) get_system_time(device=dh, node="node0") ROBOT Example: Get System Time device=${dh} Get System Time device=${dh} node=node0 :param str device: **REQUIRED** Device Handle of the DUT :param str node: *OPTIONAL* In case of HA, Node from which we need to check the time. By default it will check the primary node. ``Supported values``: "node0" and "node1" :return: Returns the date and time of the DUT :rtype: datetime """ if device is None: raise ValueError("device is a mandatory argument") rpc_str = device.get_rpc_equivalent(command="show system uptime") srx_ha_flag = 0 if re.match(".*(s|S)(r|R)(x|X).*", device.get_model(), re.DOTALL): if device.is_ha(): if node is None: node = device.node_name() elif not (node == "node1" or node == "node0"): device.log(level='ERROR', message="Invalid HA Node value") raise ValueError("Invalid HA Node value") srx_ha_flag = 1 etree_obj = device.execute_rpc(command=rpc_str).response() status = jxmlease.parse_etree(etree_obj) status = status['multi-routing-engine-results'][ 'multi-routing-engine-item'] for lst in status: if lst['re-name'] == node: date_time = lst['system-uptime-information'][ 'current-time']['date-time'] break if srx_ha_flag == 0: etree_obj = device.execute_rpc(command=rpc_str).response() status = jxmlease.parse_etree(etree_obj) date_time = status['system-uptime-information']['current-time'][ 'date-time'] match = re.match(r"([0-9-: ]+)\s([A-Z]+)", date_time, re.DOTALL) current_time_dt = datetime.strptime(match.group(1), "%Y-%m-%d %H:%M:%S") return current_time_dt
def is_node_status_primary(self): """ Module to check if node status is primary :return: return true if the current node is primary else false """ sccs = self.current_controller.cli( command="show chassis cluster status redundancy-group 0", format="xml").response() match = re.match(r"(<rpc-reply.*>)([\s\S]*)(<cli>[\s\S]*</rpc-reply>)", sccs) sccs = match.group(2) # will only get the XML response of the command try: root = etree.fromstring(sccs) status = jxmlease.parse_etree(root) status = status['chassis-cluster-status']['redundancy-group'][ 'device-stats'] except: raise TobyException('Chassis cluster is not enabled') node = self.node_name() if node == 'node0' and status['redundancy-group-status'][ 0] == 'primary': return True elif node == 'node1' and status['redundancy-group-status'][ 1] == 'primary': return True return False
def node_name(self): """ Get name of the chassis in the cluster :return: name of the chassis in the cluster ("node0"/"node1"). """ # Execute show version on the connected device sccs = self.current_controller.cli(command="show version", format="xml").response() match = re.match(r"(<rpc-reply.*>)([\s\S]*)(<cli>[\s\S]*</rpc-reply>)", sccs) sccs = match.group(2) # will only get the XML response of the command # Get RG stats try: root = etree.fromstring(sccs) status = jxmlease.parse_etree(root) host_name = self.current_controller.shell( command='hostname').response() node0_name = status['multi-routing-engine-results'][ 'multi-routing-engine-item'][0]['software-information'][ 'host-name'] node1_name = status['multi-routing-engine-results'][ 'multi-routing-engine-item'][1]['software-information'][ 'host-name'] if str(node0_name).lower() == str(host_name).lower(): return "node0" elif str(node1_name).lower() == str(host_name).lower(): return "node1" except: self.current_controller.log( level='ERROR', message='Chassis cluster is not enabled') raise TobyException("Chassis cluster is not enabled")
def get_license_usage(device=None): """ Get license usage details in the device Example :- get_license_usage(device=dh) Robot Example: get license usage device=$(dh) :param Device device: **REQUIRED** Handle of the device :return: Returns license usage details from the device :rtype : list """ if device is None: raise ValueError("Missing device handle argument") rpc_str = device.get_rpc_equivalent(command="show system license usage") etree_obj = device.execute_rpc(command=rpc_str).response() response = jxmlease.parse_etree( etree_obj)['license-usage-summary']['feature-summary'] list_of_licenses = [] if isinstance(response, list): list_of_licenses = response else: list_of_licenses.append(response) return list_of_licenses
def __init__(self): """INIT""" self.pprint = lambda x: json.dumps( x, indent=4, sort_keys=True, default=str, ensure_ascii=False) self.lxml_response_parser = lambda xml_obj: jxmlease.parse_etree( xml_obj) self.jxmlease_response_to_dict = lambda xml_obj: json.loads( self.pprint(xml_obj)) self.lxml_response_to_dict = lambda xml_obj: eval( self.pprint(self.lxml_response_parser(xml_obj))) self.xml_string_to_dict = lambda xml_str: jxmlease.parse(xml_str) self.dict_to_xml_string = lambda xml_dict: jxmlease.emit_xml(xml_dict) self.xml_obj_to_string = lambda xml_obj: self.dict_to_xml_string( self.lxml_response_parser(xml_obj)) self.xml_str_to_dict = self.xml_string_to_dict self.dict_to_xml_str = self.dict_to_xml_string self.xml_obj_to_dict = self.lxml_response_parser