浏览代码

FD3-669 se agregaron connection parameters para amqp y mysql

Espinoza Guillermo 7 年之前
父节点
当前提交
a9be96a215

+ 3 - 2
hooks/amqp/src/amqppublisher.cc

@@ -1,5 +1,6 @@
 #include <unistd.h>
 #include <string>
+#include "common.h"
 #include "amqppublisher.h"
 #include <SimpleAmqpClient/SimpleAmqpClient.h>
 
@@ -7,14 +8,14 @@ int AMQPPublisher::publish(std::string message)
 {
     using namespace AmqpClient;
 
-    Channel::ptr_t channel = Channel::Create("amqp", 5672, "guest", "guest", "/");
+    Channel::ptr_t channel = Channel::Create(host, 5672, user, password, "/");
 
     BasicMessage::ptr_t basic_message = BasicMessage::Create(message);
     basic_message->Expiration("10000");
     basic_message->DeliveryMode(BasicMessage::dm_persistent);
     basic_message->ContentType("text/text");
     basic_message->ContentEncoding("UTF-8");
-    
+
     channel->DeclareExchange("kea", "fanout");
 
     std::string queue = channel->DeclareQueue("kea", false, false, false, false);

+ 3 - 3
hooks/amqp/src/common.h

@@ -5,8 +5,8 @@ extern "C" {
 
 /* From load.cc */
 
-/* Maps of the cablemodem states */
-extern std::map<std::string, std::string> cm_map;
-extern std::map<std::string, std::string> option122_map;
+extern std::string host;
+extern std::string user;
+extern std::string password;
 
 }

+ 17 - 40
hooks/amqp/src/load.cc

@@ -9,54 +9,31 @@
 using namespace isc::hooks;
 using namespace isc::data;
 
-std::map<std::string, std::string> cm_map;
-std::map<std::string, std::string> option122_map;
+std::string host;
+std::string user;
+std::string password;
 
 extern "C" {
 
-int loadMappging(std::string base_map_config, LibraryHandle& handle, std::map<std::string, std::string>& map){
-
-    ConstElementPtr curr_map = handle.getParameter(base_map_config);
-    if (Element::map != curr_map->getType()){
-        LOG_ERROR(runscript_logger, RUNSCRIPT_MISTYPED_PARAM).arg(base_map_config);
+int load(LibraryHandle& handle)
+{
+    ConstElementPtr hostPtr = handle.getParameter("host");
+    ConstElementPtr userPtr = handle.getParameter("user");
+    ConstElementPtr passwordPtr = handle.getParameter("password");
+
+    if (
+        hostPtr->getType() != Element::string ||
+        userPtr->getType() != Element::string ||
+        passwordPtr->getType() != Element::string
+    ) {
         return 1;
     }
 
-    std::map<std::string, ConstElementPtr> map_config = curr_map->mapValue();;
-    std::pair<std::string, ConstElementPtr> me; 
-
-    BOOST_FOREACH(me, map_config) {
-	std::string class_to_map = me.first;
-	std::cout << me.first << "\n";
-	ConstElementPtr table = me.second;
-	if(table->getType() != Element::list){
-		LOG_ERROR(runscript_logger, RUNSCRIPT_MISTYPED_PARAM).arg(base_map_config + "." + me.first);
-		return 1;
-	} 
-	const std::vector<ElementPtr> mac_list = table->listValue();
-	for(int i = 0; i < mac_list.size(); i++) {
-		ElementPtr it = mac_list[i];
-		if(it->getType() != Element::string){
-			LOG_ERROR(runscript_logger, RUNSCRIPT_MISTYPED_PARAM).arg(base_map_config + "." + me.first + "[" + std::to_string(i) + "]");
-			return 1;
-		}
-		std::cout << mac_list[i]->stringValue() << "\n";
-		std::string mac = mac_list[i]->stringValue();
-		std::transform(mac.begin(), mac.end(),mac.begin(), ::toupper);
-
-		map[mac] = class_to_map;
-	}
-    }
+    host = hostPtr->stringValue();
+    user = userPtr->stringValue();
+    password = passwordPtr->stringValue();
 
     return 0;
 }
 
-int load(LibraryHandle& handle) 
-{
-    
-    return 0;
-}
-
-
-
 } // end extern "C"

+ 5 - 0
hooks/mysql/src/common.h

@@ -9,4 +9,9 @@ extern "C" {
 extern std::map<std::string, std::string> cm_map;
 extern std::map<std::string, std::string> option122_map;
 
+extern std::string host;
+extern std::string database;
+extern std::string user;
+extern std::string password;
+
 }

+ 22 - 38
hooks/mysql/src/load.cc

@@ -12,51 +12,35 @@ using namespace isc::data;
 std::map<std::string, std::string> cm_map;
 std::map<std::string, std::string> option122_map;
 
-extern "C" {
+std::string host;
+std::string database;
+std::string user;
+std::string password;
 
-int loadMappging(std::string base_map_config, LibraryHandle& handle, std::map<std::string, std::string>& map){
+extern "C" {
 
-    ConstElementPtr curr_map = handle.getParameter(base_map_config);
-    if (Element::map != curr_map->getType()){
-        LOG_ERROR(runscript_logger, RUNSCRIPT_MISTYPED_PARAM).arg(base_map_config);
+int load(LibraryHandle& handle)
+{
+    ConstElementPtr hostPtr = handle.getParameter("host");
+    ConstElementPtr databasePtr = handle.getParameter("database");
+    ConstElementPtr userPtr = handle.getParameter("user");
+    ConstElementPtr passwordPtr = handle.getParameter("password");
+
+    if (
+        hostPtr->getType() != Element::string ||
+        databasePtr->getType() != Element::string ||
+        userPtr->getType() != Element::string ||
+        passwordPtr->getType() != Element::string
+    ) {
         return 1;
     }
 
-    std::map<std::string, ConstElementPtr> map_config = curr_map->mapValue();;
-    std::pair<std::string, ConstElementPtr> me; 
-
-    BOOST_FOREACH(me, map_config) {
-	std::string class_to_map = me.first;
-	std::cout << me.first << "\n";
-	ConstElementPtr table = me.second;
-	if(table->getType() != Element::list){
-		LOG_ERROR(runscript_logger, RUNSCRIPT_MISTYPED_PARAM).arg(base_map_config + "." + me.first);
-		return 1;
-	} 
-	const std::vector<ElementPtr> mac_list = table->listValue();
-	for(int i = 0; i < mac_list.size(); i++) {
-		ElementPtr it = mac_list[i];
-		if(it->getType() != Element::string){
-			LOG_ERROR(runscript_logger, RUNSCRIPT_MISTYPED_PARAM).arg(base_map_config + "." + me.first + "[" + std::to_string(i) + "]");
-			return 1;
-		}
-		std::cout << mac_list[i]->stringValue() << "\n";
-		std::string mac = mac_list[i]->stringValue();
-		std::transform(mac.begin(), mac.end(),mac.begin(), ::toupper);
-
-		map[mac] = class_to_map;
-	}
-    }
-
-    return 0;
-}
+    host = hostPtr->stringValue();
+    database = databasePtr->stringValue();
+    user = userPtr->stringValue();
+    password = passwordPtr->stringValue();
 
-int load(LibraryHandle& handle) 
-{
-    
     return 0;
 }
 
-
-
 } // end extern "C"

+ 5 - 5
hooks/mysql/src/mysql_connection.cc

@@ -1,22 +1,22 @@
 #include <stdio.h>
 #include <zdb.h>
+#include "common.h"
 #include "mysql_connection.h"
-
 #include <string>
 
 std::string MySQLConnection::executeQuery(const char* query)
 {
-    URL_T url = URL_new("mysql://root:235r2342gtfsw@mysql:3306/fd3_dhcp");
+    URL_T url = URL_new(("mysql://" + user + ":" + password + "@" + host + ":3306/" + database).c_str());
     ConnectionPool_T pool = ConnectionPool_new(url);
     ConnectionPool_start(pool);
     Connection_T con = ConnectionPool_getConnection(pool);
-        
+
     ResultSet_T r = Connection_executeQuery(con, query);
     std::string result = ResultSet_next(r) ? ResultSet_getString(r, 1) : "";
-    
+
     Connection_close(con);
     ConnectionPool_free(&pool);
     URL_free(&url);
-    
+
     return result;
 }