be0c1683f14e0df49f6752c0cea552c8f169340f
[anna.git] / example / diameter / launcher / EventOperation.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 #include <string>
11
12 // Process
13 #include <EventOperation.hpp>
14 #include <Launcher.hpp>
15
16 // Project
17 #include <anna/diameter.comm/OriginHost.hpp>
18
19
20 //// Standard
21 //#include <sstream>      // std::istringstream
22 //#include <iostream>     // std::cout
23 //#include <math.h> // ceil
24 //#include <climits>
25 #include <unistd.h> // chdir
26 //#include <stdio.h>
27 //
28 //// Project
29 #include <anna/json/functions.hpp>
30 #include <anna/diameter/codec/Message.hpp>
31 //#include <anna/timex/Engine.hpp>
32 //#include <anna/statistics/Engine.hpp>
33 //#include <anna/diameter/codec/functions.hpp>
34 //#include <anna/diameter/codec/Engine.hpp>
35 //#include <anna/diameter/codec/EngineManager.hpp>
36 //#include <anna/http/Transport.hpp>
37 //#include <anna/diameter/stack/Engine.hpp>
38 //#include <anna/diameter/helpers/base/functions.hpp>
39 //#include <anna/time/functions.hpp>
40 //#include <anna/diameter.comm/ApplicationMessageOamModule.hpp>
41 //#include <anna/testing/defines.hpp>
42 #include <anna/xml/xml.hpp>
43 //#include <anna/diameter.comm/OriginHost.hpp>
44 //#include <anna/diameter.comm/OriginHostManager.hpp>
45 //
46 //// Process
47 //#include <Launcher.hpp>
48 //#include <Procedure.hpp>
49 //#include <EventOperation.hpp>
50 #include <MyDiameterEngine.hpp>
51 #include <MyLocalServer.hpp>
52 //#include <anna/testing/TestManager.hpp>
53 //#include <anna/testing/TestCase.hpp>
54
55
56 /////////////////////
57 // Node management //
58 /////////////////////
59 bool EventOperation::node(std::string &response, const std::string & name) {
60
61   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
62
63   anna::diameter::comm::OriginHost *workingNode;
64   try { workingNode = my_app.getWorkingNode(); } catch(anna::RuntimeException &ex) { ex.trace(); }
65
66   if (name != "") {
67     if (my_app.setWorkingNode(name)) {
68       response = anna::functions::asString("Forced node is now '%s'", name.c_str());
69       my_app.setOperatedHost(my_app.getWorkingNode()); // now is the new one
70     }
71     else {
72       response = anna::functions::asString("Node '%s' invalid. Nothing done", name.c_str());
73     }
74   }
75   else {
76     if (workingNode) {
77       if (a_http) {
78         response = anna::functions::encodeBase64(workingNode->asXMLString());
79       }
80       else {
81         response = workingNode->asXMLString();
82       }
83     }
84     else {
85       response = "Working node is automatic";
86     }
87   }
88   return true; // OK
89 }
90
91 bool EventOperation::node_auto(std::string &response) {
92
93   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
94
95   my_app.setNodeAuto();
96   response = "Working node has been set to automatic";
97
98   return true; // OK
99 }
100
101 ////////////////////////
102 // Parsing operations //
103 ////////////////////////
104 bool EventOperation::code(std::string &response, const std::string & diameterJson) {
105
106   bool success;
107   std::string diameterXml = anna::json::functions::json2xml(diameterJson, success);
108   if (!success) {
109     response += "json to xml failed, unable to encode !";
110     return false;
111   }
112   anna::diameter::codec::Message codecMsg; // auxiliary codec message
113   codecMsg.loadXMLString(diameterXml);
114   response = anna::functions::asHexString(codecMsg.code());
115
116   return true; // OK
117 }
118
119 bool EventOperation::decode(std::string &response, const std::string & diameterHex) {
120
121   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
122
123   anna::DataBlock db_aux(true);
124   anna::functions::fromHexString(diameterHex, db_aux);
125   anna::diameter::codec::Message codecMsg; // auxiliary codec message
126   try {
127     codecMsg.decode(db_aux);
128     std::string xmlString = codecMsg.asXMLString();
129     response = anna::functions::encodeBase64(xmlString);
130   }
131   catch(anna::RuntimeException &ex) { ex.trace(); }
132
133   return true; // OK
134 }
135
136 bool EventOperation::loadmsg(std::string &response, const std::string & diameterJson) {
137
138   bool success;
139   std::string diameterXml = anna::json::functions::json2xml(diameterJson, success);
140   if (!success) {
141     response += "json to xml failed, unable to load message !";
142     return false;
143   }
144   anna::diameter::codec::Message codecMsg; // auxiliary codec message
145   codecMsg.loadXMLString(diameterXml);
146   response = anna::functions::encodeBase64(codecMsg.asXMLString());
147
148   return true; // OK
149 }
150
151 /////////////////
152 // Hot changes //
153 /////////////////
154 bool EventOperation::services(std::string &response, const std::string & servicesJson) {
155
156   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
157
158   bool success;
159   std::string servicesXml = anna::json::functions::json2xml(servicesJson, success);
160   if (!success) {
161     response += "json to xml failed, unable to load services !";
162     return false;
163   }
164
165   try {
166     my_app.loadServicesFromXMLString(servicesXml, true /* bind entities */);
167   }
168   catch(anna::RuntimeException &ex) {
169     ex.trace();
170     response += "loaded services with errors";
171     return false;
172   }
173   response = "loaded services correctly";
174
175   return true; // OK
176 }
177
178 bool EventOperation::diameterServerSessions(std::string &response, int sessions) {
179
180   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
181
182   try {
183     my_app.getOperatedServer()->setMaxConnections(sessions);
184   }
185   catch(anna::RuntimeException &ex) {
186     ex.trace();
187     response += "fail to operate the server";
188     return false;
189   }
190   response = "new sessions successfully established on operated diameter server";
191
192   return true; // OK
193 }
194
195 bool EventOperation::change_dir(std::string &response, const std::string & directory) {
196
197   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
198
199   std::string dir = directory;
200   if (dir == "") dir = my_app.getInitialWorkingDirectory();
201   bool result = (chdir(dir.c_str()) == 0);
202
203   if (result)
204     response = "New execution directory configured: ";
205   else
206     response = "Cannot assign provided execution directory: ";
207
208   response += dir;
209   return result;
210 }
211
212 ////////////////////////////////
213 // Client sessions visibility //
214 ////////////////////////////////
215 bool EventOperation::visibility(std::string &response, const std::string & action, const std::string &addressPort, int socket) {
216
217   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
218
219   response = "";
220
221   if(addressPort != "") {
222     if(socket != -1) {
223       std::string key = addressPort;
224       key += "|";
225       key += anna::functions::asString(socket);
226
227       if(action == "hide") my_app.getOperatedEngine()->findClientSession(key)->hide();
228       if(action == "show") my_app.getOperatedEngine()->findClientSession(key)->show();
229       if(action == "hidden") response = my_app.getOperatedEngine()->findClientSession(key)->hidden() ? "true" : "false";
230       if(action == "shown") response = my_app.getOperatedEngine()->findClientSession(key)->shown() ? "true" : "false";
231     } else {
232       std::string address;
233       int port;
234       anna::functions::getAddressAndPortFromSocketLiteral(addressPort, address, port);
235
236       if(action == "hide") my_app.getOperatedEngine()->findServer(address, port)->hide();
237       if(action == "show") my_app.getOperatedEngine()->findServer(address, port)->show();
238       if(action == "hidden") response = my_app.getOperatedEngine()->findServer(address, port)->hidden() ? "true" : "false";
239       if(action == "shown") response = my_app.getOperatedEngine()->findServer(address, port)->shown() ? "true" : "false";
240     }
241   } else {
242     if(action == "hide") my_app.getOperatedEntity()->hide();
243     if(action == "show") my_app.getOperatedEntity()->show();
244     if(action == "hidden") response = my_app.getOperatedEntity()->hidden() ? "true" : "false";
245     if(action == "shown") response = my_app.getOperatedEntity()->shown() ? "true" : "false";
246   }
247
248   return true; // OK
249 }
250
251
252 ///////////////
253 // Snapshots //
254 ///////////////
255 bool EventOperation::collect(std::string &response) {
256
257   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
258
259   my_app.resetCounters();
260   my_app.resetStatistics();
261   response = "All process counters & statistic information have been reset";
262
263   return true; // OK
264 }
265
266 bool EventOperation::context(std::string &response, const std::string & targetFile) {
267
268   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
269
270   std::string contextFile = (targetFile != "") ? targetFile : anna::functions::asString("/var/tmp/anna.context.%05d", my_app.getPid());
271   my_app.writeContext(contextFile);
272   response = anna::functions::asString("Context dumped on file '%s'", contextFile.c_str());
273
274   return true; // OK
275 }
276
277 bool EventOperation::forceCountersRecord(std::string &response) {
278
279   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
280
281   my_app.forceCountersRecord();
282   response = "Current counters have been dump to disk";
283
284   return true; // OK
285 }
286
287 bool EventOperation::log_statistics_samples(std::string &response, const std::string & list) {
288
289   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
290
291   my_app.logStatisticsSamples(list);
292   response = anna::functions::asString("Log statistics samples for '%s' concepts", list.c_str());
293
294   return true; // OK
295 }
296
297 bool EventOperation::show_oam(std::string &response) {
298
299   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
300
301   anna::xml::Node root("root");
302   response = anna::xml::Compiler().apply(my_app.oamAsXML(&root));
303   if (a_http)
304      response = anna::functions::encodeBase64(response);
305
306   return true; // OK
307 }
308
309 bool EventOperation::show_stats(std::string &response) {
310
311   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
312
313   anna::xml::Node root("root");
314   response = anna::xml::Compiler().apply(my_app.statsAsXML(&root));
315   if (a_http)
316      response = anna::functions::encodeBase64(response);
317
318   return true; // OK
319 }
320
321 /////////////////////
322 // Flow operations //
323 /////////////////////
324 bool EventOperation::sendmsg2e(std::string &response, const std::string & diameterJson) {
325
326   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
327
328
329
330   return true; // OK
331 }
332
333 bool EventOperation::sendmsg2c(std::string &response, const std::string & diameterJson) {
334
335   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
336
337
338
339   return true; // OK
340 }
341
342 bool EventOperation::answermsg2e(std::string &response, const std::string & diameterJson) {
343
344   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
345
346
347
348   return true; // OK
349 }
350
351 bool EventOperation::answermsg2c(std::string &response, const std::string & diameterJson) {
352
353   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
354
355
356
357   return true; // OK
358 }
359
360 bool EventOperation::answermsg2e_action(std::string &response, const std::string & action) {
361
362   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
363
364
365
366   return true; // OK
367 }
368
369 bool EventOperation::answermsg2c_action(std::string &response, const std::string & action) {
370
371   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
372
373
374
375   return true; // OK
376 }
377
378 bool EventOperation::sendhex2e(std::string &response, const std::string & diameterHex) {
379
380   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
381
382
383
384   return true; // OK
385 }
386
387 bool EventOperation::sendhex2c(std::string &response, const std::string & diameterHex) {
388
389   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
390
391
392
393   return true; // OK
394 }
395
396
397 /////////////////
398 // FSM testing //
399 /////////////////
400 bool EventOperation::test_id__description(std::string &response, unsigned int id, const std::string & description) {
401
402   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
403
404
405
406   return true; // OK
407 }
408
409 bool EventOperation::test_id__ip_limit(std::string &response, unsigned int id, int amount) {
410
411   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
412
413
414
415   return true; // OK
416 }
417
418 bool EventOperation::test_id__timeout(std::string &response, unsigned int id, int msecs) {
419
420   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
421
422
423
424   return true; // OK
425 }
426
427 bool EventOperation::test_id__sendmsg2e(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) {
428
429   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
430
431
432
433   return true; // OK
434 }
435
436 bool EventOperation::test_id__sendmsg2c(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) {
437
438   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
439
440
441
442   return true; // OK
443 }
444
445 bool EventOperation::test_id__delay(std::string &response, unsigned int id, int msecs) {
446
447   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
448
449
450
451   return true; // OK
452 }
453
454 bool EventOperation::test_id__sh_command(std::string &response, unsigned int id, const std::string & script) {
455
456   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
457
458
459
460   return true; // OK
461 }
462
463 bool EventOperation::test_id__waitfe_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) {
464
465   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
466
467
468
469   return true; // OK
470 }
471
472 bool EventOperation::test_id__waitfc_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) {
473
474   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
475
476
477
478   return true; // OK
479 }
480
481 bool EventOperation::test_id__waitfe_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) {
482
483   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
484
485
486
487   return true; // OK
488 }
489
490 bool EventOperation::test_id__waitfc_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) {
491
492   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
493
494
495
496   return true; // OK
497 }
498
499 bool EventOperation::test_id__waitfe(std::string &response, unsigned int id, const std::string & condition) {
500
501   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
502
503
504
505   return true; // OK
506 }
507
508 bool EventOperation::test_id__waitfc(std::string &response, unsigned int id, const std::string & condition) {
509
510   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
511
512
513
514   return true; // OK
515 }
516
517 /////////////////////////
518 // Testcases execution //
519 /////////////////////////
520 bool EventOperation::test__ttps(std::string &response, int amount) {
521
522   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
523
524
525
526   return true; // OK
527 }
528
529 bool EventOperation::test__next(std::string &response, int syncAmount) {
530
531   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
532
533
534
535   return true; // OK
536 }
537
538 bool EventOperation::test__ip_limit(std::string &response, int amount) {
539
540   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
541
542
543
544   return true; // OK
545 }
546
547 bool EventOperation::test__goto(std::string &response, int id) {
548
549   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
550
551
552
553   return true; // OK
554 }
555
556 bool EventOperation::test__run(std::string &response, int id) {
557
558   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
559
560
561
562   return true; // OK
563 }
564
565 bool EventOperation::test__look(std::string &response, int id) {
566
567   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
568
569
570
571   return true; // OK
572 }
573
574 bool EventOperation::test__state(std::string &response, int id) {
575
576   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
577
578
579
580   return true; // OK
581 }
582
583 bool EventOperation::test__interact(std::string &response, int amount, unsigned int id) {
584
585   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
586
587
588
589   return true; // OK
590 }
591
592 bool EventOperation::test__reset(std::string &response, bool soft_hard, unsigned int id) {
593
594   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
595
596
597
598   return true; // OK
599 }
600
601 bool EventOperation::test__repeats(std::string &response, int amount) {
602
603   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
604
605
606
607   return true; // OK
608 }
609
610 bool EventOperation::test__auto_reset(std::string &response, bool soft_hard) {
611
612   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
613
614
615
616   return true; // OK
617 }
618
619 bool EventOperation::test__initialized(std::string &response) {
620
621   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
622
623
624
625   return true; // OK
626 }
627
628 bool EventOperation::test__finished(std::string &response) {
629
630   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
631
632
633
634   return true; // OK
635 }
636
637 bool EventOperation::test__clear(std::string &response) {
638
639   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
640
641
642
643   return true; // OK
644 }
645
646 bool EventOperation::test__junit(std::string &response, const std::string & targetFile) {
647
648   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
649
650
651
652   return true; // OK
653 }
654
655 bool EventOperation::test__summary_counts(std::string &response) {
656
657   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
658
659
660
661   return true; // OK
662 }
663
664 bool EventOperation::test__summary_states(std::string &response) {
665
666   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
667
668
669
670   return true; // OK
671 }
672
673 bool EventOperation::test__summary(std::string &response) {
674
675   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
676
677
678
679   return true; // OK
680 }
681
682 bool EventOperation::test__report(std::string &response, const std::string & state, bool enable) {
683
684   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
685
686
687
688   return true; // OK
689 }
690
691 bool EventOperation::test__report_hex(std::string &response, bool enable) {
692
693   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
694
695
696
697   return true; // OK
698 }
699
700 bool EventOperation::test__dump_stdout(std::string &response, bool enable) {
701
702   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
703
704
705
706   return true; // OK
707 }
708
709