OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ovsdb_entry.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  */
4 extern "C" {
5 #include <ovsdb_wrapper.h>
6 }
7 #include <ovsdb_object.h>
8 #include <ovsdb_entry.h>
9 #include <ovsdb_types.h>
10 
11 using OVSDB::OvsdbEntry;
13 using OVSDB::OvsdbObject;
15 
16 OvsdbEntry::OvsdbEntry(OvsdbObject *table) : KSyncEntry(), table_(table),
17  ovs_entry_(NULL) {
18 }
19 
20 OvsdbEntry::OvsdbEntry(OvsdbObject *table, uint32_t index) : KSyncEntry(index),
21  table_(table), ovs_entry_(NULL) {
22 }
23 
25 }
26 
28  return true;
29 }
30 
32  return true;
33 }
34 
36  return true;
37 }
38 
40  return table_;
41 }
42 
43 void OvsdbEntry::Ack(bool success) {
44  // TODO we don't handle failures for these entries
45  if (!success) {
46  OVSDB_TRACE(Error, "Transaction failed for " + ToString());
47  }
48 
49  OvsdbObject *object = static_cast<OvsdbObject*>(GetObject());
50  object->SafeNotifyEvent(this, ack_event_);
51 }
52 
54  ovs_entry_(NULL) {
55 }
56 
57 OvsdbDBEntry::OvsdbDBEntry(OvsdbDBObject *table, struct ovsdb_idl_row *ovs_entry) : KSyncDBEntry(),
58  table_(table), ovs_entry_(ovs_entry) {
59 }
60 
62 }
63 
65  OvsdbDBObject *object = static_cast<OvsdbDBObject*>(GetObject());
66  // trigger pre add/change only if idl is not marked deleted.
67  // we should not update KSync references as, these references eventually
68  // need to be released as part of delete trigger due to cleanup.
69  if (object->client_idl_ != NULL && !object->client_idl_->deleted()) {
70  PreAddChange();
71  }
72 
73  if (IsNoTxnEntry()) {
74  // trigger AddMsg with NULL pointer and return true to complete
75  // KSync state
76  AddMsg(NULL);
77  return true;
78  }
79 
80  struct ovsdb_idl_txn *txn;
81  if (UseBulkTxn()) {
82  txn = object->client_idl_->CreateBulkTxn(this, KSyncEntry::ADD_ACK);
83  } else {
84  txn = object->client_idl_->CreateTxn(this, KSyncEntry::ADD_ACK);
85  }
86 
87  if (txn == NULL) {
88  // failed to create transaction because of idl marked for
89  // deletion return from here.
91  return true;
92  }
93  AddMsg(txn);
94  return object->client_idl_->EncodeSendTxn(txn, this);
95 }
96 
98  OvsdbDBObject *object = static_cast<OvsdbDBObject*>(GetObject());
99  // trigger pre add/change only if idl is not marked deleted.
100  // we should not update KSync references as, these references eventually
101  // need to be released as part of delete trigger due to cleanup.
102  if (object->client_idl_ != NULL && !object->client_idl_->deleted()) {
103  PreAddChange();
104  }
105 
106  if (IsNoTxnEntry()) {
107  // trigger ChangeMsg with NULL pointer and return true to complete
108  // KSync state
109  ChangeMsg(NULL);
110  return true;
111  }
112 
113  struct ovsdb_idl_txn *txn;
114  if (UseBulkTxn()) {
115  txn = object->client_idl_->CreateBulkTxn(this, KSyncEntry::CHANGE_ACK);
116  } else {
117  txn = object->client_idl_->CreateTxn(this, KSyncEntry::CHANGE_ACK);
118  }
119 
120  if (txn == NULL) {
121  // failed to create transaction because of idl marked for
122  // deletion return from here.
124  return true;
125  }
126  ChangeMsg(txn);
127  return object->client_idl_->EncodeSendTxn(txn, this);
128 }
129 
131  if (IsNoTxnEntry()) {
132  // trigger DeleteMsg with NULL pointer
133  DeleteMsg(NULL);
134  // trigger Post Delete, to allow post delete processing
135  PostDelete();
136  // return true to complete KSync State
137  return true;
138  }
139 
140  OvsdbDBObject *object = static_cast<OvsdbDBObject*>(GetObject());
141  struct ovsdb_idl_txn *txn;
142  if (UseBulkTxn()) {
143  txn = object->client_idl_->CreateBulkTxn(this, KSyncEntry::DEL_ACK);
144  } else {
145  txn = object->client_idl_->CreateTxn(this, KSyncEntry::DEL_ACK);
146  }
147  if (txn == NULL) {
148  // failed to create transaction because of idl marked for
149  // deletion return from here.
151  return true;
152  }
153  DeleteMsg(txn);
154  bool ret = object->client_idl_->EncodeSendTxn(txn, this);
155  // trigger post delete if we are not waiting for Ack
156  // otherwise trigger post delete on Ack
157  if (ret) {
158  // current transaction send completed trigger post delete
159  PostDelete();
160  }
161  return ret;
162 }
163 
165  return (ovs_entry_ == NULL) ? false : true;
166 }
167 
169  KSyncState state = GetState();
170  return (state >= DEL_DEFER_DEL_ACK && state <= RENEW_WAIT);
171 }
172 
174  KSyncState state = GetState();
175  return (state == SYNC_WAIT || state == NEED_SYNC ||
176  state == DEL_DEFER_SYNC);
177 }
178 
179 void OvsdbDBEntry::NotifyAdd(struct ovsdb_idl_row *row) {
180  if (ovs_entry_ == NULL) {
181  ovs_entry_ = row;
182  } else {
183  // ovs_entry should never change from Non-NULL value to
184  // another Non-NULL value.
185  assert(ovs_entry_ == row);
186  }
187  OvsdbChange();
188 }
189 
190 void OvsdbDBEntry::NotifyDelete(struct ovsdb_idl_row *row) {
191  ovs_entry_ = NULL;
192 }
193 
195  return table_;
196 }
197 
198 void OvsdbDBEntry::Ack(bool success) {
199  OvsdbDBObject *object = static_cast<OvsdbDBObject*>(GetObject());
200 
201  // trigger post delete for Del Ack
203  // current transaction send completed trigger post delete
204  PostDelete();
205  }
206 
207  if (success) {
208  if (IsDelAckWaiting())
209  object->SafeNotifyEvent(this, KSyncEntry::DEL_ACK);
210  else if (IsAddChangeAckWaiting())
211  object->SafeNotifyEvent(this, KSyncEntry::ADD_ACK);
212  else
213  delete this;
214  } else {
215  bool trigger_ack = false;
216  // On Failure try again
217  if (IsDelAckWaiting()) {
218  OVSDB_TRACE(Error, "Delete Transaction failed for " + ToString());
219  // if we are waiting to renew, dont retry delete.
220  if (GetState() != RENEW_WAIT) {
221  // trigger ack when if no message to send, on calling delete
222  trigger_ack = Delete();
223  } else {
224  trigger_ack = true;
225  }
226 
227  if (trigger_ack) {
228  object->SafeNotifyEvent(this, KSyncEntry::DEL_ACK);
229  }
230  } else if (IsAddChangeAckWaiting()) {
231  OVSDB_TRACE(Error, "Add Transaction failed for " + ToString());
232  // if we are waiting to delete, dont retry add.
233  if (GetState() != DEL_DEFER_SYNC) {
234  // Enqueue a change before generating an ADD_ACK to keep
235  // the entry in a not sync'd state.
236  object->Change(this);
237  }
238 
239  object->SafeNotifyEvent(this, KSyncEntry::ADD_ACK);
240  } else {
241  // should never happen
242  assert(0);
243  }
244  }
245 }
246 
248  if (IsDeleted() || stale()) {
249  // skip this operation for Deleted/stale Entry, After this operation
250  // Deleted/Stale entry would become active/non-stale which is not
251  // intended use of this API, caller must ensure the entry to be Active
252  // if this operation is required
253  return;
254  }
256 }
virtual void ChangeMsg(struct ovsdb_idl_txn *)
Definition: ovsdb_entry.h:74
OvsdbDBEntry(OvsdbDBObject *table)
Definition: ovsdb_entry.cc:53
virtual void PreAddChange()
Definition: ovsdb_entry.h:66
virtual void DeleteMsg(struct ovsdb_idl_txn *)
Definition: ovsdb_entry.h:76
virtual bool Change()
Definition: ovsdb_entry.cc:97
#define OVSDB_TRACE(obj,...)
virtual void TxnDoneNoMessage()
Definition: ovsdb_entry.h:27
OvsdbClientIdlPtr client_idl_
Definition: ovsdb_object.h:83
bool IsAddChangeAckWaiting()
Definition: ovsdb_entry.cc:173
void SafeNotifyEvent(KSyncEntry *entry, KSyncEntry::KSyncEvent event)
virtual void PostDelete()
Definition: ovsdb_entry.h:70
KSyncState GetState() const
Definition: ksync_entry.h:157
KSyncObject * GetObject() const
Definition: ovsdb_entry.cc:194
bool stale() const
Definition: ksync_entry.h:161
struct ovsdb_idl_row * ovs_entry_
Definition: ovsdb_entry.h:111
virtual bool Delete()
Definition: ovsdb_entry.cc:35
virtual bool IsDataResolved()
Definition: ovsdb_entry.cc:164
OvsdbEntry(OvsdbObject *table)
Definition: ovsdb_entry.cc:16
virtual bool Delete()
Definition: ovsdb_entry.cc:130
virtual bool UseBulkTxn()
Definition: ovsdb_entry.h:108
virtual ~OvsdbEntry()
Definition: ovsdb_entry.cc:24
virtual void NotifyDelete(struct ovsdb_idl_row *)
Definition: ovsdb_entry.cc:190
virtual ~OvsdbDBEntry()
Definition: ovsdb_entry.cc:61
virtual void NotifyAdd(struct ovsdb_idl_row *)
Definition: ovsdb_entry.cc:179
bool IsDeleted()
Definition: ksync_entry.h:163
virtual bool Add()
Definition: ovsdb_entry.cc:64
virtual void AddMsg(struct ovsdb_idl_txn *)
Definition: ovsdb_entry.h:72
virtual std::string ToString() const =0
OvsdbObject * table_
Definition: ovsdb_entry.h:52
virtual void Ack(bool success)
Definition: ovsdb_entry.cc:198
KSyncObject * GetObject() const
Definition: ovsdb_entry.cc:39
virtual bool IsNoTxnEntry()
Definition: ovsdb_entry.h:105
virtual bool Change()
Definition: ovsdb_entry.cc:31
virtual void OvsdbChange()
Definition: ovsdb_entry.h:78
virtual bool Add()
Definition: ovsdb_entry.cc:27
OvsdbDBObject * table_
Definition: ovsdb_entry.h:110
void Ack(bool success)
Definition: ovsdb_entry.cc:43
KSyncEntry::KSyncEvent ack_event_
Definition: ovsdb_entry.h:34