From fae258bd1ceee83e32d7bc53a1659c56e3013cf4 Mon Sep 17 00:00:00 2001 From: Jonathan Tripathy Date: Wed, 9 Dec 2015 14:03:56 +0000 Subject: [PATCH] Added SNMP Notification Receiver --- CHANGELOG.md | 2 + lib/.rubocop.yml | 8 +- .../cmd_ref/snmp_notification_receiver.yaml | 50 ++++++ .../snmp_notification_receiver.rb | 158 ++++++++++++++++++ tests/test_snmp_notification_receiver.rb | 137 +++++++++++++++ 5 files changed, 351 insertions(+), 4 deletions(-) create mode 100644 lib/cisco_node_utils/cmd_ref/snmp_notification_receiver.yaml create mode 100644 lib/cisco_node_utils/snmp_notification_receiver.rb create mode 100644 tests/test_snmp_notification_receiver.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index b1a35039..79dbecf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,8 @@ Changelog * RADIUS * radius_global (@jonnytpuppet) * radius_server (@jonnytpuppet) +* SNMP + * snmp_notification_receiver (@jonnytpuppet) * SYSLOG * syslog_server (@jonnytpuppet) * syslog_setting (@jonnytpuppet) diff --git a/lib/.rubocop.yml b/lib/.rubocop.yml index f2d6be3f..30cfd013 100644 --- a/lib/.rubocop.yml +++ b/lib/.rubocop.yml @@ -3,16 +3,16 @@ inherit_from: ../.rubocop.yml # Baseline code complexity metrics for the lib/ subdirectory: Metrics/AbcSize: - Max: 40 + Max: 45 Metrics/CyclomaticComplexity: - Max: 17 + Max: 23 Metrics/MethodLength: - Max: 45 + Max: 48 Metrics/ParameterLists: Max: 9 Metrics/PerceivedComplexity: - Max: 19 + Max: 24 diff --git a/lib/cisco_node_utils/cmd_ref/snmp_notification_receiver.yaml b/lib/cisco_node_utils/cmd_ref/snmp_notification_receiver.yaml new file mode 100644 index 00000000..1e3e2777 --- /dev/null +++ b/lib/cisco_node_utils/cmd_ref/snmp_notification_receiver.yaml @@ -0,0 +1,50 @@ +# snmp_notification_receiver +--- +port: + config_get: "show running-config snmp all" + config_get_token: '/^snmp-server host %s.*version.* udp-port (\d+).*$/' + default_value: null + +receivers: + config_get: "show running snmp all" + config_get_token: '/^snmp-server host (\S+) (traps|informs)/' + config_set: " snmp-server host version " + multiple: + +security: + config_get: "show running-config snmp all" + config_get_token: '/^snmp-server host %s.* version 3 (auth|noauth|priv).*$/' + default_value: null + +source_interface: + config_get: "show running-config snmp all" + config_get_token: '/^snmp-server host %s.* source-interface (\S+).*$/' + config_set: "snmp-server host source-interface " + default_value: null + +type: + config_get: "show running-config snmp all" + config_get_token: '/^snmp-server host %s (traps|informs).*$/' + default_value: null + +username: + config_get: "show running-config snmp all" + config_get_token: '/^snmp-server host %s.*version.* (\S+)$/' + default_value: null + +username_with_port: + config_get: "show running-config snmp all" + config_get_token: '/^snmp-server host %s.*version.* (\S+) udp-port/' + default_value: null + +version: + config_get: "show running-config snmp all" + config_get_token: '/^snmp-server host %s.* version (\S+).*$/' + default_value: null + +vrf: + config_get: "show running-config snmp all" + config_get_token: '/^snmp-server host %s.* use-vrf (\S+).*$/' + config_set: "snmp-server host use-vrf " + default_value: null + diff --git a/lib/cisco_node_utils/snmp_notification_receiver.rb b/lib/cisco_node_utils/snmp_notification_receiver.rb new file mode 100644 index 00000000..40abd422 --- /dev/null +++ b/lib/cisco_node_utils/snmp_notification_receiver.rb @@ -0,0 +1,158 @@ +# November 2015, Jonathan Tripathy +# +# Copyright (c) 2014-2015 Cisco and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require_relative 'node_util' + +module Cisco + # SnmpNotificationReceiver - node utility class for SNMP server management + class SnmpNotificationReceiver < NodeUtil + attr_reader :name + + def initialize(name, + instantiate: true, + type: '', + version: '', + security: '', + username: '', + port: '', + vrf: '', + source_interface: '') + + fail TypeError unless name.is_a?(String) + @name = name + + fail TypeError unless type.is_a?(String) + + fail TypeError unless version.is_a?(String) || version.is_a?(Integer) + + fail TypeError unless security.is_a?(String) + + fail TypeError unless username.is_a?(String) + + fail TypeError unless port.is_a?(String) || port.is_a?(Integer) + + fail TypeError unless vrf.is_a?(String) + + fail TypeError unless source_interface.is_a?(String) + + return unless instantiate + + # Mandatory Properties + fail TypeError unless name.length > 0 + fail TypeError unless type.length > 0 + + if version.is_a?(Integer) + fail TypeError if version <= 0 + else + fail TypeError if version.length <= 0 + end + + fail TypeError unless username.length > 0 + + config_set('snmp_notification_receiver', + 'receivers', + state: '', + ip: name, + type: type, + version: version, + security: security, + username: username, + udp_port: port.empty? ? '' : "udp-port #{port}") + + unless source_interface.empty? + config_set('snmp_notification_receiver', + 'source_interface', + ip: name, + source_interface: source_interface, + port: port.empty? ? '' : "udp-port #{port}") + end + + return if vrf.empty? + config_set('snmp_notification_receiver', + 'vrf', + ip: name, + vrf: vrf, + port: port.empty? ? '' : "udp-port #{port}") + end + + def self.receivers + hash = {} + + receivers_list = config_get('snmp_notification_receiver', 'receivers') + return hash if receivers_list.nil? + + receivers_list.each do |arr| + next if !arr.is_a?(Array) || arr.empty? + id = arr[0] + hash[id] = SnmpNotificationReceiver.new(id, instantiate: false) + end + + hash + end + + def ==(other) + name == other.name + end + + def destroy + config_set('snmp_notification_receiver', + 'receivers', + state: 'no', + ip: name, + type: type, + version: version, + security: security.nil? ? '' : "#{security}", + username: username, + udp_port: port.nil? ? '' : "udp-port #{port}") + end + + def port + config_get('snmp_notification_receiver', 'port', @name) + end + + def username + if !port.nil? + endpoint = 'username_with_port' + else + endpoint = 'username' + end + + config_get('snmp_notification_receiver', endpoint, @name) + end + + def version + config_get('snmp_notification_receiver', 'version', @name) + end + + def type + config_get('snmp_notification_receiver', 'type', @name) + end + + def security + config_get('snmp_notification_receiver', 'security', @name) + end + + def vrf + config_get('snmp_notification_receiver', 'vrf', @name) + end + + def source_interface + val = config_get('snmp_notification_receiver', 'source_interface', @name) + val = val.downcase unless val.nil? + val + end + end +end diff --git a/tests/test_snmp_notification_receiver.rb b/tests/test_snmp_notification_receiver.rb new file mode 100644 index 00000000..388480a3 --- /dev/null +++ b/tests/test_snmp_notification_receiver.rb @@ -0,0 +1,137 @@ +# +# Minitest for SnmpNotificationReceiver class +# +# Copyright (c) 2014-2015 Cisco and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require_relative 'ciscotest' +require_relative '../lib/cisco_node_utils/snmp_notification_receiver' + +# TestSnmpNotificationReceiver - Minitest for SnmpNotificationReceiver +# node utility. +class TestSnmpNotificationReceiver < CiscoTestCase + def setup + # setup runs at the beginning of each test + super + no_snmpnotificationreceiver + config('vrf context red') + end + + def teardown + # teardown runs at the end of each test + no_snmpnotificationreceiver + config('no vrf context red') + super + end + + def no_snmpnotificationreceiver + # Turn the feature off for a clean test. + config('no snmp-server host 4.5.6.7 informs version 3 priv ab udp-port 45', + 'no snmp-server host 8.9.10.11 traps version 3 auth cd udp-port 46') + end + + # TESTS + + def test_create_destroy_single + id = '4.5.6.7' + refute_includes(Cisco::SnmpNotificationReceiver.receivers, id) + + receiver = \ + Cisco::SnmpNotificationReceiver.new(id, + instantiate: true, + type: 'informs', + version: '3', + security: 'priv', + username: 'ab', + port: '45', + vrf: 'red', + source_interface: 'ethernet1/3') + + assert_includes(Cisco::SnmpNotificationReceiver.receivers, id) + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id], receiver) + + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].source_interface, + 'ethernet1/3') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].port, '45') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].type, 'informs') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].username, 'ab') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].version, '3') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].vrf, 'red') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].security, 'priv') + + receiver.destroy + refute_includes(Cisco::SnmpNotificationReceiver.receivers, id) + end + + def test_create_destroy_multiple + id = '4.5.6.7' + id2 = '8.9.10.11' + + refute_includes(Cisco::SnmpNotificationReceiver.receivers, id) + refute_includes(Cisco::SnmpNotificationReceiver.receivers, id2) + + receiver = \ + Cisco::SnmpNotificationReceiver.new(id, + instantiate: true, + type: 'informs', + version: '3', + security: 'priv', + username: 'ab', + port: '45', + vrf: 'red', + source_interface: 'ethernet1/3') + + receiver2 = \ + Cisco::SnmpNotificationReceiver.new(id2, + instantiate: true, + type: 'traps', + version: '3', + security: 'auth', + username: 'cd', + port: '46', + vrf: 'red', + source_interface: 'ethernet1/4') + + assert_includes(Cisco::SnmpNotificationReceiver.receivers, id) + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id], receiver) + + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].source_interface, + 'ethernet1/3') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].port, '45') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].type, 'informs') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].username, 'ab') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].version, '3') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].vrf, 'red') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].security, 'priv') + + assert_includes(Cisco::SnmpNotificationReceiver.receivers, id2) + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2], receiver2) + + assert_equal( + Cisco::SnmpNotificationReceiver.receivers[id2].source_interface, + 'ethernet1/4') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].port, '46') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].type, 'traps') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].username, 'cd') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].version, '3') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].vrf, 'red') + assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].security, + 'auth') + + receiver.destroy + receiver2.destroy + refute_includes(Cisco::SnmpNotificationReceiver.receivers, id) + refute_includes(Cisco::SnmpNotificationReceiver.receivers, id2) + end +end