Line data Source code
1 : /* 2 : * Copyright (c) 2013 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/bgp_config_ifmap.h" 11 : #include "bgp/bgp_peer_types.h" 12 : #include "bgp/bgp_peer_internal_types.h" 13 : #include "bgp/bgp_server.h" 14 : #include "schema/bgp_schema_types.h" 15 : 16 : using contrail::regex; 17 : using contrail::regex_match; 18 : using contrail::regex_search; 19 : using std::string; 20 : using std::vector; 21 : 22 : // 23 : // Fill in information for a peering. 24 : // 25 0 : static void FillBgpPeeringConfigInfo(ShowBgpPeeringConfig *sbpc, 26 : const BgpSandeshContext *bsc, const BgpIfmapPeeringConfig *peering) { 27 0 : sbpc->set_instance_name(peering->instance()->name()); 28 0 : sbpc->set_name(peering->name()); 29 0 : sbpc->set_neighbor_count(peering->size()); 30 0 : if (peering->bgp_peering()) { 31 0 : vector<ShowBgpSessionConfig> sbsc_list; 32 0 : for (vector<autogen::BgpSession>::const_iterator sit = 33 0 : peering->bgp_peering()->data().begin(); 34 0 : sit != peering->bgp_peering()->data().end(); ++sit) { 35 0 : ShowBgpSessionConfig sbsc; 36 0 : sbsc.set_uuid(sit->uuid); 37 0 : vector<ShowBgpSessionAttributesConfig> sbsac_list; 38 0 : for (vector<autogen::BgpSessionAttributes>::const_iterator ait = 39 0 : sit->attributes.begin(); ait != sit->attributes.end(); ++ait) { 40 0 : ShowBgpSessionAttributesConfig sbsac; 41 0 : sbsac.set_bgp_router(ait->bgp_router); 42 0 : sbsac.set_address_families(ait->address_families.family); 43 0 : sbsac_list.push_back(sbsac); 44 0 : } 45 0 : sbsc.set_attributes(sbsac_list); 46 0 : sbsc_list.push_back(sbsc); 47 0 : } 48 0 : sbpc->set_sessions(sbsc_list); 49 0 : } 50 0 : } 51 : 52 : // 53 : // Specialization of BgpShowHandler<>::CallbackCommon. 54 : // 55 : template <> 56 0 : bool BgpShowHandler<ShowBgpPeeringConfigReq, ShowBgpPeeringConfigReqIterate, 57 : ShowBgpPeeringConfigResp, ShowBgpPeeringConfig>::CallbackCommon( 58 : const BgpSandeshContext *bsc, Data *data) { 59 0 : uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit; 60 0 : uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit; 61 0 : const BgpIfmapConfigManager *bcm = dynamic_cast<BgpIfmapConfigManager *>( 62 0 : bsc->bgp_server->config_manager()); 63 0 : if (!bcm) 64 0 : return true; 65 : 66 0 : regex search_expr(data->search_string); 67 : BgpConfigManager::InstanceMapRange range = 68 0 : bcm->InstanceMapItems(data->next_entry); 69 0 : BgpConfigManager::InstanceMap::const_iterator it = range.first; 70 0 : BgpConfigManager::InstanceMap::const_iterator it_end = range.second; 71 0 : for (uint32_t iter_count = 0; it != it_end; ++it, ++iter_count) { 72 : const BgpIfmapInstanceConfig *instance = 73 0 : bcm->config()->FindInstance(it->first); 74 0 : if (!instance) 75 0 : continue; 76 0 : BOOST_FOREACH(BgpIfmapInstanceConfig::PeeringMap::value_type value, 77 : instance->peerings()) { 78 0 : const BgpIfmapPeeringConfig *peering = value.second; 79 0 : if (!regex_search(peering->name(), search_expr)) 80 0 : continue; 81 0 : ShowBgpPeeringConfig sbpc; 82 0 : FillBgpPeeringConfigInfo(&sbpc, bsc, peering); 83 0 : data->show_list.push_back(sbpc); 84 0 : } 85 0 : if (data->show_list.size() >= page_limit) 86 0 : break; 87 0 : if (iter_count >= iter_limit) 88 0 : break; 89 : } 90 : 91 : // All done if we've looked at all instances. 92 0 : if (it == it_end || ++it == it_end) 93 0 : return true; 94 : 95 : // Return true if we've reached the page limit, false if we've reached the 96 : // iteration limit. 97 0 : bool done = data->show_list.size() >= page_limit; 98 0 : SaveContextToData(it->second->name(), done, data); 99 0 : return done; 100 0 : } 101 : 102 : // 103 : // Specialization of BgpShowHandler<>::FillShowList. 104 : // 105 : template <> 106 0 : void BgpShowHandler<ShowBgpPeeringConfigReq, ShowBgpPeeringConfigReqIterate, 107 : ShowBgpPeeringConfigResp, ShowBgpPeeringConfig>::FillShowList( 108 : ShowBgpPeeringConfigResp *resp, 109 : const vector<ShowBgpPeeringConfig> &show_list) { 110 0 : resp->set_peerings(show_list); 111 0 : } 112 : 113 : // 114 : // Handler for ShowBgpPeeringConfigReq. 115 : // Called via BgpSandeshContext::PeeringShowReqHandler. 116 : // 117 0 : void ShowBgpIfmapPeeringConfigReqHandler(const BgpSandeshContext *bsc, 118 : const ShowBgpPeeringConfigReq *req) { 119 0 : RequestPipeline::PipeSpec ps(req); 120 0 : RequestPipeline::StageSpec s1; 121 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance(); 122 : 123 0 : s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand"); 124 : s1.cbFn_ = boost::bind(&BgpShowHandler< 125 : ShowBgpPeeringConfigReq, 126 : ShowBgpPeeringConfigReqIterate, 127 : ShowBgpPeeringConfigResp, 128 0 : ShowBgpPeeringConfig>::Callback, _1, _2, _3, _4, _5); 129 : s1.allocFn_ = BgpShowHandler< 130 : ShowBgpPeeringConfigReq, 131 : ShowBgpPeeringConfigReqIterate, 132 : ShowBgpPeeringConfigResp, 133 0 : ShowBgpPeeringConfig>::CreateData; 134 0 : s1.instances_.push_back(0); 135 0 : ps.stages_.push_back(s1); 136 0 : RequestPipeline rp(ps); 137 0 : } 138 : 139 : // 140 : // Handler for ShowBgpPeeringConfigReqIterate. 141 : // Called via BgpSandeshContext::PeeringShowReqIterateHandler. 142 : // 143 0 : void ShowBgpIfmapPeeringConfigReqIterateHandler(const BgpSandeshContext *bsc, 144 : const ShowBgpPeeringConfigReqIterate *req_iterate) { 145 0 : RequestPipeline::PipeSpec ps(req_iterate); 146 0 : RequestPipeline::StageSpec s1; 147 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance(); 148 : 149 0 : s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand"); 150 : s1.cbFn_ = boost::bind(&BgpShowHandler< 151 : ShowBgpPeeringConfigReq, 152 : ShowBgpPeeringConfigReqIterate, 153 : ShowBgpPeeringConfigResp, 154 0 : ShowBgpPeeringConfig>::CallbackIterate, _1, _2, _3, _4, _5); 155 : s1.allocFn_ = BgpShowHandler< 156 : ShowBgpPeeringConfigReq, 157 : ShowBgpPeeringConfigReqIterate, 158 : ShowBgpPeeringConfigResp, 159 0 : ShowBgpPeeringConfig>::CreateData; 160 0 : s1.instances_.push_back(0); 161 0 : ps.stages_.push_back(s1); 162 0 : RequestPipeline rp(ps); 163 0 : }