github.com/osrg/gobgp/v3@v3.30.0/tools/grpc/cpp/add_path.cc (about)

     1  #include <iostream>
     2  #include <memory>
     3  #include <sstream>
     4  #include <string>
     5  #include <string.h>
     6  
     7  #include <grpc/grpc.h>
     8  #include <grpc++/channel.h>
     9  #include <grpc++/client_context.h>
    10  #include <grpc++/create_channel.h>
    11  #include <grpc++/security/credentials.h>
    12  #include "gobgp.grpc.pb.h"
    13  #include "attribute.grpc.pb.h"
    14  
    15  using grpc::Channel;
    16  using grpc::ClientContext;
    17  using grpc::Status;
    18  
    19  using gobgpapi::GobgpApi;
    20  
    21  class GobgpClient
    22  {
    23    public:
    24  	GobgpClient(std::shared_ptr<Channel> channel)
    25  		: stub_(GobgpApi::NewStub(channel)) {}
    26  	void AddPath()
    27  	{
    28  		std::cout << "In addRoute \n";
    29  		// Parameters to AddPath API
    30  		gobgpapi::AddPathRequest request;
    31  		ClientContext context;
    32  		gobgpapi::AddPathResponse response;
    33  
    34  		// Path info variable
    35  		gobgpapi::Path *current_path = new gobgpapi::Path;
    36  
    37  		// Updating family info of current_path
    38  		gobgpapi::Family *current_family = new gobgpapi::Family;
    39  		current_family->set_afi(gobgpapi::Family::AFI_IP);
    40  		current_family->set_safi(gobgpapi::Family::SAFI_UNICAST);
    41  		current_path->set_allocated_family(current_family);
    42  
    43  		// Updating nlri info for current_path
    44  		google::protobuf::Any *current_nlri = new google::protobuf::Any;
    45  		gobgpapi::IPAddressPrefix current_ipaddrprefix;
    46  		current_ipaddrprefix.set_prefix("10.0.0.0");
    47  		current_ipaddrprefix.set_prefix_len(24);
    48  		current_nlri->PackFrom(current_ipaddrprefix);
    49  		current_path->set_allocated_nlri(current_nlri);
    50  
    51  		// Updating OriginAttribute info for current_path
    52  		google::protobuf::Any *current_origin = current_path->add_pattrs();
    53  		gobgpapi::OriginAttribute current_origin_t;
    54  		current_origin_t.set_origin(0);
    55  		current_origin->PackFrom(current_origin_t);
    56  
    57  		// Updating NextHopAttribute info for current_path
    58  		google::protobuf::Any *current_next_hop = current_path->add_pattrs();
    59  		gobgpapi::NextHopAttribute current_next_hop_t;
    60  		current_next_hop_t.set_next_hop("1.1.1.1");
    61  		current_next_hop->PackFrom(current_next_hop_t);
    62  		// Updating CommunitiesAttribute for current_path
    63  		google::protobuf::Any *current_communities = current_path->add_pattrs();
    64  		gobgpapi::CommunitiesAttribute current_communities_t;
    65  		current_communities_t.add_communities(100);
    66  		current_communities->PackFrom(current_communities_t);
    67  
    68  		// Populating the request attributes
    69  		request.set_table_type(gobgpapi::TableType::GLOBAL);
    70  		request.set_allocated_path(current_path);
    71  
    72  		Status status = stub_->AddPath(&context, request, &response);
    73  		if (status.ok())
    74  		{
    75  		}
    76  		else
    77  		{
    78  			std::cout << status.error_code() << ": " << status.error_message()
    79  					  << std::endl;
    80  		}
    81  	}
    82  
    83    private:
    84  	std::unique_ptr<GobgpApi::Stub> stub_;
    85  };
    86  
    87  int main(int argc, char **argv)
    88  {
    89  	GobgpClient client(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
    90  
    91  	client.AddPath();
    92  }