Skip to content

Commit

Permalink
port support added for same Ip-address. Juniper#322
Browse files Browse the repository at this point in the history
  • Loading branch information
rahkumar651991 committed Jan 23, 2020
1 parent bb8e02b commit 18c1e84
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 61 deletions.
10 changes: 9 additions & 1 deletion lib/jnpr/jsnapy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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:
Expand Down
123 changes: 64 additions & 59 deletions lib/jnpr/jsnapy/jsnapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,15 @@ 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
:param output_file: filename to store snapshots
: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'):
Expand All @@ -394,22 +395,21 @@ 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
(--check, --snapcheck, --diff)
: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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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 [
Expand All @@ -715,16 +703,18 @@ 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(
config_data,
hostname,
output_file,
post_snap,
action)

action,
**kwargs)

return res

############################### functions to support module ##############
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 9 additions & 1 deletion lib/jnpr/jsnapy/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 18c1e84

Please sign in to comment.