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