Skip to content

Commit

Permalink
Templatize code to sync aggregate config and oper state
Browse files Browse the repository at this point in the history
Change-Id: Id7035f87aad9c3317cb6b2dd94653660be64fbac
Partial-Bug: 1500698
  • Loading branch information
Nischal Sheth committed Jan 26, 2016
1 parent 7334e08 commit 6321725
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 37 deletions.
45 changes: 45 additions & 0 deletions src/base/map_util.h
Expand Up @@ -42,6 +42,51 @@ void map_difference(ForwardIterator __first1, ForwardIterator __last1,
}
}

//
// map_difference
//
// Given a map and the begin/end iterators for a sorted std container and a
// compare functor, invoke add, delete or equal functors for values that are:
// add - present in the container but not in the map;
// delete - present in the map but not in the container;
// equal - present in both the container and the map;
//
template <typename MapType,
typename ForwardIterator,
typename CompFunctor,
typename AddFunctor,
typename DelFunctor,
typename EqFunctor>
void map_difference(MapType *map, ForwardIterator first, ForwardIterator last,
CompFunctor comp_fn, AddFunctor add_fn,
DelFunctor del_fn, EqFunctor eq_fn) {
typename MapType::iterator it1 = map->begin(), next1 = map->begin();
ForwardIterator it2 = first;
while (it1 != map->end() && it2 != last) {
int result = comp_fn(it1, it2);
if (result < 0) {
++next1;
del_fn(it1);
it1 = next1;
} else if (result > 0) {
add_fn(it2);
++it2;
} else {
eq_fn(it1, it2);
++it1;
++it2;
}
next1 = it1;
}
for (next1 = it1; it1 != map->end(); it1 = next1) {
++next1;
del_fn(it1);
}
for (; it2 != last; ++it2) {
add_fn(it2);
}
}

//
// map_synchronize
//
Expand Down
77 changes: 40 additions & 37 deletions src/bgp/routing-instance/route_aggregate.cc
Expand Up @@ -11,6 +11,7 @@
#include <vector>

