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 //
13 #include <EventOperation.hpp>
14 #include <Launcher.hpp>
17 #include <anna/diameter.comm/OriginHost.hpp>
21 //#include <sstream> // std::istringstream
22 //#include <iostream> // std::cout
23 //#include <math.h> // ceil
25 #include <unistd.h> // chdir
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>
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>
59 bool EventOperation::node(std::string &response, const std::string & name) {
61 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
63 anna::diameter::comm::OriginHost *workingNode;
64 try { workingNode = my_app.getWorkingNode(); } catch(anna::RuntimeException &ex) { ex.trace(); }
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
72 response = anna::functions::asString("Node '%s' invalid. Nothing done", name.c_str());
78 response = anna::functions::encodeBase64(workingNode->asXMLString());
81 response = workingNode->asXMLString();
85 response = "Working node is automatic";
91 bool EventOperation::node_auto(std::string &response) {
93 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
96 response = "Working node has been set to automatic";
101 ////////////////////////
102 // Parsing operations //
103 ////////////////////////
104 bool EventOperation::code(std::string &response, const std::string & diameterJson) {
107 std::string diameterXml = anna::json::functions::json2xml(diameterJson, success);
109 response += "json to xml failed, unable to encode !";
112 anna::diameter::codec::Message codecMsg; // auxiliary codec message
113 codecMsg.loadXMLString(diameterXml);
114 response = anna::functions::asHexString(codecMsg.code());
119 bool EventOperation::decode(std::string &response, const std::string & diameterHex) {
121 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
123 anna::DataBlock db_aux(true);
124 anna::functions::fromHexString(diameterHex, db_aux);
125 anna::diameter::codec::Message codecMsg; // auxiliary codec message
127 codecMsg.decode(db_aux);
128 std::string xmlString = codecMsg.asXMLString();
129 response = anna::functions::encodeBase64(xmlString);
131 catch(anna::RuntimeException &ex) { ex.trace(); }
136 bool EventOperation::loadmsg(std::string &response, const std::string & diameterJson) {
139 std::string diameterXml = anna::json::functions::json2xml(diameterJson, success);
141 response += "json to xml failed, unable to load message !";
144 anna::diameter::codec::Message codecMsg; // auxiliary codec message
145 codecMsg.loadXMLString(diameterXml);
146 response = anna::functions::encodeBase64(codecMsg.asXMLString());
154 bool EventOperation::services(std::string &response, const std::string & servicesJson) {
156 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
159 std::string servicesXml = anna::json::functions::json2xml(servicesJson, success);
161 response += "json to xml failed, unable to load services !";
166 my_app.loadServicesFromXMLString(servicesXml, true /* bind entities */);
168 catch(anna::RuntimeException &ex) {
170 response += "loaded services with errors";
173 response = "loaded services correctly";
178 bool EventOperation::diameterServerSessions(std::string &response, int sessions) {
180 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
183 my_app.getOperatedServer()->setMaxConnections(sessions);
185 catch(anna::RuntimeException &ex) {
187 response += "fail to operate the server";
190 response = "new sessions successfully established on operated diameter server";
195 bool EventOperation::change_dir(std::string &response, const std::string & directory) {
197 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
199 std::string dir = directory;
200 if (dir == "") dir = my_app.getInitialWorkingDirectory();
201 bool result = (chdir(dir.c_str()) == 0);
204 response = "New execution directory configured: ";
206 response = "Cannot assign provided execution directory: ";
212 ////////////////////////////////
213 // Client sessions visibility //
214 ////////////////////////////////
215 bool EventOperation::visibility(std::string &response, const std::string & action, const std::string &addressPort, int socket) {
217 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
221 if(addressPort != "") {
223 std::string key = addressPort;
225 key += anna::functions::asString(socket);
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";
234 anna::functions::getAddressAndPortFromSocketLiteral(addressPort, address, port);
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";
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";
255 bool EventOperation::collect(std::string &response) {
257 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
259 my_app.resetCounters();
260 my_app.resetStatistics();
261 response = "All process counters & statistic information have been reset";
266 bool EventOperation::context(std::string &response, const std::string & targetFile) {
268 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
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());
277 bool EventOperation::forceCountersRecord(std::string &response) {
279 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
281 my_app.forceCountersRecord();
282 response = "Current counters have been dump to disk";
287 bool EventOperation::log_statistics_samples(std::string &response, const std::string & list) {
289 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
291 my_app.logStatisticsSamples(list);
292 response = anna::functions::asString("Log statistics samples for '%s' concepts", list.c_str());
297 bool EventOperation::show_oam(std::string &response) {
299 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
301 anna::xml::Node root("root");
302 response = anna::xml::Compiler().apply(my_app.oamAsXML(&root));
304 response = anna::functions::encodeBase64(response);
309 bool EventOperation::show_stats(std::string &response) {
311 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
313 anna::xml::Node root("root");
314 response = anna::xml::Compiler().apply(my_app.statsAsXML(&root));
316 response = anna::functions::encodeBase64(response);
321 /////////////////////
322 // Flow operations //
323 /////////////////////
324 bool EventOperation::sendmsg2e(std::string &response, const std::string & diameterJson) {
326 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
333 bool EventOperation::sendmsg2c(std::string &response, const std::string & diameterJson) {
335 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
342 bool EventOperation::answermsg2e(std::string &response, const std::string & diameterJson) {
344 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
351 bool EventOperation::answermsg2c(std::string &response, const std::string & diameterJson) {
353 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
360 bool EventOperation::answermsg2e_action(std::string &response, const std::string & action) {
362 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
369 bool EventOperation::answermsg2c_action(std::string &response, const std::string & action) {
371 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
378 bool EventOperation::sendhex2e(std::string &response, const std::string & diameterHex) {
380 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
387 bool EventOperation::sendhex2c(std::string &response, const std::string & diameterHex) {
389 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
400 bool EventOperation::test_id__description(std::string &response, unsigned int id, const std::string & description) {
402 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
409 bool EventOperation::test_id__ip_limit(std::string &response, unsigned int id, int amount) {
411 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
418 bool EventOperation::test_id__timeout(std::string &response, unsigned int id, int msecs) {
420 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
427 bool EventOperation::test_id__sendmsg2e(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) {
429 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
436 bool EventOperation::test_id__sendmsg2c(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) {
438 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
445 bool EventOperation::test_id__delay(std::string &response, unsigned int id, int msecs) {
447 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
454 bool EventOperation::test_id__sh_command(std::string &response, unsigned int id, const std::string & script) {
456 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
463 bool EventOperation::test_id__waitfe_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) {
465 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
472 bool EventOperation::test_id__waitfc_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) {
474 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
481 bool EventOperation::test_id__waitfe_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) {
483 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
490 bool EventOperation::test_id__waitfc_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) {
492 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
499 bool EventOperation::test_id__waitfe(std::string &response, unsigned int id, const std::string & condition) {
501 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
508 bool EventOperation::test_id__waitfc(std::string &response, unsigned int id, const std::string & condition) {
510 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
517 /////////////////////////
518 // Testcases execution //
519 /////////////////////////
520 bool EventOperation::test__ttps(std::string &response, int amount) {
522 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
529 bool EventOperation::test__next(std::string &response, int syncAmount) {
531 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
538 bool EventOperation::test__ip_limit(std::string &response, int amount) {
540 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
547 bool EventOperation::test__goto(std::string &response, int id) {
549 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
556 bool EventOperation::test__run(std::string &response, int id) {
558 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
565 bool EventOperation::test__look(std::string &response, int id) {
567 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
574 bool EventOperation::test__state(std::string &response, int id) {
576 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
583 bool EventOperation::test__interact(std::string &response, int amount, unsigned int id) {
585 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
592 bool EventOperation::test__reset(std::string &response, bool soft_hard, unsigned int id) {
594 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
601 bool EventOperation::test__repeats(std::string &response, int amount) {
603 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
610 bool EventOperation::test__auto_reset(std::string &response, bool soft_hard) {
612 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
619 bool EventOperation::test__initialized(std::string &response) {
621 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
628 bool EventOperation::test__finished(std::string &response) {
630 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
637 bool EventOperation::test__clear(std::string &response) {
639 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
646 bool EventOperation::test__junit(std::string &response, const std::string & targetFile) {
648 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
655 bool EventOperation::test__summary_counts(std::string &response) {
657 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
664 bool EventOperation::test__summary_states(std::string &response) {
666 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
673 bool EventOperation::test__summary(std::string &response) {
675 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
682 bool EventOperation::test__report(std::string &response, const std::string & state, bool enable) {
684 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
691 bool EventOperation::test__report_hex(std::string &response, bool enable) {
693 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
700 bool EventOperation::test__dump_stdout(std::string &response, bool enable) {
702 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());