Skip to content

Commit

Permalink
add read config_db.json
Browse files Browse the repository at this point in the history
Signed-off-by: ms-junyi <t-junyixiao@microsoft.com>

format

Signed-off-by: ms-junyi <t-junyixiao@microsoft.com>

addomment for each function

Signed-off-by: ms-junyi <t-junyixiao@microsoft.com>

add comment for each function

Signed-off-by: ms-junyi <t-junyixiao@microsoft.com>
  • Loading branch information
ms-junyi committed Jul 1, 2022
1 parent c61a7ee commit 99a9719
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
36 changes: 32 additions & 4 deletions test/sai_test/config/port_configer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
'''
Expand All @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions test/sai_test/sai_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 23 additions & 8 deletions test/sai_test/sai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
Thrift SAI interface basic utils.
"""

import os
import time
import struct
import socket
import json

from functools import wraps

Expand All @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 99a9719

Please sign in to comment.