OpenSDN source code
ifmap_dependency_tracker.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 #include <sstream>
9 #include <boost/assign/list_of.hpp>
10 
11 #include "ifmap/ifmap_link.h"
12 #include "ifmap/ifmap_node.h"
13 #include "ifmap/ifmap_table.h"
14 
15 using namespace boost::assign;
16 using namespace std;
17 
19  DB *db, DBGraph *graph, ChangeObserver observer)
20  : database_(db), graph_(graph), observer_(observer) {
21 }
22 
23 //
24 // Add an IFMapNode to the NodeList for further propagation if it's an
25 // interesting node. It's interesting if there's an entry for self in the
26 // ReactionMap.
27 //
28 // Node event is added based on "add_node_event" flag
29 //
30 void IFMapDependencyTracker::NodeEvent(IFMapNode *node, bool add_node_event) {
31  if (add_node_event)
32  AddChangeEvent(node);
33  if (IsInterestingEvent(node, "self")) {
34  node_list_.push_back(
35  make_pair(node->table()->Typename(), node->name()));
36  }
37 }
38 
39 //
40 // Add an IFMapNode to the NodeList for further propagation if it's an
41 // interesting node. It's interesting if there's an entry for self in the
42 // ReactionMap.
43 //
44 // The node is always added to the ChangeList even if it's not interesting.
45 //
47  NodeEvent(node, true);
48 }
49 
50 //
51 // Check both the edges corresponding to the IFMapLink and add them to the
52 // EdgeDescriptorList if interesting. An edge is considered interesting if
53 // there's an entry for the metadata in the ReactionMap for the IFMapNode's
54 // identifier type.
55 //
56 bool IFMapDependencyTracker::LinkEvent(const string metadata,
57  IFMapNode *left, IFMapNode *right) {
58  bool interest = false;
59 
60  if ((left != NULL) && IsInterestingEvent(left, metadata)) {
61  const char *type_left = left->table()->Typename();
62  edge_list_.push_back(
63  EdgeDescriptor(metadata, type_left, left->name()));
64  interest = true;
65  }
66  if ((right != NULL) && IsInterestingEvent(right, metadata)) {
67  const char *type_right = right->table()->Typename();
68  edge_list_.push_back(
69  EdgeDescriptor(metadata, type_right, right->name()));
70  interest = true;
71  }
72 
73  return interest;
74 }
75 
76 //
77 // Walk the NodeList and EdgeDescriptorList and evaluate the NodeEventPolicy
78 // to build up the change list. The InEdgeSet is used to avoid evaluating the
79 // same EdgeDescriptor more than once, hence avoiding any loops in the graph.
80 //
82  InEdgeSet in_edges;
83 
84  for (NodeList::iterator iter = node_list_.begin();
85  iter != node_list_.end(); ++iter) {
86  IFMapTable *table =
87  IFMapTable::FindTable(database_, iter->first);
88  if (table == NULL) {
89  continue;
90  }
91  IFMapNode *node = table->FindNode(iter->second);
92  if ((node == NULL) || node->IsDeleted()) {
93  continue;
94  }
95  PropagateNode(node, &in_edges);
96  }
97 
98  for (EdgeDescriptorList::iterator iter = edge_list_.begin();
99  iter != edge_list_.end(); ++iter) {
100  const EdgeDescriptor &edge = *iter;
101  IFMapTable *table =
103  if (table == NULL) {
104  continue;
105  }
106  IFMapNode *node = table->FindNode(edge.id_name);
107  if ((node == NULL) || node->IsDeleted()) {
108  continue;
109  }
110  PropagateEdge(node, edge.metadata, &in_edges);
111  }
112 }
113 
114 //
115 // Clear all intermediate state used during propagation. This is called after
116 // we're done propagating all accumulated node and edge triggers to the change
117 // list.
118 //
120  node_list_.clear();
121  edge_list_.clear();
122 }
123 
124 //
125 // Get the PropagateList for the given identifier type and metadata.
126 //
129  const string &type, const string &metadata) const {
130 
131  NodeEventPolicy::const_iterator ploc = policy_.find(type);
132  if (ploc == policy_.end()) {
133  return NULL;
134  }
135  ReactionMap::const_iterator rloc = ploc->second.find(metadata);
136  if (rloc == ploc->second.end()) {
137  return NULL;
138  }
139  return &rloc->second;
140 }
141 
142 //
143 // Determine if the event specified by the node and metadata is interesting.
144 // It's interesting if the NodeEventPolicy has non-empty propagate list for
145 // the event.
146 //
148  const IFMapNode *node, const string &metadata) const {
149  if (node->IsDeleted()) {
150  return false;
151  }
152  return GetPropagateList(node->table()->Typename(), metadata) != NULL;
153 }
154 
155 //
156 // Propagate changes for a IFMapNode on the NodeList. The fact that it's on
157 // the NodeList means that the node must have been deemed interesting and so
158 // it's PropagateList must be non-empty.
159 //
161  IFMapNode *node, InEdgeSet *in_edges) {
162 
163  const PropagateList *plist =
164  GetPropagateList(node->table()->Typename(), "self");
165  assert(plist);
166 
167  // Iterate through the edges of node. If the metadata for an edge is in
168  // the PropagateList, we need to propagate changes for the edge itself.
169  for (DBGraphVertex::edge_iterator iter =
170  node->edge_list_begin(graph_);
171  iter != node->edge_list_end(graph_); ++iter) {
172  IFMapLink *link = static_cast<IFMapLink *>(iter.operator->());
173  IFMapNode *target = static_cast<IFMapNode *>(iter.target());
174  if (plist->find(link->metadata()) == plist->end()) {
175  continue;
176  }
177  PropagateEdge(target, link->metadata(), in_edges);
178  }
179 }
180 
181 //
182 // Propagate changes for an edge on the EdgeDescriptorList.
183 //
185  IFMapNode *node, const string &metadata, InEdgeSet *in_edges) {
186  assert(!node->IsDeleted());
187 
188  // Make a bidirectional check i.e. policy terms that apply to the two
189  // edges for a link must be symmetrical.
190  const PropagateList *plist =
191  GetPropagateList(node->table()->Typename(), metadata);
192  assert(plist);
193 
194  // Skip if this edge in already in the InEdgeSet i.e. it's a duplicate.
195  if (in_edges->count(make_pair(node, metadata)) > 0) {
196  return;
197  }
198 
199  // Add entry to InEdgeSet for loop prevention.
200  in_edges->insert(make_pair(node, metadata));
201 
202  // Add the node corresponding to this edge to the change list if there's
203  // an entry for self in the PropagateList.
204  PropagateList::const_iterator self = plist->find("self");
205  if (self != plist->end()) {
206  AddChangeEvent(node);
207  }
208 
209  // Iterate through the edges of node. If the metadata for an edge is in
210  // the PropagateList, we need to propagate changes for the edge itself.
211  for (DBGraphVertex::edge_iterator iter =
212  node->edge_list_begin(graph_);
213  iter != node->edge_list_end(graph_); ++iter) {
214  IFMapLink *link = static_cast<IFMapLink *>(iter.operator->());
215  if (plist->find(link->metadata()) == plist->end()) {
216  continue;
217  }
218  IFMapNode *target = static_cast<IFMapNode *>(iter.target());
219  PropagateEdge(target, link->metadata(), in_edges);
220  }
221 }
222 
223 //
224 // Add the IFMapNode to the change list.
225 //
227  observer_(node);
228 }
229 
bool IsDeleted() const
Definition: db_entry.h:49
edge_iterator edge_list_begin(DBGraph *graph)
edge_iterator edge_list_end(DBGraph *graph)
Definition: db.h:25
std::set< std::string > PropagateList
void NodeEvent(IFMapNode *node)
bool LinkEvent(const std::string metadata, IFMapNode *left, IFMapNode *right)
std::set< std::pair< IFMapNode *, std::string > > InEdgeSet
const PropagateList * GetPropagateList(const std::string &type, const std::string &metadata) const
boost::function< void(IFMapNode *node)> ChangeObserver
IFMapDependencyTracker(DB *db, DBGraph *graph, ChangeObserver observer)
void AddChangeEvent(IFMapNode *node)
bool IsInterestingEvent(const IFMapNode *node, const std::string &metadata) const
void PropagateEdge(IFMapNode *node, const std::string &metadata, InEdgeSet *in_edges)
void PropagateNode(IFMapNode *node, InEdgeSet *in_edges)
IFMapTable * table()
Definition: ifmap_node.h:30
const std::string & name() const
Definition: ifmap_node.h:49
static IFMapTable * FindTable(DB *db, const std::string &element_type)
Definition: ifmap_table.cc:40
virtual const char * Typename() const =0
IFMapNode * FindNode(const std::string &name)
Definition: ifmap_table.cc:24
uint8_t type
Definition: load_balance.h:2