Add first work package for REST API implementation
[anna.git] / example / diameter / launcher / EventOperation.cpp
index 2b80529..be0c168 100644 (file)
 
 // Process
 #include <EventOperation.hpp>
+#include <Launcher.hpp>
 
-std::string EventOperation::json2piped(const json &j) {
-  std::string result = "operacion piped";
+// Project
+#include <anna/diameter.comm/OriginHost.hpp>
 
+
+//// Standard
+//#include <sstream>      // std::istringstream
+//#include <iostream>     // std::cout
+//#include <math.h> // ceil
+//#include <climits>
+#include <unistd.h> // chdir
+//#include <stdio.h>
+//
+//// Project
+#include <anna/json/functions.hpp>
+#include <anna/diameter/codec/Message.hpp>
+//#include <anna/timex/Engine.hpp>
+//#include <anna/statistics/Engine.hpp>
+//#include <anna/diameter/codec/functions.hpp>
+//#include <anna/diameter/codec/Engine.hpp>
+//#include <anna/diameter/codec/EngineManager.hpp>
+//#include <anna/http/Transport.hpp>
+//#include <anna/diameter/stack/Engine.hpp>
+//#include <anna/diameter/helpers/base/functions.hpp>
+//#include <anna/time/functions.hpp>
+//#include <anna/diameter.comm/ApplicationMessageOamModule.hpp>
+//#include <anna/testing/defines.hpp>
+#include <anna/xml/xml.hpp>
+//#include <anna/diameter.comm/OriginHost.hpp>
+//#include <anna/diameter.comm/OriginHostManager.hpp>
+//
+//// Process
+//#include <Launcher.hpp>
+//#include <Procedure.hpp>
+//#include <EventOperation.hpp>
+#include <MyDiameterEngine.hpp>
+#include <MyLocalServer.hpp>
+//#include <anna/testing/TestManager.hpp>
+//#include <anna/testing/TestCase.hpp>
+
+
+/////////////////////
+// Node management //
+/////////////////////
+bool EventOperation::node(std::string &response, const std::string & name) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  anna::diameter::comm::OriginHost *workingNode;
+  try { workingNode = my_app.getWorkingNode(); } catch(anna::RuntimeException &ex) { ex.trace(); }
+
+  if (name != "") {
+    if (my_app.setWorkingNode(name)) {
+      response = anna::functions::asString("Forced node is now '%s'", name.c_str());
+      my_app.setOperatedHost(my_app.getWorkingNode()); // now is the new one
+    }
+    else {
+      response = anna::functions::asString("Node '%s' invalid. Nothing done", name.c_str());
+    }
+  }
+  else {
+    if (workingNode) {
+      if (a_http) {
+        response = anna::functions::encodeBase64(workingNode->asXMLString());
+      }
+      else {
+        response = workingNode->asXMLString();
+      }
+    }
+    else {
+      response = "Working node is automatic";
+    }
+  }
+  return true; // OK
+}
+
+bool EventOperation::node_auto(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  my_app.setNodeAuto();
+  response = "Working node has been set to automatic";
+
+  return true; // OK
+}
+
+////////////////////////
+// Parsing operations //
+////////////////////////
+bool EventOperation::code(std::string &response, const std::string & diameterJson) {
+
+  bool success;
+  std::string diameterXml = anna::json::functions::json2xml(diameterJson, success);
+  if (!success) {
+    response += "json to xml failed, unable to encode !";
+    return false;
+  }
+  anna::diameter::codec::Message codecMsg; // auxiliary codec message
+  codecMsg.loadXMLString(diameterXml);
+  response = anna::functions::asHexString(codecMsg.code());
+
+  return true; // OK
+}
+
+bool EventOperation::decode(std::string &response, const std::string & diameterHex) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  anna::DataBlock db_aux(true);
+  anna::functions::fromHexString(diameterHex, db_aux);
+  anna::diameter::codec::Message codecMsg; // auxiliary codec message
+  try {
+    codecMsg.decode(db_aux);
+    std::string xmlString = codecMsg.asXMLString();
+    response = anna::functions::encodeBase64(xmlString);
+  }
+  catch(anna::RuntimeException &ex) { ex.trace(); }
+
+  return true; // OK
+}
+
+bool EventOperation::loadmsg(std::string &response, const std::string & diameterJson) {
+
+  bool success;
+  std::string diameterXml = anna::json::functions::json2xml(diameterJson, success);
+  if (!success) {
+    response += "json to xml failed, unable to load message !";
+    return false;
+  }
+  anna::diameter::codec::Message codecMsg; // auxiliary codec message
+  codecMsg.loadXMLString(diameterXml);
+  response = anna::functions::encodeBase64(codecMsg.asXMLString());
+
+  return true; // OK
+}
+
+/////////////////
+// Hot changes //
+/////////////////
+bool EventOperation::services(std::string &response, const std::string & servicesJson) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  bool success;
+  std::string servicesXml = anna::json::functions::json2xml(servicesJson, success);
+  if (!success) {
+    response += "json to xml failed, unable to load services !";
+    return false;
+  }
+
+  try {
+    my_app.loadServicesFromXMLString(servicesXml, true /* bind entities */);
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "loaded services with errors";
+    return false;
+  }
+  response = "loaded services correctly";
+
+  return true; // OK
+}
+
+bool EventOperation::diameterServerSessions(std::string &response, int sessions) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  try {
+    my_app.getOperatedServer()->setMaxConnections(sessions);
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "fail to operate the server";
+    return false;
+  }
+  response = "new sessions successfully established on operated diameter server";
+
+  return true; // OK
+}
+
+bool EventOperation::change_dir(std::string &response, const std::string & directory) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  std::string dir = directory;
+  if (dir == "") dir = my_app.getInitialWorkingDirectory();
+  bool result = (chdir(dir.c_str()) == 0);
+
+  if (result)
+    response = "New execution directory configured: ";
+  else
+    response = "Cannot assign provided execution directory: ";
+
+  response += dir;
   return result;
 }
