8 #include <boost/algorithm/string/find.hpp>
9 #include <boost/lexical_cast.hpp>
13 #include <SimpleAmqpClient/SimpleAmqpClient.h>
14 #include "rapidjson/document.h"
23 #include "config_client_log_types.h"
26 #include "config_client_show_types.h"
28 using namespace boost;
30 using namespace contrail_rapidjson;
37 Task(amqpclient->reader_task_id()), amqpclient_(amqpclient) {
38 channel_.reset(ConfigStaticObjectFactory::Create<ConfigAmqpChannel>());
47 string Description()
const {
return "ConfigAmqpClient::RabbitMQReader"; }
53 void ConnectToRabbitMQ(
bool queue_delete =
true);
54 bool AckRabbitMessages(AmqpClient::Envelope::ptr_t &envelop);
55 bool ReceiveRabbitMessages(AmqpClient::Envelope::ptr_t &envelop);
60 mgr_(mgr), hostname_(hostname), module_name_(module_name),
61 current_server_index_(0), terminate_(false),
62 rabbitmq_user_(
options.rabbitmq_user),
63 rabbitmq_password_(
options.rabbitmq_password),
64 rabbitmq_vhost_(
options.rabbitmq_vhost),
65 rabbitmq_use_ssl_(
options.rabbitmq_use_ssl),
66 rabbitmq_ssl_version_(
options.rabbitmq_ssl_version),
67 rabbitmq_ssl_keyfile_(
options.rabbitmq_ssl_keyfile),
68 rabbitmq_ssl_certfile_(
options.rabbitmq_ssl_certfile),
69 rabbitmq_ssl_ca_certs_(
options.rabbitmq_ssl_ca_certs) {
77 if (
options.rabbitmq_server_list.empty())
80 for (vector<string>::const_iterator iter =
81 options.rabbitmq_server_list.begin();
82 iter !=
options.rabbitmq_server_list.end(); iter++) {
83 string server_info(*iter);
84 typedef boost::tokenizer<boost::char_separator<char> >
tokenizer;
85 boost::char_separator<char> sep(
":");
87 tokenizer::iterator tit = tokens.begin();
91 string port_str(*tit);
96 boost::system::error_code ec;
106 ConfigClientMgrDebug,
107 "RabbitMQ SM: StartRabbitMQReader: RabbitMQ disabled");
114 ConfigClientMgrDebug,
115 "RabbitMQ SM: StartRabbitMQReader: re init triggered,"
116 " dont start RabbitMQ");
132 const string user = hide_auth_info ?
"********" :
rabbitmq_user();
134 string uri = string(
"amqp://" + user +
":" +
148 process::ConnectionType::DATABASE,
"RabbitMQ",
149 process::ConnectionStatus::UP,
150 endpoints(),
"RabbitMQ connection established");
152 "RabbitMQ SM: RabbitMQ connection established");
155 process::ConnectionType::DATABASE,
"RabbitMQ",
156 process::ConnectionStatus::DOWN,
157 endpoints(),
"RabbitMQ connection down");
159 "RabbitMQ SM: RabbitMQ connection down");
166 string message =
"RabbitMQ SM: Connect to Rabbit MQ with queue_delete ";
167 message += queue_delete ?
"TRUE" :
"FALSE";
174 "RabbitMQ SM: Skipped connect due to reinit");
180 int port = boost::lexical_cast<int>(
196 channel_->DeclareExchange(
"vnc_config.object-update",
197 AmqpClient::Channel::EXCHANGE_TYPE_FANOUT,
false,
false,
false);
202 channel_->DeleteQueue(queue_name,
false,
false);
207 string queue =
channel_->DeclareQueue(queue_name,
false,
false,
209 channel_->BindQueue(queue,
"vnc_config.object-update");
213 true,
false,
false, 0);
214 }
catch (std::exception &e) {
215 static string what = e.what();
217 "RabbitMQ SM: Caught exception while connecting to RabbitMQ: "
220 cout << message << endl;
231 "RabbitMQ SM: Caught fatal exception while "
232 "connecting to RabbitMQ: "
235 cout << message << endl;
253 conn_info.connection_status_change_at =
260 document.Parse<0>(json_message.c_str());
262 if (document.HasParseError()) {
263 size_t pos = document.GetErrorOffset();
265 cout <<
"Error in parsing JSON message from rabbitMQ at "
266 << pos <<
"with error description"
267 << document.GetParseError() << endl;
271 string uuid_str =
"";
272 string obj_type =
"";
273 string obj_name =
"";
274 for (Value::ConstMemberIterator itr = document.MemberBegin();
275 itr != document.MemberEnd(); ++itr) {
276 string key(itr->name.GetString());
278 oper = itr->value.GetString();
279 }
else if (key ==
"type") {
280 obj_type = itr->value.GetString();
281 }
else if (key ==
"fq_name") {
282 if (!itr->value.IsArray())
285 SizeType sz = itr->value.GetArray().Size();
288 for (SizeType i = 0; i < sz-1; i++) {
289 os << itr->value[i].GetString() <<
":";
291 os << itr->value[sz-1].GetString();
293 }
else if (key ==
"uuid") {
294 uuid_str = itr->value.GetString();
298 if ((oper ==
"") || (uuid_str ==
"") || (obj_type ==
"")) {
300 "Empty object name or empty type or empty uuid", obj_type,
305 if ((oper ==
"CREATE") || (oper ==
"UPDATE")) {
306 if (obj_name.empty()) {
308 "Empty object name during CREATE/UPDATE",
309 obj_type, obj_name, uuid_str);
326 string stored_fq_name =
328 if (stored_fq_name ==
"ERROR") {
331 if (oper ==
"UPDATE") {
333 "FQ Name Cache entry not found on UPDATE:",
334 obj_type, obj_name, uuid_str);
337 AddFQNameCache(uuid_str, obj_type, obj_name);
339 }
else if (oper ==
"DELETE") {
341 InvalidateFQNameCache(uuid_str);
345 obj_type, obj_name, uuid_str);
352 AmqpClient::Envelope::ptr_t &envelope) {
357 channel_->BasicConsumeMessage(consumer_tag_, envelope, 10);
359 }
catch (std::exception &e) {
360 static string what = e.what();
362 "RabbitMQ SM: Caught exception while receiving "
363 "messages from RabbitMQ: "
364 + amqpclient_->rabbitmq_ip() +
":"
365 + amqpclient_->rabbitmq_port() +
" : " + what;
366 cout << message << endl;
371 "RabbitMQ SM: Caught fatal unknown exception while receiving "
372 "messages from RabbitMQ "
373 + amqpclient_->rabbitmq_ip() +
':'
374 + amqpclient_->rabbitmq_port();
375 cout << message << endl;
383 AmqpClient::Envelope::ptr_t &envelope) {
385 channel_->BasicAck(envelope);
386 }
catch (std::exception &e) {
387 static string what = e.what();
389 "RabbitMQ SM: Caught exception while acking "
390 "messages from RabbitMQ: "
391 + amqpclient_->rabbitmq_ip() +
':'
392 + amqpclient_->rabbitmq_port() +
':' + what;
393 cout << message << endl;
398 "RabbitMQ SM: Caught fatal unknown exception while acking messages "
399 "from RabbitMQ " + amqpclient_->rabbitmq_ip() +
':'
400 + amqpclient_->rabbitmq_port();
401 cout << message << endl;
412 if (amqpclient_->config_manager()->is_reinit_triggered()) {
414 ConfigClientMgrDebug,
415 "RabbitMQ SM: Reinit triggered, don't wait for end of config");
420 amqpclient_->config_manager()->WaitForEndOfConfig();
424 if (amqpclient_->terminate())
427 if (amqpclient_->config_manager()->is_reinit_triggered()) {
429 "RabbitMQ SM: Reinit triggered, break from message receiving loop");
432 AmqpClient::Envelope::ptr_t envelope;
433 if (ReceiveRabbitMessages(envelope) ==
false) {
434 ConnectToRabbitMQ(
false);
441 amqpclient_->ProcessMessage(envelope->Message()->Body());
442 if (AckRabbitMessages(envelope) ==
false) {
443 ConnectToRabbitMQ(
false);
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
boost::scoped_ptr< ConfigAmqpChannel > channel_
bool AckRabbitMessages(AmqpClient::Envelope::ptr_t &envelop)
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
RabbitMQReader(ConfigAmqpClient *amqpclient)
string Description() const
Gives a description of the task.
ConfigAmqpClient * amqpclient_
bool ReceiveRabbitMessages(AmqpClient::Envelope::ptr_t &envelop)
void ConnectToRabbitMQ(bool queue_delete=true)
std::string FormAmqpUri(bool hide_auth_info) const
std::string rabbitmq_port() const
std::string rabbitmq_vhost() const
ConfigAmqpClient(ConfigClientManager *mgr, std::string hostname, std::string module_name, const ConfigClientOptions &options)
ConfigClientManager * mgr_
boost::asio::ip::tcp::endpoint Endpoint
void GetConnectionInfo(ConfigAmqpConnInfo &info) const
bool ProcessMessage(const std::string &json_message)
void StartRabbitMQReader()
std::vector< Endpoint > endpoints() const
bool rabbitmq_use_ssl() const
void EnqueueUUIDRequest(std::string oper, std::string obj_type, std::string uuid_str)
std::string hostname() const
std::string rabbitmq_ip() const
void increment_rabbitmq_server_index()
void set_connected(bool connected)
std::atomic< bool > connection_status_
void ReportRabbitMQConnectionStatus(bool connected) const
std::vector< std::string > rabbitmq_ports_
std::string rabbitmq_user() const
std::atomic< uint64_t > connection_status_change_at_
std::string rabbitmq_ssl_keyfile() const
std::string rabbitmq_ssl_ca_certs() const
std::string rabbitmq_ssl_certfile() const
size_t rabbitmq_server_list_len() const
std::vector< std::string > rabbitmq_ips_
std::vector< Endpoint > endpoints_
ConfigClientManager * config_manager() const
std::string module_name() const
std::string rabbitmq_password() const
void EnqueueUUIDRequest(std::string oper, std::string obj_type, std::string uuid_str)
ConfigDbClient * config_db_client() const
bool is_reinit_triggered()
ConfigJsonParserBase * config_json_parser()
virtual std::string FindFQName(const std::string &uuid) const
const bool IsReadObjectType(std::string objectType)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
int GetTaskId(const std::string &name)
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
static TaskScheduler * GetInstance()
Task is a class to describe a computational task within OpenSDN control plane applications....
static ConnectionState * GetInstance()
#define CONFIG_CLIENT_RABBIT_MSG_TRACE(obj,...)
#define CONFIG_CLIENT_DEBUG(obj,...)
#define CONFIG_CLIENT_WARN(obj,...)
static int compare(const Type &lhs, const Type &rhs)
bool stringToInteger(const std::string &str, NumberType &num)
boost::tokenizer< boost::char_separator< char > > tokenizer
static std::string UTCUsecToString(uint64_t tstamp)
static uint64_t UTCTimestampUsec()