Skip to content

Commit 4aad821

Browse files
[sysName]: Implement sysName OID (#185)
[sysName]: Add sysName OID implementation in snmpagent. sysName OID is currently supported by snmpd. Override this implementation to make sure the latest hostname is taken from config db. Add a new MIB class to support retrieving sysName from config_db Add unit-test to test sysName OID.
1 parent 8efb4bb commit 4aad821

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

src/sonic_ax_impl/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
class SonicMIB(
2525
rfc1213.InterfacesMIB,
2626
rfc1213.IpMib,
27+
rfc1213.SysNameMIB,
2728
rfc2737.PhysicalTableMIB,
2829
rfc3433.PhysicalSensorTableMIB,
2930
rfc2863.InterfaceMIBObjects,

src/sonic_ax_impl/mibs/ietf/rfc1213.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ipaddress
22
import python_arptable
3+
import socket
34
from enum import unique, Enum
45
from bisect import bisect_right
56

@@ -673,3 +674,31 @@ class InterfacesMIB(metaclass=MIBMeta, prefix='.1.3.6.1.2.1.2'):
673674
# FIXME Placeholder
674675
ifSpecific = \
675676
SubtreeMIBEntry('2.1.22', if_updater, ValueType.OBJECT_IDENTIFIER, lambda sub_id: ObjectIdentifier.null_oid())
677+
678+
class sysNameUpdater(MIBUpdater):
679+
def __init__(self):
680+
super().__init__()
681+
self.db_conn = mibs.init_db()
682+
self.hostname = socket.gethostname()
683+
684+
def reinit_data(self):
685+
self.db_conn.connect(self.db_conn.CONFIG_DB)
686+
device_metadata = self.db_conn.get_all(self.db_conn.CONFIG_DB, "DEVICE_METADATA|localhost")
687+
688+
if device_metadata and device_metadata.get('hostname'):
689+
self.hostname = device_metadata['hostname']
690+
691+
def update_data(self):
692+
return
693+
694+
def get_sys_name(self):
695+
"""
696+
Subclass update interface information
697+
"""
698+
return self.hostname
699+
700+
701+
class SysNameMIB(metaclass=MIBMeta, prefix='.1.3.6.1.2.1.1.5'):
702+
updater = sysNameUpdater()
703+
704+
sysName = MIBEntry('0', ValueType.OCTET_STRING, updater.get_sys_name)

tests/mock_tables/config_db.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
"admin_status": "up",
1515
"alias": "mgmt2",
1616
"speed": 1000
17+
},
18+
"DEVICE_METADATA|localhost": {
19+
"chassis_serial_number": "SAMPLETESTSN",
20+
"hostname" : "test_hostname"
1721
}
1822
}

tests/mock_tables/global_db/config_db.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
"admin_status": "up",
1515
"alias": "mgmt2",
1616
"speed": 1000
17+
},
18+
"DEVICE_METADATA|localhost": {
19+
"chassis_serial_number": "SAMPLETESTSN",
20+
"hostname" : "namespace_hostname"
1721
}
1822
}

tests/namespace/test_sysname.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import sys
3+
import importlib
4+
from unittest import TestCase
5+
6+
from ax_interface import ValueType
7+
from ax_interface.pdu_implementations import GetPDU, GetNextPDU
8+
from ax_interface.encodings import ObjectIdentifier
9+
from ax_interface.constants import PduTypes
10+
from ax_interface.pdu import PDU, PDUHeader
11+
from ax_interface.mib import MIBTable
12+
from sonic_ax_impl.mibs.ietf import rfc1213
13+
import tests.mock_tables.dbconnector
14+
15+
class TestGetNextPDU(TestCase):
16+
@classmethod
17+
def setUpClass(cls):
18+
tests.mock_tables.dbconnector.load_namespace_config()
19+
importlib.reload(rfc1213)
20+
cls.lut = MIBTable(rfc1213.SysNameMIB)
21+
for updater in cls.lut.updater_instances:
22+
updater.reinit_data()
23+
updater.update_data()
24+
25+
def test_getpdu_sysname(self):
26+
oid = ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))
27+
get_pdu = GetPDU(
28+
header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0),
29+
oids=[oid]
30+
)
31+
32+
encoded = get_pdu.encode()
33+
response = get_pdu.make_response(self.lut)
34+
print(response)
35+
36+
n = len(response.values)
37+
value0 = response.values[0]
38+
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
39+
self.assertEqual(str(value0.name), str(ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))))
40+
self.assertEqual(str(value0.data), 'namespace_hostname')
41+
42+
@classmethod
43+
def tearDownClass(cls):
44+
tests.mock_tables.dbconnector.clean_up_config()

tests/test_sysname.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import sys
3+
from unittest import TestCase
4+
5+
from unittest import TestCase
6+
7+
from ax_interface import ValueType
8+
from ax_interface.pdu_implementations import GetPDU, GetNextPDU
9+
from ax_interface.encodings import ObjectIdentifier
10+
from ax_interface.constants import PduTypes
11+
from ax_interface.pdu import PDU, PDUHeader
12+
from ax_interface.mib import MIBTable
13+
from sonic_ax_impl.mibs.ietf import rfc1213
14+
15+
class TestGetNextPDU(TestCase):
16+
@classmethod
17+
def setUpClass(cls):
18+
cls.lut = MIBTable(rfc1213.SysNameMIB)
19+
for updater in cls.lut.updater_instances:
20+
updater.reinit_data()
21+
updater.update_data()
22+
23+
def test_getpdu_sysname(self):
24+
oid = ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))
25+
get_pdu = GetPDU(
26+
header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0),
27+
oids=[oid]
28+
)
29+
30+
encoded = get_pdu.encode()
31+
response = get_pdu.make_response(self.lut)
32+
print(response)
33+
34+
n = len(response.values)
35+
value0 = response.values[0]
36+
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
37+
self.assertEqual(str(value0.name), str(ObjectIdentifier(9, 0, 0, 0, (1, 3, 6, 1, 2, 1, 1, 5, 0))))
38+
self.assertEqual(str(value0.data), 'test_hostname')

0 commit comments

Comments
 (0)