class OnosTopologyProvider

Attributes

uri_resource[RW]

Public Class Methods

new(new_uri_resource) click to toggle source
# File lib/providers/apis/onos_topology_provider.rb, line 11
def initialize(new_uri_resource)
      raise ArgumentError, 'No uri recieved as parameter' unless new_uri_resource
      @uri_resource = new_uri_resource
      @topology = Topology.new
end

Public Instance Methods

add_hosts() click to toggle source

This is the info that represents a Host

{

"id"=>"9A:4A:43:D4:36:45/None", 
"mac"=>"9A:4A:43:D4:36:45", 
"vlan"=>"None", 
"configured"=>false, 
"ipAddresses"=>["10.0.0.1"], 
"location"=>{
  "elementId"=>"of:0000000000000002", 
  "port"=>"1"
}

}

# File lib/providers/apis/onos_topology_provider.rb, line 79
def add_hosts
    hosts_response = get_from_api 'hosts'
    hosts_info = (JSON.parse hosts_response.body)['hosts']
    hosts_info.each_with_index  do |host_info, index|
      
      host = @topology.add_host host_info['id'], host_info['ipAddresses'], host_info['mac']
        
      
      @topology.add_link "Link#{index}host_to_router",
                                  host, 
                                  0,
                                  host_info['location']['elementId'],
                                  host_info['location']['port'].to_i

      @topology.add_link "Link#{index}router_to_host",
                                  host_info['location']['elementId'], 
                                  host_info['location']['port'].to_i,
                                  host,
                                  0                                      
    end        
end
add_routers() click to toggle source

This is the info that represents a router {

"id"=>"of:0000000000000003", 
"type"=>"SWITCH", 
"available"=>true, 
"role"=>"MASTER", 
"mfr"=>"Nicira, Inc.", 
"hw"=>"Open vSwitch", 
"sw"=>"2.5.0", 
"serial"=>"None", 
"chassisId"=>"3", 
"annotations"=>{
  "managementAddress"=>"127.0.0.1", 
  "protocol"=>"OF_13", 
  "channelId"=>"127.0.0.1:59170"
  }

}

# File lib/providers/apis/onos_topology_provider.rb, line 51
def add_routers
    #Devices represents either hosts or routers. This function will make difference between them
    devices_response = get_from_api 'devices'
    graph_elements_info = (JSON.parse devices_response.body)['devices']
    graph_elements_info.each do |element_info|
        # To identify if a device is either a router or a host, we can ask for it flows. If the device has no flows,
        # then it's a host
        flows_response = get_from_api "flows/#{element_info['id']}"
        @topology.add_router element_info['id'] if flows_response.code == 200
    end        
end
get_from_api(resource) click to toggle source
# File lib/providers/apis/onos_topology_provider.rb, line 28
def get_from_api(resource)
    Typhoeus.get "#{@uri_resource}#{resource}", userpwd:"onos:rocks"
end
get_path_between(source, destination) click to toggle source
# File lib/providers/apis/onos_topology_provider.rb, line 130
def get_path_between(source, destination)
    raise Exception, "Source must be either from class Router or class Host to ask for a path" unless [Host, Router].include? source.class
    raise Exception, "Destination must be either from class Router or class Host to ask for a path" unless [Host, Router].include? destination.class
    
    paths_response = get_from_api "paths/#{CGI.escape(source.id)}/#{CGI.escape(destination.id)}"
    paths_info = (JSON.parse paths_response.body)['paths']
    path = Path.new(source,destination)
    
    return path if paths_info.size == 0
    
    links_info = paths_info.first['links']

    #If either the source or the destination are hosts, the path will return host instead of device
    first_link = links_info.shift
    last_link = links_info.pop
    
    path.add_link (find_link first_link, 'host', 'device')

    links_info.each do |link|
      path.add_link (find_link link, 'device', 'device')
    end
    path.add_link (find_link last_link, 'device', 'host')

    path
end
get_topology() click to toggle source
# File lib/providers/apis/onos_topology_provider.rb, line 17
def get_topology

  return @topology.topology_elements if @topology.topology_elements.size != 0
        
  add_routers 
  add_hosts
  add_links 
  
  @topology.topology_elements
end