#include "base/lifetime.h"
#include "base/map_util.h"
#include "base/task_annotations.h"
#include "bgp/routing-instance/path_resolver.h"
#include "bgp/routing-instance/routing_instance.h"
Expand Down Expand Up @@ -534,9 +535,9 @@ RouteAggregator<T>::~RouteAggregator() {
template <typename T>
void RouteAggregator<T>::ProcessAggregateRouteConfig() {
CHECK_CONCURRENCY("bgp::Config");
const BgpInstanceConfig::AggregateRouteList &list =
const AggregateRouteConfigList &list =
routing_instance()->config()->aggregate_routes(GetFamily());
typedef BgpInstanceConfig::AggregateRouteList::const_iterator iterator_t;
typedef AggregateRouteConfigList::const_iterator iterator_t;
for (iterator_t iter = list.begin(); iter != list.end(); ++iter) {
LocateAggregateRoutePrefix(*iter);
}
Expand All @@ -552,43 +553,16 @@ bool CompareAggregateRouteConfig(const AggregateRouteConfig &lhs,
template <typename T>
void RouteAggregator<T>::UpdateAggregateRouteConfig() {
CHECK_CONCURRENCY("bgp::Config");
typedef BgpInstanceConfig::AggregateRouteList AggregateRouteList;
AggregateRouteList aggregate_route_list =
AggregateRouteConfigList config_list =
routing_instance()->config()->aggregate_routes(GetFamily());
sort(config_list.begin(), config_list.end(), CompareAggregateRouteConfig);

sort(aggregate_route_list.begin(), aggregate_route_list.end(),
CompareAggregateRouteConfig);

// TODO(prakashmb): templatize the sync operation
AggregateRouteList::const_iterator aggregate_route_cfg_it =
aggregate_route_list.begin();
typename AggregateRouteMap::iterator oper_it = aggregate_route_map_.begin();

while ((aggregate_route_cfg_it != aggregate_route_list.end()) &&
(oper_it != aggregate_route_map_.end())) {
AddressT address = this->GetAddress(aggregate_route_cfg_it->aggregate);
PrefixT aggregate_route_prefix(address,
aggregate_route_cfg_it->prefix_length);
if (aggregate_route_prefix < oper_it->first) {
LocateAggregateRoutePrefix(*aggregate_route_cfg_it);
aggregate_route_cfg_it++;
} else if (aggregate_route_prefix > oper_it->first) {
RemoveAggregateRoutePrefix(oper_it->first);
oper_it++;
} else {
LocateAggregateRoutePrefix(*aggregate_route_cfg_it);
aggregate_route_cfg_it++;
oper_it++;
}
}

for (; oper_it != aggregate_route_map_.end(); oper_it++) {
RemoveAggregateRoutePrefix(oper_it->first);
}
for (; aggregate_route_cfg_it != aggregate_route_list.end();
aggregate_route_cfg_it++) {
LocateAggregateRoutePrefix(*aggregate_route_cfg_it);
}
map_difference(&aggregate_route_map_,
config_list.begin(), config_list.end(),
boost::bind(&RouteAggregator<T>::CompareAggregateRoute, this, _1, _2),
boost::bind(&RouteAggregator<T>::AddAggregateRoute, this, _1),
boost::bind(&RouteAggregator<T>::DelAggregateRoute, this, _1),
boost::bind(&RouteAggregator<T>::UpdateAggregateRoute, this, _1, _2));
}

template <typename T>
Expand Down Expand Up @@ -715,6 +689,35 @@ bool RouteAggregator<T>::FillAggregateRouteInfo(RoutingInstance *ri,
return true;
}

template <typename T>
int RouteAggregator<T>::CompareAggregateRoute(
typename AggregateRouteMap::iterator loc,
AggregateRouteConfigList::iterator it) {
AddressT address = this->GetAddress(it->aggregate);
PrefixT prefix(address, it->prefix_length);
KEY_COMPARE(loc->first, prefix);
return 0;
}

template <typename T>
void RouteAggregator<T>::AddAggregateRoute(
AggregateRouteConfigList::iterator it) {
LocateAggregateRoutePrefix(*it);
}

template <typename T>
void RouteAggregator<T>::DelAggregateRoute(
typename AggregateRouteMap::iterator loc) {
RemoveAggregateRoutePrefix(loc->first);
}

template <typename T>
void RouteAggregator<T>::UpdateAggregateRoute(
typename AggregateRouteMap::iterator loc,
AggregateRouteConfigList::iterator it) {
LocateAggregateRoutePrefix(*it);
}

template <typename T>
void RouteAggregator<T>::LocateAggregateRoutePrefix(const AggregateRouteConfig
&cfg) {
Expand Down
9 changes: 9 additions & 0 deletions src/bgp/routing-instance/route_aggregate.h
Expand Up @@ -13,6 +13,7 @@
#include "bgp/routing-instance/iroute_aggregator.h"

#include "bgp/bgp_condition_listener.h"
#include "bgp/bgp_config.h"
#include "bgp/inet/inet_route.h"
#include "bgp/inet/inet_table.h"
#include "bgp/inet6/inet6_route.h"
Expand Down Expand Up @@ -205,6 +206,14 @@ class RouteAggregator : public IRouteAggregator {
friend class RouteAggregationTest;
class DeleteActor;
typedef std::set<AggregateRoutePtr> AggregateRouteProcessList;
typedef BgpInstanceConfig::AggregateRouteList AggregateRouteConfigList;

int CompareAggregateRoute(typename AggregateRouteMap::iterator loc,
AggregateRouteConfigList::iterator it);
void AddAggregateRoute(AggregateRouteConfigList::iterator it);
void DelAggregateRoute(typename AggregateRouteMap::iterator loc);
void UpdateAggregateRoute(typename AggregateRouteMap::iterator loc,
AggregateRouteConfigList::iterator it);

void LocateAggregateRoutePrefix(const AggregateRouteConfig &cfg);
void RemoveAggregateRoutePrefix(const PrefixT &static_route);
Expand Down

0 comments on commit 6321725

Please sign in to comment.