Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "ifmap/ifmap_dependency_tracker.h" 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 : 18 8198 : IFMapDependencyTracker::IFMapDependencyTracker( 19 8198 : DB *db, DBGraph *graph, ChangeObserver observer) 20 8198 : : database_(db), graph_(graph), observer_(observer) { 21 8198 : } 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 196790 : void IFMapDependencyTracker::NodeEvent(IFMapNode *node, bool add_node_event) { 31 196790 : if (add_node_event) 32 191077 : AddChangeEvent(node); 33 196790 : if (IsInterestingEvent(node, "self")) { 34 145613 : node_list_.push_back( 35 291226 : make_pair(node->table()->Typename(), node->name())); 36 : } 37 196790 : } 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 : // 46 191077 : void IFMapDependencyTracker::NodeEvent(IFMapNode *node) { 47 191077 : NodeEvent(node, true); 48 191077 : } 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 253169 : bool IFMapDependencyTracker::LinkEvent(const string metadata, 57 : IFMapNode *left, IFMapNode *right) { 58 253169 : bool interest = false; 59 : 60 253169 : if ((left != NULL) && IsInterestingEvent(left, metadata)) { 61 109400 : const char *type_left = left->table()->Typename(); 62 109400 : edge_list_.push_back( 63 218800 : EdgeDescriptor(metadata, type_left, left->name())); 64 109400 : interest = true; 65 : } 66 253169 : if ((right != NULL) && IsInterestingEvent(right, metadata)) { 67 77693 : const char *type_right = right->table()->Typename(); 68 77693 : edge_list_.push_back( 69 155386 : EdgeDescriptor(metadata, type_right, right->name())); 70 77693 : interest = true; 71 : } 72 : 73 253169 : 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 : // 81 70037 : void IFMapDependencyTracker::PropagateChanges() { 82 70037 : InEdgeSet in_edges; 83 : 84 70037 : for (NodeList::iterator iter = node_list_.begin(); 85 215650 : iter != node_list_.end(); ++iter) { 86 : IFMapTable *table = 87 145613 : IFMapTable::FindTable(database_, iter->first); 88 145613 : if (table == NULL) { 89 0 : continue; 90 : } 91 145613 : IFMapNode *node = table->FindNode(iter->second); 92 145613 : if ((node == NULL) || node->IsDeleted()) { 93 4 : continue; 94 : } 95 145609 : PropagateNode(node, &in_edges); 96 : } 97 : 98 70037 : for (EdgeDescriptorList::iterator iter = edge_list_.begin(); 99 257130 : iter != edge_list_.end(); ++iter) { 100 187093 : const EdgeDescriptor &edge = *iter; 101 : IFMapTable *table = 102 187093 : IFMapTable::FindTable(database_, edge.id_type); 103 187093 : if (table == NULL) { 104 0 : continue; 105 : } 106 187093 : IFMapNode *node = table->FindNode(edge.id_name); 107 187093 : if ((node == NULL) || node->IsDeleted()) { 108 3 : continue; 109 : } 110 187090 : PropagateEdge(node, edge.metadata, &in_edges); 111 : } 112 70037 : } 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 : // 119 70037 : void IFMapDependencyTracker::Clear() { 120 70037 : node_list_.clear(); 121 70037 : edge_list_.clear(); 122 70037 : } 123 : 124 : // 125 : // Get the PropagateList for the given identifier type and metadata. 126 : // 127 : const IFMapDependencyTracker::PropagateList * 128 1061273 : IFMapDependencyTracker::GetPropagateList( 129 : const string &type, const string &metadata) const { 130 : 131 1061273 : NodeEventPolicy::const_iterator ploc = policy_.find(type); 132 1061273 : if (ploc == policy_.end()) { 133 159330 : return NULL; 134 : } 135 901943 : ReactionMap::const_iterator rloc = ploc->second.find(metadata); 136 901943 : if (rloc == ploc->second.end()) { 137 67696 : return NULL; 138 : } 139 834247 : 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 : // 147 662765 : bool IFMapDependencyTracker::IsInterestingEvent( 148 : const IFMapNode *node, const string &metadata) const { 149 662765 : if (node->IsDeleted()) { 150 103033 : return false; 151 : } 152 559732 : 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 : // 160 145609 : void IFMapDependencyTracker::PropagateNode( 161 : IFMapNode *node, InEdgeSet *in_edges) { 162 : 163 : const PropagateList *plist = 164 145609 : GetPropagateList(node->table()->Typename(), "self"); 165 145609 : 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 145609 : for (DBGraphVertex::edge_iterator iter = 170 145609 : node->edge_list_begin(graph_); 171 356098 : iter != node->edge_list_end(graph_); ++iter) { 172 210489 : IFMapLink *link = static_cast<IFMapLink *>(iter.operator->()); 173 210489 : IFMapNode *target = static_cast<IFMapNode *>(iter.target()); 174 210489 : if (plist->find(link->metadata()) == plist->end()) { 175 118190 : continue; 176 : } 177 92299 : PropagateEdge(target, link->metadata(), in_edges); 178 : } 179 145609 : } 180 : 181 : // 182 : // Propagate changes for an edge on the EdgeDescriptorList. 183 : // 184 355932 : void IFMapDependencyTracker::PropagateEdge( 185 : IFMapNode *node, const string &metadata, InEdgeSet *in_edges) { 186 355932 : 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 355932 : GetPropagateList(node->table()->Typename(), metadata); 192 355932 : assert(plist); 193 : 194 : // Skip if this edge in already in the InEdgeSet i.e. it's a duplicate. 195 355932 : if (in_edges->count(make_pair(node, metadata)) > 0) { 196 142220 : return; 197 : } 198 : 199 : // Add entry to InEdgeSet for loop prevention. 200 213712 : 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 213712 : PropagateList::const_iterator self = plist->find("self"); 205 213712 : if (self != plist->end()) { 206 142469 : 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 213712 : for (DBGraphVertex::edge_iterator iter = 212 213712 : node->edge_list_begin(graph_); 213 1092550 : iter != node->edge_list_end(graph_); ++iter) { 214 878838 : IFMapLink *link = static_cast<IFMapLink *>(iter.operator->()); 215 878838 : if (plist->find(link->metadata()) == plist->end()) { 216 802295 : continue; 217 : } 218 76543 : IFMapNode *target = static_cast<IFMapNode *>(iter.target()); 219 76543 : PropagateEdge(target, link->metadata(), in_edges); 220 : } 221 : } 222 : 223 : // 224 : // Add the IFMapNode to the change list. 225 : // 226 333546 : void IFMapDependencyTracker::AddChangeEvent(IFMapNode *node) { 227 333546 : observer_(node); 228 333546 : } 229 :