Skip to content

Commit 355c38d

Browse files
vdahiya12santhosh-kt
authored andcommitted
[sonic-utilities][sonic-py-common] Move logic to get port config file path to sonic-py-common and update sonic-utilities to comply (sonic-net#5264)
* [sonic-utilities]update submodule with fix This PR addresses fixes in sonic-py-common to imitate the behavior inside sonic-cfggen. Essentially this is a fix for accessing the port-config file. First check if there is a platform.json file for config generation and then for legacy port_config.ini. Also updating the sub-module sonic-utilities. Fix pfcwd stats crash with invalid queue name (sonic-net#1077) [show][bgp]Display the Total number of neighbors in the show ip bgp(v6) summary. (sonic-net#1079) [config] Update SONiC Environment Vars When Loading Minigraph (sonic-net#1073) Multi asic platform changes for interface, portchannel commands (sonic-net#878) Update Command-Reference.md (sonic-net#1075) [filter-fdb] Fix Filter FDB With IPv6 Present in Config DB (sonic-net#1059) [config] Remove _get_breakout_cfg_file_name helper function (sonic-net#1069) [SHOW][BGP] support show ip(v6) bgp summary for multi asic platform (sonic-net#1064) [fanshow] Display other fan status, such as Updating (sonic-net#1014) Add ip_prefix len based on proxy_arp status (sonic-net#1046) Enable the platform specific ssd firmware upgrade during reboot (sonic-net#954) [show][cli[show interface portchannel support for Multi ASIC (sonic-net#1005) support show interface commands for multi ASIC platforms (sonic-net#1006) Signed-off-by: vaibhav-dahiya <[email protected]>
1 parent a853da9 commit 355c38d

File tree

4 files changed

+91
-57
lines changed

4 files changed

+91
-57
lines changed

src/sonic-config-engine/portconfig.py

+3-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
from collections import OrderedDict
99
from swsssdk import ConfigDBConnector
10+
from sonic_py_common import device_info
1011
except ImportError as e:
1112
raise ImportError("%s - required module not found" % str(e))
1213

@@ -64,33 +65,6 @@ def db_connect_configdb():
6465
config_db = None
6566
return config_db
6667

67-
def get_port_config_file_name(hwsku=None, platform=None, asic=None):
68-
69-
# check 'platform.json' file presence
70-
port_config_candidates_Json = []
71-
port_config_candidates_Json.append(os.path.join(PLATFORM_ROOT_PATH_DOCKER, PLATFORM_JSON))
72-
if platform:
73-
port_config_candidates_Json.append(os.path.join(PLATFORM_ROOT_PATH, platform, PLATFORM_JSON))
74-
75-
# check 'portconfig.ini' file presence
76-
port_config_candidates = []
77-
port_config_candidates.append(os.path.join(HWSKU_ROOT_PATH, PORT_CONFIG_INI))
78-
if hwsku:
79-
if platform:
80-
if asic:
81-
port_config_candidates.append(os.path.join(PLATFORM_ROOT_PATH, platform, hwsku, asic, PORT_CONFIG_INI))
82-
port_config_candidates.append(os.path.join(PLATFORM_ROOT_PATH, platform, hwsku, PORT_CONFIG_INI))
83-
port_config_candidates.append(os.path.join(PLATFORM_ROOT_PATH_DOCKER, hwsku, PORT_CONFIG_INI))
84-
port_config_candidates.append(os.path.join(SONIC_ROOT_PATH, hwsku, PORT_CONFIG_INI))
85-
86-
elif platform and not hwsku:
87-
port_config_candidates.append(os.path.join(PLATFORM_ROOT_PATH, platform, PORT_CONFIG_INI))
88-
89-
for candidate in port_config_candidates_Json + port_config_candidates:
90-
if os.path.isfile(candidate):
91-
return candidate
92-
return None
93-
9468
def get_hwsku_file_name(hwsku=None, platform=None):
9569
hwsku_candidates_Json = []
9670
hwsku_candidates_Json.append(os.path.join(HWSKU_ROOT_PATH, HWSKU_JSON))
@@ -119,7 +93,7 @@ def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_conf
11993
return (ports, port_alias_map, port_alias_asic_map)
12094

12195
if not port_config_file:
122-
port_config_file = get_port_config_file_name(hwsku, platform, asic)
96+
port_config_file = device_info.get_path_to_port_config_file(hwsku, asic)
12397
if not port_config_file:
12498
return ({}, {}, {})
12599

@@ -289,7 +263,7 @@ def parse_platform_json_file(hwsku_json_file, platform_json_file):
289263

290264
def get_breakout_mode(hwsku=None, platform=None, port_config_file=None):
291265
if not port_config_file:
292-
port_config_file = get_port_config_file_name(hwsku, platform)
266+
port_config_file = device_info.get_path_to_port_config_file(hwsku)
293267
if not port_config_file:
294268
return None
295269
if port_config_file.endswith('.json'):

src/sonic-config-engine/sonic-cfggen

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ from minigraph import minigraph_encoder
4141
from minigraph import parse_xml
4242
from minigraph import parse_device_desc_xml
4343
from minigraph import parse_asic_sub_role
44-
from portconfig import get_port_config, get_port_config_file_name, get_breakout_mode
45-
from sonic_py_common.device_info import get_platform, get_system_mac
44+
from portconfig import get_port_config, get_breakout_mode
4645
from sonic_py_common.multi_asic import get_asic_id_from_name, is_multi_asic
46+
from sonic_py_common import device_info
4747
from config_samples import generate_sample_config
4848
from config_samples import get_available_config
4949
from swsssdk import SonicV2Connector, ConfigDBConnector, SonicDBConfig, ConfigDBPipeConnector
@@ -277,7 +277,7 @@ def main():
277277
group.add_argument("-K", "--key", help="Lookup for a specific key")
278278
args = parser.parse_args()
279279

280-
platform = get_platform()
280+
platform = device_info.get_platform()
281281

282282
db_kwargs = {}
283283
if args.redis_unix_sock_file != None:
@@ -301,7 +301,7 @@ def main():
301301
}}}
302302
deep_update(data, hardware_data)
303303
if args.port_config is None:
304-
args.port_config = get_port_config_file_name(hwsku, platform)
304+
args.port_config = device_info.get_path_to_port_config_file(hwsku)
305305
(ports, _, _) = get_port_config(hwsku, platform, args.port_config, asic_id)
306306
if not ports:
307307
print('Failed to get port config', file=sys.stderr)
@@ -356,11 +356,11 @@ def main():
356356
asic_role = parse_asic_sub_role(args.minigraph, asic_name)
357357

