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 6 : static void FillBgpPeeringConfigInfo(ShowBgpPeeringConfig *sbpc, 26 : const BgpSandeshContext *bsc, const BgpIfmapPeeringConfig *peering) { 27 6 : sbpc->set_instance_name(peering->instance()->name()); 28 6 : sbpc->set_name(peering->name()); 29 6 : sbpc->set_neighbor_count(peering->size()); 30 6 : if (peering->bgp_peering()) { 31 6 : vector<ShowBgpSessionConfig> sbsc_list; 32 6 : for (vector<autogen::BgpSession>::const_iterator sit = 33 6 : peering->bgp_peering()->data().begin(); 34 11 : sit != peering->bgp_peering()->data().end(); ++sit) { 35 5 : ShowBgpSessionConfig sbsc; 36 5 : sbsc.set_uuid(sit->uuid); 37 5 : vector<ShowBgpSessionAttributesConfig> sbsac_list; 38 5 : for (vector<autogen::BgpSessionAttributes>::const_iterator ait = 39 20 : sit->attributes.begin(); ait != sit->attributes.end(); ++ait) { 40 10 : ShowBgpSessionAttributesConfig sbsac; 41 10 : sbsac.set_bgp_router(ait->bgp_router); 42 10 : sbsac.set_address_families(ait->address_families.family); 43 10 : sbsac_list.push_back(sbsac); 44 10 : } 45 5 : sbsc.set_attributes(sbsac_list); 46 5 : sbsc_list.push_back(sbsc); 47 5 : } 48 6 : sbpc->set_sessions(sbsc_list); 49 6 : } 50 6 : } 51 : 52 : // 53 : // Specialization of BgpShowHandler<>::CallbackCommon. 54 : // 55 : template <> 56 3 : bool BgpShowHandler<ShowBgpPeeringConfigReq, ShowBgpPeeringConfigReqIterate, 57 : ShowBgpPeeringConfigResp, ShowBgpPeeringConfig>::CallbackCommon( 58 : const BgpSandeshContext *bsc, Data *data) { 59 3 : uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit; 60 3 : uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit; 61 3 : const BgpIfmapConfigManager *bcm = dynamic_cast<BgpIfmapConfigManager *>( 62 3 : bsc->bgp_server->config_manager()); 63 3 : if (!bcm) 64 0 : return true; 65 : 66 3 : regex search_expr(data->search_string); 67 : BgpConfigManager::InstanceMapRange range = 68 3 : bcm->InstanceMapItems(data->next_entry); 69 3 : BgpConfigManager::InstanceMap::const_iterator it = range.first; 70 3 : BgpConfigManager::InstanceMap::const_iterator it_end = range.second; 71 10 : for (uint32_t iter_count = 0; it != it_end; ++it, ++iter_count) { 72 : const BgpIfmapInstanceConfig *instance = 73 7 : bcm->config()->FindInstance(it->first); 74 7 : if (!instance) 75 0 : continue; 76 19 : BOOST_FOREACH(BgpIfmapInstanceConfig::PeeringMap::value_type value, 77 : instance->peerings()) { 78 6 : const BgpIfmapPeeringConfig *peering = value.second; 79 6 : if (!regex_search(peering->name(), search_expr)) 80 0 : continue; 81 6 : ShowBgpPeeringConfig sbpc; 82 6 : FillBgpPeeringConfigInfo(&sbpc, bsc, peering); 83 6 : data->show_list.push_back(sbpc); 84 12 : } 85 7 : if (data->show_list.size() >= page_limit) 86 0 : break; 87 7 : if (iter_count >= iter_limit) 88 0 : break; 89 : } 90 : 91 : // All done if we've looked at all instances. 92 3 : if (it == it_end || ++it == it_end) 93 3 : 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 3 : } 101 : 102 : // 103 : // Specialization of BgpShowHandler<>::FillShowList. 104 : // 105 : template <> 106 3 : void BgpShowHandler<ShowBgpPeeringConfigReq, ShowBgpPeeringConfigReqIterate, 107 : ShowBgpPeeringConfigResp, ShowBgpPeeringConfig>::FillShowList( 108 : ShowBgpPeeringConfigResp *resp, 109 : const vector<ShowBgpPeeringConfig> &show_list) { 110 3 : resp->set_peerings(show_list); 111 3 : } 112 : 113 : // 114 : // Handler for ShowBgpPeeringConfigReq. 115 : // Called via BgpSandeshContext::PeeringShowReqHandler. 116 : // 117 3 : void ShowBgpIfmapPeeringConfigReqHandler(const BgpSandeshContext *bsc, 118 : const ShowBgpPeeringConfigReq *req) { 119 3 : RequestPipeline::PipeSpec ps(req); 120 3 : RequestPipeline::StageSpec s1; 121 3 : TaskScheduler *scheduler = TaskScheduler::GetInstance(); 122 : 123 3 : s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand"); 124 : s1.cbFn_ = boost::bind(&BgpShowHandler< 125 : ShowBgpPeeringConfigReq, 126 : ShowBgpPeeringConfigReqIterate, 127 : ShowBgpPeeringConfigResp, 128 3 : ShowBgpPeeringConfig>::Callback, _1, _2, _3, _4, _5); 129 : s1.allocFn_ = BgpShowHandler< 130 : ShowBgpPeeringConfigReq, 131 : ShowBgpPeeringConfigReqIterate, 132 : ShowBgpPeeringConfigResp, 133 3 : ShowBgpPeeringConfig>::CreateData; 134 3 : s1.instances_.push_back(0); 135 3 : ps.stages_.push_back(s1); 136 3 : RequestPipeline rp(ps); 137 3 : } 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 : }