Class: Cisco::SpanSession

Inherits:
NodeUtil show all
Defined in:
lib/cisco_node_utils/span_session.rb

Overview

node utils class for SPAN (switchport analyzer) sessions

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(session_id, instantiate = true) ⇒ SpanSession

Returns a new instance of SpanSession


10
11
12
13
# File 'lib/cisco_node_utils/span_session.rb', line 10

def initialize(session_id, instantiate=true)
  validate_args(session_id.to_i)
  create if instantiate
end

Instance Attribute Details

#session_idObject

Returns the value of attribute session_id


8
9
10
# File 'lib/cisco_node_utils/span_session.rb', line 8

def session_id
  @session_id
end

Class Method Details

.sessionsObject


15
16
17
18
19
20
21
22
23
24
# File 'lib/cisco_node_utils/span_session.rb', line 15

def self.sessions
  hash = {}
  all = config_get('span_session', 'all_sessions')
  return hash if all.nil?

  all.each do |id|
    hash[id] = SpanSession.new(id, false)
  end
  hash
end

Instance Method Details

#createObject


31
32
33
# File 'lib/cisco_node_utils/span_session.rb', line 31

def create
  config_set('span_session', 'create', id: @session_id)
end

#default_source_interfacesObject


104
105
106
# File 'lib/cisco_node_utils/span_session.rb', line 104

def default_source_interfaces
  config_get_default('span_session', 'source_interfaces')
end

#default_source_vlansObject


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

def default_source_vlans
  config_get_default('span_session', 'source_vlans')
end

#descriptionObject


39
40
41
# File 'lib/cisco_node_utils/span_session.rb', line 39

def description
  config_get('span_session', 'description', id: @session_id)
end

#description=(val) ⇒ Object


43
44
45
46
47
48
49
50
51
52
# File 'lib/cisco_node_utils/span_session.rb', line 43

def description=(val)
  val = val.to_s
  if val.empty?
    config_set('span_session', 'description', id: @session_id,
                state: 'no', description: '')
  else
    config_set('span_session', 'description', id: @session_id,
                state: '', description: val)
  end
end

#destinationObject


54
55
56
# File 'lib/cisco_node_utils/span_session.rb', line 54

def destination
  config_get('span_session', 'destination', id: @session_id).downcase
end

#destination=(int) ⇒ Object


58
59
60
61
62
63
# File 'lib/cisco_node_utils/span_session.rb', line 58

def destination=(int)
  # fail if int is not a valid interface
  fail TypeError unless Interface.interfaces.key?(int.downcase)
  config_set('span_session', 'destination', state: '', id: @session_id,
             intf_name: int)
end

#destroyObject


35
36
37
# File 'lib/cisco_node_utils/span_session.rb', line 35

def destroy
  config_set('span_session', 'destroy', id: @session_id)
end

#shutdownObject


74
75
76
# File 'lib/cisco_node_utils/span_session.rb', line 74

def shutdown
  config_get('span_session', 'shutdown', id: @session_id)
end

#shutdown=(bool) ⇒ Object


78
79
80
81
# File 'lib/cisco_node_utils/span_session.rb', line 78

def shutdown=(bool)
  fail TypeError unless bool.is_a?(Boolean)
  config_set('span_session', 'shutdown', id: @session_id, shutdown: bool)
end

#source_interfacesObject


83
84
85
86
87
88
# File 'lib/cisco_node_utils/span_session.rb', line 83

def source_interfaces
  ints = config_get('span_session', 'source_interfaces', id: @session_id)
  intf = []
  ints.each { |i| intf << i.map(&:downcase) }
  intf
end

#source_interfaces=(sources) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cisco_node_utils/span_session.rb', line 90

def source_interfaces=(sources)
  fail TypeError unless sources.is_a?(Hash)
  delta_hash = Utils.delta_add_remove(sources.to_a, source_interfaces.to_a,
                                      :updates_not_allowed)
  return if delta_hash.values.flatten.empty?
  [:remove, :add].each do |action|
    delta_hash[action].each do |name, dir|
      state = (action == :add) ? '' : 'no'
      config_set('span_session', 'source_interfaces', id: @session_id,
                 state: state, int_name: name, direction: dir)
    end
  end
end

#source_vlansObject


108
109
110
111
# File 'lib/cisco_node_utils/span_session.rb', line 108

def source_vlans
  v = config_get('span_session', 'source_vlans', id: @session_id)
  v.empty? ? v : [Utils.normalize_range_array(v[0]), v[1]]
end

#source_vlans=(sources) ⇒ Object


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cisco_node_utils/span_session.rb', line 113

def source_vlans=(sources)
  fail TypeError unless sources.is_a?(Hash)
  is = Utils.dash_range_to_elements(source_vlans[0]) unless
    source_vlans.empty?
  should = Utils.dash_range_to_elements(sources[:vlans])
  direction = sources[:direction]
  delta_hash = Utils.delta_add_remove(should, is)
  [:add, :remove].each do |action|
    delta_hash[action].each do |vlans|
      state = (action == :add) ? '' : 'no'
      config_set('span_session', 'source_vlans',
                 id: @session_id, state: state,
                 vlans: vlans, direction: direction)
    end
  end
end

#typeObject


134
135
136
# File 'lib/cisco_node_utils/span_session.rb', line 134

def type
  config_get('span_session', 'type', id: @session_id)
end

#type=(str) ⇒ Object


138
139
140
141
142
143
144
145
146
147
# File 'lib/cisco_node_utils/span_session.rb', line 138

def type=(str)
  valid_types = ['local', 'rspan', 'erspan-source']
  fail TypeError unless valid_types.include?(str)
  destroy # need to destroy session before changing type
  if str.empty?
    config_set('span_session', 'type', id: @session_id, type: 'local')
  else
    config_set('span_session', 'type', id: @session_id, type: str)
  end
end

#validate_args(session_id) ⇒ Object


26
27
28
29
# File 'lib/cisco_node_utils/span_session.rb', line 26

def validate_args(session_id)
  fail TypeError unless session_id.is_a?(Integer)
  @session_id = session_id
end