Adding parsing and object for clock conf files

Adding keyword, parsing and objects for ptp clock conf files.

Change-Id: I394d09f36ca0aac0448d5904a036da0e21e7d40b
This commit is contained in:
jpike
2025-04-25 15:13:01 -04:00
parent cb1395e92c
commit fdf988430b
5 changed files with 256 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
from framework.ssh.ssh_connection import SSHConnection
from keywords.base_keyword import BaseKeyword
from keywords.ptp.cat.objects.clock_conf_output import ClockConfOutput
class CatClockConfKeywords(BaseKeyword):
"""
Class for CAT Clock Conf Keywords.
"""
def __init__(self, ssh_connection: SSHConnection):
self.ssh_connection = ssh_connection
def cat_clock_conf(self, clock_conf_location: str) -> ClockConfOutput:
"""
Runs the command sudo cat <clock_conf_location> ex. /etc/linuxptp/ptpinstance/clock-conf.conf.
Args:
clock_conf_location (str): the clock conf location.
Returns:
ClockConfOutput: the ClockConfOutput.
"""
output = self.ssh_connection.send_as_sudo(f"cat {clock_conf_location}")
clock_conf_output = ClockConfOutput(output)
return clock_conf_output

View File

@@ -0,0 +1,52 @@
from framework.exceptions.keyword_exception import KeywordException
class CatClockConfParser:
"""
Class for cat clock conf parsing
Example:
ifname [enp138s0f0]
base_port [enp138s0f0]
sma1 input
ifname [enp81s0f2]
base_port [enp81s0f0]
sma1 output
"""
def __init__(self, cat_clock_conf_output: list[str]):
"""
Constructor
Args:
cat_clock_conf_output (list[str]): a list of strings representing the output of a 'cat clock-conf.conf' command.
"""
self.cat_clock_conf_output = cat_clock_conf_output
def get_output_values_dict_list(self) -> list[dict]:
"""
Getter for output values dict list
Returns:
list[dict]: the output values dict list
"""
output_values_dict_list = []
output_values_dict = {}
is_first_ifname = True
for row in self.cat_clock_conf_output:
if "~$" in row:
continue # this is a prompt and should be ignored
values = row.strip("\n").split(None, 1) # split once
if len(values) == 2:
key, value = values
if key.strip() == "ifname" and not is_first_ifname: # we are now entering a new interface
output_values_dict_list.append(output_values_dict)
output_values_dict = {}
elif key.strip() == "ifname" and is_first_ifname: # this is the first interface
is_first_ifname = False
output_values_dict[key.strip()] = value.strip("[]")
else:
raise KeywordException(f"Line with values: {row} was not in the expected format")
output_values_dict_list.append(output_values_dict)
return output_values_dict_list

View File

@@ -0,0 +1,90 @@
class ClockConfObject:
"""
Object to hold the values of Clock conf Object
"""
def __init__(self):
self.ifname: str = ""
self.base_port: str = ""
self.sma_name: str = ""
self.sma_mode: str = ""
def set_ifname(self, ifname: str):
"""
Setter for ifname
Args:
ifname (str): the ifname
"""
self.ifname = ifname
def get_ifname(self) -> str:
"""
Getter for ifname
Returns:
str: the ifname
"""
return self.ifname
def set_base_port(self, base_port: str):
"""
Setter for base_port
Args:
base_port (str): the base_port
"""
self.base_port = base_port
def get_base_port(self) -> str:
"""
Getter for base_port
Returns:
str: the base_port
"""
return self.base_port
def set_sma_name(self, sma_name: str):
"""
Setter for sma_name
Args:
sma_name (str): the sma_name
"""
self.sma_name = sma_name
def get_sma_name(self) -> str:
"""
Getter for sma_name
Returns:
str: the sma_name
"""
return self.sma_name
def set_sma_mode(self, sma_mode: str):
"""
Setter for sma_mode
Args:
sma_mode (str): the sma_mode
"""
self.sma_mode = sma_mode
def get_sma_mode(self) -> str:
"""
Getter for sma_mode
Returns:
str: the sma_mode
"""
return self.sma_mode

View File

@@ -0,0 +1,60 @@
from keywords.ptp.cat.cat_clock_conf_parser import CatClockConfParser
from keywords.ptp.cat.objects.clock_conf_object import ClockConfObject
class ClockConfOutput:
"""
This class parses the output of cat clock conf file
Example:
ifname [enp138s0f0]
base_port [enp138s0f0]
sma1 input
ifname [enp81s0f0]
base_port [enp81s0f0]
sma1 output
"""
def __init__(self, clock_conf_output: list[str]):
"""
Constructor.
Create an internal ClockConfObject from the passed parameter.
Args:
clock_conf_output (list[str]): a list of strings representing the clock conf output
"""
cat_clock_conf_parser = CatClockConfParser(clock_conf_output)
output_values = cat_clock_conf_parser.get_output_values_dict_list()
self.clock_conf_objects: list[ClockConfObject] = []
for values in output_values:
clock_conf_object = ClockConfObject()
if "ifname" in values:
clock_conf_object.set_ifname(values["ifname"])
if "base_port" in values:
clock_conf_object.set_base_port(values["base_port"])
if "sma1" in values:
clock_conf_object.set_sma_name("sma1")
clock_conf_object.set_sma_mode(values["sma1"])
if "sma2" in values:
clock_conf_object.set_sma_name("sma2")
clock_conf_object.set_sma_mode(values["sma2"])
self.clock_conf_objects.append(clock_conf_object)
def get_clock_conf_objects(self) -> list[ClockConfObject]:
"""
Getter for ClockConfObject objects.
Returns:
list[ClockConfObject]: A list ClockDescriptionObjects
"""
return self.clock_conf_objects

View File

@@ -0,0 +1,27 @@
from keywords.ptp.cat.objects.clock_conf_output import ClockConfOutput
# fmt off
clock_conf_output = ["ifname [enp138s0f0]\n", "base_port [enp138s0f0]\n", "sma1 input\n", "ifname [enp81s0f0]\n", "base_port [enp81s0f0]\n", "sma1 output\n", "sysadmin@controller-0:~$ \n"]
def test_clock_conf_output():
"""
Test the cat clock conf parser and output.
"""
clock_config_output = ClockConfOutput(clock_conf_output)
clock_config_objects = clock_config_output.get_clock_conf_objects()
assert len(clock_config_objects), 2
clock_config_object = clock_config_objects[0]
assert clock_config_object.get_ifname() == "enp138s0f0"
assert clock_config_object.get_base_port() == "enp138s0f0"
assert clock_config_object.get_sma_name() == "sma1"
assert clock_config_object.get_sma_mode() == "input"
clock_config_object = clock_config_objects[1]
assert clock_config_object.get_ifname() == "enp81s0f0"
assert clock_config_object.get_base_port() == "enp81s0f0"
assert clock_config_object.get_sma_name() == "sma1"
assert clock_config_object.get_sma_mode() == "output"