Adds JSON RPC API on web socket to dynamically configure bgp. This is
the initial support (there are still tons of APIs that are not
exported via JSON RPC API).

$ sudo PYTHONPATH=.:$PYTHONPATH ryu-manager 
ryu/services/protocols/bgp/api/jsonrpc.py 
ryu/services/protocols/bgp/application.py

then you can configure on another terminal:

~ fujita$ wsdump.py ws://127.0.0.1:8080/bgp/ws
Press Ctrl+C to quit
> {"jsonrpc": "2.0", "id": 1, "method": "core.start", "params" : 
> {"as_number":64512, "router_id":"10.0.0.2"}}
< {"jsonrpc": "2.0", "id": 1, "result": {}}
> {"jsonrpc": "2.0", "id": 1, "method": "neighbor.create", "params" : 
> {"ip_address":"192.168.177.32", "remote_as":64513}}
< {"jsonrpc": "2.0", "id": 1, "result": {}}
> {"jsonrpc": "2.0", "id": 1, "method": "network.add", "params" : 
> {"prefix":"10.20.0.0/24"}}
< {"jsonrpc": "2.0", "id": 1, "result": {}}

Signed-off-by: FUJITA Tomonori <fujita.tomon...@lab.ntt.co.jp>
---
 ryu/services/protocols/bgp/api/jsonrpc.py | 84 +++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
 create mode 100644 ryu/services/protocols/bgp/api/jsonrpc.py

diff --git a/ryu/services/protocols/bgp/api/jsonrpc.py 
b/ryu/services/protocols/bgp/api/jsonrpc.py
new file mode 100644
index 0000000..b44e40d
--- /dev/null
+++ b/ryu/services/protocols/bgp/api/jsonrpc.py
@@ -0,0 +1,84 @@
+# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+import json
+from webob import Response
+from ryu.base import app_manager
+from ryu.lib import hub
+from ryu.app.wsgi import route, websocket, ControllerBase, WSGIApplication
+from ryu.app.wsgi import rpc_public, WebSocketRPCServer
+from ryu.services.protocols.bgp.api.base import call
+from ryu.services.protocols.bgp.api.base import PREFIX
+from ryu.services.protocols.bgp.rtconf.common import LOCAL_AS
+from ryu.services.protocols.bgp.rtconf.common import ROUTER_ID
+from ryu.services.protocols.bgp.rtconf import neighbors
+
+bgp_instance_name = 'bgp_api_app'
+url = '/bgp/ws'
+
+
+class BgpWSJsonRpc(app_manager.RyuApp):
+    _CONTEXTS = {
+        'wsgi': WSGIApplication,
+    }
+
+    def __init__(self, *args, **kwargs):
+        super(BgpWSJsonRpc, self).__init__(*args, **kwargs)
+
+        wsgi = kwargs['wsgi']
+        wsgi.register(
+            BgpWSJsonRpcController,
+            data={bgp_instance_name: self},
+        )
+        self._ws_manager = wsgi.websocketmanager
+
+    @rpc_public('core.start')
+    def _core_start(self, as_number=64512, router_id='10.0.0.1'):
+        common_settings = {}
+        common_settings[LOCAL_AS] = as_number
+        common_settings[ROUTER_ID] = str(router_id)
+        waiter = hub.Event()
+        call('core.start', waiter=waiter, **common_settings)
+        waiter.wait()
+        return {}
+
+    @rpc_public('neighbor.create')
+    def _neighbor_create(self, ip_address='192.168.177.32',
+                         remote_as=64513):
+        bgp_neighbor = {}
+        bgp_neighbor[neighbors.IP_ADDRESS] = str(ip_address)
+        bgp_neighbor[neighbors.REMOTE_AS] = remote_as
+        call('neighbor.create', **bgp_neighbor)
+        return {}
+
+    @rpc_public('network.add')
+    def _prefix_add(self, prefix='10.20.0.0/24'):
+        networks = {}
+        networks[PREFIX] = str(prefix)
+        call('network.add', **networks)
+        return {}
+
+
+class BgpWSJsonRpcController(ControllerBase):
+    def __init__(self, req, link, data, **config):
+        super(BgpWSJsonRpcController, self).__init__(
+            req, link, data, **config)
+        self.bgp_api_app = data[bgp_instance_name]
+
+    @websocket('bgp', url)
+    def _websocket_handler(self, ws):
+        rpc_server = WebSocketRPCServer(ws, self.bgp_api_app)
+        rpc_server.serve_forever()
-- 
1.8.5.2 (Apple Git-48)


------------------------------------------------------------------------------
The best possible search technologies are now affordable for all companies.
Download your FREE open source Enterprise Search Engine today!
Our experts will assist you in its installation for $59/mo, no commitment.
Test it for FREE on our Cloud platform anytime!
http://pubads.g.doubleclick.net/gampad/clk?id=145328191&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to