Dynamic lib selection and deployment
[anna.git] / example / diameter / launcher / testing / TestCondition.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Standard
10
11 // Project
12 #include <anna/xml/Node.hpp>
13 #include <anna/xml/Compiler.hpp>
14 #include <anna/diameter/defines.hpp>
15 #include <anna/diameter/codec/functions.hpp>
16 #include <anna/diameter/codec/Message.hpp>
17 #include <anna/diameter/helpers/base/functions.hpp>
18
19 #include <anna/diameter/helpers/dcca/defines.hpp>
20 #include <anna/diameter/helpers/dcca/functions.hpp>
21 #include <anna/core/util/defines.hpp>
22
23 // Process
24 #include <TestCondition.hpp>
25
26
27 const char* TestCondition::asText(const Type::_v type)
28 throw() {
29   static const char* text [] = { "Generic", "Basic" };
30   return text [type];
31 }
32
33 bool TestCondition::exists() const throw() {
34   if (a_type == Type::Generic)
35     return (a_regexp != "");
36   else
37     return (a_code != "" || a_bitR != "" || a_hopByHop != "" || a_applicationId != "" || a_sessionId != "" || a_resultCode != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
38 }
39
40 bool operator==(const TestCondition &c1, const TestCondition &c2) throw() {
41
42   if (c1.a_type != c2.a_type) return false;
43
44   if (c1.a_type == TestCondition::Type::Generic) {
45     if (c1.a_regexp != c2.a_regexp) return false;
46   }
47   else {
48     if (c1.a_code != c2.a_code) return false;
49     if (c1.a_bitR != c2.a_bitR) return false;
50     if (c1.a_hopByHop != c2.a_hopByHop) return false;
51     if (c1.a_applicationId != c2.a_applicationId) return false;
52     if (c1.a_sessionId != c2.a_sessionId) return false;
53     if (c1.a_resultCode != c2.a_resultCode) return false;
54     if (c1.a_msisdn != c2.a_msisdn) return false;
55     if (c1.a_imsi != c2.a_imsi) return false;
56     if (c1.a_serviceContextId != c2.a_serviceContextId) return false;
57   }
58
59   return true;
60 }
61
62 bool TestCondition::comply(const anna::DataBlock &message/*, bool matchSessionId*/) const throw() {
63
64   if (a_type == Type::Generic) {
65     anna::diameter::codec::Message codecMsg;
66     try { codecMsg.decode(message); } catch (anna::RuntimeException &ex) { ex.trace(); }
67     return codecMsg.isLike(a_regexp);
68   }
69
70   // Basic
71   std::string compare;
72   anna::diameter::CommandId cid;
73
74   if (a_code != "" || a_bitR != "") {
75     try {
76       cid = anna::diameter::codec::functions::getCommandId(message);
77     }
78     catch (anna::RuntimeException &) { return false; }
79   }
80
81   if (a_code != "") {
82     compare = anna::functions::asString(cid.first);
83     if (a_code != compare) return false;
84   }
85
86   if (a_bitR != "") {
87     compare = (cid.second ? "1":"0");
88     if (a_bitR != compare) return false;
89   }
90
91   if (a_hopByHop != "") {
92     try {
93       anna::diameter::HopByHop h = anna::diameter::codec::functions::getHopByHop(message);
94       compare = anna::functions::asString(h);
95     }
96     catch (anna::RuntimeException &) { return false; }
97     if (a_hopByHop != compare) return false;
98   }
99
100   if (a_applicationId != "") {
101     try {
102       anna::diameter::ApplicationId a = anna::diameter::codec::functions::getApplicationId(message);
103       compare = anna::functions::asString(a);
104     }
105     catch (anna::RuntimeException &) { return false; }
106     if (a_applicationId != compare) return false;
107   }
108
109   //if (matchSessionId) {
110   if (a_sessionId != "") {
111     try {
112       compare = anna::diameter::helpers::base::functions::getSessionId(message);
113     }
114     catch (anna::RuntimeException &) { return false; }
115     if (a_sessionId != compare) return false;
116   }
117   //}
118
119   if (a_resultCode != "") {
120     try {
121       anna::U32 rc = anna::diameter::helpers::base::functions::getResultCode(message);
122       compare = anna::functions::asString(rc);
123     }
124     catch (anna::RuntimeException &) { return false; }
125     if (a_resultCode != compare) return false;
126   }
127
128   if (a_msisdn != "") {
129     try {
130       compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
131     }
132     catch (anna::RuntimeException &) { return false; }
133     if (a_msisdn != compare) return false;
134   }
135
136   if (a_imsi != "") {
137     try {
138       compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
139     }
140     catch (anna::RuntimeException &) { return false; }
141     if (a_imsi != compare) return false;
142   }
143
144   if (a_serviceContextId != "") {
145     try {
146       compare = anna::diameter::helpers::dcca::functions::getServiceContextId(message);
147     }
148     catch (anna::RuntimeException &) { return false; }
149     if (a_serviceContextId != compare) return false;
150   }
151
152   return true;
153 }
154
155 anna::xml::Node* TestCondition::asXML(anna::xml::Node* parent) const
156 throw() {
157   anna::xml::Node* result = parent->createChild("TestCondition");
158   if (!exists()) return result;
159
160   if (a_type == Type::Generic) {
161     if (a_regexp != "") result->createAttribute("Regexp", a_regexp);
162   }
163   else {
164     if (a_code != "") result->createAttribute("Code", atoi(a_code.c_str()));
165     if (a_bitR != "") result->createAttribute("BitR", ((a_bitR == "1") ? "yes":"no"));
166     if (a_hopByHop != "") result->createAttribute("HopByHop", atoll(a_hopByHop.c_str()));
167     if (a_applicationId != "") result->createAttribute("ApplicationId", atoll(a_applicationId.c_str()));
168     if (a_sessionId != "") result->createAttribute("SessionId", a_sessionId);
169     if (a_resultCode != "") result->createAttribute("ResultCode", atoi(a_resultCode.c_str()));
170     if (a_msisdn != "") result->createAttribute("Msisdn", a_msisdn);
171     if (a_imsi != "") result->createAttribute("Imsi", a_imsi);
172     if (a_serviceContextId != "") result->createAttribute("ServiceContextId", a_serviceContextId);
173   }
174
175   result->createAttribute("ExpectedSource", a_rcvFromEntity ? "entity":"client");
176
177   return result;
178 }