OpenSDN source code
ovsdb_object.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 extern "C" {
7 #include <ovsdb_wrapper.h>
8 };
9 
10 #include <ovsdb_entry.h>
11 #include <ovsdb_object.h>
12 
13 using OVSDB::OvsdbObject;
16 
17 OvsdbObject::OvsdbObject(OvsdbClientIdl *idl) : KSyncObject("OvsdbDBObject"),
18  client_idl_(idl) {
19 }
20 
22 }
23 
25  KSyncEntry *entry = Find(key);
26  if (entry != NULL && entry->IsActive()) {
27  return entry;
28  }
29  return NULL;
30 }
31 
33  client_idl_->ksync_obj_manager()->Delete(this);
34  // trigger DeleteTableDone for derived class to take action on
35  // delete table callback
37 }
38 
40  if (delete_scheduled()) {
41  client_idl_ = NULL;
42  }
43 }
44 
46  bool init_stale_entry_cleanup) :
47  KSyncDBObject("OvsdbDBObject"),
48  client_idl_(idl), delete_triggered_(false) {
49  if (init_stale_entry_cleanup) {
50  InitStaleEntryCleanup(*(idl->agent()->event_manager())->io_service(),
53  }
54 }
55 
57  bool init_stale_entry_cleanup) :
58  KSyncDBObject("OvsdbDBObject", tbl), client_idl_(idl),
59  delete_triggered_(false) {
60  // Start a walker to get the entries which were already present,
61  // when we register to the DB Table
62  walk_ref_ = tbl->AllocWalker(
63  boost::bind(&OvsdbDBObject::DBWalkNotify, this, _1, _2),
64  boost::bind(&OvsdbDBObject::DBWalkDone, this, _2));
65  tbl->WalkAgain(walk_ref_);
66  if (init_stale_entry_cleanup) {
67  InitStaleEntryCleanup(*(idl->agent()->event_manager())->io_service(),
70  }
71 }
72 
74  assert(walk_ref_ == NULL);
75 }
76 
78  RegisterDb(tbl);
79  // Start a walker to get the entries which were already present,
80  // when we register to the DB Table
81  if (walk_ref_.get() == NULL) {
82  walk_ref_ = tbl->AllocWalker(
83  boost::bind(&OvsdbDBObject::DBWalkNotify, this, _1, _2),
84  boost::bind(&OvsdbDBObject::DBWalkDone, this, _2));
85  }
86  tbl->WalkAgain(walk_ref_);
87 }
88 
90  (static_cast<DBTable*>(GetDBTable()))->WalkAgain(walk_ref_);
91 }
92 
93 void OvsdbDBObject::NotifyAddOvsdb(OvsdbDBEntry *key, struct ovsdb_idl_row *row) {
94  OvsdbDBEntry *entry = static_cast<OvsdbDBEntry *>(Find(key));
95  // Check if entry is present and active. in case if we find an entry
96  // which is inactive, we will need to alloc ovs entry and trigger
97  // delete to update ovsdb-server
98  if (entry && entry->IsActive()) {
99  // trigger notify add for the entry, to update ovs_idl state
100  entry->NotifyAdd(row);
101  } else {
102  OvsdbDBEntry *del_entry = AllocOvsEntry(row);
103  // trigger notify add for the entry, to update ovs_idl state
104  del_entry->NotifyAdd(row);
105  // entry created by AllocOvsEntry should always be stale
106  assert(del_entry->stale());
107  // if delete of table is already triggered delete the created
108  // stale entry immediately
109  if (delete_triggered_) {
110  Delete(del_entry);
111  }
112  }
113 }
114 
116  struct ovsdb_idl_row *row) {
117  OvsdbDBEntry *entry = static_cast<OvsdbDBEntry *>(Find(key));
118  if (entry) {
119  // trigger notify delete for the entry, to reset ovs_idl state
120  entry->NotifyDelete(row);
121  if (!entry->IsDelAckWaiting()) {
122  // we were not waiting for delete to happen, state for
123  // OVSDB server and client mismatch happend.
124  // Trigger Add/Change Req on entry to resync
126  }
127  }
128 }
129 
131  Notify(part, entry);
132  return true;
133 }
134 
136 }
137 
139  return client_idl_->agent();
140 }
141 
143  // validate delete should not be triggered twice for an object
144  assert(!delete_triggered_);
145  delete_triggered_ = true;
146  if (client_idl_ != NULL) {
147  client_idl_->ksync_obj_manager()->Delete(this);
148  } else {
150  }
151  // trigger DeleteTableDone for derived class to take action on
152  // delete table callback
153  DeleteTableDone();
154 }
155 
157  if (delete_scheduled()) {
158  if (walk_ref_.get() != NULL)
159  (static_cast<DBTable*>(GetDBTable()))->ReleaseWalker(walk_ref_);
160  walk_ref_ = NULL;
161  client_idl_ = NULL;
162  }
163 }
164 
166  const DBEntry *entry, const OvsdbDBEntry *ovsdb_entry) {
167  // Accept by default, unless overriden by dereived class
168  return DBFilterAccept;
169 }
170 
172  const DBEntry *entry, const KSyncDBEntry *ksync) {
173  // Ignore Add/Change notifications while client idl is deleted
174  // there can be cases that the current table might be pending
175  // for delete schedule in KSyncObjectManager Queue and in
176  // the mean while we get a notification, where we should not
177  // process it further.
178  if (client_idl_ != NULL && client_idl_->deleted()) {
179  return DBFilterIgnore;
180  }
181 
182  return OvsdbDBEntryFilter(entry, static_cast<const OvsdbDBEntry *>(ksync));
183 }
184 
Definition: agent.h:361
EventManager * event_manager() const
Definition: agent.h:1106
void WalkAgain(DBTableWalkRef walk)
Definition: db_table.cc:658
DBTableWalkRef AllocWalker(WalkFn walk_fn, WalkCompleteFn walk_complete)
Definition: db_table.cc:640
DBTableBase * GetDBTable()
Definition: ksync_object.h:245
void Notify(DBTablePartBase *partition, DBEntryBase *entry)
void RegisterDb(DBTableBase *table)
bool IsActive()
Definition: ksync_entry.h:191
@ ADD_CHANGE_REQ
Definition: ksync_entry.h:53
bool stale() const
Definition: ksync_entry.h:180
static KSyncObjectManager * GetInstance()
void Delete(KSyncObject *)
bool delete_scheduled()
Definition: ksync_object.h:147
KSyncEntry * Find(const KSyncEntry *key)
void SafeNotifyEvent(KSyncEntry *entry, KSyncEntry::KSyncEvent event)
void InitStaleEntryCleanup(boost::asio::io_context &ios, uint32_t cleanup_time, uint32_t cleanup_intvl, uint16_t entries_per_intvl)
Definition: ksync_object.cc:82
void Delete(KSyncEntry *entry)
virtual void NotifyAdd(struct ovsdb_idl_row *)
Definition: ovsdb_entry.cc:180
virtual void NotifyDelete(struct ovsdb_idl_row *)
Definition: ovsdb_entry.cc:191
OvsdbClientIdlPtr client_idl_
Definition: ovsdb_object.h:84
void NotifyAddOvsdb(OvsdbDBEntry *key, struct ovsdb_idl_row *row)
Definition: ovsdb_object.cc:93
virtual void DeleteTableDone(void)
Definition: ovsdb_object.h:82
bool DBWalkNotify(DBTablePartBase *partition, DBEntryBase *entry)
static const uint16_t StaleEntryDeletePerIteration
Definition: ovsdb_object.h:48
static const uint32_t StaleEntryYeildTimer
Definition: ovsdb_object.h:47
void NotifyDeleteOvsdb(OvsdbDBEntry *key, struct ovsdb_idl_row *row)
OvsdbDBObject(OvsdbClientIdl *idl, bool init_stale_entry_cleanup)
Definition: ovsdb_object.cc:45
virtual ~OvsdbDBObject()
Definition: ovsdb_object.cc:73
static const uint32_t StaleEntryCleanupTimer
Definition: ovsdb_object.h:46
void DBWalkDone(DBTableBase *partition)
virtual DBFilterResp OvsdbDBEntryFilter(const DBEntry *entry, const OvsdbDBEntry *ovsdb_entry)
virtual void OvsdbRegisterDBTable(DBTable *tbl)
Definition: ovsdb_object.cc:77
DBTable::DBTableWalkRef walk_ref_
Definition: ovsdb_object.h:88
virtual OvsdbDBEntry * AllocOvsEntry(struct ovsdb_idl_row *row)
Definition: ovsdb_object.h:64
virtual void EmptyTable(void)
DBFilterResp DBEntryFilter(const DBEntry *entry, const KSyncDBEntry *ksync)
virtual Agent * agent() const
virtual void DeleteTableDone(void)
Definition: ovsdb_object.h:38
OvsdbClientIdlPtr client_idl_
Definition: ovsdb_object.h:35
virtual ~OvsdbObject()
Definition: ovsdb_object.cc:21
void DeleteTable(void)
Definition: ovsdb_object.cc:32
virtual void EmptyTable(void)
Definition: ovsdb_object.cc:39
KSyncEntry * FindActiveEntry(KSyncEntry *key)
Definition: ovsdb_object.cc:24