086a0741ee176333f4a3a51be1cce3069fcf3d2f
[anna.git] / dynamic / launcher / gx / 00001 / Procedure.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 // Standard
9 #include <iostream>
10 #include <string>
11
12 // Project
13 #include <anna/core/util/Tokenizer.hpp>
14 #include <anna/core/Exception.hpp>
15 #include <anna/testing/TestManager.hpp>
16 #include <anna/diameter/codec/Message.hpp>
17 #include <anna/diameter/codec/Avp.hpp>
18 #include <anna/diameter/helpers/dcca/functions.hpp>
19 #include <anna/diameter/helpers/dcca/functions.hpp>
20 #include <anna/diameter.comm/OriginHost.hpp>
21
22 // Local
23 #include "Procedure.hpp"
24
25 namespace {
26
27    void usage (std::string &response) {
28      response += "\n\nInvalid arguments. Provide these ones:";
29      response += "\n";
30      response += "\n<initial sequence>|<final sequence>|<test timeout ms (0: no timeout step)>|<digits>|<CCR-I xml file>[|CCR-T xml file]";
31      response += "\n";
32      response += "\nSequences are parsed when needed, over AVPs or internal values:";
33      response += "\n";
34      response += "\nSession-Id: <DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>]";
35      response += "\n            \\_fixed_/\\_digits_/                            \\_fixed_/\\_digits_/";
36      response += "\n";
37      response += "\nFor example, imagine a 13-digits diameter identity, and a 10-digits optional part.";
38      response += "\nThis procedure will sequence the range for 8 digits in this way:";
39      response += "\n";
40      response += "\n111111<7-digit-sequence>;BAT004;esmdx0900.gxrel10plusrealm.com;901<7-digit-sequence>";
41      response += "\n";
42      response += "\nThe same will be done in MSISDN and IMSI (Subscription-Data AVPs).";
43      response += "\nFramed-IP-Address will be sequenced with a direct correspondence to hex value.";
44      response += "\n";
45      response += "\nThen, you could provide these arguments: \"2000000|9000000|5000|7|CCR-I.xml|CCR-T.xml\"";
46      response += "\n";
47    }
48 }
49
50 void Procedure::execute(const std::string &args, std::string &response, anna::diameter::comm::OriginHost *originHost)  throw(anna::RuntimeException) {
51
52   response = "Dynamic procedure failed to process '"; response += args; response += "': ";
53
54   anna::Tokenizer targs;
55   targs.apply(args, "|");
56
57   if (targs.size() < 5) {
58     usage(response);
59     return;
60   }
61
62   anna::Tokenizer::const_iterator tok_it = targs.begin();
63
64   std::string seq_i = anna::Tokenizer::data(tok_it); tok_it++;
65   std::string seq_f = anna::Tokenizer::data(tok_it); tok_it++;
66   std::string timeout = 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 != "");
71
72   // Test cases cycles:
73   int i_timeout = std::atoi(timeout.c_str());
74   unsigned int ll_seq_i = std::atol(seq_i.c_str());
75   unsigned int ll_seq_f = std::atol(seq_f.c_str());
76   unsigned int ll_seq_size = ll_seq_f - ll_seq_i + 1;
77   int i_digits = std::atoi(digits.c_str());
78
79   if (ll_seq_i > ll_seq_f) {
80     response += "<final sequence> must be greater or equal than <initial sequence>";
81     return;
82   }
83
84   if (seq_f.size() > i_digits) {
85     response += "<final sequence> must be lesser than number of <digits>";
86     return;
87   }
88
89   unsigned int ll_seq, ll_index;
90   anna::Millisecond timeoutMS(i_timeout);
91
92   // Load xml messages:
93   anna::diameter::codec::Message ccri, ccrt;
94   anna::diameter::codec::Avp *ccri_sessionId, *ccrt_sessionId, *ccri_framedIPAddress, *ccrt_framedIPAddress, *ccri_msisdn, *ccri_imsi;
95   anna::diameter::codec::Avp *si1, *si2, *sidata1, *sidata2, *sitype1;
96
97   ///////// CCR-Initial:
98   ccri.loadXML(ccr_i);
99
100   // Session-Id & Framed-Ip-Address AVPs
101   ccri_sessionId = ccri.getAvp("Session-Id");
102   ccri_framedIPAddress = ccri.getAvp("Framed-IP-Address");
103
104   // Subscription-Id AVPs
105   if (ccri.countAvp("Subscription-Id") != 2) {
106     response += "Both Subscription-Id MSISDN & IMSI Avps must be present in the CCR-Initial provided !";
107     return;
108   }
109
110   si1 = ccri.getAvp("Subscription-Id", 1, anna::Exception::Mode::Ignore);
111   si2 = ccri.getAvp("Subscription-Id", 2, anna::Exception::Mode::Ignore);
112
113   if (!si1 || !si2) {
114     response += "Cannot found Subscription-Id MSISDN & IMSI Avps !" ;
115     return;
116   }
117
118   sidata1 = si1->getAvp("Subscription-Id-Data");
119   sidata2 = si2->getAvp("Subscription-Id-Data");
120   sitype1 = si1->getAvp("Subscription-Id-Type");
121   //sitype2 = si2->getAvp("Subscription-Id-Type");
122
123   if (sitype1->getEnumerated()->getValue() == anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164) {
124     ccri_msisdn = sidata1;
125     ccri_imsi = sidata2;
126   }
127   else {
128     ccri_msisdn = sidata2;
129     ccri_imsi = sidata1;
130   }
131
132   ///////// CCR-Termination:
133   if (haveTermination) {
134     ccrt.loadXML(ccr_t);
135
136     // Session-Id & Framed-Ip-Address AVPs
137     ccrt_sessionId = ccrt.getAvp("Session-Id");
138     ccrt_framedIPAddress = ccrt.getAvp("Framed-IP-Address");
139   }
140
141
142   // Prepare session-id string:
143   std::string sessionId = ccri_sessionId->getUTF8String()->getValue();
144   std::size_t last_semicolon = sessionId.rfind(";");
145   anna::Tokenizer tsessionid;
146   tsessionid.apply(sessionId, ";");
147
148   if (tsessionid.size() < 4) {
149     response += "Session-Id must be in form '<a>;<b>;<c>;<d>'.\n\n";
150     usage(response);
151     return;
152   }
153
154   tok_it = tsessionid.begin();
155   std::string d_identity = anna::Tokenizer::data(tok_it); tok_it++; tok_it++; tok_it++;
156   std::string o_part = anna::Tokenizer::data(tok_it);
157   int d_identity_len = d_identity.size();
158   int left_di = d_identity_len - i_digits;
159   if (left_di < 0) {
160     response += "Session-Id diameter identity length is lower than selected number of digits ";
161     response += anna::functions::asString("(%d < %d).\n\n", d_identity_len, i_digits);
162     usage(response);
163     return;
164   }
165   int o_part_len = o_part.size();
166   int left_op = o_part_len - i_digits;
167   if (left_op < 0) {
168     response += "Session-Id optional part length is lower than selected number of digits ";
169     response += anna::functions::asString("(%d < %d).\n\n", o_part_len, i_digits);
170     usage(response);
171     return;
172   }
173
174   // Idem for MSISDN & IMSI Subscription-Data:
175   std::string msisdn = ccri_msisdn->getUTF8String()->getValue();
176   int msisdn_len = msisdn.size();
177   int left_msisdn = msisdn_len - i_digits;
178   if (left_msisdn < 0) {
179     response += "MSISDN Subscription-Data length is lower than selected number of digits ";
180     response += anna::functions::asString("(%d < %d).\n\n", left_msisdn, i_digits);
181     usage(response);
182     return;
183   }
184   std::string imsi = ccri_imsi->getUTF8String()->getValue();
185   int imsi_len = imsi.size();
186   int left_imsi = imsi_len - i_digits;
187   if (left_imsi < 0) {
188     response += "IMSI Subscription-Data length is lower than selected number of digits ";
189     response += anna::functions::asString("(%d < %d).\n\n", left_imsi, i_digits);
190     usage(response);
191     return;
192   }
193
194
195   // Format strings:
196   char ndigit_format[32];
197   sprintf(ndigit_format, "%s%d%s", "%0", i_digits, "u");
198
199   //response += "\nSession-Id original: "; response += sessionId;
200   //response += "\nMSISDN original:     "; response += msisdn;
201   //response += "\nIMSI original:       "; response += imsi;
202
203   // TestManager:
204   anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
205   char cad_aux[16];
206   char cad_framed[16];
207   anna::testing::TestCase *tc;
208
209   for (ll_index = 0; ll_index < ll_seq_size; ll_index++) {
210
211     // Calculate next values ////////////////////////////////////////////////////////////
212     ll_seq = ll_seq_i + ll_index;
213     sprintf(cad_aux, ndigit_format, ll_seq);
214     sprintf(cad_framed, "%08x", ll_seq);
215
216     // Activity indicator:
217     if (ll_seq % 10000 == 0) std::cout << ".";
218
219     sessionId.replace(left_di, i_digits, cad_aux);
220     sessionId.replace(last_semicolon + left_op + 1, i_digits, cad_aux);
221
222     msisdn.replace(left_msisdn, i_digits, cad_aux);
223
224     imsi.replace(left_imsi, i_digits, cad_aux);
225
226     //response += "\nSession-Id modified: "; response += sessionId;
227     //response += "\nMSISDN modified:     "; response += msisdn;
228     //response += "\nIMSI modified:       "; response += imsi;
229     //response += "\nFramedIP modified:   "; response += cad_framed;
230
231     // Update diameter messages /////////////////////////////////////////////////////////
232     ccri_sessionId->getUTF8String()->setValue(sessionId);
233     ccri_framedIPAddress->getOctetString()->fromHexString(cad_framed);
234     ccri_msisdn->getUTF8String()->setValue(msisdn);
235     ccri_imsi->getUTF8String()->setValue(imsi);
236
237     if (haveTermination) {
238       ccrt_sessionId->getUTF8String()->setValue(sessionId);
239       ccrt_framedIPAddress->getOctetString()->fromHexString(cad_framed);
240     }
241
242     // Create testcase:
243     tc = testManager.getTestCase(ll_index+1);
244     //    test|__TESTID__|timeout|6000
245     //    test|__TESTID__|sendxml2e|ccr-i.xml
246     //    test|__TESTID__|waitfe|272|0|||11111__SEQ8__;BAT004;esmdx0900.gxrel10plusrealm.com;90__SEQ8__|2001
247     //    test|__TESTID__|sendxml2e|ccr-t.xml
248     //    test|__TESTID__|waitfe|272|0|||11111__SEQ8__;BAT004;esmdx0900.gxrel10plusrealm.com;90__SEQ8__|2001
249
250     if (i_timeout != 0) {
251       // Step 1: timeout 5000 ms:
252       tc->addTimeout(timeoutMS);
253     }
254
255     // Step 2: sendxml2e: CCR-Initial
256     tc->addSendxml2e(ccri.code(), originHost, -1 /* 'wait for request' step number for answers */);
257
258     // Step 3: waitfe: CCA with same session id
259     // PARAM: 1     2            3      4          5           6             7           8          9       10         11
260     //             wait<fe/fc>|[code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]
261     tc->addWait(true /* from entity */, "272", "0", "", "", sessionId, "2001", "", "", "");
262
263     if (haveTermination) {
264       // Step 4: sendxml2e: CCR-Termination
265       tc->addSendxml2e(ccrt.code(), originHost, -1 /* 'wait for request' step number for answers */);
266
267       // Step 5: waitfe: CCA with same session id
268       tc->addWait(true /* from entity */, "272", "0", "", "", sessionId, "2001", "", "", "");
269     }
270   } // loop
271
272   response = "Completed provision for pid "; response += anna::functions::asString(a_app->getPid()); response += "; range [";
273   response += seq_i; response += ", "; response += seq_f; response += "]; scenary: ";
274   response += "CCR-Initial"; if (haveTermination) response += " + CCR-Termination";
275 }
276