Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse platform.json, hwsku.json for generate minigraph #2887

Merged
merged 2 commits into from
Feb 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions ansible/library/port_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import os
import traceback
import subprocess
import json
import ast
from operator import itemgetter
from itertools import groupby
from collections import defaultdict
from collections import OrderedDict

try:
from sonic_py_common import multi_asic
Expand Down Expand Up @@ -49,6 +52,8 @@
FILE_PATH = '/usr/share/sonic/device'
PORTMAP_FILE = 'port_config.ini'
ALLOWED_HEADER = ['name', 'lanes', 'alias', 'index', 'asic_port_name', 'role', 'speed']
PLATFORM_FILE = 'platform.json'
HWSKU_FILE = 'hwsku.json'

MACHINE_CONF = '/host/machine.conf'
ONIE_PLATFORM_KEY = 'onie_platform'
Expand Down Expand Up @@ -89,11 +94,98 @@ def get_portconfig_path(self, asic_id=None):
return portconfig
return None

def get_platform_path(self):
platform = self.get_platform_type()
if platform is None:
return None
platform_path = os.path.join(FILE_PATH, platform, PLATFORM_FILE)
if os.path.exists(platform_path):
return platform_path
return None

def get_hwsku_path(self):
platform = self.get_platform_type()
if platform is None:
return None
hwsku_path = os.path.join(FILE_PATH, platform, self.hwsku, HWSKU_FILE)
if os.path.exists(hwsku_path):
return hwsku_path
return None

def get_portmap_from_platform_hwsku(self, platform_file, hwsku_file, aliases, portmap, aliasmap, portspeed):
decoder = json.JSONDecoder(object_pairs_hook=OrderedDict)
brkout_pattern = r'(\d{1,3})x(\d{1,3}G)(\[\d{1,3}G\])?(\((\d{1,3})\))?'
with open(platform_file) as f:
data = json.load(f, object_pairs_hook=OrderedDict)
platform_dict = decoder.decode(json.dumps(data))
with open(hwsku_file) as f:
data = json.load(f)
hwsku_dict = ast.literal_eval(json.dumps(data))
for interface in platform_dict["interfaces"]:
if interface not in hwsku_dict["interfaces"]:
raise Exception("interface {} not in hwsku dict".format(interface))
alias_at_lanes = platform_dict["interfaces"][interface]['alias_at_lanes']
lanes = platform_dict["interfaces"][interface]['lanes']
breakout_mode = hwsku_dict["interfaces"][interface]["default_brkout_mode"]

# Asymmetric breakout mode
if re.search("\+", breakout_mode) is not None:
breakout_parts = breakout_mode.split("+")
match_list = [re.match(brkout_pattern, i).groups() for i in breakout_parts]
# Symmetric breakout mode
else:
match_list = [re.match(brkout_pattern, breakout_mode).groups()]

offset = 0
parent_intf_id = int(re.search("Ethernet(\d+)", interface).group(1))
for k in match_list:
if k is None:
continue
num_lane_used, speed, assigned_lane = k[0], k[1], k[4]

if assigned_lane is None:
assigned_lane = len(lanes.split(","))
parent_intf_id = int(offset)+int(parent_intf_id)

alias_start = 0 + offset
step = int(assigned_lane)//int(num_lane_used)
for i in range(0, int(assigned_lane), step):
intf_name = "Ethernet" + str(parent_intf_id)
alias = alias_at_lanes.split(",")[alias_start]
aliases.append(alias)
portmap[intf_name] = alias
aliasmap[alias] = intf_name
if speed:
speed_pat = re.search("^((\d+)G|\d+)$", speed.upper())
if speed_pat is None:
raise Exception('{} speed is not Supported...'.format(speed))
speed_G, speed_orig = speed_pat.group(2), speed_pat.group(1)
if speed_G:
portspeed[intf_name] = str(int(speed_G)*1000)
else:
portspeed[intf_name] = speed_orig
else:
raise Exception('Regex return for speed is None...')

parent_intf_id += step
alias_start += step
parent_intf_id = 0
offset = int(assigned_lane) + int(offset)

def get_portmap(self, asic_id=None):
aliases = []
portmap = {}
aliasmap = {}
portspeed = {}

# if platform.json and hwsku.json exist, than get portmap from hwsku
platform_file = self.get_platform_path()
hwsku_file = self.get_hwsku_path()
if platform_file and hwsku_file:
self.get_portmap_from_platform_hwsku(platform_file, hwsku_file, aliases, portmap, aliasmap, portspeed)
return (aliases, portmap, aliasmap, portspeed)

# platform.json or hwsku.json does not exist so get portmap from port_config.ini
filename = self.get_portconfig_path(asic_id)
if filename is None:
raise Exception("Something wrong when trying to find the portmap file, either the hwsku is not available or file location is not correct")
Expand Down