Implement dynamic procedure at REST interface
[anna.git] / dynamic / launcher / gx / 00001 / Procedure.cpp
index 086a074..042efee 100644 (file)
@@ -18,6 +18,7 @@
 #include <anna/diameter/helpers/dcca/functions.hpp>
 #include <anna/diameter/helpers/dcca/functions.hpp>
 #include <anna/diameter.comm/OriginHost.hpp>
+#include <anna/diameter.comm/OriginHostManager.hpp>
 
 // Local
 #include "Procedure.hpp"
@@ -27,8 +28,19 @@ namespace {
    void usage (std::string &response) {
      response += "\n\nInvalid arguments. Provide these ones:";
      response += "\n";
+     response += "\nSIGUSR2 Interface:";
      response += "\n<initial sequence>|<final sequence>|<test timeout ms (0: no timeout step)>|<digits>|<CCR-I xml file>[|CCR-T xml file]";
      response += "\n";
+     response += "\nREST Interface:";
+     response += "\n{";
+     response += "\n   \"seqI\":\"<initial sequence>\"";
+     response += "\n   ,\"seqF\":\"<final sequence>\"";
+     response += "\n   ,\"msecsTimeout\":\"<test timeout ms (0: no timeout step)>\"";
+     response += "\n   ,\"digits\":\"<digits>\"";
+     response += "\n   ,\"ccrI\":\"<CCR-I xml file>\"";
+     response += "\n   [,\"ccrT\":\"<CCR-T xml file>\"]";
+     response += "\n}";
+     response += "\n";
      response += "\nSequences are parsed when needed, over AVPs or internal values:";
      response += "\n";
      response += "\nSession-Id: <DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>]";
@@ -47,7 +59,7 @@ namespace {
    }
 }
 
-void Procedure::execute(const std::string &args, std::string &response, anna::diameter::comm::OriginHost *originHost)  throw(anna::RuntimeException) {
+void Procedure::execute(const std::string &args, std::string &response)  throw(anna::RuntimeException) {
 
   response = "Dynamic procedure failed to process '"; response += args; response += "': ";
 
@@ -93,13 +105,15 @@ void Procedure::execute(const std::string &args, std::string &response, anna::di
   anna::diameter::codec::Message ccri, ccrt;
   anna::diameter::codec::Avp *ccri_sessionId, *ccrt_sessionId, *ccri_framedIPAddress, *ccrt_framedIPAddress, *ccri_msisdn, *ccri_imsi;
   anna::diameter::codec::Avp *si1, *si2, *sidata1, *sidata2, *sitype1;
+  anna::diameter::codec::Avp *ccri_originHost;
 
   ///////// CCR-Initial:
-  ccri.loadXML(ccr_i);
+  ccri.loadXMLFile(ccr_i);
 
   // Session-Id & Framed-Ip-Address AVPs
   ccri_sessionId = ccri.getAvp("Session-Id");
   ccri_framedIPAddress = ccri.getAvp("Framed-IP-Address");
+  ccri_originHost = ccri.getAvp("Origin-Host");
 
   // Subscription-Id AVPs
   if (ccri.countAvp("Subscription-Id") != 2) {
@@ -131,7 +145,7 @@ void Procedure::execute(const std::string &args, std::string &response, anna::di
 
   ///////// CCR-Termination:
   if (haveTermination) {
-    ccrt.loadXML(ccr_t);
+    ccrt.loadXMLFile(ccr_t);
 
     // Session-Id & Framed-Ip-Address AVPs
     ccrt_sessionId = ccrt.getAvp("Session-Id");
@@ -206,6 +220,12 @@ void Procedure::execute(const std::string &args, std::string &response, anna::di
   char cad_framed[16];
   anna::testing::TestCase *tc;
 
+  // Origin host manager:
+  anna::diameter::comm::OriginHostManager &ohm = anna::diameter::comm::OriginHostManager::instantiate();
+  // Assume the oh name from CCR-I (same as CCR-T):
+  std::string originHostName = ccri_originHost->getDiameterIdentity()->getValue();
+  anna::diameter::comm::OriginHost *originHost = ohm.getOriginHost(originHostName);
+
   for (ll_index = 0; ll_index < ll_seq_size; ll_index++) {
 
     // Calculate next values ////////////////////////////////////////////////////////////
@@ -253,24 +273,46 @@ void Procedure::execute(const std::string &args, std::string &response, anna::di
     }
 
     // Step 2: sendxml2e: CCR-Initial
-    tc->addSendxml2e(ccri.code(), originHost, -1 /* 'wait for request' step number for answers */);
+    tc->addSendDiameterXml2e(ccri.code(), originHost, -1 /* 'wait for request' step number for answers */);
 
     // Step 3: waitfe: CCA with same session id
     // PARAM: 1     2            3      4          5           6             7           8          9       10         11
     //             wait<fe/fc>|[code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]
-    tc->addWait(true /* from entity */, "272", "0", "", "", sessionId, "2001", "", "", "");
+    tc->addWaitDiameter(true /* from entity */, "272", "0", "", "", sessionId, "2001", "", "", "");
 
     if (haveTermination) {
       // Step 4: sendxml2e: CCR-Termination
-      tc->addSendxml2e(ccrt.code(), originHost, -1 /* 'wait for request' step number for answers */);
+      tc->addSendDiameterXml2e(ccrt.code(), originHost, -1 /* 'wait for request' step number for answers */);
 
       // Step 5: waitfe: CCA with same session id
-      tc->addWait(true /* from entity */, "272", "0", "", "", sessionId, "2001", "", "", "");
+      tc->addWaitDiameter(true /* from entity */, "272", "0", "", "", sessionId, "2001", "", "", "");
     }
   } // loop
 
-  response = "Completed provision for pid "; response += anna::functions::asString(a_app->getPid()); response += "; range [";
+  response = "Completed provision: range [";
   response += seq_i; response += ", "; response += seq_f; response += "]; scenary: ";
   response += "CCR-Initial"; if (haveTermination) response += " + CCR-Termination";
 }
 
+void Procedure::execute(const nlohmann::json &args, std::string &response)  throw(anna::RuntimeException) {
+
+  // Build the arguments string and call the previous centralized logic procedure execution:
+  //   <initial sequence>|<final sequence>|<test timeout ms (0: no timeout step)>|<digits>|<CCR-I xml file>[|CCR-T xml file]
+  const char *arg_names[6] = { "seqI", "seqF", "msecsTimeout", "digits", "ccrI", "ccrT" };
+  std::string args_string, arg, pipe("|");
+
+  for (int i = 0; i < 6; i++)
+  {
+    auto it = args.find(arg_names[i]);
+    arg = (it != args.end() && it->is_string()) ? *it : "";
+    if (arg != "") args_string += arg + pipe;
+  }
+
+  // Remove last 'pipe':
+  if (args_string != "")
+    args_string = args_string.substr(0, args_string.size()-1);
+
+  execute(args_string, response);
+}
+
+