Skip to content

Commit

Permalink
[sysName]: Implement sysName OID (sonic-net#185)
Browse files Browse the repository at this point in the history
[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.
  • Loading branch information
SuvarnaMeenakshi authored Dec 30, 2020
1 parent 8efb4bb commit 4aad821
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
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
29 changes: 29 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 @@ -673,3 +674,31 @@ 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")

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'):
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": {
"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')

0 comments on commit 4aad821

Please sign in to comment.