1 # Copyright (C) 2014 SDN Hub
3 # Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.
4 # You may not use this file except in compliance with this License.
5 # You may obtain a copy of the License at
7 # http://www.gnu.org/licenses/gpl-3.0.txt
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16 ############# Host tracker ##############
21 # get all hosts associated with a switch
28 from webob import Response
31 from ryu.base import app_manager
32 from ryu.controller import ofp_event
33 from ryu.controller.handler import MAIN_DISPATCHER
34 from ryu.controller.handler import set_ev_cls
35 from ryu.controller import dpset
36 from ryu.app.wsgi import ControllerBase, WSGIApplication, route
38 from ryu.lib.packet import packet
39 from ryu.lib.packet import ethernet
40 from ryu.lib.packet import ipv4
41 from ryu.ofproto import ether
42 from ryu.ofproto import ofproto_v1_0, ofproto_v1_3
44 from ryu.lib import dpid as dpid_lib
46 class HostTrackerController(ControllerBase):
47 def __init__(self, req, link, data, **config):
48 super(HostTrackerController, self).__init__(req, link, data, **config)
49 self.host_tracker = data['host_tracker']
50 self.dpset = data['dpset']
52 @route('hosts', '/v1.0/hosts', methods=['GET'])
53 def get_all_hosts(self, req, **kwargs):
54 return Response(status=200,content_type='application/json',
55 body=json.dumps(self.host_tracker.hosts))
57 @route('hosts', '/v1.0/hosts/{dpid}', methods=['GET'])
58 #requirements={'dpid': dpid_lib.DPID_PATTERN})
59 def get_hosts(self, req, dpid, **_kwargs):
60 dp = self.dpset.get(int(dpid))
62 return Response(status=404)
65 for key,val in self.host_tracker.hosts.iteritems():
66 if val['dpid']== dpid_lib.dpid_to_str(dp.id):
67 switch_hosts[key] = val
69 return Response(status=200,content_type='application/json',
70 body=json.dumps(switch_hosts))
73 class HostTrackerRestApi(app_manager.RyuApp):
74 OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION,
75 ofproto_v1_3.OFP_VERSION]
79 'wsgi': WSGIApplication,
80 'host_tracker': host_tracker.HostTracker
83 def __init__(self, *args, **kwargs):
84 super(HostTrackerRestApi, self).__init__(*args, **kwargs)
85 dpset = kwargs['dpset']
87 host_tracker = kwargs['host_tracker']
90 self.data['dpset'] = dpset
91 self.data['waiters'] = {}
92 self.data['host_tracker'] = host_tracker
94 wsgi.register(HostTrackerController, self.data)
97 #mapper.connect('hosts', '/v1.0/hosts', controller=HostTrackerController, action='get_all_hosts',
98 # conditions=dict(method=['GET']))
99 #mapper.connect('hosts', '/v1.0/hosts/{dpid}', controller=HostTrackerController, action='get_hosts',
100 # conditions=dict(method=['GET']), requirements={'dpid': dpid_lib.DPID_PATTERN})