Class: Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/controller/controller.rb

Overview

Copyright © 2015, BROCADE COMMUNICATIONS SYSTEMS, INC

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this

list of conditions and the following disclaimer.

  1. Redistributions in binary form must reproduce the above copyright notice,

this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  1. Neither the name of the copyright holder nor the names of its contributors

may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip_addr: nil, port_number: 8181, admin_name: nil, admin_password: nil, timeout_in_s: 5) ⇒ Controller

Returns a new instance of Controller.

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/controller/controller.rb', line 46

def initialize(ip_addr: nil, port_number: 8181, admin_name: nil,
    admin_password: nil, timeout_in_s: 5)
  raise ArgumentError, "IP Address (ip_addr) required" unless ip_addr
  raise ArgumentError, "Admin Username (admin_name) required" unless admin_name
  raise ArgumentError, "Admin Password (admin_password) required" unless admin_password
  @ip = ip_addr
  @port = port_number
  @username = admin_name
  @password = admin_password
  @timeout = timeout_in_s
  
  @rest_agent = RestAgent.new("http://#{@ip}:#{@port}", username: @username,
    password: @password, open_timeout: @timeout)
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



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

def ip
  @ip
end

#passwordObject (readonly)

Returns the value of attribute password.



42
43
44
# File 'lib/controller/controller.rb', line 42

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



40
41
42
# File 'lib/controller/controller.rb', line 40

def port
  @port
end

#rest_agentObject (readonly)

Returns the value of attribute rest_agent.



44
45
46
# File 'lib/controller/controller.rb', line 44

def rest_agent
  @rest_agent
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



43
44
45
# File 'lib/controller/controller.rb', line 43

def timeout
  @timeout
end

#usernameObject (readonly)

Returns the value of attribute username.



41
42
43
# File 'lib/controller/controller.rb', line 41

def username
  @username
end

Instance Method Details

#add_netconf_node(node) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/controller/controller.rb', line 345

def add_netconf_node(node)
  post_uri = "/restconf/config/opendaylight-inventory:nodes/node/"\
    "controller-config/yang-ext:mount/config:modules"
  post_body = generate_node_xml(node)    
  response = @rest_agent.post_request(post_uri, post_body,
    headers: {'Content-Type' => "application/xml",
      'Accept' => "application/xml"})
  check_response_for_success(response) do
      NetconfResponse.new(NetconfResponseStatus::OK)
  end
end

#check_node_config_status(node_name) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/controller/controller.rb', line 285

def check_node_config_status(node_name)
  get_uri = "/restconf/config/opendaylight-inventory:nodes/node/#{node_name}"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do
    NetconfResponse.new(NetconfResponseStatus::NODE_CONFIGURED,
      JSON.parse(response.body))
  end
end

#check_node_conn_status(node_name) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/controller/controller.rb', line 294

def check_node_conn_status(node_name)
  get_uri = "/restconf/operational/opendaylight-inventory:nodes/node/"\
    "#{node_name}"
  response = @rest_agent.get_request(get_uri)
  if response.code.to_i == 404
    NetconfResponse.new(NetconfResponseStatus::NODE_NOT_FOUND)
  else
    check_response_for_success(response) do |body|
      connected = false
      if body.has_key?('node') && body['node'][0] && body['node'][0].has_key?('id')
        if body['node'][0].has_key?('netconf-node-inventory:connected')
          if body['node'][0]['netconf-node-inventory:connected']
            connected = true
          end
        end
      end
      if connected
        NetconfResponse.new(NetconfResponseStatus::NODE_CONNECTED)
      else
        NetconfResponse.new(NetconfResponseStatus::NODE_DISCONNECTED)
      end
    end
  end
end

#delete_netconf_node(node) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/controller/controller.rb', line 357

def delete_netconf_node(node)
  delete_uri = "/restconf/config/opendaylight-inventory:nodes/node/"\
    "controller-config/yang-ext:mount/config:modules/module/"\
    "odl-sal-netconf-connector-cfg:sal-netconf-connector/#{node.name}"
  response = @rest_agent.delete_request(delete_uri)
  # need to do the check here because there is no response body and the code
  # is a 200 instead of 204
  if response.code.to_i == 200
    NetconfResponse.new(NetconfResponseStatus::OK)
  else
    handle_error_response(response)
  end
end

#get_all_modules_operational_stateObject



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/controller/controller.rb', line 132

