Class: Cisco::SnmpNotificationReceiver

Inherits:
NodeUtil
  • Object
show all
Defined in:
lib/cisco_node_utils/snmp_notification_receiver.rb

Overview

SnmpNotificationReceiver - node utility class for SNMP server management

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeUtil

#client, client, #config_get, config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, node, #node, platform, #platform, supports?, #supports?

Constructor Details

#initialize(name, instantiate: true, type: '', version: '', security: '', username: '', port: '', vrf: '', source_interface: '') ⇒ SnmpNotificationReceiver

rubocop:disable CyclomaticComplexity, MethodLength, PerceivedComplexity, LineLength, AbcSize


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 24

def initialize(name, # rubocop:disable CyclomaticComplexity, MethodLength, PerceivedComplexity, LineLength, AbcSize
               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.empty? ? '' : "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?
  if platform == :nexus
    config_set('snmp_notification_receiver',
               'vrf',
               ip:   name,
               vrf:  vrf,
               port: port.empty? ? '' : "udp-port #{port}")
  else
    config_set('snmp_notification_receiver',
               'vrf',
               vrf:      vrf,
               ip:       name,
               username: username)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


22
23
24
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 22

def name
  @name
end

Class Method Details

.receiversObject


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 99

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

Instance Method Details

#==(other) ⇒ Object


114
115
116
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 114

def ==(other)
  name == other.name
end

#destroyObject


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 118

def destroy
  config_set('snmp_notification_receiver',
             'receivers',
             state:    'no',
             ip:       name,
             type:     type,
             version:  version.nil? ? '' : "version #{version}",
             security: security.nil? ? '' : "#{security}",
             username: username,
             udp_port: port.nil? ? '' : "udp-port #{port}")
end

#portObject


130
131
132
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 130

def port
  config_get('snmp_notification_receiver', 'port', @name)
end

#securityObject


152
153
154
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 152

def security
  config_get('snmp_notification_receiver', 'security', @name)
end

#source_interfaceObject


170
171
172
173
174
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 170

def source_interface
  val = config_get('snmp_notification_receiver', 'source_interface', @name)
  val = val.downcase unless val.nil?
  val
end

#typeObject


148
149
150
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 148

def type
  config_get('snmp_notification_receiver', 'type', @name)
end

#usernameObject


134
135
136
137
138
139
140
141
142
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 134

def username
  if !port.nil?
    endpoint = 'username_with_port'
  else
    endpoint = 'username'
  end

  config_get('snmp_notification_receiver', endpoint, @name)
end

#versionObject


144
145
146
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 144

def version
  config_get('snmp_notification_receiver', 'version', @name)
end

#vrfObject


156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cisco_node_utils/snmp_notification_receiver.rb', line 156

def vrf
  if platform == :nexus
    config_get('snmp_notification_receiver', 'vrf', @name)
  else
    # get all vrf
    all_vrf = config_get('snmp_notification_receiver', 'vrf_all')
    all_vrf.each do |vrf|
      vrf_list = config_get('snmp_notification_receiver', 'vrf_values', vrf: vrf) # rubocop:disable Metrics/LineLength
      # if vrf contains @name return vrf
      return vrf if vrf_list.include?(@name)
    end
  end
end