l3_cxprofile.py : updated to use logger

Signed-off-by: Chuck SmileyRekiere <chuck.smileyrekiere@candelatech.com>
This commit is contained in:
Chuck SmileyRekiere
2022-01-28 10:50:28 -07:00
committed by shivam
parent ff56cdccfb
commit 2164d5fc16

View File

@@ -2,10 +2,10 @@
import sys import sys
import os import os
import importlib import importlib
import pprint
import pandas as pd import pandas as pd
import time import time
import datetime import datetime
import logging
sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../"))) sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../")))
@@ -15,6 +15,8 @@ pandas_extensions = importlib.import_module("py-json.LANforge.pandas_extensions"
port_probe = importlib.import_module("py-json.port_probe") port_probe = importlib.import_module("py-json.port_probe")
ProbePort = port_probe.ProbePort ProbePort = port_probe.ProbePort
logger = logging.getLogger(__name__)
class L3CXProfile(LFCliBase): class L3CXProfile(LFCliBase):
def __init__(self, def __init__(self,
@@ -83,9 +85,8 @@ class L3CXProfile(LFCliBase):
def __get_rx_values(self): def __get_rx_values(self):
cx_list = self.json_get("endp?fields=name,rx+bytes") cx_list = self.json_get("endp?fields=name,rx+bytes")
if self.debug: logger.debug(self.created_cx.values())
print(self.created_cx.values()) logger.debug("==============\n {cx_list}\n==============".format(cx_list=cx_list))
print("==============\n", cx_list, "\n==============")
cx_rx_map = {} cx_rx_map = {}
for cx_name in cx_list['endpoint']: for cx_name in cx_list['endpoint']:
if cx_name != 'uri' and cx_name != 'handler': if cx_name != 'uri' and cx_name != 'handler':
@@ -132,30 +133,39 @@ class L3CXProfile(LFCliBase):
if duration_sec: if duration_sec:
duration_sec = self.parse_time(duration_sec).seconds duration_sec = self.parse_time(duration_sec).seconds
else: else:
logger.critical("L3CXProfile::monitor wants duration_sec > 1 second")
raise ValueError("L3CXProfile::monitor wants duration_sec > 1 second") raise ValueError("L3CXProfile::monitor wants duration_sec > 1 second")
if duration_sec <= monitor_interval_ms: if duration_sec <= monitor_interval_ms:
logger.critical("L3CXProfile::monitor wants duration_sec > monitor_interval")
raise ValueError("L3CXProfile::monitor wants duration_sec > monitor_interval") raise ValueError("L3CXProfile::monitor wants duration_sec > monitor_interval")
if report_file is None: if report_file is None:
logger.critical("Monitor requires an output file to be defined")
raise ValueError("Monitor requires an output file to be defined") raise ValueError("Monitor requires an output file to be defined")
if systeminfopath is None: if systeminfopath is None:
raise ValueError("Monitor requires a system info path to be defined") raise ValueError("Monitor requires a system info path to be defined")
if created_cx is None: if created_cx is None:
logger.critical("Monitor needs a list of Layer 3 connections")
raise ValueError("Monitor needs a list of Layer 3 connections") raise ValueError("Monitor needs a list of Layer 3 connections")
if (monitor_interval_ms is None) or (monitor_interval_ms < 1): if (monitor_interval_ms is None) or (monitor_interval_ms < 1):
logger.critical("L3CXProfile::monitor wants monitor_interval >= 1 second")
raise ValueError("L3CXProfile::monitor wants monitor_interval >= 1 second") raise ValueError("L3CXProfile::monitor wants monitor_interval >= 1 second")
if layer3_cols is None: if layer3_cols is None:
logger.critical("L3CXProfile::monitor wants a list of column names to monitor")
raise ValueError("L3CXProfile::monitor wants a list of column names to monitor") raise ValueError("L3CXProfile::monitor wants a list of column names to monitor")
if output_format: if output_format:
if output_format.lower() != report_file.split('.')[-1]: if output_format.lower() != report_file.split('.')[-1]:
raise ValueError('Filename %s has an extension that does not match output format %s .' % ( logger.critical('Filename {report_file} has an extension that does not match output format {output_format} '.format(
report_file, output_format)) report_file=report_file, output_format=output_format))
raise ValueError('Filename {report_file} has an extension that does not match output format {output_format} '.format(
report_file=report_file, output_format=output_format))
else: else:
output_format = report_file.split('.')[-1] output_format = report_file.split('.')[-1]
# default save to csv first # default save to csv first
if report_file.split('.')[-1] != 'csv': if report_file.split('.')[-1] != 'csv':
report_file = report_file.replace(str(output_format), 'csv', 1) report_file = report_file.replace(str(output_format), 'csv', 1)
print("Saving rolling data into..." + str(report_file)) logger.info("Saving rolling data into...{report_file}".format(report_file=report_file))
# ================== Step 1, set column names and header row # ================== Step 1, set column names and header row
layer3_cols = [self.replace_special_char(x) for x in layer3_cols] layer3_cols = [self.replace_special_char(x) for x in layer3_cols]
@@ -216,22 +226,21 @@ class L3CXProfile(LFCliBase):
layer_3_response = self.json_get("/endp/%s?fields=%s" % (created_cx, layer3_fields)) layer_3_response = self.json_get("/endp/%s?fields=%s" % (created_cx, layer3_fields))
new_cx_rx_values = self.__get_rx_values() new_cx_rx_values = self.__get_rx_values()
if debug: logger.debug(old_cx_rx_values, new_cx_rx_values)
print(old_cx_rx_values, new_cx_rx_values) logger.debug("\n-----------------------------------")
print("\n-----------------------------------") logger.debug(t)
print(t) logger.debug("-----------------------------------\n")
print("-----------------------------------\n")
expected_passes += 1 expected_passes += 1
if self.__compare_vals(old_cx_rx_values, new_cx_rx_values): if self.__compare_vals(old_cx_rx_values, new_cx_rx_values):
passes += 1 passes += 1
else: else:
# TODO track where this goes?
self.fail("FAIL: Not all stations increased traffic") self.fail("FAIL: Not all stations increased traffic")
result = dict() # create dataframe from layer 3 results result = dict() # create dataframe from layer 3 results
if type(layer_3_response) is dict: if type(layer_3_response) is dict:
for dictionary in layer_3_response['endpoint']: for dictionary in layer_3_response['endpoint']:
# if debug: logger.debug('layer_3_data: {dictionary}'.format(dictionary=dictionary))
print('layer_3_data: %s' % dictionary)
result.update(dictionary) result.update(dictionary)
else: else:
pass pass
@@ -241,25 +250,22 @@ class L3CXProfile(LFCliBase):
if port_mgr_cols: # create dataframe from port mgr results if port_mgr_cols: # create dataframe from port mgr results
result = dict() result = dict()
if type(port_mgr_response) is dict: if type(port_mgr_response) is dict:
print("port_mgr_response {pmr}".format(pmr=port_mgr_response)) logger.info("port_mgr_response {pmr}".format(pmr=port_mgr_response))
if 'interfaces' in port_mgr_response: if 'interfaces' in port_mgr_response:
for dictionary in port_mgr_response['interfaces']: for dictionary in port_mgr_response['interfaces']:
if debug: logger.debug('port mgr data: {dictionary}'.format(dictionary=dictionary))
print('port mgr data: %s' % dictionary)
result.update(dictionary) result.update(dictionary)
elif 'interface' in port_mgr_response: elif 'interface' in port_mgr_response:
dict_update = {port_mgr_response['interface']['alias']: port_mgr_response['interface']} dict_update = {port_mgr_response['interface']['alias']: port_mgr_response['interface']}
if debug: logger.debug(dict_update)
print(dict_update)
result.update(dict_update) result.update(dict_update)
if debug: logger.debug(result)
print(result)
else: else:
print('interfaces and interface not in port_mgr_response') logger.critical('interfaces and interface not in port_mgr_response')
exit(1) raise ValueError('interfaces and interface not in port_mgr_response')
portdata_df = pd.DataFrame(result.values()) portdata_df = pd.DataFrame(result.values())
print("portdata_df {pd}".format(pd=portdata_df)) logger.info("portdata_df {pd}".format(pd=portdata_df))
portdata_df.columns = ['port-' + x for x in portdata_df.columns] portdata_df.columns = ['port-' + x for x in portdata_df.columns]
portdata_df['alias'] = portdata_df['port-alias'] portdata_df['alias'] = portdata_df['port-alias']
@@ -271,8 +277,10 @@ class L3CXProfile(LFCliBase):
if len(layer3_alias) == layer3.shape[0]: if len(layer3_alias) == layer3.shape[0]:
layer3['alias'] = layer3_alias layer3['alias'] = layer3_alias
else: else:
raise ValueError("The Stations or Connection on LANforge did not match expected, \ logger.critical(("The Stations or Connection on LANforge did not match expected,",
Check if LANForge initial state correct or delete/cleanup corrects") " Check if LANForge initial state correct or delete/cleanup corrects"))
raise ValueError(("The Stations or Connection on LANforge did not match expected,",
" Check if LANForge initial state correct or delete/cleanup corrects"))
timestamp_df = pd.merge(layer3, portdata_df, on='alias') timestamp_df = pd.merge(layer3, portdata_df, on='alias')
else: else:
@@ -350,6 +358,7 @@ class L3CXProfile(LFCliBase):
if compared_report: if compared_report:
pandas_extensions.compare_two_df(dataframe_one=pandas_extensions.file_to_df(report_file), pandas_extensions.compare_two_df(dataframe_one=pandas_extensions.file_to_df(report_file),
dataframe_two=pandas_extensions.file_to_df(compared_report)) dataframe_two=pandas_extensions.file_to_df(compared_report))
# TODO why is this exit here?
exit(1) exit(1)
# append compared df to created one # append compared df to created one
if output_format.lower() != 'csv': if output_format.lower() != 'csv':
@@ -364,27 +373,29 @@ class L3CXProfile(LFCliBase):
"test_mgr": "ALL", "test_mgr": "ALL",
"cross_connect": cx_name "cross_connect": cx_name
}, debug_=self.debug) }, debug_=self.debug)
# this is for a visual affect someone watching the screen, leave as print
print(".", end='') print(".", end='')
def start_cx(self): def start_cx(self):
print("Starting CXs...") logger.info("Starting CXs...")
for cx_name in self.created_cx.keys(): for cx_name in self.created_cx.keys():
if self.debug: logger.debug("cx-name: {cx_name}".format(cx_name=cx_name))
print("cx-name: %s" % cx_name)
self.json_post("/cli-json/set_cx_state", { self.json_post("/cli-json/set_cx_state", {
"test_mgr": "default_tm", "test_mgr": "default_tm",
"cx_name": cx_name, "cx_name": cx_name,
"cx_state": "RUNNING" "cx_state": "RUNNING"
}, debug_=self.debug) }, debug_=self.debug)
# this is for a visual affect someone watching the screen, leave as print
if self.debug: if self.debug:
print(".", end='') print(".", end='')
if self.debug: if self.debug:
print("") print("")
def stop_cx(self): def stop_cx(self):
print("Stopping CXs...") logger.info("Stopping CXs...")
for cx_name in self.created_cx.keys(): for cx_name in self.created_cx.keys():
self.local_realm.stop_cx(cx_name) self.local_realm.stop_cx(cx_name)
# this is for a visual affect someone watching the screen, leave as print
print(".", end='') print(".", end='')
print("") print("")
@@ -392,17 +403,15 @@ class L3CXProfile(LFCliBase):
self.local_realm.cleanup_cxe_prefix(self.name_prefix) self.local_realm.cleanup_cxe_prefix(self.name_prefix)
def cleanup(self): def cleanup(self):
print("Cleaning up cxs and endpoints") logger.info("Cleaning up cxs and endpoints")
if len(self.created_cx) != 0: if len(self.created_cx) != 0:
for cx_name in self.created_cx.keys(): for cx_name in self.created_cx.keys():
if self.debug: logger.debug("Cleaning cx: {cx_name}".format(cx_name=cx_name))
print("Cleaning cx: %s" % cx_name)
self.local_realm.rm_cx(cx_name) self.local_realm.rm_cx(cx_name)
for side in range(len(self.created_cx[cx_name])): for side in range(len(self.created_cx[cx_name])):
ename = self.created_cx[cx_name][side] ename = self.created_cx[cx_name][side]
if self.debug: logger.debug("Cleaning endpoint: {ename}".format(ename=ename))
print("Cleaning endpoint: %s" % ename)
self.local_realm.rm_endp(self.created_cx[cx_name][side]) self.local_realm.rm_endp(self.created_cx[cx_name][side])
self.clean_cx_lists() self.clean_cx_lists()
@@ -421,19 +430,19 @@ class L3CXProfile(LFCliBase):
# if Endpoints creation is OK, but CX creation fails, returns False, list of endp # if Endpoints creation is OK, but CX creation fails, returns False, list of endp
if self.debug: if self.debug:
debug_ = True debug_ = True
print('Start L3CXProfile.create') logger.info('Start L3CXProfile.create')
cx_post_data = [] cx_post_data = []
timer_post_data = [] timer_post_data = []
these_endp = [] these_endp = []
these_cx = [] these_cx = []
# print(self.side_a_min_rate, self.side_a_max_rate)
# print(self.side_b_min_rate, self.side_b_max_rate)
if (self.side_a_min_bps is None) \ if (self.side_a_min_bps is None) \
or (self.side_a_max_bps is None) \ or (self.side_a_max_bps is None) \
or (self.side_b_min_bps is None) \ or (self.side_b_min_bps is None) \
or (self.side_b_max_bps is None): or (self.side_b_max_bps is None):
logger.critical(
"side_a_min_bps, side_a_max_bps, side_b_min_bps, and side_b_max_bps must all be set to a value")
raise ValueError( raise ValueError(
"side_a_min_bps, side_a_max_bps, side_b_min_bps, and side_b_max_bps must all be set to a value") "side_a_min_bps, side_a_max_bps, side_b_min_bps, and side_b_max_bps must all be set to a value")
@@ -487,14 +496,11 @@ class L3CXProfile(LFCliBase):
"multi_conn": mconn_b, "multi_conn": mconn_b,
} }
# print("1: endp-side-b: ", endp_side_b)
url = "/cli-json/add_endp" url = "/cli-json/add_endp"
self.local_realm.json_post(url, endp_side_a, debug_=debug_, self.local_realm.json_post(url, endp_side_a, debug_=debug_,
suppress_related_commands_=suppress_related_commands) suppress_related_commands_=suppress_related_commands)
self.local_realm.json_post(url, endp_side_b, debug_=debug_, self.local_realm.json_post(url, endp_side_b, debug_=debug_,
suppress_related_commands_=suppress_related_commands) suppress_related_commands_=suppress_related_commands)
# print("napping %f sec"%sleep_time)
time.sleep(sleep_time) time.sleep(sleep_time)
url = "cli-json/set_endp_flag" url = "cli-json/set_endp_flag"
@@ -528,7 +534,6 @@ class L3CXProfile(LFCliBase):
"tx_endp": endp_a_name, "tx_endp": endp_a_name,
"rx_endp": endp_b_name, "rx_endp": endp_b_name,
} }
# pprint(data)
cx_post_data.append(data) cx_post_data.append(data)
timer_post_data.append({ timer_post_data.append({
"test_mgr": "default_tm", "test_mgr": "default_tm",
@@ -540,10 +545,9 @@ class L3CXProfile(LFCliBase):
side_a_info = self.local_realm.name_to_eid(side_a, debug=debug_) side_a_info = self.local_realm.name_to_eid(side_a, debug=debug_)
side_a_shelf = side_a_info[0] side_a_shelf = side_a_info[0]
side_a_resource = side_a_info[1] side_a_resource = side_a_info[1]
# side_a_name = side_a_info[2]
for port_name in side_b: for port_name in side_b:
print(side_b) logger.info(side_b)
side_b_info = self.local_realm.name_to_eid(port_name, debug=debug_) side_b_info = self.local_realm.name_to_eid(port_name, debug=debug_)
side_b_shelf = side_b_info[0] side_b_shelf = side_b_info[0]
side_b_resource = side_b_info[1] side_b_resource = side_b_info[1]
@@ -587,14 +591,11 @@ class L3CXProfile(LFCliBase):
"multi_conn": mconn_b, "multi_conn": mconn_b,
} }
# print("2: endp-side-b: ", endp_side_b)
url = "/cli-json/add_endp" url = "/cli-json/add_endp"
self.local_realm.json_post(url, endp_side_a, debug_=debug_, self.local_realm.json_post(url, endp_side_a, debug_=debug_,
suppress_related_commands_=suppress_related_commands) suppress_related_commands_=suppress_related_commands)
self.local_realm.json_post(url, endp_side_b, debug_=debug_, self.local_realm.json_post(url, endp_side_b, debug_=debug_,
suppress_related_commands_=suppress_related_commands) suppress_related_commands_=suppress_related_commands)
# print("napping %f sec" %sleep_time )
time.sleep(sleep_time) time.sleep(sleep_time)
url = "cli-json/set_endp_flag" url = "cli-json/set_endp_flag"
@@ -614,7 +615,6 @@ class L3CXProfile(LFCliBase):
} }
self.local_realm.json_post(url, data, debug_=debug_, self.local_realm.json_post(url, data, debug_=debug_,
suppress_related_commands_=suppress_related_commands) suppress_related_commands_=suppress_related_commands)
# print("CXNAME451: %s" % cx_name)
data = { data = {
"alias": cx_name, "alias": cx_name,
"test_mgr": "default_tm", "test_mgr": "default_tm",
@@ -628,15 +628,18 @@ class L3CXProfile(LFCliBase):
"milliseconds": self.report_timer "milliseconds": self.report_timer
}) })
else: else:
logger.critical(
"side_a or side_b must be of type list but not both: side_a is type {side_a} side_b is type {side_b}".format(
side_a=type(side_a), side_b=type(side_b)))
raise ValueError( raise ValueError(
"side_a or side_b must be of type list but not both: side_a is type %s side_b is type %s" % ( "side_a or side_b must be of type list but not both: side_a is type %s side_b is type %s" % (
type(side_a), type(side_b))) type(side_a), type(side_b)))
if debug_: logger.debug("wait_until_endps_appear these_endp: {these_endp} debug_ {debug_}".format(
print("wait_until_endps_appear these_endp: {} debug_ {}".format(these_endp, debug_)) these_endp=these_endp, debug_=debug_))
rv = self.local_realm.wait_until_endps_appear(these_endp, debug=debug_, timeout=timeout) rv = self.local_realm.wait_until_endps_appear(these_endp, debug=debug_, timeout=timeout)
if not rv: if not rv:
if debug_: logger.error("L3CXProfile::create, Could not create/find endpoints")
print("ERROR: L3CXProfile::create, Could not create/find endpoints")
return False, False return False, False
for data in cx_post_data: for data in cx_post_data:
@@ -646,11 +649,7 @@ class L3CXProfile(LFCliBase):
rv = self.local_realm.wait_until_cxs_appear(these_cx, debug=debug_, timeout=timeout) rv = self.local_realm.wait_until_cxs_appear(these_cx, debug=debug_, timeout=timeout)
if not rv: if not rv:
if debug_: logger.error("L3CXProfile::create, Could not create/find connections.")
print("ERROR: L3CXProfile::create, Could not create/find connections.")
return False, these_endp return False, these_endp
return these_cx, these_endp return these_cx, these_endp
def to_string(self):
pprint.pprint(self)