+
+////////////////////////////////
+// Client sessions visibility //
+////////////////////////////////
+bool EventOperation::visibility(std::string &response, const std::string & action, const std::string &addressPort, int socket) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  response = "";
+
+  if(addressPort != "") {
+    if(socket != -1) {
+      std::string key = addressPort;
+      key += "|";
+      key += anna::functions::asString(socket);
+
+      if(action == "hide") my_app.getOperatedEngine()->findClientSession(key)->hide();
+      if(action == "show") my_app.getOperatedEngine()->findClientSession(key)->show();
+      if(action == "hidden") response = my_app.getOperatedEngine()->findClientSession(key)->hidden() ? "true" : "false";
+      if(action == "shown") response = my_app.getOperatedEngine()->findClientSession(key)->shown() ? "true" : "false";
+    } else {
+      std::string address;
+      int port;
+      anna::functions::getAddressAndPortFromSocketLiteral(addressPort, address, port);
+
+      if(action == "hide") my_app.getOperatedEngine()->findServer(address, port)->hide();
+      if(action == "show") my_app.getOperatedEngine()->findServer(address, port)->show();
+      if(action == "hidden") response = my_app.getOperatedEngine()->findServer(address, port)->hidden() ? "true" : "false";
+      if(action == "shown") response = my_app.getOperatedEngine()->findServer(address, port)->shown() ? "true" : "false";
+    }
+  } else {
+    if(action == "hide") my_app.getOperatedEntity()->hide();
+    if(action == "show") my_app.getOperatedEntity()->show();
+    if(action == "hidden") response = my_app.getOperatedEntity()->hidden() ? "true" : "false";
+    if(action == "shown") response = my_app.getOperatedEntity()->shown() ? "true" : "false";
+  }
+
+  return true; // OK
+}
+
+
+///////////////
+// Snapshots //
+///////////////
+bool EventOperation::collect(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  my_app.resetCounters();
+  my_app.resetStatistics();
+  response = "All process counters & statistic information have been reset";
+
+  return true; // OK
+}
+
+bool EventOperation::context(std::string &response, const std::string & targetFile) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  std::string contextFile = (targetFile != "") ? targetFile : anna::functions::asString("/var/tmp/anna.context.%05d", my_app.getPid());
+  my_app.writeContext(contextFile);
+  response = anna::functions::asString("Context dumped on file '%s'", contextFile.c_str());
+
+  return true; // OK
+}
+
+bool EventOperation::forceCountersRecord(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  my_app.forceCountersRecord();
+  response = "Current counters have been dump to disk";
+
+  return true; // OK
+}
+
+bool EventOperation::log_statistics_samples(std::string &response, const std::string & list) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  my_app.logStatisticsSamples(list);
+  response = anna::functions::asString("Log statistics samples for '%s' concepts", list.c_str());
+
+  return true; // OK
+}
+
+bool EventOperation::show_oam(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  anna::xml::Node root("root");
+  response = anna::xml::Compiler().apply(my_app.oamAsXML(&root));
+  if (a_http)
+     response = anna::functions::encodeBase64(response);
+
+  return true; // OK
+}
+
+bool EventOperation::show_stats(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+  anna::xml::Node root("root");
+  response = anna::xml::Compiler().apply(my_app.statsAsXML(&root));
+  if (a_http)
+     response = anna::functions::encodeBase64(response);
+
+  return true; // OK
+}
+
+/////////////////////
+// Flow operations //
+/////////////////////
+bool EventOperation::sendmsg2e(std::string &response, const std::string & diameterJson) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::sendmsg2c(std::string &response, const std::string & diameterJson) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::answermsg2e(std::string &response, const std::string & diameterJson) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::answermsg2c(std::string &response, const std::string & diameterJson) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::answermsg2e_action(std::string &response, const std::string & action) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::answermsg2c_action(std::string &response, const std::string & action) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::sendhex2e(std::string &response, const std::string & diameterHex) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::sendhex2c(std::string &response, const std::string & diameterHex) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+
+/////////////////
+// FSM testing //
+/////////////////
+bool EventOperation::test_id__description(std::string &response, unsigned int id, const std::string & description) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__ip_limit(std::string &response, unsigned int id, int amount) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__timeout(std::string &response, unsigned int id, int msecs) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__sendmsg2e(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__sendmsg2c(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__delay(std::string &response, unsigned int id, int msecs) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__sh_command(std::string &response, unsigned int id, const std::string & script) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__waitfe_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__waitfc_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__waitfe_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__waitfc_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__waitfe(std::string &response, unsigned int id, const std::string & condition) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test_id__waitfc(std::string &response, unsigned int id, const std::string & condition) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+/////////////////////////
+// Testcases execution //
+/////////////////////////
+bool EventOperation::test__ttps(std::string &response, int amount) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__next(std::string &response, int syncAmount) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__ip_limit(std::string &response, int amount) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__goto(std::string &response, int id) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__run(std::string &response, int id) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__look(std::string &response, int id) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__state(std::string &response, int id) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__interact(std::string &response, int amount, unsigned int id) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__reset(std::string &response, bool soft_hard, unsigned int id) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__repeats(std::string &response, int amount) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__auto_reset(std::string &response, bool soft_hard) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__initialized(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__finished(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__clear(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__junit(std::string &response, const std::string & targetFile) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__summary_counts(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__summary_states(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__summary(std::string &response) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__report(std::string &response, const std::string & state, bool enable) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__report_hex(std::string &response, bool enable) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+bool EventOperation::test__dump_stdout(std::string &response, bool enable) {
+
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+
+
+
+  return true; // OK
+}
+
+