Skip to content

Commit

Permalink
Introspect support for routing policy
Browse files Browse the repository at this point in the history
Enhanced UT cases to invoke routing-policy introspect

Change-Id: I6809c0cfe7ab3a093afd40e97ab1c364bbd025fe
Related-bug: #1500698
  • Loading branch information
bailkeri committed Jan 25, 2016
1 parent 73211b7 commit 793eeb5
Show file tree
Hide file tree
Showing 11 changed files with 591 additions and 23 deletions.
8 changes: 7 additions & 1 deletion src/bgp/bgp_peer.sandesh
Expand Up @@ -243,6 +243,11 @@ struct ShowRoutingInstanceTable {
15: u64 walkers;
}

struct ShowInstanceRoutingPolicyInfo {
1: string policy_name (link="ShowRoutingPolicyReq");
2: u32 generation;
}

struct ShowRoutingInstance {
1: string name (link="ShowRoutingInstanceReq"); // Routing instance name
3: string virtual_network; // Virtual network
Expand All @@ -254,6 +259,7 @@ struct ShowRoutingInstance {
7: bool deleted; // Deletion in progress
9: string deleted_at; // Delete timestamp
2: optional list<ShowRoutingInstanceTable> tables;
11: optional list<ShowInstanceRoutingPolicyInfo> routing_policies;
}

response sandesh ShowRoutingInstanceResp {
Expand Down Expand Up @@ -406,7 +412,7 @@ struct ShowBgpStaticRouteConfig {
}

struct ShowBgpInstanceRoutingPolicyConfig {
1: string policy_name;
1: string policy_name (link="ShowBgpRoutingPolicyConfigReq");
2: string sequence;
}

Expand Down
10 changes: 10 additions & 0 deletions src/bgp/bgp_show_routing_instance.cc
Expand Up @@ -9,6 +9,7 @@
#include "bgp/bgp_peer_internal_types.h"
#include "bgp/bgp_peer_membership.h"
#include "bgp/routing-instance/routing_instance.h"
#include "bgp/routing-policy/routing_policy.h"

using std::string;
using std::vector;
Expand Down Expand Up @@ -72,6 +73,15 @@ static void FillRoutingInstanceInfo(ShowRoutingInstance *sri,
srit_list.push_back(srit);
}
sri->set_tables(srit_list);
vector<ShowInstanceRoutingPolicyInfo> policy_list;
BOOST_FOREACH(RoutingPolicyInfo info, rtinstance->routing_policies()) {
ShowInstanceRoutingPolicyInfo show_policy_info;
RoutingPolicyPtr policy = info.first;
show_policy_info.set_policy_name(policy->name());
show_policy_info.set_generation(info.second);
policy_list.push_back(show_policy_info);
}
sri->set_routing_policies(policy_list);
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/bgp/routing-policy/SConscript
Expand Up @@ -12,8 +12,14 @@ env.Append(CPPPATH = [env['TOP'] + '/bgp'])
env.Append(CPPPATH = [env['TOP'] + '/db'])
env.Append(CPPPATH = [env['TOP'] + '/io'])

librouting_policy = env.Library('routing_policy',
# Generate the source files
SandeshGenFiles = env.SandeshGenCpp('routing_policy.sandesh')
SandeshGenFiles += env.SandeshGenOnlyCpp('routing_policy_internal.sandesh')
SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)

librouting_policy = env.Library('routing_policy', SandeshGenSrcs +
['routing_policy.cc',
'routing_policy_action.cc',
'routing_policy_match.cc'])
'routing_policy_match.cc',
'show_routing_policy.cc'])

