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