def get_all_modules_operational_state
  get_uri = "/restconf/operational/opendaylight-inventory:nodes/node/"\
    "controller-config/yang-ext:mount/config:modules"
  response = @rest_agent.get_request(get_uri)
  response.body.gsub!("\\\n", "")
  check_response_for_success(response) do |body|
    if body.has_key?('modules') && body['modules'].has_key?('module')
      NetconfResponse.new(NetconfResponseStatus::OK, body['modules']['module'])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_all_nodes_conn_statusObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/controller/controller.rb', line 319

def get_all_nodes_conn_status
  get_uri = "/restconf/operational/opendaylight-inventory:nodes"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('nodes') && body['nodes'].has_key?('node')
      conn_list = []
      body['nodes']['node'].each do |node|
        is_connected = false
        if node['id'].include?('openflow')
          # OpenFlow devices connect to controller (unlike NETCONF nodes),
          # so if we see one, then it is 'connected'
          is_connected = true
        elsif node.has_key?('netconf-node-inventory:connected')
          is_connected = node['netconf-node-inventory:connected']
        end
        conn_status = {:node => node['id'],
          :connected => is_connected}
        conn_list << conn_status
      end
      NetconfResponse.new(NetconfResponseStatus::OK, conn_list)
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_all_nodes_in_configObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/controller/controller.rb', line 188

def get_all_nodes_in_config
  get_uri = "/restconf/config/opendaylight-inventory:nodes"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('nodes') && body['nodes'].has_key?('node')
      devices = []
      body['nodes']['node'].each do |node|
        devices << node['id']
      end
      NetconfResponse.new(NetconfResponseStatus::OK, devices)
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_ext_mount_config_uri(node) ⇒ Object

Raises:

  • (ArgumentError)


385
386
387
388
389
# File 'lib/controller/controller.rb', line 385

def get_ext_mount_config_uri(node)
  raise ArgumentError, "Node (node) must be a 'Node' object or a 'Node' "\
    "subclass object" unless node.is_a?(Node)
  "/restconf/config/opendaylight-inventory:nodes/node/#{node.name}/yang-ext:mount"
end

#get_module_operational_state(type: nil, name: nil) ⇒ Object

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/controller/controller.rb', line 146

def get_module_operational_state(type: nil, name: nil)
  raise ArgumentError, "Type (type) required" unless type
  raise ArgumentError, "Name (name) required" unless name
  get_uri = "/restconf/operational/opendaylight-inventory:nodes/node/"\
    "controller-config/yang-ext:mount/config:modules/module/"\
    "#{type}/#{name}"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('module')
      NetconfResponse.new(NetconfResponseStatus::OK, body["module"])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_netconf_nodes_conn_statusObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/controller/controller.rb', line 220

def get_netconf_nodes_conn_status
  get_uri = "/restconf/operational/opendaylight-inventory:nodes"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('nodes') && body['nodes'].has_key?('node')
      conn_list = []
      body['nodes']['node'].each do |node|
        unless node['id'].include?('openflow')
          conn_status = {:node => node['id'],
            :connected => node['netconf-node-inventory:connected']}
          conn_list << conn_status
        end
      end
      NetconfResponse.new(NetconfResponseStatus::OK, conn_list)
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_netconf_nodes_in_configObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/controller/controller.rb', line 204

