backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / simple_switch_websocket_13.py
1 # Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
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 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 Usage example
18
19 Run this application:
20 $ PYTHONPATH=. ./bin/ryu run --verbose ryu.app.simple_switch_websocket_13
21
22 Install and run websocket client(in other terminal):
23 $ pip install websocket-client
24 $ wsdump.py ws://127.0.0.1:8080/simpleswitch/ws
25 < "ethernet(dst='ff:ff:ff:ff:ff:ff',ethertype=2054,src='32:1a:51:fb:91:77'), a
26 rp(dst_ip='10.0.0.2',dst_mac='00:00:00:00:00:00',hlen=6,hwtype=1,opcode=1,plen
27 =4,proto=2048,src_ip='10.0.0.1',src_mac='32:1a:51:fb:91:77')"
28 < "ethernet(dst='32:1a:51:fb:91:77',ethertype=2054,src='26:8c:15:0c:de:49'), a
29 rp(dst_ip='10.0.0.1',dst_mac='32:1a:51:fb:91:77',hlen=6,hwtype=1,opcode=2,plen
30 =4,proto=2048,src_ip='10.0.0.2',src_mac='26:8c:15:0c:de:49')"
31 < "ethernet(dst='26:8c:15:0c:de:49',ethertype=2048,src='32:1a:51:fb:91:77'), i
32 pv4(csum=9895,dst='10.0.0.2',flags=2,header_length=5,identification=0,offset=0
33 ,option=None,proto=1,src='10.0.0.1',tos=0,total_length=84,ttl=64,version=4), i
34 cmp(code=0,csum=43748,data=echo(data='`\\xb9uS\\x00\\x00\\x00\\x00\\x7f\\'\\x0
35 1\\x00\\x00\\x00\\x00\\x00\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\
36 x1a\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./01234567',id=14355,seq=1),type=
37 8)"
38
39 Get arp table:
40 > {"jsonrpc": "2.0", "id": 1, "method": "get_arp_table", "params" : {}}
41 < {"jsonrpc": "2.0", "id": 1, "result": {"1": {"32:1a:51:fb:91:77": 1, "26:8c:
42 15:0c:de:49": 2}}}
43 """
44
45 from ryu.app import simple_switch_13
46 from ryu.app.wsgi import ControllerBase
47 from ryu.app.wsgi import rpc_public
48 from ryu.app.wsgi import websocket
49 from ryu.app.wsgi import WebSocketRPCServer
50 from ryu.app.wsgi import WSGIApplication
51 from ryu.controller import ofp_event
52 from ryu.controller.handler import set_ev_cls
53 from ryu.lib.packet import packet
54
55
56 simple_switch_instance_name = 'simple_switch_api_app'
57 url = '/simpleswitch/ws'
58
59
60 class SimpleSwitchWebSocket13(simple_switch_13.SimpleSwitch13):
61     _CONTEXTS = {
62         'wsgi': WSGIApplication,
63     }
64
65     def __init__(self, *args, **kwargs):
66         super(SimpleSwitchWebSocket13, self).__init__(*args, **kwargs)
67
68         wsgi = kwargs['wsgi']
69         wsgi.register(
70             SimpleSwitchWebSocketController,
71             data={simple_switch_instance_name: self},
72         )
73         self._ws_manager = wsgi.websocketmanager
74
75     @set_ev_cls(ofp_event.EventOFPPacketIn)
76     def _packet_in_handler(self, ev):
77         super(SimpleSwitchWebSocket13, self)._packet_in_handler(ev)
78
79         pkt = packet.Packet(ev.msg.data)
80         self._ws_manager.broadcast(str(pkt))
81
82     @rpc_public
83     def get_arp_table(self):
84         return self.mac_to_port
85
86
87 class SimpleSwitchWebSocketController(ControllerBase):
88     def __init__(self, req, link, data, **config):
89         super(SimpleSwitchWebSocketController, self).__init__(
90             req, link, data, **config)
91         self.simple_switch_app = data[simple_switch_instance_name]
92
93     @websocket('simpleswitch', url)
94     def _websocket_handler(self, ws):
95         simple_switch = self.simple_switch_app
96         simple_switch.logger.debug('WebSocket connected: %s', ws)
97         rpc_server = WebSocketRPCServer(ws, simple_switch)
98         rpc_server.serve_forever()
99         simple_switch.logger.debug('WebSocket disconnected: %s', ws)