Skip to content

Commit

Permalink
Merge pull request #271 from sushanthakumar/custom-mibpath
Browse files Browse the repository at this point in the history
Custom mib path enhancement
  • Loading branch information
NajmudheenCT authored Aug 11, 2020
2 parents 1d10be1 + 7d2d405 commit 7856a95
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
23 changes: 14 additions & 9 deletions delfin/alert_manager/trap_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import six

Expand All @@ -34,10 +35,8 @@

LOG = log.getLogger(__name__)

# Currently static mib file list is loaded
# Mechanism to be changed to load all mib file
MIB_LOAD_LIST = ['SNMPv2-MIB', 'IF_MIB', 'EMCGATEWAY-MIB', 'FCMGMT-MIB',
'ISM-HUAWEI-MIB']
# Mib file format to be loaded
MIB_LOAD_FILE_FORMAT = '.py'


class TrapReceiver(manager.Manager):
Expand Down Expand Up @@ -152,11 +151,17 @@ def _mib_builder(self):
try:
self.mib_view_controller = view.MibViewController(mib_builder)

# set mib path to mib_builder object and load mibs
mib_path = builder.DirMibSource(self.snmp_mib_path),
mib_builder.setMibSources(*mib_path)
if len(MIB_LOAD_LIST) > 0:
mib_builder.loadModules(*MIB_LOAD_LIST)
# Append custom mib path to default path for loading mibs
mib_sources = mib_builder.getMibSources() + (builder.DirMibSource(
self.snmp_mib_path),)
mib_builder.setMibSources(*mib_sources)
files = []
for file in os.listdir(self.snmp_mib_path):
# Pick up all .py files, remove extenstion and load them
if file.endswith(MIB_LOAD_FILE_FORMAT):
files.append(os.path.splitext(file)[0])
if len(files) > 0:
mib_builder.loadModules(*files)
except Exception:
raise ValueError("Mib load failed.")

Expand Down
21 changes: 21 additions & 0 deletions delfin/tests/unit/alert_manager/test_trap_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def _get_trap_receiver(self):
@mock.patch('delfin.db.api.alert_source_get_all')
@mock.patch('pysnmp.carrier.asyncore.dgram.udp.UdpTransport'
'.openServerMode', mock.Mock())
@mock.patch('delfin.alert_manager.trap_receiver.TrapReceiver'
'._mib_builder', mock.Mock())
def test_start_success(self, mock_alert_source, mock_dispatcher):
mock_alert_source.return_value = {}
trap_receiver_inst = self._get_trap_receiver()
Expand All @@ -54,6 +56,23 @@ def test_start_success(self, mock_alert_source, mock_dispatcher):
# Verify that snmp engine is initialised and transport config is set
self.assertTrue(trap_receiver_inst.snmp_engine is not None)

@mock.patch('pysnmp.carrier.asyncore.dispatch.AbstractTransportDispatcher'
'.jobStarted')
@mock.patch('delfin.db.api.alert_source_get_all')
@mock.patch('pysnmp.carrier.asyncore.dgram.udp.UdpTransport'
'.openServerMode', mock.Mock())
@mock.patch('pysnmp.smi.builder.MibBuilder.getMibSources', mock.Mock())
def test_start_mib_lod_failure(self, mock_alert_source, mock_dispatcher):
mock_alert_source.return_value = {}
trap_receiver_inst = self._get_trap_receiver()
trap_receiver_inst.trap_receiver_address = self.DEF_TRAP_RECV_ADDR
trap_receiver_inst.trap_receiver_port = self.DEF_TRAP_RECV_PORT
trap_receiver_inst.snmp_mib_path = self.SNMP_MIB_PATH

self.assertRaisesRegex(ValueError,
"Failed to setup for trap listener.",
trap_receiver_inst.start)

@mock.patch('pysnmp.carrier.asyncore.dispatch.AbstractTransportDispatcher'
'.jobStarted')
@mock.patch('delfin.db.api.alert_source_get_all')
Expand Down Expand Up @@ -96,6 +115,8 @@ def test_add_transport_exception(self):
@mock.patch('pysnmp.carrier.asyncore.dgram.udp.UdpTransport'
'.openServerMode', mock.Mock())
@mock.patch('pysnmp.entity.config.addTransport', fakes.mock_add_transport)
@mock.patch('delfin.alert_manager.trap_receiver.TrapReceiver'
'._mib_builder', mock.Mock())
def test_stop_with_snmp_engine(self, mock_alert_source,
mock_close_dispatcher, mock_dispatcher):
mock_alert_source.return_value = {}
Expand Down

0 comments on commit 7856a95

Please sign in to comment.