1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
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>
19 #include <anna/diameter/helpers/dcca/defines.hpp>
20 #include <anna/diameter/helpers/dcca/functions.hpp>
21 #include <anna/core/util/defines.hpp>
24 #include <TestCondition.hpp>
27 const char* TestCondition::asText(const Type::_v type)
29 static const char* text [] = { "Generic", "Basic" };
33 bool TestCondition::exists() const throw() {
34 if (a_type == Type::Generic)
35 return (a_regexp != "");
37 return (a_code != "" || a_bitR != "" || a_hopByHop != "" || a_applicationId != "" || a_sessionId != "" || a_resultCode != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
40 bool operator==(const TestCondition &c1, const TestCondition &c2) throw() {
42 if (c1.a_type != c2.a_type) return false;
44 if (c1.a_type == TestCondition::Type::Generic) {
45 if (c1.a_regexp != c2.a_regexp) return false;
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;
62 bool TestCondition::comply(const anna::DataBlock &message/*, bool matchSessionId*/) const throw() {
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);
72 anna::diameter::CommandId cid;
74 if (a_code != "" || a_bitR != "") {
76 cid = anna::diameter::codec::functions::getCommandId(message);
78 catch (anna::RuntimeException &) { return false; }
82 compare = anna::functions::asString(cid.first);
83 if (a_code != compare) return false;
87 compare = (cid.second ? "1":"0");
88 if (a_bitR != compare) return false;
91 if (a_hopByHop != "") {
93 anna::diameter::HopByHop h = anna::diameter::codec::functions::getHopByHop(message);
94 compare = anna::functions::asString(h);
96 catch (anna::RuntimeException &) { return false; }
97 if (a_hopByHop != compare) return false;
100 if (a_applicationId != "") {
102 anna::diameter::ApplicationId a = anna::diameter::codec::functions::getApplicationId(message);
103 compare = anna::functions::asString(a);
105 catch (anna::RuntimeException &) { return false; }
106 if (a_applicationId != compare) return false;
109 //if (matchSessionId) {
110 if (a_sessionId != "") {
112 compare = anna::diameter::helpers::base::functions::getSessionId(message);
114 catch (anna::RuntimeException &) { return false; }
115 if (a_sessionId != compare) return false;
119 if (a_resultCode != "") {
121 anna::U32 rc = anna::diameter::helpers::base::functions::getResultCode(message);
122 compare = anna::functions::asString(rc);
124 catch (anna::RuntimeException &) { return false; }
125 if (a_resultCode != compare) return false;
128 if (a_msisdn != "") {
130 compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
132 catch (anna::RuntimeException &) { return false; }
133 if (a_msisdn != compare) return false;
138 compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
140 catch (anna::RuntimeException &) { return false; }
141 if (a_imsi != compare) return false;
144 if (a_serviceContextId != "") {
146 compare = anna::diameter::helpers::dcca::functions::getServiceContextId(message);
148 catch (anna::RuntimeException &) { return false; }
149 if (a_serviceContextId != compare) return false;
155 anna::xml::Node* TestCondition::asXML(anna::xml::Node* parent) const
157 anna::xml::Node* result = parent->createChild("TestCondition");
158 if (!exists()) return result;
160 if (a_type == Type::Generic) {
161 if (a_regexp != "") result->createAttribute("Regexp", a_regexp);
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);
175 result->createAttribute("ExpectedSource", a_rcvFromEntity ? "entity":"client");