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