1 change: 1 addition & 0 deletions src/bgp/routing-policy/routing_policy.h
Expand Up @@ -255,6 +255,7 @@ class RoutingPolicy {

PolicyResult operator()(const BgpRoute *route, BgpAttr *attr) const;
uint32_t generation() const { return generation_; }
uint32_t refcount() const { return refcount_; }

private:
friend class RoutingPolicyMgr;
Expand Down
30 changes: 30 additions & 0 deletions src/bgp/routing-policy/routing_policy.sandesh
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/

include "bgp/bgp_peer.sandesh"
include "bgp/routing-policy/routing_policy_internal.sandesh"

struct PolicyTermInfo {
1: bool terminal;
2: list<string> matches;
3: list<string> actions;
}

struct ShowRoutingPolicyInfo {
1: string name;
2: u32 generation;
3: u32 ref_count;
4: list<PolicyTermInfo> terms;
5: bool deleted;
}

response sandesh ShowRoutingPolicyResp {
1: list<ShowRoutingPolicyInfo> routing_policies;
2: optional string next_batch (link="ShowRoutingPolicyReqIterate",
link_title="next_batch");
}

request sandesh ShowRoutingPolicyReq {
1: string search_string;
}
31 changes: 24 additions & 7 deletions src/bgp/routing-policy/routing_policy_action.cc
Expand Up @@ -7,16 +7,20 @@
#include <boost/foreach.hpp>

#include <algorithm>
#include <sstream>

#include <bgp/bgp_attr.h>
#include <bgp/bgp_server.h>
#include <bgp/community.h>
#include <net/community_type.h>

using std::copy;
using std::ostringstream;
using std::string;

UpdateCommunity::UpdateCommunity(const std::vector<std::string> communities,
std::string op) {
BOOST_FOREACH(const std::string &community, communities) {
UpdateCommunity::UpdateCommunity(const std::vector<string> communities,
string op) {
BOOST_FOREACH(const string &community, communities) {
uint32_t value = CommunityType::CommunityFromString(community);
if (value) communities_.push_back(value);
}
Expand Down Expand Up @@ -53,8 +57,19 @@ void UpdateCommunity::operator()(BgpAttr *attr) const {
}
}

std::string UpdateCommunity::ToString() const {
return "Update Community";
string UpdateCommunity::ToString() const {
ostringstream oss;
if (op_ == SET) oss << "community set [ ";
else if (op_ == ADD) oss << "community add [ ";
else if (op_ == REMOVE) oss << "community remove [ ";

BOOST_FOREACH(uint32_t community, communities()) {
string name = CommunityType::CommunityToString(community);
oss << name << ",";
}
oss.seekp(-1, oss.cur);
oss << " ]";
return oss.str();
}

bool UpdateCommunity::IsEqual(const RoutingPolicyAction &community) const {
Expand All @@ -73,8 +88,10 @@ void UpdateLocalPref::operator()(BgpAttr *attr) const {
attr->set_local_pref(local_pref_);
}

std::string UpdateLocalPref::ToString() const {
return "Update LocalPref";
string UpdateLocalPref::ToString() const {
ostringstream oss;
oss << "local-pref " << local_pref_;
return oss.str();
}

bool UpdateLocalPref::IsEqual(const RoutingPolicyAction &local_pref) const {
Expand Down
6 changes: 6 additions & 0 deletions src/bgp/routing-policy/routing_policy_internal.sandesh
@@ -0,0 +1,6 @@
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/
request sandesh ShowRoutingPolicyReqIterate {
1: string iterate_info;
}
30 changes: 22 additions & 8 deletions src/bgp/routing-policy/routing_policy_match.cc
Expand Up @@ -7,12 +7,15 @@
#include <boost/foreach.hpp>

#include <algorithm>
#include <sstream>

#include <bgp/bgp_attr.h>
#include <net/community_type.h>

MatchCommunity::MatchCommunity(const std::vector<std::string> &communities) {
BOOST_FOREACH(const std::string &community, communities) {
using std::ostringstream;
using std::string;
MatchCommunity::MatchCommunity(const std::vector<string> &communities) {
BOOST_FOREACH(const string &community, communities) {
uint32_t value = CommunityType::CommunityFromString(community);
// Invalid community from config is ignored
if (value) {
Expand Down Expand Up @@ -43,8 +46,16 @@ bool MatchCommunity::Match(const BgpRoute *route,
return false;
}

std::string MatchCommunity::ToString() const {
return "Match community";
string MatchCommunity::ToString() const {
ostringstream oss;
oss << "community [ ";
BOOST_FOREACH(uint32_t community, communities()) {
string name = CommunityType::CommunityToString(community);
oss << name << ",";
}
oss.seekp(-1, oss.cur);
oss << " ]";
return oss.str();
}

bool MatchCommunity::IsEqual(const RoutingPolicyMatch &community) const {
Expand All @@ -54,8 +65,7 @@ bool MatchCommunity::IsEqual(const RoutingPolicyMatch &community) const {
}

template <typename T>
MatchPrefix<T>::MatchPrefix(const std::string &prefix,
const std::string &match_type) {
MatchPrefix<T>::MatchPrefix(const string &prefix, const string &match_type) {
boost::system::error_code ec;
match_prefix_ = PrefixT::FromString(prefix, &ec);
if (strcmp(match_type.c_str(), "exact") == 0) {
Expand Down Expand Up @@ -98,8 +108,12 @@ bool MatchPrefix<T>::IsEqual(const RoutingPolicyMatch &prefix) const {
}

template <typename T>
std::string MatchPrefix<T>::ToString() const {
return "Match Prefix";
string MatchPrefix<T>::ToString() const {
ostringstream oss;
oss << "prefix " << match_prefix_.ToString();
if (match_type_ == LONGER) oss << " longer";
else if (match_type_ == ORLONGER) oss << " orlonger";
return oss.str();
}

template class MatchPrefix<InetPrefixMatch>;
Expand Down
154 changes: 154 additions & 0 deletions src/bgp/routing-policy/show_routing_policy.cc
@@ -0,0 +1,154 @@
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/

#include "bgp/bgp_show_handler.h"

#include <boost/foreach.hpp>

#include "bgp/bgp_server.h"
#include "bgp/bgp_show_handler.h"
#include "bgp/routing-policy/routing_policy.h"
#include "bgp/routing-policy/routing_policy_action.h"
#include "bgp/routing-policy/routing_policy_match.h"
#include "bgp/routing-policy/routing_policy_types.h"

using std::string;
using std::vector;

//
// Fill in information for a policy.
//
static void FillRoutingPolicyInfo(ShowRoutingPolicyInfo *srpi,
const BgpSandeshContext *bsc, const RoutingPolicy *policy,
bool summary) {
srpi->set_name(policy->name());
srpi->set_generation(policy->generation());
srpi->set_ref_count(policy->refcount());
srpi->set_deleted(policy->deleted());
vector<PolicyTermInfo> term_list;
BOOST_FOREACH(RoutingPolicy::PolicyTermPtr term, policy->terms()) {
PolicyTermInfo show_term;
show_term.set_terminal(term->terminal());
vector<string> match_list;
BOOST_FOREACH(RoutingPolicyMatch *match, term->matches()) {
match_list.push_back(match->ToString());
}
show_term.set_matches(match_list);
vector<string> action_list;
BOOST_FOREACH(RoutingPolicyAction *action, term->actions()) {
action_list.push_back(action->ToString());
}
show_term.set_actions(action_list);
term_list.push_back(show_term);
}
srpi->set_terms(term_list);
}

//
// Fill in information for list of policies.
//
static bool FillRoutingPolicyInfoList(const BgpSandeshContext *bsc,
bool summary, uint32_t page_limit, uint32_t iter_limit,
const string &start_policy, const string &search_string,
vector<ShowRoutingPolicyInfo> *srpi_list, string *next_policy) {
RoutingPolicyMgr *rpm = bsc->bgp_server->routing_policy_mgr();
RoutingPolicyMgr::const_name_iterator it =
rpm->name_clower_bound(start_policy);
for (uint32_t iter_count = 0; it != rpm->name_cend(); ++it, ++iter_count) {
const RoutingPolicy *policy = it->second;
if (!search_string.empty() &&
(policy->name().find(search_string) == string::npos) &&
(search_string != "deleted" || !policy->deleted())) {
continue;
}
ShowRoutingPolicyInfo srpi;
FillRoutingPolicyInfo(&srpi, bsc, policy, summary);
srpi_list->push_back(srpi);
if (srpi_list->size() >= page_limit)
break;
if (iter_count >= iter_limit)
break;
}

// All done if we've looked at all policies.
if (it == rpm->name_cend() || ++it == rpm->name_end())
return true;

// Return true if we've reached the page limit, false if we've reached the
// iteration limit.
bool done = srpi_list->size() >= page_limit;
*next_policy = it->second->name();
return done;
}

// Specialization of BgpShowHandler<>::CallbackCommon.
template <>
bool BgpShowHandler<ShowRoutingPolicyReq, ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp, ShowRoutingPolicyInfo>::CallbackCommon(
const BgpSandeshContext *bsc, Data *data) {
uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
string next_policy;
bool done = FillRoutingPolicyInfoList(bsc, false, page_limit, iter_limit,
data->next_entry, data->search_string, &data->show_list,
&next_policy);
if (!next_policy.empty())
SaveContextToData(next_policy, done, data);
return done;
}

// Specialization of BgpShowHandler<>::FillShowList.
template <>
void BgpShowHandler<ShowRoutingPolicyReq, ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp, ShowRoutingPolicyInfo>::FillShowList(
ShowRoutingPolicyResp *resp,
const vector<ShowRoutingPolicyInfo> &show_list) {
resp->set_routing_policies(show_list);
}

// Handler for ShowRoutingPolicyReq.
void ShowRoutingPolicyReq::HandleRequest() const {
RequestPipeline::PipeSpec ps(this);
RequestPipeline::StageSpec s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();

s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
s1.cbFn_ = boost::bind(&BgpShowHandler<
ShowRoutingPolicyReq,
ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp,
ShowRoutingPolicyInfo>::Callback, _1, _2, _3, _4, _5);
s1.allocFn_ = BgpShowHandler<
ShowRoutingPolicyReq,
ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp,
ShowRoutingPolicyInfo>::CreateData;
s1.instances_.push_back(0);
ps.stages_.push_back(s1);
RequestPipeline rp(ps);
}

//
// Handler for ShowRoutingPolicyReqIterate.
//
void ShowRoutingPolicyReqIterate::HandleRequest() const {
RequestPipeline::PipeSpec ps(this);
RequestPipeline::StageSpec s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();

s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
s1.cbFn_ = boost::bind(&BgpShowHandler<
ShowRoutingPolicyReq,
ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp,
ShowRoutingPolicyInfo>::CallbackIterate, _1, _2, _3, _4, _5);
s1.allocFn_ = BgpShowHandler<
ShowRoutingPolicyReq,
ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp,
ShowRoutingPolicyInfo>::CreateData;
s1.instances_.push_back(0);
ps.stages_.push_back(s1);
RequestPipeline rp(ps);
}

0 comments on commit 793eeb5

Please sign in to comment.