def get_netconf_nodes_in_config
  get_uri = "/restconf/config/opendaylight-inventory:nodes"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('nodes') && body['nodes'].has_key?('node')
      devices = []
      body['nodes']['node'].each do |node|
        devices << node['id'] unless node['id'].include?('openflow')
      end
      NetconfResponse.new(NetconfResponseStatus::OK, devices)
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_netconf_operations(node_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/controller/controller.rb', line 119

def get_netconf_operations(node_name)
  get_uri = "/restconf/operations/opendaylight-inventory:nodes/node/"\
    "#{node_name}/yang-ext:mount"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('operations')
      NetconfResponse.new(NetconfResponseStatus::OK, body['operations'])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_node_config_uri(node) ⇒ Object

Raises:

  • (ArgumentError)


378
379
380
381
382
383
# File 'lib/controller/controller.rb', line 378

def get_node_config_uri(node)
  raise ArgumentError, "Node (node) must be a 'Node' object or a 'Node' "\
    "subclass object" unless node.is_a?(Node) ||
    (node.methods.include?(:ancestors) && node.ancestors.include?(Node))
  "/restconf/config/opendaylight-inventory:nodes/node/#{node.name}"
end

#get_node_info(node_name) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/controller/controller.rb', line 272

def get_node_info(node_name)
  get_uri = "/restconf/operational/opendaylight-inventory:nodes/node/"\
    "#{node_name}"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('node')
      NetconfResponse.new(NetconfResponseStatus::OK, body['node'])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_node_operational_uri(node) ⇒ Object

Raises:

  • (ArgumentError)


371
372
373
374
375
376
# File 'lib/controller/controller.rb', line 371

def get_node_operational_uri(node)
  raise ArgumentError, "Node (node) must be a 'Node' object or a 'Node' "\
    "subclass object" unless node.is_a?(Node) ||
    (node.methods.include?(:ancestors) && node.ancestors.include?(Node))
  "/restconf/operational/opendaylight-inventory:nodes/node/#{node.name}"
end

#get_nodes_operational_listObject



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/controller/controller.rb', line 240

def get_nodes_operational_list
  get_uri = "/restconf/operational/opendaylight-inventory:nodes"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('nodes') && body['nodes'].has_key?('node')
      list = []
      body['nodes']['node'].each do |node|
        list << node['id'] if node['id']
      end
      NetconfResponse.new(NetconfResponseStatus::OK, list)
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_openflow_nodes_operational_listObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/controller/controller.rb', line 256

def get_openflow_nodes_operational_list
  get_uri = "/restconf/operational/opendaylight-inventory:nodes"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('nodes') && body['nodes'].has_key?('node')
      filtered_list = []
      body['nodes']['node'].each do |node|
        filtered_list << node['id'] if node['id'].start_with?('openflow')
      end
      NetconfResponse.new(NetconfResponseStatus::OK, filtered_list)
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_schema(node_name, id: nil, version: nil) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/controller/controller.rb', line 74

def get_schema(node_name, id: nil, version: nil)
  raise ArgumentError, "Identifier (id) required" unless id
  raise ArgumentError, "Version (version) required" unless version
  post_uri = "/restconf/operations/opendaylight-inventory:nodes/node/"\
    "#{node_name}/yang-ext:mount/ietf-netconf-monitoring:get-schema"
  post_body = {:input => {:identifier => id, :version => version,
      :format => 'yang'}}
  response = @rest_agent.post_request(post_uri, post_body)
  check_response_for_success(response) do |body|
    if body.has_key?('get-schema') && body['get-schema'].has_key?('output') &&
        body['get-schema']['output'].has_key?('data')
      NetconfResponse.new(NetconfResponseStatus::OK,
        body['get-schema']['output']['data'])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_schemas(node_name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/controller/controller.rb', line 61

def get_schemas(node_name)
  get_uri = "/restconf/operational/opendaylight-inventory:nodes/node/"\
    "#{node_name}/yang-ext:mount/ietf-netconf-monitoring:netconf-state/schemas"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('schemas') && body['schemas'].has_key?('schema')
      NetconfResponse.new(NetconfResponseStatus::OK, body['schemas']['schema'])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_service_provider_info(provider_name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/controller/controller.rb', line 106

def get_service_provider_info(provider_name)
  get_uri = "/restconf/config/opendaylight-inventory:nodes/node/"\
    "controller-config/yang-ext:mount/config:services/service/#{provider_name}"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('service')
      NetconfResponse.new(NetconfResponseStatus::OK, body['service'])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_service_providers_infoObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/controller/controller.rb', line 93

def get_service_providers_info
  get_uri = "/restconf/config/opendaylight-inventory:nodes/node/"\
    "controller-config/yang-ext:mount/config:services"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('services') && body['services'].has_key?('service')
      NetconfResponse.new(NetconfResponseStatus::OK, body['services']['service'])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_sessions_info(node_name) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/controller/controller.rb', line 162

def get_sessions_info(node_name)
  get_uri = "/restconf/operational/opendaylight-inventory:nodes/node/"\
    "#{node_name}/yang-ext:mount/ietf-netconf-monitoring:netconf-state/"\
    "sessions"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('sessions')
      NetconfResponse.new(NetconfResponseStatus::OK, body["sessions"])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#get_streams_infoObject



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/controller/controller.rb', line 176

def get_streams_info
  get_uri = "restconf/streams"
  response = @rest_agent.get_request(get_uri)
  check_response_for_success(response) do |body|
    if body.has_key?('streams')
      NetconfResponse.new(NetconfResponseStatus::OK, body['streams'])
    else
      NetconfResponse.new(NetconfResponseStatus::DATA_NOT_FOUND)
    end
  end
end

#to_hashObject



391
392
393
394
# File 'lib/controller/controller.rb', line 391

def to_hash
  {:ip_addr => @ip, :port_num => @port, :admin_name => @username,
    :admin_password => @password}
end