LCOV - code coverage report
Current view: top level - bgp - bgp_show_neighbor.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 90 90 100.0 %
Date: 2026-11-09 02:10:49 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/bgp_show_handler.h"
       7             : 
       8             : #include "base/regex.h"
       9             : #include "bgp/bgp_peer.h"
      10             : #include "bgp/bgp_peer_internal_types.h"
      11             : #include "bgp/bgp_server.h"
      12             : #include "bgp/routing-instance/peer_manager.h"
      13             : #include "bgp/routing-instance/routing_instance.h"
      14             : 
      15             : using contrail::regex;
      16             : using contrail::regex_match;
      17             : using contrail::regex_search;
      18             : using std::string;
      19             : using std::vector;
      20             : 
      21             : //
      22             : // Build the list of BgpPeers in one shot for now.
      23             : //
      24         120 : static bool FillBgpNeighborInfoList(const BgpSandeshContext *bsc,
      25             :     bool summary, uint32_t page_limit, uint32_t iter_limit,
      26             :     const string &start_neighbor, const string &search_string,
      27             :     vector<BgpNeighborResp> *show_list, string *next_instance) {
      28         120 :     regex search_expr(search_string);
      29             : 
      30         360 :     for (const BgpPeer *peer = bsc->bgp_server->FindNextPeer(); peer != NULL;
      31         240 :          peer = bsc->bgp_server->FindNextPeer(peer->peer_name())) {
      32         240 :         if ((!regex_search(peer->peer_basename(), search_expr)) &&
      33         632 :             (!regex_search(peer->peer_address_string(), search_expr)) &&
      34         152 :             (search_string != "deleted" || !peer->IsDeleted())) {
      35         128 :             continue;
      36             :         }
      37             : 
      38         112 :         BgpNeighborResp bnr;
      39         112 :         peer->FillNeighborInfo(bsc, &bnr, summary);
      40         112 :         show_list->push_back(bnr);
      41         112 :     }
      42         120 :     return true;
      43         120 : }
      44             : 
      45             : //
      46             : // Specialization of BgpShowHandler<>::CallbackCommon for regular introspect.
      47             : //
      48             : // Note that we don't both paginating bgp neighbors since the list should be
      49             : // pretty small. We always add at least one xmpp neighbor to the list and then
      50             : // paginate the xmpp neighbors as needed.
      51             : //
      52             : template <>
      53         193 : bool BgpShowHandler<BgpNeighborReq, BgpNeighborReqIterate,
      54             :     BgpNeighborListResp, BgpNeighborResp>::CallbackCommon(
      55             :     const BgpSandeshContext *bsc, Data *data) {
      56         193 :     uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
      57         193 :     uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
      58             : 
      59             :     // Build the list of BgpPeers in one shot for now.
      60         193 :     if (data->next_entry.empty()) {
      61          60 :         FillBgpNeighborInfoList(bsc, false, page_limit, iter_limit, string(),
      62          60 :             data->search_string, &data->show_list, NULL);
      63             :     }
      64             : 
      65             :     // Add xmpp neighbors.
      66         193 :     string next_neighbor;
      67         386 :     bool done = bsc->ShowNeighborExtension(bsc, false, page_limit, iter_limit,
      68         193 :         data->next_entry, data->search_string, &data->show_list,
      69             :         &next_neighbor);
      70         193 :     if (!next_neighbor.empty())
      71         124 :         SaveContextToData(next_neighbor, done, data);
      72         193 :     return done;
      73         193 : }
      74             : 
      75             : //
      76             : // Specialization of BgpShowHandler<>::FillShowList for regular introspect.
      77             : //
      78             : template <>
      79          76 : void BgpShowHandler<BgpNeighborReq, BgpNeighborReqIterate,
      80             :     BgpNeighborListResp, BgpNeighborResp>::FillShowList(
      81             :     BgpNeighborListResp *resp, const vector<BgpNeighborResp> &show_list) {
      82          76 :     resp->set_neighbors(show_list);
      83          76 : }
      84             : 
      85             : //
      86             : // Specialization of BgpShowHandler<>::CallbackCommon for summary introspect.
      87             : //
      88             : // Note that we don't both paginating bgp neighbors since the list should be
      89             : // pretty small. We always add at least one xmpp neighbor to the list and then
      90             : // paginate the xmpp neighbors as needed.
      91             : //
      92             : template <>
      93         193 : bool BgpShowHandler<ShowBgpNeighborSummaryReq, ShowBgpNeighborSummaryReqIterate,
      94             :     ShowBgpNeighborSummaryResp, BgpNeighborResp>::CallbackCommon(
      95             :     const BgpSandeshContext *bsc, Data *data) {
      96         193 :     uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
      97         193 :     uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
      98             : 
      99             :     // Build the list of BgpPeers in one shot for now.
     100         193 :     if (data->next_entry.empty()) {
     101          60 :         FillBgpNeighborInfoList(bsc, true, page_limit, iter_limit, string(),
     102          60 :             data->search_string, &data->show_list, NULL);
     103             :     }
     104             : 
     105             :     // Add xmpp neighbors.
     106         193 :     string next_neighbor;
     107         386 :     bool done = bsc->ShowNeighborExtension(bsc, true, page_limit, iter_limit,
     108         193 :         data->next_entry, data->search_string, &data->show_list,
     109             :         &next_neighbor);
     110         193 :     if (!next_neighbor.empty())
     111         124 :         SaveContextToData(next_neighbor, done, data);
     112         193 :     return done;
     113         193 : }
     114             : 
     115             : //
     116             : // Specialization of BgpShowHandler<>::FillShowList for summary introspect.
     117             : //
     118             : template <>
     119          76 : void BgpShowHandler<ShowBgpNeighborSummaryReq, ShowBgpNeighborSummaryReqIterate,
     120             :     ShowBgpNeighborSummaryResp, BgpNeighborResp>::FillShowList(
     121             :     ShowBgpNeighborSummaryResp *resp,
     122             :     const vector<BgpNeighborResp> &show_list) {
     123          76 :     resp->set_neighbors(show_list);
     124          76 : }
     125             : 
     126             : //
     127             : // Handler for BgpNeighborReq.
     128             : //
     129          60 : void BgpNeighborReq::HandleRequest() const {
     130          60 :     RequestPipeline::PipeSpec ps(this);
     131          60 :     RequestPipeline::StageSpec s1;
     132          60 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     133             : 
     134          60 :     s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
     135             :     s1.cbFn_ = boost::bind(&BgpShowHandler<
     136             :         BgpNeighborReq,
     137             :         BgpNeighborReqIterate,
     138             :         BgpNeighborListResp,
     139          60 :         BgpNeighborResp>::Callback, _1, _2, _3, _4, _5);
     140             :     s1.allocFn_ = BgpShowHandler<
     141             :         BgpNeighborReq,
     142             :         BgpNeighborReqIterate,
     143             :         BgpNeighborListResp,
     144          60 :         BgpNeighborResp>::CreateData;
     145          60 :     s1.instances_.push_back(0);
     146          60 :     ps.stages_.push_back(s1);
     147          60 :     RequestPipeline rp(ps);
     148          60 : }
     149             : 
     150             : //
     151             : // Handler for BgpNeighborReqIterate.
     152             : //
     153          44 : void BgpNeighborReqIterate::HandleRequest() const {
     154          44 :     RequestPipeline::PipeSpec ps(this);
     155          44 :     RequestPipeline::StageSpec s1;
     156          44 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     157             : 
     158          44 :     s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
     159             :     s1.cbFn_ = boost::bind(&BgpShowHandler<
     160             :         BgpNeighborReq,
     161             :         BgpNeighborReqIterate,
     162             :         BgpNeighborListResp,
     163          44 :         BgpNeighborResp>::CallbackIterate, _1, _2, _3, _4, _5);
     164             :     s1.allocFn_ = BgpShowHandler<
     165             :         BgpNeighborReq,
     166             :         BgpNeighborReqIterate,
     167             :         BgpNeighborListResp,
     168          44 :         BgpNeighborResp>::CreateData;
     169          44 :     s1.instances_.push_back(0);
     170          44 :     ps.stages_.push_back(s1);
     171          44 :     RequestPipeline rp(ps);
     172          44 : }
     173             : 
     174             : //
     175             : // Handler for ShowBgpNeighborSummaryReq.
     176             : //
     177          60 : void ShowBgpNeighborSummaryReq::HandleRequest() const {
     178          60 :     RequestPipeline::PipeSpec ps(this);
     179          60 :     RequestPipeline::StageSpec s1;
     180          60 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     181             : 
     182          60 :     s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
     183             :     s1.cbFn_ = boost::bind(&BgpShowHandler<
     184             :         ShowBgpNeighborSummaryReq,
     185             :         ShowBgpNeighborSummaryReqIterate,
     186             :         ShowBgpNeighborSummaryResp,
     187          60 :         BgpNeighborResp>::Callback, _1, _2, _3, _4, _5);
     188             :     s1.allocFn_ = BgpShowHandler<
     189             :         ShowBgpNeighborSummaryReq,
     190             :         ShowBgpNeighborSummaryReqIterate,
     191             :         ShowBgpNeighborSummaryResp,
     192          60 :         BgpNeighborResp>::CreateData;
     193          60 :     s1.instances_.push_back(0);
     194          60 :     ps.stages_.push_back(s1);
     195          60 :     RequestPipeline rp(ps);
     196          60 : }
     197             : 
     198             : //
     199             : // Handler for ShowBgpNeighborSummaryReqIterate.
     200             : //
     201          44 : void ShowBgpNeighborSummaryReqIterate::HandleRequest() const {
     202          44 :     RequestPipeline::PipeSpec ps(this);
     203          44 :     RequestPipeline::StageSpec s1;
     204          44 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     205             : 
     206          44 :     s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
     207             :     s1.cbFn_ = boost::bind(&BgpShowHandler<
     208             :         ShowBgpNeighborSummaryReq,
     209             :         ShowBgpNeighborSummaryReqIterate,
     210             :         ShowBgpNeighborSummaryResp,
     211          44 :         BgpNeighborResp>::CallbackIterate, _1, _2, _3, _4, _5);
     212             :     s1.allocFn_ = BgpShowHandler<
     213             :         ShowBgpNeighborSummaryReq,
     214             :         ShowBgpNeighborSummaryReqIterate,
     215             :         ShowBgpNeighborSummaryResp,
     216          44 :         BgpNeighborResp>::CreateData;
     217          44 :     s1.instances_.push_back(0);
     218          44 :     ps.stages_.push_back(s1);
     219          44 :     RequestPipeline rp(ps);
     220          44 : }

Generated by: LCOV version 1.14