From 95c2405d6373ca3c6b69efc3faf293cd41a55c76 Mon Sep 17 00:00:00 2001 From: imarom Date: Wed, 9 Dec 2015 15:01:25 -0500 Subject: read only support --- .../trex_control_plane/common/trex_stats.py | 16 ++++-- .../trex_control_plane/common/trex_streams.py | 61 +++++++++++++++++++- .../trex_control_plane/common/trex_types.py | 66 ++++++++++++++++++++++ 3 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 scripts/automation/trex_control_plane/common/trex_types.py (limited to 'scripts/automation/trex_control_plane/common') diff --git a/scripts/automation/trex_control_plane/common/trex_stats.py b/scripts/automation/trex_control_plane/common/trex_stats.py index 671a0656..2f6ea38d 100755 --- a/scripts/automation/trex_control_plane/common/trex_stats.py +++ b/scripts/automation/trex_control_plane/common/trex_stats.py @@ -5,6 +5,7 @@ from common.text_opts import format_text from client.trex_async_client import CTRexAsyncStats import copy import datetime +import time import re GLOBAL_STATS = 'g' @@ -134,7 +135,8 @@ class CTRexStatsGenerator(object): # fetch owned ports ports = [port_obj for _, port_obj in self._ports_dict.iteritems() - if port_obj.is_acquired() and port_obj.port_id in port_id_list] + if port_obj.port_id in port_id_list] + # display only the first FOUR options, by design if len(ports) > 4: print format_text("[WARNING]: ", 'magenta', 'bold'), format_text("displaying up to 4 ports", 'magenta') @@ -155,7 +157,7 @@ class CTRexStats(object): def __init__(self): self.reference_stats = None self.latest_stats = {} - self.last_update_ts = datetime.datetime.now() + self.last_update_ts = time.time() def __getitem__(self, item): @@ -204,13 +206,16 @@ class CTRexStats(object): def update(self, snapshot): # update - self.last_update_ts = datetime.datetime.now() - self.latest_stats = snapshot - if self.reference_stats == None: + diff_time = time.time() - self.last_update_ts + + # 3 seconds is too much - this is the new reference + if (self.reference_stats == None) or (diff_time > 3): self.reference_stats = self.latest_stats + self.last_update_ts = time.time() + def clear_stats(self): self.reference_stats = self.latest_stats @@ -225,6 +230,7 @@ class CTRexStats(object): def get_rel(self, field, format=False, suffix=""): if not field in self.latest_stats: return "N/A" + if not format: return (self.latest_stats[field] - self.reference_stats[field]) else: diff --git a/scripts/automation/trex_control_plane/common/trex_streams.py b/scripts/automation/trex_control_plane/common/trex_streams.py index 89de7286..86eee1f4 100755 --- a/scripts/automation/trex_control_plane/common/trex_streams.py +++ b/scripts/automation/trex_control_plane/common/trex_streams.py @@ -10,6 +10,7 @@ import copy import os StreamPack = namedtuple('StreamPack', ['stream_id', 'stream']) +LoadedStreamList = namedtuple('LoadedStreamList', ['loaded', 'compiled']) class CStreamList(object): @@ -254,5 +255,61 @@ class CStream(object): raise RuntimeError("CStream object isn't loaded with data. Use 'load_data' method.") -if __name__ == "__main__": - pass + +# describes a stream DB +class CStreamsDB(object): + + def __init__(self): + self.stream_packs = {} + + def load_yaml_file(self, filename): + + stream_pack_name = filename + if stream_pack_name in self.get_loaded_streams_names(): + self.remove_stream_packs(stream_pack_name) + + stream_list = CStreamList() + loaded_obj = stream_list.load_yaml(filename) + + try: + compiled_streams = stream_list.compile_streams() + rc = self.load_streams(stream_pack_name, + LoadedStreamList(loaded_obj, + [StreamPack(v.stream_id, v.stream.dump()) + for k, v in compiled_streams.items()])) + + except Exception as e: + return None + + return self.get_stream_pack(stream_pack_name) + + def load_streams(self, name, LoadedStreamList_obj): + if name in self.stream_packs: + return False + else: + self.stream_packs[name] = LoadedStreamList_obj + return True + + def remove_stream_packs(self, *names): + removed_streams = [] + for name in names: + removed = self.stream_packs.pop(name) + if removed: + removed_streams.append(name) + return removed_streams + + def clear(self): + self.stream_packs.clear() + + def get_loaded_streams_names(self): + return self.stream_packs.keys() + + def stream_pack_exists (self, name): + return name in self.get_loaded_streams_names() + + def get_stream_pack(self, name): + if not self.stream_pack_exists(name): + return None + else: + return self.stream_packs.get(name) + diff --git a/scripts/automation/trex_control_plane/common/trex_types.py b/scripts/automation/trex_control_plane/common/trex_types.py new file mode 100644 index 00000000..3de36e4c --- /dev/null +++ b/scripts/automation/trex_control_plane/common/trex_types.py @@ -0,0 +1,66 @@ + +from collections import namedtuple +from common.text_opts import * + +RpcCmdData = namedtuple('RpcCmdData', ['method', 'params']) + +class RpcResponseStatus(namedtuple('RpcResponseStatus', ['success', 'id', 'msg'])): + __slots__ = () + def __str__(self): + return "{id:^3} - {msg} ({stat})".format(id=self.id, + msg=self.msg, + stat="success" if self.success else "fail") + +# simple class to represent complex return value +class RC(): + + def __init__ (self, rc = None, data = None): + self.rc_list = [] + + if (rc != None) and (data != None): + tuple_rc = namedtuple('RC', ['rc', 'data']) + self.rc_list.append(tuple_rc(rc, data)) + + def add (self, rc): + self.rc_list += rc.rc_list + + def good (self): + return all([x.rc for x in self.rc_list]) + + def bad (self): + return not self.good() + + def data (self): + return [x.data if x.rc else "" for x in self.rc_list] + + def err (self): + return [x.data if not x.rc else "" for x in self.rc_list] + + def annotate (self, desc = None, show_status = True): + if desc: + print format_text('\n{:<60}'.format(desc), 'bold'), + else: + print "" + + if self.bad(): + # print all the errors + print "" + for x in self.rc_list: + if not x.rc: + print format_text("\n{0}".format(x.data), 'bold') + + print "" + if show_status: + print format_text("[FAILED]\n", 'red', 'bold') + + + else: + if show_status: + print format_text("[SUCCESS]\n", 'green', 'bold') + + +def RC_OK(data = ""): + return RC(True, data) +def RC_ERR (err): + return RC(False, err) + -- cgit 1.2.3-korg