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