OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ifmap_dependency_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef AGENT_OPER_IFMAP_DEPENDENCY_MANAGER_H__
6 #define AGENT_OPER_IFMAP_DEPENDENCY_MANAGER_H__
7 
8 #include <map>
9 #include <boost/function.hpp>
10 #include <boost/intrusive_ptr.hpp>
11 #include <boost/uuid/uuid.hpp>
12 #include <boost/uuid/nil_generator.hpp>
13 
14 #include "db/db_entry.h"
15 #include "db/db_table.h"
17 
18 class Agent;
19 class DB;
20 class DBGraph;
22 class IFMapNode;
23 class TaskTrigger;
25 class IFMapDependencyManagerTest;
26 
27 //IFMapNodeState is a DBState for IFMapNode with listener ID
28 // of IFMapDependency Manager. DBState is created when the first
29 // time SetState is invoked. The caller of SetState gets intrusive
30 // pointer to IFMapNodeState enabling the caller hold IFMapNode
31 // till reference is removed. Caller does not need to invoke
32 // clear state as it gets automatically cleared when references are
33 // removed. Optionally a DBEntry can be added to this IFMapNodeState
34 // to trigger the IFMapDependency tracker.
35 // IFMapDependency tracker also uses the same state, to ensure that
36 // across the traversal, IFMapNode is not removed.
37 class IFMapNodeState : public DBState {
38  public:
40  : manager_(manager), node_(node), object_(NULL),
41  uuid_(boost::uuids::nil_uuid()), refcount_(0),
42  notify_(true), oper_db_request_enqueued_(false) {
43  }
44 
45  IFMapNode *node() { return node_; }
46  DBEntry *object() { return object_; }
47  void set_object(DBEntry *object) {
48  object_ = object;
49  }
50 
51  void set_uuid(const boost::uuids::uuid &u) {
52  uuid_ = u;
53  }
54 
55  void set_notify(bool flag) {
56  notify_ = flag;
57  }
58 
61  }
62 
63  bool notify() { return notify_;};
64 
66 
67  void clear_object() {
68  object_ = NULL;
69  }
70 
71  bool oper_db_request_enqueued() const {
73  }
74 
75  private:
76  friend void intrusive_ptr_add_ref(IFMapNodeState *state);
77  friend void intrusive_ptr_release(IFMapNodeState *state);
78 
83  int refcount_;
84  bool notify_;
86 };
87 
88 
90 public:
91  typedef boost::intrusive_ptr<IFMapNodeState> IFMapNodePtr;
92  typedef boost::function<void(IFMapNode *, DBEntry *)> ChangeEventHandler;
93 
94  struct Link {
95  Link(const std::string &edge, const std::string &vertex, bool interest):
96  edge_(edge), vertex_(vertex), vertex_interest_(interest) {
97  }
98  std::string edge_;
99  std::string vertex_;
101  };
102  typedef std::vector<Link> Path;
103 
104  IFMapDependencyManager(DB *database, DBGraph *graph);
105  virtual ~IFMapDependencyManager();
106 
107  /*
108  * Initialize must be called after the ifmap tables are registered
109  * via <schema>_Agent_ModuleInit.
110  */
111  void Initialize(Agent *agent);
112 
113  /*
114  * Unregister from all tables.
115  */
116  void Terminate();
117 
118  void AddDependencyPath(const std::string &node, Path path);
119  void InitializeDependencyRules(Agent *agent);
120  /*
121  * Register reactor-map for an IFMap node
122  */
123  void RegisterReactionMap(const char *node_name,
125  /*
126  * Associate an IFMapNode with an object in the operational database.
127  */
128  void SetObject(IFMapNode *node, DBEntry *entry);
129 
130  /*
131  * Add DBState to an IFMapNode
132  */
134  void SetNotify(IFMapNode *node, bool notfiy_flag);
135  void SetRequestEnqueued(IFMapNode *node, bool oper_db_request_enqueued);
137 
138  /*
139  * Get DBEntry object set for an IFMapNode
140  */
141  DBEntry *GetObject(IFMapNode *node);
142 
143  /*
144  * Register a notification callback.
145  */
146  void Register(const std::string &type, ChangeEventHandler handler);
147 
148  /*
149  * Unregister a notification callback.
150  */
151  void Unregister(const std::string &type);
152 
153  IFMapDependencyTracker *tracker() const { return tracker_.get(); }
154  void PropogateNodeChange(IFMapNode *node);
156 
157  // Check if a IFMapNode type is registerd with dependency manager
158  bool IsRegistered(const IFMapNode *node);
159  bool IsNodeIdentifiedByUuid(const IFMapNode *node);
160 
161  //Routines used by unit-test
162  void enable_trigger() {trigger_->set_enable();}
163  void disable_trigger() {trigger_->set_disable();}
164 
165 private:
166  /*
167  * IFMapNodeState (DBState) should exist:
168  * a) if the object is set
169  * b) if the entry is on the change list.
170  */
171  friend void intrusive_ptr_add_ref(IFMapNodeState *state);
172  friend void intrusive_ptr_release(IFMapNodeState *state);
173 
174  typedef std::vector<IFMapNodePtr> ChangeList;
175  typedef std::map<std::string, DBTable::ListenerId> TableMap;
176  typedef std::map<std::string, ChangeEventHandler> EventMap;
177 
178  bool ProcessChangeList();
179 
180  void NodeObserver(DBTablePartBase *root, DBEntryBase *db_entry);
181  void LinkObserver(DBTablePartBase *root, DBEntryBase *db_entry);
182  void ChangeListAdd(IFMapNode *node);
183 
184  void IFMapNodeReset(IFMapNode *node);
185 
189  std::unique_ptr<IFMapDependencyTracker> tracker_;
190  std::unique_ptr<TaskTrigger> trigger_;
194 };
195 
196 #endif
boost::uuids::uuid uuid()
bool IsRegistered(const IFMapNode *node)
void NodeObserver(DBTablePartBase *root, DBEntryBase *db_entry)
void Unregister(const std::string &type)
void PropogateNodeChange(IFMapNode *node)
void SetObject(IFMapNode *node, DBEntry *entry)
void LinkObserver(DBTablePartBase *root, DBEntryBase *db_entry)
IFMapNodeState * IFMapNodeGet(IFMapNode *node)
void set_uuid(const boost::uuids::uuid &u)
void Register(const std::string &type, ChangeEventHandler handler)
bool oper_db_request_enqueued() const
void InitializeDependencyRules(Agent *agent)
friend void intrusive_ptr_add_ref(IFMapNodeState *state)
void IFMapNodeReset(IFMapNode *node)
boost::uuids::uuid uuid
void RegisterReactionMap(const char *node_name, const IFMapDependencyTracker::ReactionMap &react)
friend void intrusive_ptr_release(IFMapNodeState *state)
void ChangeListAdd(IFMapNode *node)
IFMapDependencyManager(DB *database, DBGraph *graph)
Definition: db.h:24
void AddDependencyPath(const std::string &node, Path path)
std::unique_ptr< IFMapDependencyTracker > tracker_
friend void intrusive_ptr_add_ref(IFMapNodeState *state)
boost::uuids::uuid uuid_
IFMapDependencyManager * manager_
uint8_t type
Definition: load_balance.h:109
Definition: agent.h:358
Definition: path.h:10
void set_object(DBEntry *object)
std::unique_ptr< TaskTrigger > trigger_
void SetRequestEnqueued(IFMapNode *node, bool oper_db_request_enqueued)
void set_notify(bool flag)
std::map< std::string, ChangeEventHandler > EventMap
bool IsNodeIdentifiedByUuid(const IFMapNode *node)
IFMapNodePtr SetState(IFMapNode *node)
friend void intrusive_ptr_release(IFMapNodeState *state)
std::vector< IFMapNodePtr > ChangeList
std::map< std::string, DBTable::ListenerId > TableMap
boost::intrusive_ptr< IFMapNodeState > IFMapNodePtr
DBEntry * GetObject(IFMapNode *node)
void PropogateNodeAndLinkChange(IFMapNode *node)
void SetNotify(IFMapNode *node, bool notfiy_flag)
void set_oper_db_request_enqueued(bool oper_db_request_enqueued)
std::map< std::string, PropagateList > ReactionMap
IFMapNodeState(IFMapDependencyManager *manager, IFMapNode *node)
boost::function< void(IFMapNode *, DBEntry *)> ChangeEventHandler
IFMapDependencyTracker * tracker() const