OpenSDN source code
vncapi_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <boost/bind/bind.hpp>
6 #include "base/util.h"
7 #include "base/test/task_test_util.h"
8 #include <atomic>
9 #include <iostream>
10 #include <rapidjson/stringbuffer.h>
11 
12 #include "vncapi.h"
13 
15 using namespace boost::placeholders;
16 
17 class Controller {
18  public:
19  Controller() : x(0), pc_(0),
20  kill_timer_(TimerManager::CreateTimer(*evm.io_service(), "Bla")),
21  poll_timer_(TimerManager::CreateTimer(*evm.io_service(), "Bla")),
22  done_it_(false) {
23  kill_timer_->Start(1500, boost::bind(&Controller::wait_for_done, this));
24  poll_timer_->Start(5000, boost::bind(&Controller::Main, this));
25  SetMore(true);
26 
27  std::string ip("10.84.14.38");// <-- good
28  // std::string ip("10.84.14.39");// <-- bad
29  std::string user("admin");
30  std::string passwd("c0ntrail123");
31  std::string tenant("admin");
32  std::string proto("http");
33 
34  vnccfg_.cfg_srv_ip = ip,
35  vnccfg_.cfg_srv_port = 8082,
36  vnccfg_.ks_srv_ip = ip,
37  vnccfg_.ks_srv_port = 35357,
38  vnccfg_.protocol = proto,
39  vnccfg_.user = user,
40  vnccfg_.password = passwd,
41  vnccfg_.tenant = tenant,
42 
43  vnc_.reset(new VncApi(&evm, &vnccfg_));
44  }
45 #define MAXIT 3
46  bool Main() {
47 
48  std::vector<std::string> ids;
49  std::vector<std::string> filters;
50  std::vector<std::string> parents;
51  std::vector<std::string> refs;
52  std::vector<std::string> fields;
53 
54  fields.push_back("user_defined_counter");
55 
56  vnc_->GetConfig("global-system-config", ids, filters, parents, refs,
57  fields, boost::bind(&Controller::UDCHandler, this, _1, _2, _3,
58  _4, _5, _6));
59 
60  done_it_ = ++pc_ > MAXIT;
61  std::cout << "It : " << pc_ << std::endl;
62  return !done_it_;
63 
64  }
65  void Run() {
66  evm.Run();
67  }
68  protected:
69  void SetMore(bool x) { more_ = x; }
70  void SetDone() { SetMore(false); }
71  private:
72  int x;
73  int pc_;
76  bool done_it_;
77  boost::scoped_ptr<VncApi> vnc_;
78  std::atomic<bool> more_;
79  std::string resp_body_;
81  bool wait_for_done() {
82  std::cout << "------ entering wait ------------\n";
83  if (more_) {
84  //std::this_thread::sleep_for(std::chrono::seconds(1));
85  //usleep(1000);
86  std::cout << "\n" << ++x << " waiting...\n";
87  return true;
88  }
89  std::cout << "\n" << x << " Done waiting...\n";
91  vnc_->Stop();
92  evm.Shutdown();
93  std::cout << "\n" << x << " all Done waiting...\n";
94  return false;
95  }
96  void UDCHandler(contrail_rapidjson::Document &jdoc, boost::system::error_code &ec,
97  std::string version, int status, std::string reason,
98  std::map<std::string, std::string> *headers) {
99 
100  if (jdoc.IsObject() && jdoc.HasMember("global-system-configs")) {
101  for (contrail_rapidjson::SizeType j=0;
102  j < jdoc["global-system-configs"].Size(); j++) {
103  const contrail_rapidjson::Value& gsc = jdoc["global-system-configs"][j]
104  ["user_defined_counter"]["counter"];
105  if (gsc.IsArray()) {
106  for (contrail_rapidjson::SizeType i = 0; i < gsc.Size(); i++)
107  std::cout << "\nname: " << gsc[i]["name"].GetString()
108  << "\npattern: " << gsc[i]["pattern"].GetString()
109  << "\n";
110  } else {
111  //SetDone();
112  }
113  }
114  }
115 
116  if (done_it_)
117  SetDone();
118  }
119 };
120 
121 int main() {
122  Controller c;
123 
124  //c.Main();
125  c.Run();
126 
127 
128  return 0;
129 }
std::atomic< bool > more_
Definition: vncapi_test.cc:78
void SetMore(bool x)
Definition: vncapi_test.cc:69
boost::scoped_ptr< VncApi > vnc_
Definition: vncapi_test.cc:77
void Run()
Definition: vncapi_test.cc:65
Timer * poll_timer_
Definition: vncapi_test.cc:75
void UDCHandler(contrail_rapidjson::Document &jdoc, boost::system::error_code &ec, std::string version, int status, std::string reason, std::map< std::string, std::string > *headers)
Definition: vncapi_test.cc:96
bool wait_for_done()
Definition: vncapi_test.cc:81
bool done_it_
Definition: vncapi_test.cc:76
bool Main()
Definition: vncapi_test.cc:46
void SetDone()
Definition: vncapi_test.cc:70
Timer * kill_timer_
Definition: vncapi_test.cc:74
std::string resp_body_
Definition: vncapi_test.cc:79
VncApiConfig vnccfg_
Definition: vncapi_test.cc:80
Definition: timer.h:57
Definition: vncapi.h:60
static void WaitForIdle()
EventManager evm
Definition: vncapi_test.cc:14
#define MAXIT
Definition: vncapi_test.cc:45