OpenSDN source code
ovsdb_sandesh.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 #include <ovsdb_client.h>
9 #include <ovsdb_client_idl.h>
10 #include <ovsdb_client_session.h>
11 #include <ovsdb_object.h>
12 #include <ovsdb_sandesh.h>
13 #include <ovsdb_types.h>
14 
15 using namespace OVSDB;
16 
17 static void SandeshError(const std::string msg, const std::string &context) {
18  ErrorResp *resp = new ErrorResp();
19  resp->set_resp(msg);
20  resp->set_context(context);
21  resp->Response();
22 }
23 
24 static void SetErrorMsg(bool &error, std::string &error_msg, std::string msg) {
25  if (error) {
26  // previous error present, add separator
27  error_msg += ", ";
28  }
29  error_msg += msg;
30  error = true;
31 }
32 
34  AgentSandeshArguments &args) :
35  Task((TaskScheduler::GetInstance()->GetTaskId("Agent::KSync")), 0),
36  ip_(), port_(0), resp_(NULL), resp_data_(resp_ctx), first_(0),
37  last_(kEntriesPerPage - 1), total_count_(0), needs_next_(false),
38  error_(false), error_msg_("") {
39  if (!args.Get("session_ip", &ip_)) {
40  SetErrorMsg(error_, error_msg_, "Error fetching session ip");
41  }
42 
43  int val;
44  if (args.Get("session_port", &val)) {
45  port_ = (uint32_t) val;
46  } else {
47  SetErrorMsg(error_, error_msg_, "Error fetching session port");
48  }
49 
50  if (args.Get("first", &val)) {
51  first_ = (uint32_t) val;
52  } else {
53  SetErrorMsg(error_, error_msg_, "Error fetching first");
54  }
55 
56  if (args.Get("last", &val)) {
57  last_ = (uint32_t) val;
58  } else {
59  SetErrorMsg(error_, error_msg_, "Error fetching last");
60  }
61 }
62 
63 OvsdbSandeshTask::OvsdbSandeshTask(std::string resp_ctx, const std::string &ip,
64  uint32_t port) :
65  Task((TaskScheduler::GetInstance()->GetTaskId("Agent::KSync")), 0),
66  ip_(ip), port_(port), resp_(NULL), resp_data_(resp_ctx), first_(0),
67  last_(kEntriesPerPage - 1), total_count_(0), needs_next_(false),
68  error_(false), error_msg_("") {
69 }
70 
72 }
73 
75  if (error_) {
77  return true;
78  }
79  OvsdbClient *ovsdb_client = Agent::GetInstance()->ovsdb_client();
80  OvsdbClientSession *session;
81  if (ip_.empty()) {
82  session = ovsdb_client->NextSession(NULL);
83  } else {
84  boost::system::error_code ec;
85  Ip4Address ip_addr = Ip4Address::from_string(ip_, ec);
86  session = ovsdb_client->FindSession(ip_addr, port_);
87  }
88  uint32_t table_size = 0;
89  uint32_t display_count = 0;
90  if (NoSessionObject() == true ||
91  (session != NULL && session->client_idl() != NULL)) {
92  KSyncObject *table = NULL;
93  if (NoSessionObject()) {
94  table = GetObject(NULL);
95  } else {
96  ip_ = session->remote_ip().to_string();
97  port_ = session->remote_port();
98  table = GetObject(session);
99  }
100 
101  if (table == NULL) {
102  SandeshError("Error: Ovsdb Object not available", resp_data_);
103  return true;
104  }
105 
106  KSyncEntry *entry = table->Next(NULL);
107  table_size = table->Size();
108  uint8_t cur_count = 0;
109  while (entry != NULL) {
110  if (FilterAllow == Filter(entry)) {
111  if ((first_ == (uint32_t)-1 || total_count_ >= first_) &&
112  (total_count_ <= last_)) {
113  cur_count++;
114  display_count++;
115  // allocate resp if not available
116  if (resp_ == NULL)
117  resp_ = Alloc();
118  UpdateResp(entry, resp_);
119  }
120  total_count_++;
121  }
122  entry = table->Next(entry);
123  if (total_count_ == (last_ + 1)) {
124  if (entry != NULL) {
125  // will need next page link
126  needs_next_ = true;
127  }
128  }
129  if (cur_count == kEntriesPerSandesh && entry != NULL) {
130  SendResponse(true);
131  cur_count = 0;
132  }
133  }
134  } else {
135  SandeshError("Error: Session not available", resp_data_);
136  return true;
137  }
138 
139  SendResponse(true);
140 
141  EncodeSendPageReq(display_count, table_size);
142  return true;
143 }
144 
147  std::string s;
148  args.Add("ovsdb_table", GetTableType());
149  args.Add("session_ip", ip_);
150  args.Add("session_port", port_);
151  EncodeArgs(args);
152  args.Add("first", 0);
153  args.Add("last", kEntriesPerPage - 1);
154  args.Encode(&s);
155 
156  return s;
157 }
158 
159 void OvsdbSandeshTask::EncodeSendPageReq(uint32_t display_count,
160  uint32_t table_size) {
161  OvsdbPageResp *resp = new OvsdbPageResp();
163  OvsdbPageRespData resp_data;
164  std::string s;
165  args.Add("ovsdb_table", GetTableType());
166  args.Add("session_ip", ip_);
167  args.Add("session_port", port_);
168  EncodeArgs(args);
169 
170  // encode first page
171  args.Add("first", 0);
172  args.Add("last", kEntriesPerPage - 1);
173  args.Encode(&s);
174  args.Del("first");
175  args.Del("last");
176  resp_data.set_first_page(s);
177  s.clear();
178 
179  uint32_t first = 0;
180  // encode prev page
181  if (first_ != 0 && first_ != (uint32_t)-1 && first_ <= total_count_) {
182  first = first_;
183  args.Add("first",
185  args.Add("last",
186  (first_ < kEntriesPerPage) ? (kEntriesPerPage -1) : (first_ - 1));
187  args.Encode(&s);
188  args.Del("first");
189  args.Del("last");
190  resp_data.set_prev_page(s);
191  s.clear();
192  }
193 
194  // encode next page
195  if (needs_next_) {
196  args.Add("first", last_ + 1);
197  args.Add("last", last_ + kEntriesPerPage);
198  args.Encode(&s);
199  args.Del("first");
200  args.Del("last");
201  resp_data.set_next_page(s);
202  s.clear();
203  }
204 
205  // encode all page
206  args.Add("first", -1);
207  args.Add("last", -1);
208  args.Encode(&s);
209  args.Del("first");
210  args.Del("last");
211  resp_data.set_all(s);
212  s.clear();
213 
214  std::stringstream match_str;
215  if (display_count != 0) {
216  uint32_t last = first + display_count - 1;
217  match_str << first << " - " << (last);
218  } else {
219  match_str << "0";
220  }
221  match_str << " / " << total_count_;
222  resp_data.set_entries(match_str.str());
223  resp_data.set_table_size(table_size);
224 
225  resp->set_req(resp_data);
226  resp_ = resp;
227  SendResponse(false);
228 }
229 
231  if (resp_ == NULL) {
232  // no response formed to send
233  return;
234  }
236  resp_->set_more(more);
237  resp_->Response();
238  resp_ = NULL;
239 }
240 
241 void OvsdbPageReq::HandleRequest() const {
243  args.Decode(get_key());
244 
245  OvsdbSandeshTask *task = NULL;
246  int table_type;
247  args.Get("ovsdb_table", &table_type);
248  switch (table_type) {
250  task = new PhysicalPortSandeshTask(context(), args);
251  break;
253  task = new LogicalSwitchSandeshTask(context(), args);
254  break;
256  task = new VlanPortBindingSandeshTask(context(), args);
257  break;
259  task = new OvsdbVrfSandeshTask(context(), args);
260  break;
262  task = new UnicastMacRemoteSandeshTask(context(), args);
263  break;
265  task = new UnicastMacLocalSandeshTask(context(), args);
266  break;
268  task = new MulticastMacLocalSandeshTask(context(), args);
269  break;
271  task = new HaStaleDevVnSandeshTask(context(), args);
272  break;
274  task = new HaStaleL2RouteSandeshTask(context(), args);
275  break;
276  default:
277  break;
278  }
279 
280  if (task != NULL) {
282  scheduler->Enqueue(task);
283  } else {
284  SandeshError("Error: Invalid table type", context());
285  }
286 }
287 
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
bool Add(const std::string &key, const std::string &val)
bool Del(const std::string &key)
bool Get(const std::string &key, std::string *val) const
int Decode(const std::string &str)
int Encode(std::string *str)
OVSDB::OvsdbClient * ovsdb_client() const
Definition: agent.h:1129
static Agent * GetInstance()
Definition: agent.h:439
KSyncEntry * Next(const KSyncEntry *entry) const
std::size_t Size()
Definition: ksync_object.h:145
virtual Ip4Address remote_ip() const =0
virtual uint16_t remote_port() const =0
virtual OvsdbClientSession * NextSession(OvsdbClientSession *session)=0
virtual OvsdbClientSession * FindSession(Ip4Address ip, uint16_t port)=0
void EncodeSendPageReq(uint32_t display_count, uint32_t table_size)
static const uint8_t kEntriesPerSandesh
Definition: ovsdb_sandesh.h:34
virtual TableType GetTableType()=0
virtual void UpdateResp(KSyncEntry *entry, SandeshResponse *resp)=0
virtual bool NoSessionObject()
Definition: ovsdb_sandesh.h:59
virtual FilterResp Filter(KSyncEntry *entry)
Definition: ovsdb_sandesh.h:53
SandeshResponse * resp_
Definition: ovsdb_sandesh.h:65
void SendResponse(bool more)
OvsdbSandeshTask(std::string resp_ctx, AgentSandeshArguments &args)
virtual SandeshResponse * Alloc()=0
static const uint8_t kEntriesPerPage
Definition: ovsdb_sandesh.h:35
std::string EncodeFirstPage()
virtual void EncodeArgs(AgentSandeshArguments &args)
Definition: ovsdb_sandesh.h:52
virtual KSyncObject * GetObject(OvsdbClientSession *session)=0
bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
virtual void Response()
Definition: cpp/sandesh.h:505
virtual void set_more(const bool val)=0
void set_context(std::string context)
Definition: cpp/sandesh.h:313
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
Definition: task.cc:662
static TaskScheduler * GetInstance()
Definition: task.cc:574
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
Definition: agent.h:44
static void SetErrorMsg(bool &error, std::string &error_msg, std::string msg)
static void SandeshError(const std::string msg, const std::string &context)
Definition: task_int.h:11