OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ovsdb_client_connection_state.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <base/string_util.h>
7 #include <ovsdb_types.h>
8 #include "ha_stale_dev_vn.h"
9 
10 using namespace OVSDB;
11 using namespace process;
12 using namespace std;
13 
15  const std::string &device_name,
16  const boost::uuids::uuid &u) :
17  table_(table), device_name_(device_name), device_uuid_(u),
18  device_entry_(NULL), ha_stale_dev_vn_table_(NULL) {
19  refcount_ = 0;
20 }
21 
23  if (ha_stale_dev_vn_table_ != NULL) {
26  }
27 }
28 
30  if (idl_list_.empty()) {
31  return false;
32  }
33 
34  IdlList::iterator it = idl_list_.begin();
35  for (; it != idl_list_.end(); ++it) {
36  // if there is any non deleted IDL available
37  // return true
38  if (!(*it)->IsDeleted()) {
39  return true;
40  }
41  }
42 
43  return false;
44 }
45 
46 namespace OVSDB
47 {
49  assert(p->device_entry_ != NULL || !p->idl_list_.empty());
50  p->refcount_++;
51  }
52 
54  int count = --p->refcount_;
55  if (count == 0 && p->idl_list_.empty() && p->device_entry_ == NULL) {
56  // delete the entry and free the state
57  p->table_->UpdateConnectionInfo(p, true);
58  p->table_->entry_map_.erase(p->device_name_);
59  delete p;
60  }
61  }
62 } // namespace OVSDB
63 
65  OvsPeerManager *manager)
66  : agent_(agent), table_(agent->physical_device_table()),
67  manager_(manager) {
68  id_ = table_->Register(
70  this, _1, _2));
71 }
72 
75 }
76 
77 void ConnectionStateTable::AddIdlToConnectionState(const std::string &dev_name,
78  OvsdbClientIdl *idl) {
79  ConnectionStateEntry *state = new ConnectionStateEntry(this, dev_name,
80  boost::uuids::nil_uuid());
81  pair<EntryMap::iterator, bool> ret;
82  ret = entry_map_.insert(pair<string, ConnectionStateEntry*>(dev_name,
83  state));
84  if (ret.second == false) {
85  // entry already existed, delete allocated memory
86  delete state;
87  }
88  ret.first->second->idl_list_.insert(idl);
89  UpdateConnectionInfo(ret.first->second, false);
90 }
91 
92 void ConnectionStateTable::DelIdlToConnectionState(const std::string &dev_name,
93  OvsdbClientIdl *idl) {
94  EntryMap::iterator it = entry_map_.find(dev_name);
95  if (it == entry_map_.end()) {
96  return;
97  }
98  it->second->idl_list_.erase(idl);
99  if (it->second->idl_list_.empty()) {
100  if (it->second->device_entry_ == NULL && it->second->refcount_ == 0) {
101  UpdateConnectionInfo(it->second, true);
102  delete it->second;
103  entry_map_.erase(it);
104  } else {
105  UpdateConnectionInfo(it->second, false);
106  }
107  }
108 }
109 
110 ConnectionStateEntry *ConnectionStateTable::Find(const std::string &dev_name) {
111  EntryMap::iterator it = entry_map_.find(dev_name);
112  if (it == entry_map_.end()) {
113  return NULL;
114  }
115  return it->second;
116 }
117 
119  DBEntryBase *e) {
120  PhysicalDevice *dev = static_cast<PhysicalDevice *>(e);
121  ConnectionStateEntry *state =
122  static_cast<ConnectionStateEntry *>(dev->GetState(table_, id_));
123  if (dev->IsDeleted()) {
124  if (state) {
125  state->device_entry_ = NULL;
126  if (state->idl_list_.empty() && state->refcount_ == 0) {
127  UpdateConnectionInfo(state, true);
128  entry_map_.erase(state->device_name_);
129  delete state;
130  } else {
131  UpdateConnectionInfo(state, false);
132  }
133  dev->ClearState(table_, id_);
134 
135  if (state && state->ha_stale_dev_vn_table_ != NULL) {
137  state->ha_stale_dev_vn_table_ = NULL;
138  }
139  }
140  return;
141  }
142  if (!state) {
143  if (dev->name().empty())
144  return;
145 
146  state = new ConnectionStateEntry(this, dev->name(), dev->uuid());
147  pair<EntryMap::iterator, bool> ret;
148  ret = entry_map_.insert(pair<string, ConnectionStateEntry*>(dev->name(),
149  state));
150  if (ret.second == false) {
151  // entry already existed, delete allocated memory
152  delete state;
153  }
154  state = ret.first->second;
155  ret.first->second->device_uuid_ = dev->uuid();
156  ret.first->second->device_entry_ = dev;
157  dev->SetState(table_, id_, ret.first->second);
158  UpdateConnectionInfo(ret.first->second, false);
159  }
160 
161  if (state->ha_stale_dev_vn_table_ == NULL) {
162  state->ha_stale_dev_vn_table_ =
163  new HaStaleDevVnTable(agent_, manager_, state,
164  state->device_name_);
165  }
166 }
167 
169  bool deleted) {
170  NotifyUve(entry, deleted);
171  if (agent_->connection_state() == NULL)
172  return;
173 
174  if (deleted) {
175  agent_->connection_state()->Delete(ConnectionType::TOR,
176  entry->device_name_);
177  } else {
178  ConnectionStatus::type status = ConnectionStatus::UP;
179  string message;
180  boost::asio::ip::tcp::endpoint ep;
181  if (!entry->idl_list_.empty()) {
182  ConnectionStateEntry::IdlList::iterator it =
183  entry->idl_list_.begin();
184  ep.address((*it)->remote_ip());
185  ep.port((*it)->remote_port());
186  }
187 
188  if (entry->device_entry_ == NULL) {
189  status = ConnectionStatus::DOWN;
190  message = "Config Not Available";
191  } else if (entry->idl_list_.empty()) {
192  status = ConnectionStatus::DOWN;
193  message = "ToR Not Available";
194  } else {
195  message = "Active sessions = " +
196  integerToString(entry->idl_list_.size());
197  }
198  agent_->connection_state()->Update(ConnectionType::TOR,
199  entry->device_name_, status,
200  ep, message);
201  }
202 }
203 
205  bool deleted) {
206  /* If device uuid is not available we cannot notify Uve Module */
207  if (entry->device_uuid_ == boost::uuids::nil_uuid()) {
208  OVSDB_TRACE(Trace, "UVE notification failed for " +
209  entry->device_name_ + " No UUID yet");
210  return;
211  }
212 
213  if (agent_->uve() == NULL) {
214  OVSDB_TRACE(Trace, "UVE notification failed for " +
215  entry->device_name_ + " UVE object absent");
216  return;
217  }
218 
219  if (agent_->uve()->prouter_uve_table() == NULL) {
220  OVSDB_TRACE(Trace, "UVE notification failed for " +
221  entry->device_name_ + " Prouter UVE object absent");
222  return;
223  }
224 
226  if (deleted) {
227  ptable->UpdateMastership(entry->device_uuid_, false);
228  } else {
229  bool mastership = true;
230  if (entry->device_entry_ == NULL) {
231  mastership = false;
232  } else if (entry->idl_list_.empty()) {
233  mastership = false;
234  }
235  ptable->UpdateMastership(entry->device_uuid_, mastership);
236  }
237 }
void intrusive_ptr_release(ConnectionStateEntry *p)
void PhysicalDeviceNotify(DBTablePartBase *part, DBEntryBase *e)
ConnectionStateTable(Agent *agent, OvsPeerManager *manager)
#define OVSDB_TRACE(obj,...)
void AddIdlToConnectionState(const std::string &device_name, OvsdbClientIdl *idl)
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:37
AgentUveBase * uve() const
Definition: agent.cc:909
void intrusive_ptr_add_ref(ConnectionStateEntry *p)
bool IsDeleted() const
Definition: db_entry.h:49
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:22
boost::uuids::uuid uuid
void DelIdlToConnectionState(const std::string &device_name, OvsdbClientIdl *idl)
const boost::uuids::uuid & uuid() const
void Unregister(ListenerId listener)
Definition: db_table.cc:186
void UpdateMastership(const boost::uuids::uuid &u, bool value)
void Delete(ConnectionType::type ctype, const std::string &name)
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:181
uint8_t type
Definition: load_balance.h:109
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:19
Definition: agent.h:358
Definition: trace.h:220
ConnectionStateEntry(ConnectionStateTable *table, const std::string &device_name, const boost::uuids::uuid &u)
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:73
void UpdateConnectionInfo(ConnectionStateEntry *entry, bool deleted)
ConnectionStateEntry * Find(const std::string &device_name)
void NotifyUve(ConnectionStateEntry *entry, bool deleted)
const std::string & name() const
ProuterUveTable * prouter_uve_table() const
process::ConnectionState * connection_state() const
Definition: agent.h:953