Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / sdnhub_apps / host_tracker_rest.py
1 # Copyright (C) 2014 SDN Hub
2 #
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
6 #
7 #    http://www.gnu.org/licenses/gpl-3.0.txt
8 #
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
12 # implied.
13
14 # REST API
15 #
16 ############# Host tracker ##############
17 #
18 # get all hosts
19 # GET /hosts
20 #
21 # get all hosts associated with a switch
22 # GET /hosts/{dpid}
23 #
24 #
25
26 import logging
27 import json
28 from webob import Response
29 import time
30
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
37
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
43 import host_tracker
44 from ryu.lib import dpid as dpid_lib
45
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']
51
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))
56
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))
61         if dp is None:
62             return Response(status=404)
63
64         switch_hosts = {}
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
68
69         return Response(status=200,content_type='application/json',
70                 body=json.dumps(switch_hosts))
71
72
73 class HostTrackerRestApi(app_manager.RyuApp):
74     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION,
75             ofproto_v1_3.OFP_VERSION]
76
77     _CONTEXTS = {
78             'dpset': dpset.DPSet,
79             'wsgi': WSGIApplication,
80             'host_tracker': host_tracker.HostTracker
81             }
82
83     def __init__(self, *args, **kwargs):
84         super(HostTrackerRestApi, self).__init__(*args, **kwargs)
85         dpset = kwargs['dpset']
86         wsgi = kwargs['wsgi']
87         host_tracker = kwargs['host_tracker']
88
89         self.data = {}
90         self.data['dpset'] = dpset
91         self.data['waiters'] = {}
92         self.data['host_tracker'] = host_tracker
93
94         wsgi.register(HostTrackerController, self.data)
95         #mapper = wsgi.mapper
96
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})