358358
if asic_role is not None and asic_role.lower() == "backend":
359-
mac = get_system_mac(namespace=asic_name)
359+
mac = device_info.get_system_mac(namespace=asic_name)
360360
else:
361-
mac = get_system_mac()
361+
mac = device_info.get_system_mac()
362362
else:
363-
mac = get_system_mac()
363+
mac = device_info.get_system_mac()
364364

365365
hardware_data = {'DEVICE_METADATA': {'localhost': {
366366
'platform': platform,

src/sonic-py-common/sonic_py_common/device_info.py

+80-20
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,15 @@ def get_asic_conf_file_path():
153153
return None
154154

155155

156-
def get_paths_to_platform_and_hwsku_dirs():
156+
def get_path_to_platform_dir():
157157
"""
158-
Retreives the paths to the device's platform and hardware SKU data
159-
directories
158+
Retreives the paths to the device's platform directory
160159
161160
Returns:
162-
A tuple of two strings, the first containing the path to the platform
163-
directory of the device, the second containing the path to the hardware
164-
SKU directory of the device
161+
A string containing the path to the platform directory of the device
165162
"""
166-
# Get platform and hwsku
167-
(platform, hwsku) = get_platform_and_hwsku()
163+
# Get platform
164+
platform = get_platform()
168165

169166
# Determine whether we're running in a container or on the host
170167
platform_path_host = os.path.join(HOST_DEVICE_PATH, platform)
@@ -176,31 +173,94 @@ def get_paths_to_platform_and_hwsku_dirs():
176173
else:
177174
raise OSError("Failed to locate platform directory")
178175

176+
return platform_path
177+
178+
def get_path_to_hwsku_dir():
179+
"""
180+
Retreives the path to the device's hardware SKU data directory
181+
182+
Returns:
183+
A string, containing the path to the hardware SKU directory of the device
184+
"""
185+
186+
# Get Platform path first
187+
platform_path = get_path_to_platform_dir()
188+
189+
# Get hwsku
190+
hwsku = get_hwsku()
191+
179192
hwsku_path = os.path.join(platform_path, hwsku)
180193

181-
return (platform_path, hwsku_path)
194+
return hwsku_path
182195

196+
def get_paths_to_platform_and_hwsku_dirs():
197+
"""
198+
Retreives the paths to the device's platform and hardware SKU data
199+
directories
183200
184-
def get_path_to_port_config_file():
201+
Returns:
202+
A tuple of two strings, the first containing the path to the platform
203+
directory of the device, the second containing the path to the hardware
204+
SKU directory of the device
205+
"""
206+
207+
# Get Platform path first
208+
platform_path = get_path_to_platform_dir()
209+
210+
# Get hwsku
211+
hwsku = get_hwsku()
212+
213+
hwsku_path = os.path.join(platform_path, hwsku)
214+
215+
return (platform_path, hwsku_path)
216+
217+
def get_path_to_port_config_file(hwsku=None, asic=None):
185218
"""
186219
Retrieves the path to the device's port configuration file
187220
221+
Args:
222+
hwsku: a string, it is allowed to be passed in args because when loading the
223+
initial configuration on the device, the HwSKU is not yet present in ConfigDB.
224+
asic: a string , asic argument should be passed on multi-ASIC devices only,
225+
it should be omitted on single-ASIC platforms.
226+
188227
Returns:
189228
A string containing the path the the device's port configuration file
190229
"""
191-
# Get platform and hwsku path
192-
(platform_path, hwsku_path) = get_paths_to_platform_and_hwsku_dirs()
193230

194-
# First check for the presence of the new 'platform.json' file
195-
port_config_file_path = os.path.join(platform_path, PLATFORM_JSON_FILE)
196-
if not os.path.isfile(port_config_file_path):
197-
# platform.json doesn't exist. Try loading the legacy 'port_config.ini' file
198-
port_config_file_path = os.path.join(hwsku_path, PORT_CONFIG_FILE)
199-
if not os.path.isfile(port_config_file_path):
200-
raise OSError("Failed to detect port config file: {}".format(port_config_file_path))
231+
"""
232+
This platform check is performed to make sure we return a None
233+
in case of unit-tests within sonic-cfggen where platform is not expected to be
234+
present because tests are not run on actual Hardware/Container.
235+
TODO: refactor sonic-cfggen such that we can remove this check
236+
"""
237+
238+
platform = get_platform()
239+
if not platform:
240+
return None
241+
242+
if hwsku:
243+
platform_path = get_path_to_platform_dir()
244+
hwsku_path = os.path.join(platform_path, hwsku)
245+
else:
246+
(platform_path, hwsku_path) = get_paths_to_platform_and_hwsku_dirs()
201247

202-
return port_config_file_path
248+
port_config_candidates = []
203249

250+
# Check for 'platform.json' file presence first
251+
port_config_candidates.append(os.path.join(platform_path, PLATFORM_JSON_FILE))
252+
253+
# Check for 'port_config.ini' file presence in a few locations
254+
if asic:
255+
port_config_candidates.append(os.path.join(hwsku_path, asic, PORT_CONFIG_FILE))
256+
else:
257+
port_config_candidates.append(os.path.join(hwsku_path, PORT_CONFIG_FILE))
258+
259+
for candidate in port_config_candidates:
260+
if os.path.isfile(candidate):
261+
return candidate
262+
263+
return None
204264

205265
def get_sonic_version_info():
206266
if not os.path.isfile(SONIC_VERSION_YAML_PATH):

src/sonic-utilities

0 commit comments

Comments
 (0)