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 "agent_cmn.h"
7 : #include "agent_db.h"
8 : #include <cfg/cfg_init.h>
9 :
10 287 : void AgentDBEntry::SetRefState() const {
11 287 : AgentDBTable *table = static_cast<AgentDBTable *>(get_table());
12 : // Force calling SetState on a const object.
13 : // Ideally, SetState should be 'const method' and StateMap mutable
14 287 : AgentDBEntry *entry = (AgentDBEntry *)this;
15 287 : entry->SetState(table, table->GetRefListenerId(), new AgentDBState(this));
16 287 : }
17 :
18 287 : void AgentDBEntry::ClearRefState() const {
19 287 : AgentDBTable *table = static_cast<AgentDBTable *>(get_table());
20 : // Force calling SetState on a const object.
21 : // Ideally, ClearState should be 'const method' and StateMap mutable
22 287 : AgentDBEntry *entry = (AgentDBEntry *)this;
23 287 : table->OnZeroRefcount(entry);
24 287 : delete(entry->GetState(table, table->GetRefListenerId()));
25 287 : entry->ClearState(table, table->GetRefListenerId());
26 287 : }
27 :
28 2020 : bool AgentDBEntry::IsActive() const {
29 2020 : return !IsDeleted();
30 : }
31 :
32 94 : DBState *AgentDBEntry::GetAgentDBEntryState(int listener_id) {
33 : DBState *state = dynamic_cast<DBState *>
34 94 : (GetState(get_table(), listener_id));
35 94 : return state;
36 : }
37 :
38 77 : const DBState *AgentDBEntry::GetAgentDBEntryState(int listener_id) const {
39 : const DBState *state = dynamic_cast<const DBState *>
40 77 : (GetState(get_table(), listener_id));
41 77 : return state;
42 : }
43 :
44 284 : void AgentDBEntry::AllocateResources(ResourceManager *resource_manager) {
45 284 : }
46 :
47 284 : void AgentDBEntry::FreeResources(ResourceManager *resource_manager) {
48 284 : }
49 :
50 116 : void AgentDBEntry::PostAdd() {
51 116 : }
52 :
53 10 : static bool FlushNotify(DBTablePartBase *partition, DBEntryBase *e) {
54 10 : if (e->IsDeleted()) {
55 2 : return true;
56 : }
57 :
58 8 : DBRequest req(DBRequest::DB_ENTRY_DELETE);
59 8 : req.key = e->GetDBRequestKey();
60 8 : (static_cast<AgentDBTable *>(e->get_table()))->Process(req);
61 8 : return true;
62 8 : }
63 :
64 8 : static void FlushWalkDone(DBTable::DBTableWalkRef walk_ref,
65 : DBTableBase *partition) {
66 8 : AgentDBTable *table = static_cast<AgentDBTable *>(partition);
67 8 : table->ReleaseWalker(walk_ref);
68 8 : table->reset_flush_walk_ref();
69 8 : }
70 :
71 24 : AgentDBTable::AgentDBTable(DB *db, const std::string &name) :
72 24 : DBTable(db, name), ref_listener_id_(-1), agent_(NULL),
73 24 : OperDBTraceBuf(SandeshTraceBufferCreate(("Oper " + name), 5000)),
74 48 : flush_walk_ref_() {
75 24 : ref_listener_id_ = Register(boost::bind(&AgentDBTable::Notify,
76 : this, _1, _2));
77 24 : };
78 :
79 0 : AgentDBTable::AgentDBTable(DB *db, const std::string &name,
80 0 : bool del_on_zero_refcount) :
81 0 : DBTable(db, name), ref_listener_id_(-1) , agent_(NULL),
82 0 : OperDBTraceBuf(SandeshTraceBufferCreate(("Oper " + name), 5000)),
83 0 : flush_walk_ref_() {
84 0 : ref_listener_id_ = Register(boost::bind(&AgentDBTable::Notify,
85 : this, _1, _2));
86 0 : };
87 :
88 24 : AgentDBTable::~AgentDBTable() {
89 24 : assert(HasWalkers() == false);
90 24 : }
91 :
92 0 : void AgentDBTable::NotifyEntry(DBEntryBase *e) {
93 0 : agent_->ConcurrencyCheck();
94 : DBTablePartBase *tpart =
95 0 : static_cast<DBTablePartition *>(GetTablePartition(e));
96 0 : tpart->Notify(e);
97 0 : }
98 :
99 8 : void AgentDBTable::reset_flush_walk_ref() {
100 8 : flush_walk_ref_ = NULL;
101 8 : }
102 :
103 0 : AgentDBEntry *AgentDBTable::FindActiveEntryNoLock(const DBEntry *key) {
104 0 : AgentDBEntry *entry = static_cast<AgentDBEntry *> (FindNoLock(key));
105 0 : if (entry && (entry->IsActive() == false)) {
106 0 : return NULL;
107 : }
108 0 : return entry;
109 : }
110 :
111 212 : AgentDBEntry *AgentDBTable::FindActiveEntry(const DBEntry *key) {
112 212 : AgentDBEntry *entry = static_cast<AgentDBEntry *> (Find(key));
113 212 : if (entry && (entry->IsActive() == false)) {
114 0 : return NULL;
115 : }
116 212 : return entry;
117 : }
118 :
119 9 : AgentDBEntry *AgentDBTable::FindActiveEntryNoLock(const DBRequestKey *key) {
120 9 : AgentDBEntry *entry = static_cast<AgentDBEntry *>(FindNoLock(key));
121 9 : if (entry && (entry->IsActive() == false)) {
122 0 : return NULL;
123 : }
124 9 : return entry;
125 : }
126 :
127 1978 : AgentDBEntry *AgentDBTable::FindActiveEntry(const DBRequestKey *key) {
128 1978 : AgentDBEntry *entry = static_cast<AgentDBEntry *>(Find(key));
129 1978 : if (entry && (entry->IsActive() == false)) {
130 0 : return NULL;
131 : }
132 1978 : return entry;
133 : }
134 :
135 0 : AgentDBEntry *AgentDBTable::Find(const DBEntry *key, bool ret_del) {
136 0 : if (ret_del) {
137 0 : return Find(key);
138 : } else {
139 0 : return FindActiveEntry(key);
140 : }
141 : }
142 :
143 1353 : AgentDBEntry *AgentDBTable::Find(const DBRequestKey *key, bool ret_del) {
144 1353 : if (ret_del) {
145 1291 : return Find(key);
146 : } else {
147 62 : return FindActiveEntry(key);
148 : }
149 : }
150 :
151 212 : AgentDBEntry *AgentDBTable::Find(const DBEntry *key) {
152 212 : return static_cast<AgentDBEntry *> (DBTable::Find(key));
153 : }
154 :
155 3269 : AgentDBEntry *AgentDBTable::Find(const DBRequestKey *key) {
156 3269 : return static_cast<AgentDBEntry *>(DBTable::Find(key));
157 : }
158 :
159 284 : void AgentDBTablePartition::Add(DBEntry *entry) {
160 284 : entry->set_table_partition(static_cast<DBTablePartBase *>(this));
161 284 : Agent *agent = (static_cast<AgentDBTable *>(parent()))->agent();
162 284 : if (agent) {
163 : (static_cast<AgentDBEntry *>(entry))->AllocateResources
164 284 : (agent->resource_manager());
165 : }
166 284 : DBTablePartition::Add(entry);
167 284 : static_cast<AgentDBEntry *>(entry)->PostAdd();
168 284 : }
169 :
170 284 : void AgentDBTablePartition::Remove(DBEntryBase *entry) {
171 284 : AgentDBEntry *agent_dbentry = static_cast<AgentDBEntry *>(entry);
172 284 : if (agent_dbentry->GetRefCount() != 0) {
173 0 : agent_dbentry->ClearOnRemoveQ();
174 0 : return;
175 : }
176 284 : Agent *agent = (static_cast<AgentDBTable *>(parent()))->agent();
177 284 : if (agent) {
178 : (static_cast<AgentDBEntry *>(entry))->FreeResources
179 284 : (agent->resource_manager());
180 : }
181 284 : DBTablePartition::Remove(entry);
182 : }
183 :
184 82 : bool AgentDBTable::IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &id) {
185 82 : id = boost::uuids::nil_uuid();
186 82 : return false;
187 : }
188 :
189 1256 : void AgentDBTable::Input(DBTablePartition *partition, DBClient *client,
190 : DBRequest *req) {
191 1256 : AgentKey *key = static_cast<AgentKey *>(req->key.get());
192 :
193 1256 : if (key->sub_op_ == AgentKey::ADD_DEL_CHANGE) {
194 1170 : DBTable::Input(partition, client, req);
195 1170 : return;
196 : }
197 :
198 86 : AgentDBEntry *entry = static_cast<AgentDBEntry *>(partition->Find(key));
199 86 : if (entry && (entry->IsActive() == false)) {
200 4 : return;
201 : }
202 :
203 : // Dont create an entry on RESYNC sub_op
204 82 : if (key->sub_op_ == AgentKey::RESYNC) {
205 82 : if (entry == NULL) {
206 40 : return;
207 : }
208 42 : if (Resync(entry, req)) {
209 38 : partition->Change(entry);
210 : }
211 42 : return;
212 : }
213 0 : return;
214 : }
215 :
216 1 : void AgentDBTable::Clear() {
217 1 : Unregister(ref_listener_id_);
218 1 : assert(!HasListeners());
219 : DBTablePartition *partition = static_cast<DBTablePartition *>(
220 1 : GetTablePartition(0));
221 :
222 1 : DBEntryBase *next = NULL;
223 1 : for (DBEntryBase *entry = partition->GetFirst(); entry; entry = next) {
224 0 : next = partition->GetNext(entry);
225 0 : if (entry->IsDeleted()) {
226 0 : continue;
227 : }
228 0 : partition->Delete(entry);
229 : }
230 1 : }
231 :
232 8 : void AgentDBTable::Process(DBRequest &req) {
233 8 : agent_->ConcurrencyCheck();
234 : DBTablePartition *tpart =
235 8 : static_cast<DBTablePartition *>(GetTablePartition(req.key.get()));
236 8 : tpart->Process(NULL, &req);
237 8 : }
238 :
239 8 : void AgentDBTable::Flush() {
240 16 : flush_walk_ref_ = AllocWalker(boost::bind(FlushNotify, _1, _2),
241 8 : boost::bind(FlushWalkDone, _1, _2));
242 8 : WalkAgain(flush_walk_ref_);
243 8 : }
|