github.com/osrg/gobgp/v3@v3.30.0/tools/grpc/python/add_path.py (about)

     1  #!/usr/bin/env python
     2  
     3  from __future__ import absolute_import
     4  from __future__ import print_function
     5  
     6  import grpc
     7  from google.protobuf.any_pb2 import Any
     8  
     9  import gobgp_pb2
    10  import gobgp_pb2_grpc
    11  import attribute_pb2
    12  
    13  _TIMEOUT_SECONDS = 1000
    14  
    15  
    16  def run():
    17      channel = grpc.insecure_channel('localhost:50051')
    18      stub = gobgp_pb2_grpc.GobgpApiStub(channel)
    19  
    20      nlri = Any()
    21      nlri.Pack(attribute_pb2.IPAddressPrefix(
    22          prefix_len=24,
    23          prefix="10.0.0.0",
    24      ))
    25      origin = Any()
    26      origin.Pack(attribute_pb2.OriginAttribute(
    27          origin=2,  # INCOMPLETE
    28      ))
    29      as_segment = attribute_pb2.AsSegment(
    30          # type=2,  # "type" causes syntax error
    31          numbers=[100, 200],
    32      )
    33      as_segment.type = 2  # SEQ
    34      as_path = Any()
    35      as_path.Pack(attribute_pb2.AsPathAttribute(
    36          segments=[as_segment],
    37      ))
    38      next_hop = Any()
    39      next_hop.Pack(attribute_pb2.NextHopAttribute(
    40          next_hop="1.1.1.1",
    41      ))
    42      attributes = [origin, as_path, next_hop]
    43  
    44      stub.AddPath(
    45          gobgp_pb2.AddPathRequest(
    46              table_type=gobgp_pb2.GLOBAL,
    47              path=gobgp_pb2.Path(
    48                  nlri=nlri,
    49                  pattrs=attributes,
    50                  family=gobgp_pb2.Family(afi=gobgp_pb2.Family.AFI_IP, safi=gobgp_pb2.Family.SAFI_UNICAST),
    51              )
    52          ),
    53          _TIMEOUT_SECONDS,
    54      )
    55  
    56  if __name__ == '__main__':
    57      run()