Skip to content

Commit

Permalink
Remove alias_map.json (sonic-net#87)
Browse files Browse the repository at this point in the history
This file was added long time ago and now it is not needed anymore.
We can simply get port aliases from DB.

This also means that aliases will be now updated from DB once
a reinit timeout, instead of beeing static in alias_map.json

Signed-off-by: Stepan Blyschak <stepanb@mellanox.com>
  • Loading branch information
stepanblyschak authored and lguohan committed Sep 26, 2018
1 parent b32f3ee commit 7383cd5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 166 deletions.
16 changes: 0 additions & 16 deletions src/sonic_ax_impl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import json
import logging.handlers

# path where a user may define an alias map
USER_DEFINED_ALIAS_MAP_FILEPATH = '/etc/snmp/alias_map.json'

# configure logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(logging.NullHandler())

_if_alias_map = None

# open user-defined alias map first
try:
with open(USER_DEFINED_ALIAS_MAP_FILEPATH) as f:
_if_alias_map = json.load(f)
_if_alias_map = {k.encode('ascii'): v.encode('ascii') for k, v in _if_alias_map.items()}
except ValueError as e:
logger.error(
"User map contains error(s). Ensure file is well-formed JSON. Falling back to default map. {}".format(str(e))
)
except OSError:
# No alias map found, error is emitted and handled in mibs/__init__.py
logger.info("No user-defined alias map found, using default map.")
15 changes: 9 additions & 6 deletions src/sonic_ax_impl/mibs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from swsssdk import SonicV2Connector
from swsssdk import port_util
from swsssdk.port_util import get_index, get_index_from_str
from sonic_ax_impl import logger, _if_alias_map
from sonic_ax_impl import logger

COUNTERS_PORT_NAME_MAP = b'COUNTERS_PORT_NAME_MAP'
COUNTERS_QUEUE_NAME_MAP = b'COUNTERS_QUEUE_NAME_MAP'
Expand Down Expand Up @@ -156,12 +156,15 @@ def init_sync_d_interface_tables(db_conn):
.format(port_util.SONIC_ETHERNET_RE_PATTERN))
logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2))

# { SONiC name -> optional rename }
if_alias_map = _if_alias_map
db_conn.connect(APPL_DB)

if_alias_map = dict()

for if_name in if_name_map:
if_entry = db_conn.get_all(APPL_DB, if_entry_table(if_name), blocking=True)
if_alias_map[if_name] = if_entry.get(b'alias', if_name)

logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2))
if if_alias_map is None or len(if_alias_map) == 0:
logger.warning("No alias map found--port names will use SONiC names.")
if_alias_map = dict(zip(if_name_map.keys(), if_name_map.keys()))

return if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map

