summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@gmail.com>2018-12-24 21:03:16 +0900
committerFUJITA Tomonori <fujita.tomonori@gmail.com>2018-12-24 21:41:54 +0900
commit28d96f985bc59f344894f2e37cea5cb1bea062e5 (patch)
tree69f9272994d7e49918912d6200bb579a03188c10
parent8e7741a0a80a1bd15a343563a29142f755c29f3f (diff)
docs: update python gRPC API example
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
-rw-r--r--docs/sources/grpc-client.md36
-rw-r--r--tools/grpc/python/add_path.py57
-rw-r--r--tools/grpc/python/get_neighbor.py29
3 files changed, 72 insertions, 50 deletions
diff --git a/docs/sources/grpc-client.md b/docs/sources/grpc-client.md
index 1b0d758a..e244f60c 100644
--- a/docs/sources/grpc-client.md
+++ b/docs/sources/grpc-client.md
@@ -16,42 +16,36 @@ Ruby, C++, Node.js, and Java. It assumes that you use Ubuntu 16.04 (64bit).
## Prerequisite
-We assumes that you have finished installing `protoc`
-[protocol buffer](https://github.com/google/protobuf) compiler to generate stub
-server and client code and "protobuf runtime" for your favorite language.
-
-Please refer to [the official docs of gRPC](http://www.grpc.io/docs/) for
+We assumes that you have the relevant tools installed to generate the server and client interface for your favorite language from proto files. Please refer to [the official docs of gRPC](http://www.grpc.io/docs/) for
details.
## Python
-### Generating Stub Code
+### Generating Interface
-We need to generate stub code GoBGP at first.
+You need to generate the server and client interface from GoBGP proto files at first.
```bash
-$ cd $GOPATH/src/github.com/osrg/gobgp/tools/grpc/python
-$ GOBGP_API=$GOPATH/src/github.com/osrg/gobgp/api
-$ protoc -I $GOBGP_API --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` $GOBGP_API/gobgp.proto
+$ python -m grpc_tools.protoc -I./api --python_out=. --grpc_python_out=. api/gobgp.proto api/attribute.proto api/capability.proto
```
-### Get Neighbor
+### Adding Path
-['tools/grpc/python/get_neighbor.py'](https://github.com/osrg/gobgp/blob/master/tools/grpc/python/get_neighbor.py)
-shows an example for getting neighbor's information.
+[`tools/grpc/python/add_path.py`](https://github.com/osrg/gobgp/blob/master/tools/grpc/python/add_path.py)
+shows an example for adding a route.
Let's run this script.
```bash
-$ python get_neighbor.py 172.18.0.2
-BGP neighbor is 10.0.0.2, remote AS 65002
- BGP version 4, remote router ID
- BGP state = active, up for 0
- BGP OutQ = 0, Flops = 0
- Hold time is 0, keepalive interval is 0 seconds
- Configured hold time is 90, keepalive interval is 30 seconds
+$ PYTHONPATH=$PYTHONPATH:. python add_path.py
```
-We got the neighbor information successfully.
+See if he route was added to the global rib.
+
+```bash
+$ gobgp g r
+ Network Next Hop AS_PATH Age Attrs
+*> 10.0.0.0/24 1.1.1.1 100 200 00:08:02 [{Origin: ?}]
+```
## Ruby
diff --git a/tools/grpc/python/add_path.py b/tools/grpc/python/add_path.py
new file mode 100644
index 00000000..1af4882f
--- /dev/null
+++ b/tools/grpc/python/add_path.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+import grpc
+from google.protobuf.any_pb2 import Any
+
+import gobgp_pb2
+import gobgp_pb2_grpc
+import attribute_pb2
+
+_TIMEOUT_SECONDS = 1000
+
+
+def run():
+ channel = grpc.insecure_channel('localhost:50051')
+ stub = gobgp_pb2_grpc.GobgpApiStub(channel)
+
+ nlri = Any()
+ nlri.Pack(attribute_pb2.IPAddressPrefix(
+ prefix_len=24,
+ prefix="10.0.0.0",
+ ))
+ origin = Any()
+ origin.Pack(attribute_pb2.OriginAttribute(
+ origin=2, # INCOMPLETE
+ ))
+ as_segment = attribute_pb2.AsSegment(
+ # type=2, # "type" causes syntax error
+ numbers=[100, 200],
+ )
+ as_segment.type = 2 # SEQ
+ as_path = Any()
+ as_path.Pack(attribute_pb2.AsPathAttribute(
+ segments=[as_segment],
+ ))
+ next_hop = Any()
+ next_hop.Pack(attribute_pb2.NextHopAttribute(
+ next_hop="1.1.1.1",
+ ))
+ attributes = [origin, as_path, next_hop]
+
+ stub.AddPath(
+ gobgp_pb2.AddPathRequest(
+ table_type=gobgp_pb2.GLOBAL,
+ path=gobgp_pb2.Path(
+ nlri=nlri,
+ pattrs=attributes,
+ family=gobgp_pb2.Family(afi=gobgp_pb2.Family.AFI_IP, safi=gobgp_pb2.Family.SAFI_UNICAST),
+ )
+ ),
+ _TIMEOUT_SECONDS,
+ )
+
+if __name__ == '__main__':
+ run()
diff --git a/tools/grpc/python/get_neighbor.py b/tools/grpc/python/get_neighbor.py
deleted file mode 100644
index ff94569d..00000000
--- a/tools/grpc/python/get_neighbor.py
+++ /dev/null
@@ -1,29 +0,0 @@
-import gobgp_pb2_grpc
-import gobgp_pb2
-import sys
-
-import grpc
-from grpc.framework.interfaces.face.face import ExpirationError
-
-_TIMEOUT_SECONDS = 1
-
-
-def run(gobgpd_addr):
- channel = grpc.insecure_channel(gobgpd_addr + ':50051')
- stub = gobgp_pb2_grpc.GobgpApiStub(channel)
- try:
- peers = stub.GetNeighbor(gobgp_pb2.GetNeighborRequest()).peers
- for peer in peers:
- print("BGP neighbor is %s, remote AS %d" % (peer.conf.neighbor_address, peer.conf.peer_as))
- print(" BGP version 4, remote router ID %s" % (peer.conf.id))
- print(" BGP state = %s, up for %s" % (peer.info.bgp_state, peer.timers.state.uptime))
- print(" BGP OutQ = %d, Flops = %d" % (peer.info.out_q, peer.info.flops))
- print(" Hold time is %d, keepalive interval is %d seconds" % (peer.timers.state.negotiated_hold_time, peer.timers.state.keepalive_interval))
- print(" Configured hold time is %d, keepalive interval is %d seconds" % (peer.timers.config.hold_time, peer.timers.config.keepalive_interval))
- except ExpirationError, e:
- print str(e)
- sys.exit(-1)
-
-if __name__ == '__main__':
- gobgp = sys.argv[1]
- run(gobgp)