New st-client deployment. Allow to load same stack id (accumulative services), ignori...
[anna.git] / example / diameter / launcher / Launcher.cpp
index f7921e7..8ea7511 100644 (file)
@@ -132,7 +132,11 @@ Launcher::Launcher() : anna::comm::Application("launcher", "DiameterLauncher", "
 
 
 void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOperation) throw(anna::RuntimeException) {
-  //<!ATTLIST stack id CDATA #REQUIRED dictionary CDATA #REQUIRED>
+
+  CommandLine& cl(anna::CommandLine::instantiate());
+  bool allLogsDisabled = cl.exists("disableLogs");
+
+    //<!ATTLIST stack id CDATA #REQUIRED dictionary CDATA #REQUIRED>
   const anna::xml::Attribute *id, *dictionary;
 
   // <!ATTLIST node originRealm CDATA #REQUIRED applicationId CDATA #REQUIRED originHost CDATA #IMPLIED cer CDATA #IMPLIED dwr CDATA #IMPLIED allowedInactivityTime CDATA #IMPLIED tcpConnectDelay CDATA #IMPLIED answersTimeout CDATA #IMPLIED ceaTimeout CDATA #IMPLIED watchdogPeriod CDATA #IMPLIED entity CDATA #IMPLIED entityServerSessions CDATA #IMPLIED diameterServer CDATA #IMPLIED diameterServerSessions CDATA #IMPLIED balance (yes | no) #IMPLIED sessionBasedModelsClientSocketSelection (SessionIdLowPart | SessionIdHighPart | SessionIdOptionalPart | RoundRobin) #IMPLIED retries CDATA #IMPLIED log CDATA #IMPLIED splitLog (yes | no) #IMPLIED detailedLog (yes | no) #IMPLIED dumpLog (yes | no) #IMPLIED burstLog (yes | no) #IMPLIED>
@@ -140,7 +144,6 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
   *answersTimeout, *ceaTimeout, *watchdogPeriod, *entity, *entityServerSessions,
   *diameterServer, *diameterServerSessions, *balance, *sessionBasedModelsClientSocketSelection,
   *retries, *log, *splitLog, *detailedLog, *dumpLog, *burstLog;
-
   // Never clear services content from here (append new data from xml). At the moment no node removing is implemented in this process
 
   // Stacks
@@ -159,7 +162,6 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
   anna::diameter::comm::ApplicationMessageOamModule & appMsgOamModule = anna::diameter::comm::ApplicationMessageOamModule::instantiate();
   appMsgOamModule.enableCounters(); // this special module is disabled by default (the only)
   static int scope_id = 3;
-  bool id_0_registered = false;
   unsigned int id_value;
   std::string codecEngineName;
 
@@ -172,6 +174,11 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
       dictionary = (*it)->getAttribute("dictionary");
       id_value = id->getIntegerValue();
 
+      if (stackEngine.getDictionary(id_value)) { // Ignore (but don't fail) dictionary load with same stack id already registered
+        LOGWARNING(anna::Logger::warning(anna::functions::asString("Ignore dictionary load for stack id already registered: %llu", id_value), ANNA_FILE_LOCATION));
+        continue;
+      }
+
       try {
         d = stackEngine.createDictionary(id_value, dictionary->getValue());
         LOGDEBUG(anna::Logger::debug(anna::functions::asString("Created dictionary (%p) for stack id %llu", d, id_value), ANNA_FILE_LOCATION));
@@ -185,10 +192,7 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
         throw ex;
       }
 
-      if (id_value == 0) {
-        id_0_registered = true;
-        bpd = d;
-      }
+      bpd = d; // base protocol dictionary in case of monostack. If multistack, will be calculated later
 
       // Create codec engine and register it in the codec engine manager:
       codecEngineName = anna::functions::asString("CodecEngineForStackId_%llu", id_value);
@@ -236,14 +240,10 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
   // Basic checking for multistack:
   bool multistack = (stackEngine.stack_size() > 1);
   if (multistack) {
-    if(!id_0_registered)
+    bpd = stackEngine.getDictionary(0);
+    if(!bpd)
       throw anna::RuntimeException("In multistack applications is mandatory register a stack id = 0 using a dictionary which contains the needed elements to build base protocol messages (CER/A, DWR/A, DPR/A, STR/A, etc.)", ANNA_FILE_LOCATION);
   }
-  else { // monostack
-    if (!bpd)
-      bpd = ce->getDictionary(); // in mono-stack applications, we understand the existing stack as the used
-                                 // for base protocol, regardless if it is registered with stack id 0 or not
-  }
 
   // REALMS:
   for(anna::xml::Node::const_child_iterator it = servicesNode->child_begin(); it != servicesNode->child_end(); it++) {
@@ -356,13 +356,15 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
       }
 
       // Logs:
-      std::string realm = commEngine->getRealm();
-      std::string s_log = realm + ".launcher.log"; if (log) s_log = log->getValue();
-      bool b_splitLog = (splitLog ? (splitLog->getValue() == "yes") : false);
-      bool b_detailedLog = (detailedLog ? (detailedLog->getValue() == "yes") : false);
-      bool b_dumpLog = (dumpLog ? (dumpLog->getValue() == "yes") : false);
-      std::string s_burstLog = realm + ".launcher.burst"; if (burstLog) s_burstLog = burstLog->getValue();
-      a_workingNode->setLogs(s_log, b_splitLog, b_detailedLog, b_dumpLog, s_burstLog);
+      if (!allLogsDisabled) {
+        std::string realm = commEngine->getRealm();
+        std::string s_log = realm + ".launcher.log"; if (log) s_log = log->getValue();
+        bool b_splitLog = (splitLog ? (splitLog->getValue() == "yes") : false);
+        bool b_detailedLog = (detailedLog ? (detailedLog->getValue() == "yes") : false);
+        bool b_dumpLog = (dumpLog ? (dumpLog->getValue() == "yes") : false);
+        std::string s_burstLog = realm + ".launcher.burst"; if (burstLog) s_burstLog = burstLog->getValue();
+        a_workingNode->setLogs(s_log, b_splitLog, b_detailedLog, b_dumpLog, s_burstLog);
+      }
 
 
       // Lazy initialization for comm engine:
@@ -971,12 +973,14 @@ std::string Launcher::help() const throw() {
   result += "\n------------------------------------------------------------------------------------------- Hot changes";
   result += "\n";
   result += "\nservices[|source file]               Adds and starts the services specified in the xml file provided.";
-  result += "\n                                      (if missing, the file 'services.xml' will be used).";
-  result += "\n                                     The last loaded realm node will be automatically the new current";
-  result += "\n                                      working node. This is used to load new nodes once the ADML is";
-  result += "\n                                      started, regardless if '--services' command line parameter was";
-  result += "\n                                      used or not. Those services which are not correctly loaded, will";
-  result += "\n                                      be ignored, keeping the process alive.";
+  result += "\n                                      (if missing, the file 'services.xml' will be used). This is used";
+  result += "\n                                      to load new nodes once the ADML is started, regardless if command";
+  result += "\n                                      line '--services' parameter was used or not. Those services which";
+  result += "\n                                      are not correctly loaded will be ignored to keep the process alive.";
+  result += "\n                                     If you need to load services as deltas, you must firstly load the";
+  result += "\n                                      diameter base dictionary with stack id 0, because all the realms";
+  result += "\n                                      will use this dictionary to encode/decode base protocol messages";
+  result += "\n                                      managed by the communication engine.";
   result += "\n";
   result += "\ndiameterServerSessions|<integer>     Updates the maximum number of accepted connections to diameter";
   result += "\n                                      server socket.";
@@ -990,6 +994,10 @@ std::string Launcher::help() const throw() {
   result += "\n                                     This operation applies over all the registered realm nodes";
   result += "\n                                      except if one specific working node has been set.";
   result += "\nforceCountersRecord                  Forces dump to file the current counters of the process.";
+  result += "\nshow-oam                             Dumps current counters of the process. This is also done at";
+  result += "\n                                      process context dump.";
+  result += "\nshow-stats                           Dumps statistics of the process. This is also done at process";
+  result += "\n                                      context dump.";
   result += "\n";
   result += "\n<visibility action>[|<address>:<port>][|socket id]";
   result += "\n";
@@ -1159,30 +1167,29 @@ std::string Launcher::help() const throw() {
   result += "\n                                                          received from entity (waitfe) or from client (waitfc).";
   result += "\n";
   result += "\n                              wait<fe/fc>-regexp|<regexp>";
-  result += "\n                                                          Wait condition, from entity (waitfe-regexp) or client (waitfc-regexp)";
+  result += "\n                                                         Wait condition, from entity (waitfe-regexp) or client (waitfc-regexp)";
   result += "\n                                                          to match the serialized xml content for received messages. CPU cost";
   result += "\n                                                          is bigger than the former ones because the whole message must be";
   result += "\n                                                          decoded and converted to xml instead of doing a direct hexadecimal";
   result += "\n                                                          buffer search. The main advantage is the great flexibility to identify";
   result += "\n                                                          any content with a regular expression.";
   result += "\n";
-  result += "\n                              sh-command|<script>[|parameters]";
-  result += "\n                                                          External execution for script/executable via shell through an";
-  result += "\n                                                          independent thread, providing the script name and the parameters.";
-  result += "\n                                                          You could use dynamic variables ##<tag> to have more flexibility:";
+  result += "\n                              sh-command|<script>        External execution for script/executable via shell through a dedicated";
+  result += "\n                                                          thread, providing the command and parameters. You could use dynamic";
+  result += "\n                                                          variables ##<tag> to have more flexibility:";
   result += "\n                                                             Test pool cycle id: "; result += SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID;
   result += "\n                                                             Test case id:       "; result += SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID;
   result += "\n                                                             Test step id:       "; result += SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID;
   result += "\n";
-  result += "\n                                                          For example, your command could be something like this:";
-  result += "\n                                                             script:     insert_sql.sh";
-  result += "\n                                                             parameters: -db dbname --verbose > /tmp/cycle-"; result += SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID; result += ".";
-  result += "\n                                                                            testcase-"; result += SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID; result += ".teststep-"; result += SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID; result += ".out";
-  result += "\n";
-  result += "\n                                                          You could also make substitutions on script name: insert_sql_"; result += SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID; result += ".sh";
-  result += "\n";
-  result += "\n                                                          Don't try to redirect stdout and stderr to avoid ADML output contamination";
-  result += "\n                                                          with the possible outputs from the scripts.";
+  result += "\n                                                         For example, your command could be something like this:";
+  result += "\n                                                          insert_sql_"; result += SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID; result += ".sh -db dbname --verbose";
+  result += "\n                                                             > /tmp/cycle-"; result += SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID;
+  result += ".testcase-"; result += SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID;
+  result += ".teststep-"; result += SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID;
+  result += ".out";
+  result += "\n                                                         Try to redirect stdout and stderr to avoid ADML output contamination";
+  result += "\n                                                          with the possible outputs from the scripts. You could also put your";
+  result += "\n                                                          job in background although sh-command will return 0-value immediately.";
   result += "\n";
   result += "\n                           <condition>: Optional parameters which must be fulfilled to continue through the next step.";
   result += "\n                                        Any received message over diameter interfaces will be evaluated against the";
@@ -1308,13 +1315,18 @@ std::string Launcher::help() const throw() {
   result += "\n";
   result += "\n                                 Provide 0 in order to stop the timer triggering.";
   result += "\n";
-  result += "\n                                 There timer manager resolution currently harcoded allows a maximum  of ";
+  result += "\n                                 The timer manager resolution currently harcoded allows a maximum  of ";
   result += anna::functions::asString(1000/a_admlMinResolution); result += " events";
   result += "\n                                 per second. To reach greater rates ADML will join synchronously the needed number of";
   result += "\n                                 new time-triggered test cases per a single event, writting a warning-level trace to";
   result += "\n                                 advice about the risk of burst sendings and recommend launching multiple instances";
   result += "\n                                 to achieve such load with a lower rate per instance.";
   result += "\n";
+  result += "\n   test|next[|<sync-amount>]     Forces the execution of the next test case(s) without waiting for test manager tick.";
+  result += "\n                                 Provide an integer value for 'sync-amount' to send a burst synchronous amount of the";
+  result += "\n                                 next programmed test cases (1 by default). This event works regardless the timer tick";
+  result += "\n                                 function, but it is normally used with the test manager tick stopped.";
+  result += "\n";
   result += "\n   test|ip-limit[|amount]        In-progress limit of test cases. No new test cases will be launched over this value";
   result += "\n                                 (test Manager tick work will be ignored). Zero-value is equivalent to stop the clock.";
   result += "\n                                 tick, -1 is used to specify 'no limit' which is the default. If missing amount, the";
@@ -1335,7 +1347,13 @@ std::string Launcher::help() const throw() {
   result += "\n";
   result += "\n   test|report-hex[|[yes]|no]    Reports could include the diameter messages in hexadecimal format. Disabled by default.";
   result += "\n";
-  result += "\n   test|report-hex[|[yes]|no]    Reports could include the diameter messages in hexadecimal format. Disabled by default.";
+  result += "\n   test|interact|amount|id       Makes interactive a specific test case id. The amount is the margin of execution steps";
+  result += "\n                                 to be done. Normally, we will execute 'test|interact|0|<test case id>', which means that";
+  result += "\n                                 the test case is selected to be interactive, but no step is executed. Then you have to";
+  result += "\n                                 interact with positive amounts (usually 1), executing the provided number of steps if";
+  result += "\n                                 they are ready and fulfill the needed conditions. The value of 0, implies no execution";
+  result += "\n                                 steps margin, which could be useful to 'freeze' a test in the middle of its execution.";
+  result += "\n                                 You could also provide -1 to make it non-interactive resuming it from the current step.";
   result += "\n";
   result += "\n   test|reset|<soft/hard>[|id]   Reset the test case for id provided, all the tests when missing. It could be hard/soft:";
   result += "\n                                 - hard: you probably may need to stop the load rate before. This operation initializes";
@@ -1351,6 +1369,10 @@ std::string Launcher::help() const throw() {
   result += "\n";
   result += "\n   test|clear                    Clears all the programmed test cases and stop testing (if in progress).";
   result += "\n";
+  result += "\n   test|summary                  Test manager general report (number of test cases, counts by state, global configuration,";
+  result += "\n                                 forced in-progress limitation, reports visibility, etc.). Be careful when you have reports";
+  result += "\n                                 dumps enabled because all the programmed test cases will be dump and that could be heavy.";
+  result += "\n";
   result += "\n";
   result += "\nUSING OPERATIONS INTERFACE";
   result += "\n--------------------------";
@@ -1431,6 +1453,18 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     return;
   }
 
+  // OAM & statistics:
+  if(operation == "show-oam") {
+    anna::xml::Node root("root");
+    response_content = anna::xml::Compiler().apply(oamAsXML(&root));
+    return;
+  }
+  if(operation == "show-stats") {
+    anna::xml::Node root("root");
+    response_content = anna::xml::Compiler().apply(statsAsXML(&root));
+    return;
+  }
+
   ///////////////////////////////////////////////////////////////////
   // Tokenize operation
   Tokenizer params;
@@ -1510,7 +1544,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     }
     catch(anna::RuntimeException &ex) {
       ex.trace();
-      response_content = anna::functions::asString("Loaded services from file '%s' with some problems (ignored ones)", servicesFile.c_str());
+      response_content = anna::functions::asString("Loaded services from file '%s' with errors", servicesFile.c_str());
       return;
     }
     response_content = anna::functions::asString("Loaded services from file '%s'", servicesFile.c_str());
@@ -1733,14 +1767,18 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
   } else if((opType == "test")) {
     // test|<id>|<command>[|parameters]   Add a new step to the test case ...
     // test|ttps|<amount>                 Starts/resume the provided number of time ticks per second (ttps). The ADML starts ...
+    // test|next[|<sync-amount>]          Forces the execution of the next test case(s) without waiting for test manager tick ...
     // test|ip-limit[|amount]             In-progress limit of test cases. No new test cases will be launched over this value ...
     // test|repeats|<amount>              Restarts the whole programmed test list when finished the amount number of times ...
     // test|report[|[yes]|no]             Every time a test case is finished a report file in xml format will be created under ...
     // test|report-hex[|[yes]|no]         Reports could include the diameter messages in hexadecimal format. Disabled by default.
     // test|goto|<id>                     Updates current test pointer position.
     // test|look[|id]                     Show programmed test case for id provided, current when missing ...
+    // test|interact|amount|id            Makes interactive a specific test case id. The amount is the margin of execution steps ...
     // test|reset|<soft/hard>[|id]        Reset the test case for id provided, all the tests when missing ...
     // test|clear                         Clears all the programmed test cases.
+    // test|summary                       Test manager general report (number of test cases, counts by state ...
+
 
     if(param1 == "ttps") {
       if (numParams > 2)
@@ -1756,6 +1794,22 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
         opt_response_content += "unable to configure the test rate provided";
       }
     }
+    else if (param1 == "next") {
+      if (numParams > 2)
+        throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
+
+      int sync_amount = ((param2 != "") ? atoi(param2.c_str()) : 1);
+
+      if (sync_amount < 1)
+        throw anna::RuntimeException("The parameter 'sync-amount' must be a positive integer value", ANNA_FILE_LOCATION);
+
+      bool success = testManager.execTestCases(sync_amount);
+
+      opt_response_content = (success ? "" : "not completely " /* completed cycle and no repeats, rare case */);
+      opt_response_content += "processed ";
+      opt_response_content += anna::functions::asString(sync_amount);
+      opt_response_content += ((sync_amount > 1) ? " test cases synchronously" : " test case");
+    }
     else if(param1 == "ip-limit") {
       if (numParams > 2)
         throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
@@ -1777,7 +1831,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       }
     }
     else if(param1 == "repeats") {
-      if (numParams > 2 || numParams < 2)
+      if (numParams != 2)
         throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
       int repeats = atoi(param2.c_str());
       if (repeats < 0) repeats = -1;
@@ -1838,6 +1892,37 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
         }
       }
     }
+    else if (param1 == "interact") {
+      if (numParams != 3)
+        throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
+
+      int amount = atoi(param2.c_str());
+      if (amount < -1)
+        throw anna::RuntimeException("Interactive amount must be -1 (to disable interactive mode) or a positive number.", ANNA_FILE_LOCATION);
+
+      int id = atoi(param3.c_str());
+      TestCase *testCase = testManager.findTestCase(id);
+      if (testCase) {
+        if (amount == -1) {
+          testCase->makeInteractive(false);
+          opt_response_content = "interactive mode disabled";
+        }
+        else {
+          testCase->addInteractiveAmount(amount);
+          opt_response_content = "added interactive amount of ";
+          opt_response_content += anna::functions::asString(amount);
+          opt_response_content += " units";
+          if (amount == 0) opt_response_content += " (0: freezing a non-interactive testcase, no effect on already interactive)";
+        }
+        opt_response_content += " for test case id ";
+        opt_response_content += anna::functions::asString(id);
+      }
+      else {
+        opt_response_content = "cannot found test id (";
+        opt_response_content += anna::functions::asString(id);
+        opt_response_content += ")";
+      }
+    }
     else if(param1 == "reset") {
       if (numParams > 3)
         throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
@@ -1859,7 +1944,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       else {
         if (id == -1) {
           bool anyReset = testManager.resetPool((param2 == "hard") ? true:false);
-          opt_response_content = "reset have been sent to all programmed tests: "; opt_response_content += anyReset ? "some/all was actually reset" : "nothing was reset";
+          opt_response_content = "reset have been sent to all programmed tests: "; opt_response_content += anyReset ? "some/all have been reset" : "nothing was reset";
         }
         else {
           opt_response_content = "cannot found test id (";
@@ -1879,6 +1964,10 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
         opt_response_content = "there are not programmed test cases to be removed";
       }
     }
+    else if(param1 == "summary") {
+      response_content = testManager.asXMLString();
+      return;
+    }
     else {
       int id = atoi(param1.c_str());
       if(id < 0)
@@ -1955,8 +2044,8 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       else if (param2 == "sh-command") {
         if (numParams > 4)
           throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
-        if(param3 == "") throw anna::RuntimeException("Missing script for 'sh-command' in test id operation", ANNA_FILE_LOCATION);
-        testManager.getTestCase(id)->addCmd(param3, param4); // creates / reuses
+        if(param3 == "") throw anna::RuntimeException("Missing script/executable command-line for 'sh-command' in test id operation", ANNA_FILE_LOCATION);
+        testManager.getTestCase(id)->addCommand(param3); // creates / reuses
       }
       else {
         throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
@@ -2074,15 +2163,34 @@ throw() {
     it->second->asXML(result);
   }
 
+  // OAM & statistics:
+  oamAsXML(result);
+  statsAsXML(result);
+
+  // Testing: could be heavy if test case reports are enabled
+  TestManager::instantiate().asXML(result);
+
+  return result;
+}
+
+anna::xml::Node* Launcher::oamAsXML(anna::xml::Node* parent) const
+throw() {
+  anna::xml::Node* result = parent->createChild("Oam");
+
   // OAM:
   anna::diameter::comm::OamModule::instantiate().asXML(result);
   anna::diameter::comm::ApplicationMessageOamModule::instantiate().asXML(result);
   anna::diameter::codec::OamModule::instantiate().asXML(result);
+
+  return result;
+}
+
+anna::xml::Node* Launcher::statsAsXML(anna::xml::Node* parent) const
+throw() {
+  anna::xml::Node* result = parent->createChild("Statistics");
+
   // Statistics:
   anna::statistics::Engine::instantiate().asXML(result);
 
-  // Testing: could be heavy if test case reports are enabled
-  //TestManager::instantiate().asXML(result);
-
   return result;
 }