Expand Down
160 changes: 33 additions & 127 deletions tests/mock_tables/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -456,254 +456,160 @@
},
"PORT_TABLE:Ethernet0": {
"description": "snowflake",
"alias": "etp1",
"speed": 100000
},
"PORT_TABLE:Ethernet4": {
"description": "snowflake",
"alias": "etp2",
"speed": 100000
},
"PORT_TABLE:Ethernet8": {
"description": "snowflake",
"alias": "etp3",
"speed": 100000
},
"PORT_TABLE:Ethernet12": {
"description": "snowflake",
"alias": "etp4",
"speed": 100000
},
"PORT_TABLE:Ethernet16": {
"description": "snowflake",
"alias": "etp5",
"speed": 100000
},
"PORT_TABLE:Ethernet20": {
"description": "snowflake",
"alias": "etp6",
"speed": 100000
},
"PORT_TABLE:Ethernet24": {
"description": "snowflake",
"alias": "etp7",
"speed": 100000
},
"PORT_TABLE:Ethernet28": {
"description": "snowflake",
"alias": "etp8",
"speed": 100000
},
"PORT_TABLE:Ethernet32": {
"description": "snowflake",
"alias": "etp9",
"speed": 100000
},
"PORT_TABLE:Ethernet36": {
"description": "snowflake",
"alias": "etp10",
"speed": 100000
},
"PORT_TABLE:Ethernet40": {
"description": "snowflake",
"alias": "etp11",
"speed": 100000
},
"PORT_TABLE:Ethernet44": {
"description": "snowflake",
"alias": "etp12",
"speed": 100000
},
"PORT_TABLE:Ethernet48": {
"description": "snowflake",
"alias": "etp13",
"speed": 100000
},
"PORT_TABLE:Ethernet52": {
"description": "snowflake",
"alias": "etp14",
"speed": 100000
},
"PORT_TABLE:Ethernet56": {
"description": "snowflake",
"alias": "etp15",
"speed": 100000
},
"PORT_TABLE:Ethernet60": {
"description": "snowflake",
"alias": "etp16",
"speed": 100000
},
"PORT_TABLE:Ethernet64": {
"description": "snowflake",
"alias": "etp17",
"speed": 100000
},
"PORT_TABLE:Ethernet68": {
"description": "snowflake",
"alias": "etp18",
"speed": 100000
},
"PORT_TABLE:Ethernet72": {
"description": "snowflake",
"alias": "etp19",
"speed": 100000
},
"PORT_TABLE:Ethernet76": {
"description": "snowflake",
"alias": "etp20",
"speed": 100000
},
"PORT_TABLE:Ethernet80": {
"description": "snowflake",
"alias": "etp21",
"speed": 100000
},
"PORT_TABLE:Ethernet84": {
"description": "snowflake",
"alias": "etp22",
"speed": 100000
},
"PORT_TABLE:Ethernet88": {
"description": "snowflake",
"alias": "etp23",
"speed": 100000
},
"PORT_TABLE:Ethernet92": {
"description": "snowflake",
"alias": "etp24",
"speed": 100000
},
"PORT_TABLE:Ethernet96": {
"description": "snowflake",
"alias": "etp25",
"speed": 100000
},
"PORT_TABLE:Ethernet100": {
"description": "snowflake",
"alias": "etp26",
"speed": 100000
},
"PORT_TABLE:Ethernet104": {
"description": "snowflake",
"alias": "etp27",
"speed": 100000
},
"PORT_TABLE:Ethernet108": {
"description": "snowflake",
"alias": "etp28",
"speed": 100000
},
"PORT_TABLE:Ethernet112": {
"description": "snowflake",
"alias": "etp29",
"speed": 100000
},
"PORT_TABLE:Ethernet116": {
"speed": 1000
"speed": 1000,
"alias": "etp30"
},
"PORT_TABLE:Ethernet120": {
"description": "snowflake"
},
"PORT_TABLE:Ethernet124": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet0": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet4": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet8": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet12": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet16": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet20": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet24": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet28": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet32": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet36": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet40": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet44": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet48": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet52": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet56": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet60": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet64": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet68": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet72": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet76": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet80": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet84": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet88": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet92": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet96": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet100": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet104": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet108": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet112": {
"description": "snowflake",
"speed": 100000
},
"PORT_TABLE:Ethernet116": {
"speed": 1000
},
"PORT_TABLE:Ethernet120": {
"description": "snowflake"
"alias": "et31"
},
"PORT_TABLE:Ethernet124": {
"description": "snowflake",
"alias": "etp32",
"speed": 100000
},
"ROUTE_TABLE:0.0.0.0/0": {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_lldp.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_ipv6_rem_man_addr(self):
def test_local_port_identification(self):
mib_entry = self.lut[(1, 0, 8802, 1, 1, 2, 1, 3, 7, 1, 3)]
ret = mib_entry(sub_id=(1,))
self.assertEquals(ret, b'Ethernet0')
self.assertEquals(ret, b'etp1')
print(ret)

def test_local_port_num(self):
Expand All @@ -154,7 +154,7 @@ def test_getnextpdu_local_port_identification(self):
response = get_pdu.make_response(self.lut)
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
self.assertEqual(str(value0.data), "Ethernet0")
self.assertEqual(str(value0.data), "etp1")

def test_lab_breaks(self):
break1 = b'\x01\x06\x10\x00\x00\x00\x00q\x00\x01\xd1\x02\x00\x01\xd1\x03\x00\x00\x00P\t\x00\x01\x00\x00' \
Expand Down
Loading

0 comments on commit 7383cd5

Please sign in to comment.