Line data Source code
1 : /* 2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "bgp/bgp_show_handler.h" 6 : 7 : #include <boost/foreach.hpp> 8 : 9 : #include <base/regex.h> 10 : #include "bgp/ermvpn/ermvpn_table.h" 11 : #include "bgp/bgp_peer_internal_types.h" 12 : #include "bgp/bgp_mvpn.h" 13 : #include "bgp/bgp_server.h" 14 : #include "bgp/mvpn/mvpn_table.h" 15 : #include "bgp/routing-instance/routing_instance.h" 16 : 17 : using contrail::regex; 18 : using contrail::regex_match; 19 : using contrail::regex_search; 20 : using std::string; 21 : using std::vector; 22 : 23 : // 24 : // Fill in information for an ermvpn table. 25 : // 26 0 : static void FillMvpnProjectManagerInfo(ShowMvpnProjectManager *smm, 27 : const BgpSandeshContext *bsc, const ErmVpnTable *table) { 28 0 : smm->set_name(table->name()); 29 0 : const MvpnProjectManager *manager = table->mvpn_project_manager(); 30 0 : if (!manager) 31 0 : return; 32 : 33 0 : size_t total_sg_states = 0; 34 0 : BOOST_FOREACH(MvpnProjectManagerPartition *pm_partition, 35 : manager->partitions()) { 36 0 : total_sg_states += pm_partition->states().size(); 37 : } 38 0 : smm->set_total_sg_states(total_sg_states); 39 0 : smm->set_total_managers(table->routing_instance()->manager()-> 40 0 : GetMvpnProjectManagerCount(table->routing_instance()->name())); 41 0 : smm->set_deleted(manager->deleted()); 42 0 : smm->set_deleted_at( 43 0 : UTCUsecToString(manager->deleter()->delete_time_stamp_usecs())); 44 : } 45 : 46 : // 47 : // Specialization of BgpShowHandler<>::CallbackCommon. 48 : // 49 : template <> 50 0 : bool BgpShowHandler<ShowMvpnProjectManagerReq, ShowMvpnProjectManagerReqIterate, 51 : ShowMvpnProjectManagerResp, ShowMvpnProjectManager>::CallbackCommon( 52 : const BgpSandeshContext *bsc, Data *data) { 53 0 : uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit; 54 0 : uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit; 55 0 : RoutingInstanceMgr *rim = bsc->bgp_server->routing_instance_mgr(); 56 : 57 0 : regex search_expr(data->search_string); 58 : RoutingInstanceMgr::const_name_iterator it = 59 0 : rim->name_clower_bound(data->next_entry); 60 0 : for (uint32_t iter_count = 0; it != rim->name_cend(); ++it, ++iter_count) { 61 0 : const RoutingInstance *rtinstance = it->second; 62 : const ErmVpnTable *table = static_cast<const ErmVpnTable *>( 63 0 : rtinstance->GetTable(Address::ERMVPN)); 64 0 : if (!table) 65 0 : continue; 66 0 : if ((!regex_search(table->name(), search_expr)) && 67 0 : (data->search_string != "deleted" || !table->IsDeleted())) { 68 0 : continue; 69 : } 70 0 : ShowMvpnProjectManager smm; 71 0 : FillMvpnProjectManagerInfo(&smm, bsc, table); 72 0 : data->show_list.push_back(smm); 73 0 : if (data->show_list.size() >= page_limit) 74 0 : break; 75 0 : if (iter_count >= iter_limit) 76 0 : break; 77 0 : } 78 : 79 : // All done if we've looked at all instances. 80 0 : if (it == rim->name_cend() || ++it == rim->name_cend()) 81 0 : return true; 82 : 83 : // Return true if we've reached the page limit, false if we've reached the 84 : // iteration limit. 85 0 : bool done = data->show_list.size() >= page_limit; 86 0 : SaveContextToData(it->second->name(), done, data); 87 0 : return done; 88 0 : } 89 : 90 : // 91 : // Specialization of BgpShowHandler<>::FillShowList. 92 : // 93 : template <> 94 0 : void BgpShowHandler<ShowMvpnProjectManagerReq, ShowMvpnProjectManagerReqIterate, 95 : ShowMvpnProjectManagerResp, ShowMvpnProjectManager>::FillShowList( 96 : ShowMvpnProjectManagerResp *resp, 97 : const vector<ShowMvpnProjectManager> &show_list) { 98 0 : resp->set_managers(show_list); 99 0 : } 100 : 101 : // 102 : // Handler for ShowMvpnProjectManagerReq. 103 : // 104 0 : void ShowMvpnProjectManagerReq::HandleRequest() const { 105 0 : RequestPipeline::PipeSpec ps(this); 106 0 : RequestPipeline::StageSpec s1; 107 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance(); 108 : 109 0 : s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand"); 110 : s1.cbFn_ = boost::bind(&BgpShowHandler< 111 : ShowMvpnProjectManagerReq, 112 : ShowMvpnProjectManagerReqIterate, 113 : ShowMvpnProjectManagerResp, 114 0 : ShowMvpnProjectManager>::Callback, _1, _2, _3, _4, _5); 115 : s1.allocFn_ = BgpShowHandler< 116 : ShowMvpnProjectManagerReq, 117 : ShowMvpnProjectManagerReqIterate, 118 : ShowMvpnProjectManagerResp, 119 0 : ShowMvpnProjectManager>::CreateData; 120 0 : s1.instances_.push_back(0); 121 0 : ps.stages_.push_back(s1); 122 0 : RequestPipeline rp(ps); 123 0 : } 124 : 125 : // 126 : // Handler for ShowMvpnProjectManagerReqIterate. 127 : // 128 0 : void ShowMvpnProjectManagerReqIterate::HandleRequest() const { 129 0 : RequestPipeline::PipeSpec ps(this); 130 0 : RequestPipeline::StageSpec s1; 131 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance(); 132 : 133 0 : s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand"); 134 : s1.cbFn_ = boost::bind(&BgpShowHandler< 135 : ShowMvpnProjectManagerReq, 136 : ShowMvpnProjectManagerReqIterate, 137 : ShowMvpnProjectManagerResp, 138 0 : ShowMvpnProjectManager>::CallbackIterate, _1, _2, _3, _4, _5); 139 : s1.allocFn_ = BgpShowHandler< 140 : ShowMvpnProjectManagerReq, 141 : ShowMvpnProjectManagerReqIterate, 142 : ShowMvpnProjectManagerResp, 143 0 : ShowMvpnProjectManager>::CreateData; 144 0 : s1.instances_.push_back(0); 145 0 : ps.stages_.push_back(s1); 146 0 : RequestPipeline rp(ps); 147 0 : }