Changes for Sy
[anna.git] / example / diameter / launcher / testing / TestManager.cpp
index 75955ce..fbee74d 100644 (file)
@@ -16,6 +16,7 @@
 #include <anna/app/functions.hpp>
 #include <anna/timex/Engine.hpp>
 #include <anna/diameter/helpers/base/functions.hpp>
+#include <anna/diameter/helpers/dcca/functions.hpp>
 #include <anna/diameter.comm/ClientSession.hpp>
 #include <anna/diameter.comm/ServerSession.hpp>
 
@@ -114,6 +115,21 @@ void TestManager::registerSessionId(const std::string &sessionId, const TestCase
   }
 }
 
+void TestManager::registerSubscriberId(const std::string &subscriberId, const TestCase *testCase)  throw(anna::RuntimeException) {
+
+  std::map<std::string /* subscriber id's */, TestCase*>::const_iterator it = a_subscriberIdTestCaseMap.find(subscriberId);
+  if (it != a_subscriberIdTestCaseMap.end()) { // found
+    unsigned int id = it->second->getId();
+    if (id != testCase->getId()) {
+      throw anna::RuntimeException(anna::functions::asString("There is another test case (id = %llu) which registered such subscriberId: %s", id, subscriberId.c_str()), ANNA_FILE_LOCATION);
+    }
+  }
+  else {
+    a_subscriberIdTestCaseMap[subscriberId] = const_cast<TestCase*>(testCase);
+    LOGDEBUG(anna::Logger::debug(anna::functions::asString("TestManager::registerSubscriberId for test case (id = %llu): %s)", testCase->getId(), subscriberId.c_str()), ANNA_FILE_LOCATION));
+  }
+}
+
 TestTimer* TestManager::createTimer(TestCaseStep* testCaseStep, const anna::Millisecond &timeout, const TestTimer::Type::_v type)
 throw(anna::RuntimeException) {
   TestTimer* result(NULL);
@@ -266,6 +282,7 @@ bool TestManager::clearPool() throw() {
 
   a_testPool.clear();
   a_sessionIdTestCaseMap.clear();
+  a_subscriberIdTestCaseMap.clear();
   a_currentTestIt = a_testPool.end();
   a_poolCycle = 1;
   configureTTPS(0); // stop
@@ -282,6 +299,7 @@ bool TestManager::resetPool(bool hard) throw() {
       result = true;
   }
   //a_sessionIdTestCaseMap.clear();
+  //a_subscriberIdTestCaseMap.clear();
   return result;
 }
 
@@ -362,14 +380,33 @@ TestCase *TestManager::getTestCaseFromSessionId(const anna::DataBlock &message,
   }
   catch (anna::RuntimeException &ex) {
     //ex.trace();
-    LOGWARNING(anna::Logger::warning("Cannot get the Session-Id from received DataBlock in order to identify the Test Case", ANNA_FILE_LOCATION));
+    LOGDEBUG(anna::Logger::debug("Cannot get the Session-Id from received DataBlock in order to identify the Test Case", ANNA_FILE_LOCATION));
     return NULL;
   }
   std::map<std::string /* session id's */, TestCase*>::const_iterator sessionIdIt = a_sessionIdTestCaseMap.find(sessionId);
   if (sessionIdIt != a_sessionIdTestCaseMap.end())
     return sessionIdIt->second;
 
-  LOGWARNING(anna::Logger::warning(anna::functions::asString("Cannot identify the Test Case for received Session-Id: %s", sessionId.c_str()), ANNA_FILE_LOCATION));
+  LOGDEBUG(anna::Logger::debug(anna::functions::asString("Cannot identify the Test Case for received Session-Id: %s", sessionId.c_str()), ANNA_FILE_LOCATION));
+  return NULL;
+}
+
+TestCase *TestManager::getTestCaseFromSubscriberId(const anna::DataBlock &message, std::string &subscriberId) throw() {
+  try {
+    subscriberId = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
+    if (subscriberId == "") // try with IMSI
+      subscriberId = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
+  }
+  catch (anna::RuntimeException &ex) {
+    //ex.trace();
+    LOGDEBUG(anna::Logger::debug("Cannot get the Subscriber-Id from received DataBlock in order to identify the Test Case", ANNA_FILE_LOCATION));
+    return NULL;
+  }
+  std::map<std::string /* subscriber id's */, TestCase*>::const_iterator subscriberIdIt = a_subscriberIdTestCaseMap.find(subscriberId);
+  if (subscriberIdIt != a_subscriberIdTestCaseMap.end())
+    return subscriberIdIt->second;
+
+  LOGDEBUG(anna::Logger::debug(anna::functions::asString("Cannot identify the Test Case for received Subscriber-Id: %s", subscriberId.c_str()), ANNA_FILE_LOCATION));
   return NULL;
 }
 
@@ -379,9 +416,14 @@ void TestManager::receiveMessage(const anna::DataBlock &message, OriginHost *hos
   if (!tests()) return;
 
   // Identify the test case:
-  std::string sessionId;
-  TestCase *tc = getTestCaseFromSessionId(message, sessionId);
-  if (!tc) return;
+  std::string sessionId, subscriberId;
+  TestCase *tc;
+  tc = getTestCaseFromSessionId(message, sessionId);
+  if (!tc) getTestCaseFromSubscriberId(message, subscriberId);
+  if (!tc) {
+    LOGWARNING(anna::Logger::warning(anna::comm::functions::asText("Cannot identify the Test Case for the message received from server: ", message), ANNA_FILE_LOCATION)); // this should not appear
+    return;
+  }
 
   // Work with Test case:
   TestStepWait *tsw = tc->searchNextWaitConditionFulfilled(message, true /* comes from entity */);
@@ -419,9 +461,14 @@ void TestManager::receiveMessage(const anna::DataBlock &message, OriginHost *hos
   if (!tests()) return;
 
   // Identify the test case:
-  std::string sessionId;
-  TestCase *tc = getTestCaseFromSessionId(message, sessionId);
-  if (!tc) return;
+  std::string sessionId, subscriberId;
+  TestCase *tc;
+  tc = getTestCaseFromSessionId(message, sessionId);
+  if (!tc) getTestCaseFromSubscriberId(message, subscriberId);
+  if (!tc) {
+    LOGWARNING(anna::Logger::warning(anna::comm::functions::asText("Cannot identify the Test Case for the message received from client: ", message), ANNA_FILE_LOCATION)); // this should not appear
+    return;
+  }
 
   // Work with Test case:
   TestStepWait *tsw = tc->searchNextWaitConditionFulfilled(message, false /* comes from client */);