summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-05-27 13:21:29 -0700
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-05-27 13:27:51 -0700
commit45ac298d01c743c1b2a4414b706c908a50fb214b (patch)
tree3c8919952c4995cdaf225ba631f5f8746cc07d42
parent85c1917f7e9334c31d81c87f635d3eff8f175e1a (diff)
bgp: add JSON RPC API
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.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/services/protocols/bgp/api/jsonrpc.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/ryu/services/protocols/bgp/api/jsonrpc.py b/ryu/services/protocols/bgp/api/jsonrpc.py
new file mode 100644
index 00000000..b44e40dc
--- /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()