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

[sysName]: Implement sysName OID #185

Merged
merged 5 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions src/sonic_ax_impl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class SonicMIB(
rfc1213.InterfacesMIB,
rfc1213.IpMib,
rfc1213.SysNameMIB,
rfc2737.PhysicalTableMIB,
rfc3433.PhysicalSensorTableMIB,
rfc2863.InterfaceMIBObjects,
Expand Down
32 changes: 32 additions & 0 deletions src/sonic_ax_impl/mibs/ietf/rfc1213.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ipaddress
import python_arptable
import socket
from enum import unique, Enum
from bisect import bisect_right

Expand Down Expand Up @@ -667,3 +668,34 @@ class InterfacesMIB(metaclass=MIBMeta, prefix='.1.3.6.1.2.1.2'):
# FIXME Placeholder
ifSpecific = \
SubtreeMIBEntry('2.1.22', if_updater, ValueType.OBJECT_IDENTIFIER, lambda sub_id: ObjectIdentifier.null_oid())

class sysNameUpdater(MIBUpdater):
def __init__(self):
super().__init__()
self.db_conn = mibs.init_db()
self.hostname = socket.gethostname()

def reinit_data(self):
self.db_conn.connect(self.db_conn.CONFIG_DB)
device_metadata = self.db_conn.get_all(self.db_conn.CONFIG_DB, "DEVICE_METADATA|localhost")
Copy link
Contributor

@qiluo-msft qiluo-msft Dec 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_all [](start = 39, length = 7)

Did you test the empty table on DUT? I think device_metadata will be an empty dict instead of None.
Suggest check device_metadata instead of device_metadata is not None below. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you can use get function to get hostname directly.


In reply to: 548318135 [](ancestors = 548318135)

Copy link
Contributor Author

@SuvarnaMeenakshi SuvarnaMeenakshi Dec 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If DEVICE_METADATA table is not present, then get_all returns None.
Updated code as per suggestion and tested on sonic-vs image.
#Resolved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am okay with the code. However I am confused by your comment "If DEVICE_METADATA table is not present, then get_all returns None".
Could you double check?


In reply to: 548356652 [](ancestors = 548356652)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a device, to check this behavior, I tried the below commands; here "DEVICE_METADATA" exists, without DEVICE_METADATA in config_db, hwsku will not be known so dockers will not come up. So instead of checking directly for "DEVICE_METADATA", I checked for some modified fields that will not exist.

device_metadata = db_conn.get_all(db_conn.CONFIG_DB, "DEVICE_METADATA|loca")
print(device_metadata)
None
device_metadata = db_conn.get_all(db_conn.CONFIG_DB, "DEVICE_METADAT")
print(device_metadata)
None
...
hostname=device_metadata.get(b'hos')
print(hostname)
None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above comment does not hold good for SNMP docker with master branch with latest changes.
If a specific key does not exist in the table, A SWIG object is returned and None is not returned.
If key does not exist:

>>> from sonic_ax_impl import mibs
db_conn.connect(db_conn.CONFIG_DB)
>>> device_metadata = db_conn.get_all(db_conn.CONFIG_DB, "DEVICE_METADATA|loca")    
>>> print(device_metadata)
<swsscommon.swsscommon.FieldValueMap; proxy of <Swig Object of type 'swss::TableMap *' at 0x7f291e85a450> >

If DEVICE_METADATA exists, but key within DEVICE_METADATA table does not exist:

>>> device_metadata = db_conn.get_all(db_conn.CONFIG_DB, "DEVICE_METADATA|localhost")
>>> print(device_metadata)
<swsscommon.swsscommon.FieldValueMap; proxy of <Swig Object of type 'swss::TableMap *' at 0x7f291e85ab70> >
>>> print(device_metadata.get('foo'))
None


if device_metadata and device_metadata.get('hostname'):
self.hostname = device_metadata['hostname']

def update_data(self):
return

def get_sys_name(self):
"""
Subclass update interface information
"""
return self.hostname


class SysNameMIB(metaclass=MIBMeta, prefix='.1.3.6.1.2.1.1.5'):
"""

"""
Copy link
Contributor

@qiluo-msft qiluo-msft Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the unused lines? or add some useful comment? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed lines.

updater = sysNameUpdater()

sysName = MIBEntry('0', ValueType.OCTET_STRING, updater.get_sys_name)
4 changes: 4 additions & 0 deletions tests/mock_tables/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"admin_status": "up",
"alias": "mgmt2",
"speed": 1000
},
"DEVICE_METADATA|localhost": {
"chassis_serial_number": "SAMPLETESTSN",
"hostname" : "test_hostname"
}
}
4 changes: 4 additions & 0 deletions tests/mock_tables/global_db/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"admin_status": "up",
"alias": "mgmt2",
"speed": 1000
},
"DEVICE_METADATA|localhost": {
Copy link
Contributor

@qiluo-msft qiluo-msft Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[](start = 0, length = 1)

wrong indentation. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed indendation.

"chassis_serial_number": "SAMPLETESTSN",
"hostname" : "namespace_hostname"
}
}
44 changes: 44 additions & 0 deletions tests/namespace/test_sysname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import sys
import importlib
from unittest import TestCase

from ax_interface import ValueType
from ax_interface.pdu_implementations import GetPDU, GetNextPDU
from ax_interface.encodings import ObjectIdentifier
from ax_interface.constants import PduTypes
from ax_interface.pdu import PDU, PDUHeader
from ax_interface.mib import MIBTable
from sonic_ax_impl.mibs.ietf import rfc1213
import tests.mock_tables.dbconnector

class TestGetNextPDU(TestCase):
@classmethod
def setUpClass(cls):
tests.mock_tables.dbconnector.load_namespace_config()
importlib.reload(rfc1213)
cls.lut = MIBTable(rfc1213.SysNameMIB)
for updater in cls.lut.updater_instances:
updater.reinit_data()
updater.update_data()

def test_getpdu_sysname(self):
oid = ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))
get_pdu = GetPDU(
header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0),
oids=[oid]
)

encoded = get_pdu.encode()
response = get_pdu.make_response(self.lut)
print(response)

n = len(response.values)
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
self.assertEqual(str(value0.name), str(ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))))
self.assertEqual(str(value0.data), 'namespace_hostname')

@classmethod
def tearDownClass(cls):
tests.mock_tables.dbconnector.clean_up_config()
38 changes: 38 additions & 0 deletions tests/test_sysname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import sys
from unittest import TestCase

from unittest import TestCase

from ax_interface import ValueType
from ax_interface.pdu_implementations import GetPDU, GetNextPDU
from ax_interface.encodings import ObjectIdentifier
from ax_interface.constants import PduTypes
from ax_interface.pdu import PDU, PDUHeader
from ax_interface.mib import MIBTable
from sonic_ax_impl.mibs.ietf import rfc1213

class TestGetNextPDU(TestCase):
@classmethod
def setUpClass(cls):
cls.lut = MIBTable(rfc1213.SysNameMIB)
for updater in cls.lut.updater_instances:
updater.reinit_data()
updater.update_data()

def test_getpdu_sysname(self):
oid = ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))
get_pdu = GetPDU(
header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0),
oids=[oid]
)

encoded = get_pdu.encode()
response = get_pdu.make_response(self.lut)
print(response)

n = len(response.values)
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
self.assertEqual(str(value0.name), str(ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))))
self.assertEqual(str(value0.data), 'test_hostname')