diff --git a/lib/jnpr/jsnapy/check.py b/lib/jnpr/jsnapy/check.py index 399a58aa..bc2114bb 100755 --- a/lib/jnpr/jsnapy/check.py +++ b/lib/jnpr/jsnapy/check.py @@ -27,7 +27,13 @@ class Comparator: - def __init__(self): + def __init__(self,**kwargs): + """ + Comparator object constructor. + :param int port. + + """ + self.port = kwargs.get('port', None) self.logger_check = logging.getLogger(__name__) self.log_detail = {'hostname': None} @@ -63,6 +69,8 @@ def generate_snap_file(self, device, prefix, name, reply_format): """ This function generates name of snapshot files """ + if self.port is not None: + device = device + "_" + str(self.port) if os.path.isfile(prefix): return prefix else: diff --git a/lib/jnpr/jsnapy/jsnapy.py b/lib/jnpr/jsnapy/jsnapy.py index e653694a..0c91809f 100755 --- a/lib/jnpr/jsnapy/jsnapy.py +++ b/lib/jnpr/jsnapy/jsnapy.py @@ -369,7 +369,7 @@ def get_hosts(self): sys.exit(1) self.login(output_file) - def generate_rpc_reply(self, dev, output_file, hostname, config_data): + def generate_rpc_reply(self, dev, output_file, hostname, config_data,**kwargs): """ Generates rpc-reply based on command/rpc given and stores them in snap_files :param dev: device handler @@ -377,6 +377,7 @@ def generate_rpc_reply(self, dev, output_file, hostname, config_data): :param hostname: hostname of device :param config_data : data of main config file """ + port = kwargs.get('port', None) val = None test_files = [] for tfile in config_data.get('tests'): @@ -394,14 +395,13 @@ def generate_rpc_reply(self, dev, output_file, hostname, config_data): colorama.Fore.RED + "ERROR!! File %s is not found for taking snapshots" % tfile, extra=self.log_detail) - - g = Parser() + g = Parser(**kwargs) for tests in test_files: val = g.generate_reply(tests, dev, output_file, hostname, self.db) return val def compare_tests( - self, hostname, config_data, pre_snap=None, post_snap=None, action=None): + self, hostname, config_data, pre_snap=None, post_snap=None, action=None, **kwargs): """ called by check and snapcheck argument, to compare snap files calls the function to compare snapshots based on arguments given @@ -409,7 +409,7 @@ def compare_tests( :param hostname: device name :return: return object of Operator containing test details """ - comp = Comparator() + comp = Comparator(**kwargs) chk = self.args.check diff = self.args.diff pre_snap_file = self.args.pre_snapfile if pre_snap is None else pre_snap @@ -488,41 +488,27 @@ def login(self, output_file): gp = first_entry.get('group', 'all') dgroup = [i.strip().lower() for i in gp.split(',')] + iter = 0; for dgp in dev_file: if dgroup[0].lower() == 'all' or dgp.lower() in dgroup: for val in dev_file[dgp]: hostname = list(val)[0] + iter+=1 self.log_detail = {'hostname': hostname} - if val.get(hostname) is not None and hostname not in host_dict: - host_dict[hostname] = deepcopy(val.get(hostname)) + if val.get(hostname) is not None and hostname not in self.host_list: + #host_dict[hostname] = deepcopy(val.get(hostname)) self.host_list.append(hostname) + host_dict[iter] = deepcopy(val.get(hostname)) + host_dict[iter]["device"] = hostname + # login credentials are given in main config file, can connect to multiple devices else: #key_value = deepcopy(k) - for host in hosts_val: - try: - hostname = host['device'] - self.log_detail = {'hostname': hostname} - except KeyError as ex: - self.logger.error( - colorama.Fore.RED + - "ERROR!! KeyError 'device' key not found", - extra=self.log_detail) - #raise Exception(ex) - except Exception as ex: - self.logger.error( - colorama.Fore.RED + - "ERROR!! %s" % - ex, - extra=self.log_detail) - #raise Exception(ex) - else: - if hostname not in host_dict: - self.host_list.append(hostname) - # host.pop('device') - host_dict[hostname] = deepcopy(host) - - for (hostname, key_value) in iteritems(host_dict): + self.get_hosts_list(hosts_val,host_dict) + + for (iter, key_value) in iteritems(host_dict): + hostname = key_value.get('device') + #it is under check that args.hostname is None #The file config takes precedence over cmd line params -- no changes made username = self.args.login or key_value.get('username') password = self.args.passwd or key_value.get('passwd') @@ -556,7 +542,7 @@ def login(self, output_file): key_value = {'port': port} if port is not None else {} self.connect(hostname, username, password, output_file, **key_value) - def get_test(self, config_data, hostname, snap_file, post_snap, action): + def get_test(self, config_data, hostname, snap_file, post_snap, action,**kwargs): """ Analyse testfile and return object of operator.Operator containing test details called by connect() function and other functions of Jsnapy module functions @@ -574,7 +560,8 @@ def get_test(self, config_data, hostname, snap_file, post_snap, action): config_data, snap_file, post_snap, - action) + action, + **kwargs) result_status = res.result @@ -696,7 +683,8 @@ def connect(self, hostname, username, password, output_file, dev, output_file, hostname, - config_data) + config_data, + **kwargs) self.snap_q.put(res) dev.close() if self.args.check is True or self.args.snapcheck is True or self.args.diff is True or action in [ @@ -715,7 +703,8 @@ def connect(self, hostname, username, password, output_file, hostname, local_snap, post_snap, - action) + action, + **kwargs) res[local_snap] = ret_obj else: res = self.get_test( @@ -723,7 +712,8 @@ def connect(self, hostname, username, password, output_file, hostname, output_file, post_snap, - action) + action, + **kwargs) return res @@ -760,36 +750,23 @@ def multiple_device_details( gp = first_entry.get('group', 'all') dgroup = [i.strip().lower() for i in gp.split(',')] + iter = 0 for dgp in dev_file: if dgroup[0].lower() == 'all' or dgp.lower() in dgroup: for val in dev_file[dgp]: hostname = list(val)[0] self.log_detail = {'hostname': hostname} - if val.get(hostname) is not None and hostname not in host_dict: - host_dict[hostname] = deepcopy(val.get(hostname)) + iter += 1 + if val.get(hostname) is not None and hostname not in self.host_list: self.host_list.append(hostname) + host_dict[iter] = deepcopy(val.get(hostname)) + host_dict[iter]["device"] = hostname else: - for host in hosts: - try: - hostname = host['device'] - self.log_detail = {'hostname': hostname} - except KeyError as ex: - self.logger.error( - colorama.Fore.RED + - "ERROR!! KeyError 'device' key not found", - extra=self.log_detail) - except Exception as ex: - self.logger.error( - colorama.Fore.RED + - "ERROR!! %s" % - ex, - extra=self.log_detail) - else: - if hostname not in host_dict: - self.host_list.append(hostname) - host_dict[hostname] = deepcopy(host) + # changes to support port + self.get_hosts_list(hosts, host_dict) - for (hostname, key_value) in iteritems(host_dict): + for (iter, key_value) in iteritems(host_dict): + hostname = key_value.get('device') username = key_value.get('username') password = key_value.get('passwd') key_value = self.get_values(key_value) @@ -1033,6 +1010,34 @@ def check(self, data, pre_file=None, post_file=None, dev=None, folder=None): res = self.extract_data(data, pre_file, "check", post_file) return res + def get_hosts_list(self, hosts_val, host_dict): + """ + Function extracts list of hosts from the details given. + :param hosts_val: has the list of hosts to be parsed + :param host_dict: The dictionary to be created to store the parsed values + """ + iter = -1 #iterator keeps count of number of hosts + for host in hosts_val: + iter += 1 + try: + hostname = host['device'] + self.log_detail = {'hostname': hostname} + except KeyError as ex: + self.logger.error( + colorama.Fore.RED + + "ERROR!! KeyError 'device' key not found", + extra=self.log_detail) + except Exception as ex: + self.logger.error( + colorama.Fore.RED + + "ERROR!! %s" % + ex, + extra=self.log_detail) + else: + if hostname not in self.host_list: + self.host_list.append(hostname) + host_dict[iter] = deepcopy(host) + ####### generate init folder ###### ''' def generate_init(self): diff --git a/lib/jnpr/jsnapy/snap.py b/lib/jnpr/jsnapy/snap.py index 92a5326a..2946d499 100755 --- a/lib/jnpr/jsnapy/snap.py +++ b/lib/jnpr/jsnapy/snap.py @@ -22,7 +22,13 @@ class Parser: - def __init__(self): + def __init__(self, **kwargs): + """ + Parser object constructor. + :param int port. + + """ + self.port = kwargs.get('port', None) self.logger_snap = logging.getLogger(__name__) self.log_detail = {'hostname': None} self.reply = {} @@ -89,6 +95,8 @@ def generate_snap_file(self, output_file, hostname, name, cmd_format): :param cmd_format: xml/text :return: return output file """ + if self.port is not None: + hostname = hostname + "_" + str(self.port) name = name.split('|')[0].strip() cmd_rpc = re.sub('/|\*|\.|-|\|', '_', name) if os.path.isfile(output_file):