From 05304c123811d72b14253470d3dc250f191ed9da Mon Sep 17 00:00:00 2001 From: ms-junyi <106718431+ms-junyi@users.noreply.github.com> Date: Fri, 1 Jul 2022 17:01:36 +0800 Subject: [PATCH] add read config_db.json (#1519) Signed-off-by: ms-junyi format Signed-off-by: ms-junyi addomment for each function Signed-off-by: ms-junyi add comment for each function Signed-off-by: ms-junyi --- test/sai_test/config/port_configer.py | 36 ++++++++++++++++++++++++--- test/sai_test/sai_test_base.py | 8 ++++++ test/sai_test/sai_utils.py | 31 +++++++++++++++++------ 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/test/sai_test/config/port_configer.py b/test/sai_test/config/port_configer.py index 3b1c61d2e..fa73929d3 100644 --- a/test/sai_test/config/port_configer.py +++ b/test/sai_test/config/port_configer.py @@ -94,6 +94,8 @@ def __init__(self, test_obj) -> None: """ self.test_obj = test_obj self.client = test_obj.client + config_driver = ConfigDBOpertion() + self.config = config_driver.get_port_config() def create_bridge_ports(self, bridge_id, port_list): """ @@ -356,7 +358,8 @@ def turn_on_port_admin_state(self, port_list): print("Set port...") for i, port in enumerate(port_list): sai_thrift_set_port_attribute( - self.client, port_oid=port, mtu=PORT_MTU, admin_state=True, fec_mode=SAI_PORT_FEC_MODE_RS) + self.client, port_oid=port, mtu=self.get_mtu(), admin_state=True, + fec_mode=self.get_fec_mode()) def turn_up_and_check_ports(self, port_list): ''' @@ -381,14 +384,39 @@ def turn_up_and_check_ports(self, port_list): sai_thrift_set_port_attribute( self.client, port_oid=port_id, - mtu=PORT_MTU, - admin_state=True, - fec_mode=SAI_PORT_FEC_MODE_RS) + mtu=self.get_mtu(), + admin_state=True, + fec_mode=self.get_fec_mode()) if all_ports_are_up: print("Retry {} times turn up port.".format(num_of_tries)) break if not all_ports_are_up: print("Not all the ports are up after {} rounds of retries.".format(retries)) + + + def get_fec_mode(self): + ''' + get fec mode from config_db.json + + RETURN: + int: SAI_PORT_FEC_MODE_X + ''' + fec_mode = self.config.get('fec') + fec_change = { + None : SAI_PORT_FEC_MODE_NONE, + 'rs' : SAI_PORT_FEC_MODE_RS, + 'fc' : SAI_PORT_FEC_MODE_FC, + } + return fec_change[fec_mode] + + def get_mtu(self): + ''' + get mtu from config_db.json + + RETURN: + int: mtu number + ''' + return int(self.config.get('mtu')) class PortConfig(object): diff --git a/test/sai_test/sai_test_base.py b/test/sai_test/sai_test_base.py index 055ebc03c..44398568c 100644 --- a/test/sai_test/sai_test_base.py +++ b/test/sai_test/sai_test_base.py @@ -33,6 +33,14 @@ import sai_thrift.sai_adapter as adapter from sai_thrift.sai_adapter import * from sai_utils import * +from config.port_configer import t0_port_config_helper +from config.port_configer import PortConfiger +from config.switch_configer import t0_switch_config_helper +from config.switch_configer import SwitchConfiger +from config.vlan_configer import t0_vlan_config_helper +from config.vlan_configer import VlanConfiger +from config.fdb_configer import t0_fdb_config_helper +from config.fdb_configer import FdbConfiger THRIFT_PORT = 9092 is_configured = False diff --git a/test/sai_test/sai_utils.py b/test/sai_test/sai_utils.py index 47a8ee369..0e7a2a55e 100644 --- a/test/sai_test/sai_utils.py +++ b/test/sai_test/sai_utils.py @@ -22,9 +22,11 @@ Thrift SAI interface basic utils. """ +import os import time import struct import socket +import json from functools import wraps @@ -33,14 +35,7 @@ from sai_thrift.sai_adapter import * from constant import * -from config.port_configer import t0_port_config_helper -from config.port_configer import PortConfiger -from config.switch_configer import t0_switch_config_helper -from config.switch_configer import SwitchConfiger -from config.vlan_configer import t0_vlan_config_helper -from config.vlan_configer import VlanConfiger -from config.fdb_configer import t0_fdb_config_helper -from config.fdb_configer import FdbConfiger + def sai_ipprefix(prefix_str): @@ -99,3 +94,23 @@ def num_to_dotted_quad(address, ipv4=True): result = result + sign i += 1 return result[:-1] + + +class ConfigDBOpertion(): + ''' + read config from config_db.json + ''' + def __init__(self): + path =os.path.join( os.path.dirname(__file__),"resources/config_db.json") #REPLACE + self.config_json = None + with open(path,mode='r') as f: + self.config_json = json.load(f) + + def get_port_config(self): + ''' + RETURN: + dict: port config + ''' + port_conf = self.config_json.get('PORT') + key_0 = list(port_conf.keys())[0] + return self.config_json.get('PORT').get(key_0)