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 //
14 #include <anna/core/util/Tokenizer.hpp>
15 #include <anna/core/Exception.hpp>
16 #include <TestManager.hpp>
17 #include <anna/diameter/codec/Message.hpp>
18 #include <anna/diameter/codec/Avp.hpp>
19 #include <anna/diameter/helpers/dcca/functions.hpp>
20 #include <anna/diameter/helpers/dcca/functions.hpp>
23 #include "Procedure.hpp"
24 #include "Launcher.hpp"
28 void usage (std::string &response) {
29 response += "\n\nInvalid arguments. Provide these ones:";
31 response += "\n<initial sequence>|<final sequence>|<digits>|<CCR-I xml file>[|CCR-T xml file]";
33 response += "\nSequences are parsed when needed, over AVPs or internal values:";
35 response += "\nSession-Id: <DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>]";
36 response += "\n \\_fixed_/\\_digits_/ \\_fixed_/\\_digits_/";
38 response += "\nFor example, imagine a 13-digits diameter identity, and a 10-digits optional part.";
39 response += "\nThis procedure will sequence the range for 8 digits in this way:";
41 response += "\n111111<7-digit-sequence>;BAT004;esmdx0900.gxrel10plusrealm.com;901<7-digit-sequence>";
43 response += "\nThe same will be done in MSISDN and IMSI (Subscription-Data AVPs).";
44 response += "\nFramed-IP-Address will be sequenced with a direct correspondence to hex value.";
46 response += "\nThen, you could provide these arguments: \"2000000|9000000|7|CCR-I.xml|CCR-T.xml\"";
51 void Procedure::execute(const std::string &args, std::string &response) throw(anna::RuntimeException) {
53 response = "ERROR : ";
55 anna::Tokenizer targs;
56 targs.apply(args, "|");
58 if (targs.size() < 4) {
63 anna::Tokenizer::const_iterator tok_it = targs.begin();
65 std::string seq_i = anna::Tokenizer::data(tok_it); tok_it++;
66 std::string seq_f = anna::Tokenizer::data(tok_it); tok_it++;
67 std::string digits = anna::Tokenizer::data(tok_it); tok_it++;
68 std::string ccr_i = anna::Tokenizer::data(tok_it); tok_it++;
69 std::string ccr_t = ((tok_it != targs.end()) ? anna::Tokenizer::data(tok_it):"");
70 bool haveTermination = (ccr_t != "");
72 //response = "\n\nInitial sequence: "; response += seq_i; response += "\n";
73 //response += "Final sequence: "; response += seq_f; response += "\n";
74 //response += "Sequence digits: "; response += digits; response += "\n";
75 //response += "CCR-Initial xml: "; response += ccr_i; response += "\n";
76 //if (haveTermination) {
77 // response += "CCR-Termination xml: "; response += ccr_t; response += "\n";
81 unsigned int ll_seq_i = std::atol(seq_i.c_str());
82 unsigned int ll_seq_f = std::atol(seq_f.c_str());
83 unsigned int ll_seq_size = ll_seq_f - ll_seq_i + 1;
84 int i_digits = std::atoi(digits.c_str());
86 if (ll_seq_i > ll_seq_f) {
87 response += "<final sequence> must be greater or equal than <initial sequence>";
91 if (seq_f.size() > i_digits) {
92 response += "<final sequence> must be lesser than number of <digits>";
96 unsigned int ll_seq, ll_index;
97 anna::Millisecond timeout(5000);
100 anna::diameter::codec::Message ccri, ccrt;
101 anna::diameter::codec::Avp *ccri_sessionId, *ccrt_sessionId, *ccri_framedIPAddress, *ccrt_framedIPAddress, *ccri_msisdn, *ccri_imsi;
102 anna::diameter::codec::Avp *si1, *si2, *sidata1, *sidata2, *sitype1, *sitype2;
106 ///////// CCR-Initial:
109 // Session-Id & Framed-Ip-Address AVPs
110 ccri_sessionId = ccri.getAvp("Session-Id");
111 ccri_framedIPAddress = ccri.getAvp("Framed-IP-Address");
113 // Subscription-Id AVPs
114 if (ccri.countAvp("Subscription-Id") != 2) {
115 response += "Both Subscription-Id MSISDN & IMSI Avps must be present in the CCR-Initial provided !";
119 si1 = ccri.getAvp("Subscription-Id", 1, anna::Exception::Mode::Ignore);
120 si2 = ccri.getAvp("Subscription-Id", 2, anna::Exception::Mode::Ignore);
123 response += "Cannot found Subscription-Id MSISDN & IMSI Avps !" ;
127 sidata1 = si1->getAvp("Subscription-Id-Data");
128 sidata2 = si2->getAvp("Subscription-Id-Data");
129 sitype1 = si1->getAvp("Subscription-Id-Type");
130 //sitype2 = si2->getAvp("Subscription-Id-Type");
132 if (sitype1->getEnumerated()->getValue() == anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164) {
133 ccri_msisdn = sidata1;
137 ccri_msisdn = sidata2;
141 ///////// CCR-Termination:
142 if (haveTermination) {
145 // Session-Id & Framed-Ip-Address AVPs
146 ccrt_sessionId = ccrt.getAvp("Session-Id");
147 ccrt_framedIPAddress = ccrt.getAvp("Framed-IP-Address");
150 catch(anna::RuntimeException &ex) {
154 // Prepare session-id string:
155 std::string sessionId = ccri_sessionId->getUTF8String()->getValue();
156 std::size_t last_semicolon = sessionId.rfind(";");
157 anna::Tokenizer tsessionid;
158 tsessionid.apply(sessionId, ";");
160 if (tsessionid.size() < 4) {
161 response += "Session-Id must be in form '<a>;<b>;<c>;<d>'.\n\n";
166 tok_it = tsessionid.begin();
167 std::string d_identity = anna::Tokenizer::data(tok_it); tok_it++; tok_it++; tok_it++;
168 std::string o_part = anna::Tokenizer::data(tok_it);
169 int d_identity_len = d_identity.size();
170 int left_di = d_identity_len - i_digits;
172 response += "Session-Id diameter identity length is lower than selected number of digits ";
173 response += anna::functions::asString("(%d < %d).\n\n", d_identity_len, i_digits);
177 int o_part_len = o_part.size();
178 int left_op = o_part_len - i_digits;
180 response += "Session-Id optional part length is lower than selected number of digits ";
181 response += anna::functions::asString("(%d < %d).\n\n", o_part_len, i_digits);
186 // Idem for MSISDN & IMSI Subscription-Data:
187 std::string msisdn = ccri_msisdn->getUTF8String()->getValue();
188 int msisdn_len = msisdn.size();
189 int left_msisdn = msisdn_len - i_digits;
190 if (left_msisdn < 0) {
191 response += "MSISDN Subscription-Data length is lower than selected number of digits ";
192 response += anna::functions::asString("(%d < %d).\n\n", left_msisdn, i_digits);
196 std::string imsi = ccri_imsi->getUTF8String()->getValue();
197 int imsi_len = imsi.size();
198 int left_imsi = imsi_len - i_digits;
200 response += "IMSI Subscription-Data length is lower than selected number of digits ";
201 response += anna::functions::asString("(%d < %d).\n\n", left_imsi, i_digits);
208 char ndigit_format[32];
209 sprintf(ndigit_format, "%s%d%s", "%0", i_digits, "u");
211 //response += "\nSession-Id original: "; response += sessionId;
212 //response += "\nMSISDN original: "; response += msisdn;
213 //response += "\nIMSI original: "; response += imsi;
216 TestManager &testManager = TestManager::instantiate();
220 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
221 OriginHost *oh = my_app.getWorkingNode();
223 for (ll_index = 0; ll_index < ll_seq_size; ll_index++) {
225 // Calculate next values ////////////////////////////////////////////////////////////
226 ll_seq = ll_seq_i + ll_index;
227 sprintf(cad_aux, ndigit_format, ll_seq);
228 sprintf(cad_framed, "%08x", ll_seq);
230 if (ll_seq % 10000 == 0) {
231 std::cout << "Processing sequence " << ll_seq << std::endl;
234 sessionId.replace(left_di, i_digits, cad_aux);
235 sessionId.replace(last_semicolon + left_op + 1, i_digits, cad_aux);
237 msisdn.replace(left_msisdn, i_digits, cad_aux);
239 imsi.replace(left_imsi, i_digits, cad_aux);
241 //response += "\nSession-Id modified: "; response += sessionId;
242 //response += "\nMSISDN modified: "; response += msisdn;
243 //response += "\nIMSI modified: "; response += imsi;
244 //response += "\nFramedIP modified: "; response += cad_framed;
246 // Update diameter messages /////////////////////////////////////////////////////////
247 ccri_sessionId->getUTF8String()->setValue(sessionId);
248 ccri_framedIPAddress->getOctetString()->fromHexString(cad_framed);
249 ccri_msisdn->getUTF8String()->setValue(msisdn);
250 ccri_imsi->getUTF8String()->setValue(imsi);
252 if (haveTermination) {
253 ccrt_sessionId->getUTF8String()->setValue(sessionId);
254 ccrt_framedIPAddress->getOctetString()->fromHexString(cad_framed);
258 tc = testManager.getTestCase(ll_index+1);
259 // test|__TESTID__|timeout|6000
260 // test|__TESTID__|sendxml2e|ccr-i.xml
261 // test|__TESTID__|waitfe|272|0|||11111__SEQ8__;BAT004;esmdx0900.gxrel10plusrealm.com;90__SEQ8__|2001
262 // test|__TESTID__|sendxml2e|ccr-t.xml
263 // test|__TESTID__|waitfe|272|0|||11111__SEQ8__;BAT004;esmdx0900.gxrel10plusrealm.com;90__SEQ8__|2001
265 // Step 1: timeout 5000 ms:
266 tc->addTimeout(timeout);
268 // Step 2: sendxml2e: CCR-Initial
269 tc->addSendxml2e(ccri.code(), oh, -1 /* 'wait for request' step number for answers */);
271 // Step 3: waitfe: CCA with same session id
272 // PARAM: 1 2 3 4 5 6 7 8 9 10 11
273 // wait<fe/fc>|[code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]
274 tc->addWait(true /* from entity */, "272", "0", "", "", sessionId, "2001", "", "", "");
276 if (haveTermination) {
277 // Step 4: sendxml2e: CCR-Termination
278 tc->addSendxml2e(ccrt.code(), oh, -1 /* 'wait for request' step number for answers */);
280 // Step 5: waitfe: CCA with same session id
281 tc->addWait(true /* from entity */, "272", "0", "", "", sessionId, "2001", "", "", "");
285 response = "Programming complete !";