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 //
16 #include <anna/http/Request.hpp>
17 #include <anna/core/functions.hpp>
20 #include <MyHandler.hpp>
21 #include <Launcher.hpp>
22 #include <EventOperation.hpp>
24 void MyHandler::splitURI(const std::string &uri, std::string & operation, std::string & param1, std::string & param2) const {
26 std::string::size_type slash_pos = uri.find("/", 1u);
27 if (slash_pos == std::string::npos) {
28 operation = uri.substr(0);
31 operation = uri.substr(0, slash_pos);
32 param1 = uri.substr(slash_pos + 1);
34 std::string::size_type slash2_pos = uri.find("/", slash_pos + 1);
35 if (slash2_pos != std::string::npos) {
36 param1 = uri.substr(slash_pos + 1, slash2_pos - slash_pos - 1);
37 param2 = uri.substr(slash2_pos + 1);
42 void MyHandler::sendResponse(anna::comm::ClientSocket& clientSocket, anna::http::Response *response)
45 clientSocket.send(*response);
46 } catch(Exception& ex) {
51 void MyHandler::evRequest(anna::comm::ClientSocket& clientSocket, const anna::http::Request& request)
52 throw(anna::RuntimeException) {
54 const anna::DataBlock& body = request.getBody();
55 anna::http::Method::Type::_v method = request.getMethod();
56 std::string uri = request.getURI();
57 bool isGET = (method == anna::http::Method::Type::Get);
58 bool isPOST = (method == anna::http::Method::Type::Post);
59 std::string body_content;
60 nlohmann::json json_body;
63 anna::http::Response* response = allocateResponse();
64 response->setStatusCode(200);
67 if(body.getSize() != 0)
68 response->setStatusCode(400); // bad request
71 if(body.getSize() != 0) {
72 body_content.assign(body.getData(), body.getSize()); // assign body content:
75 json_body = nlohmann::json::parse(body_content);
77 std::string msg("Json body received:\n\n");
78 msg += json_body.dump(4); // pretty print json body
79 anna::Logger::information(msg, ANNA_FILE_LOCATION);
82 catch (nlohmann::json::parse_error& e)
84 ss << "Json body parse error: " << e.what() << '\n'
85 << "exception id: " << e.id << '\n'
86 << "byte position of error: " << e.byte << std::endl;
87 anna::Logger::error(ss.str(), ANNA_FILE_LOCATION);
92 response->setStatusCode(405); // method not allowed
95 //////////////////////
96 // Prepare response //
97 //////////////////////
100 http::Header* contentType =response->find(http::Header::Type::ContentType);
101 if(contentType == NULL)
102 contentType = response->createHeader(http::Header::Type::ContentType);
103 contentType->setValue("application/json");
106 std::string response_content = "Internal error (check ADML traces): ";
107 bool success = false;
109 success = doGET(uri, response_content);
113 success = doPOST(uri, json_body, response_content);
115 catch(anna::RuntimeException &ex) {
117 LOGINFORMATION(anna::Logger::information("XXXXXXXXXXXX EXCEPCION PENDIENTE DE CATCHEAR ABAJO !!! XXXXXXXXXXXXXXXXX", ANNA_FILE_LOCATION));
121 ss << R"({ "success":")" << (success ? "true":"false")
122 << R"(", "response": )" << std::quoted(response_content);
125 anna::DataBlock db_content(true);
126 db_content = ss.str();
127 response->setBody(db_content);
129 sendResponse(clientSocket, response);
132 bool MyHandler::doGET(const std::string &uri, std::string &response) {
136 EventOperation eop(true /* is HTTP */);
139 if (uri == "/show-oam") {
140 result = eop.show_oam(response);
142 else if (uri == "/show-stats") {
143 result = eop.show_stats(response);
149 bool MyHandler::doPOST(const std::string &uri, const nlohmann::json &j, std::string &response) {
153 EventOperation eop(true /* is HTTP */);
155 std::string opType{}, param1{}, param2{};
156 splitURI(uri, opType, param1, param2);
159 if (opType == "/node") {
160 auto it = j.find("name");
162 result = eop.node(response, *it);
164 response += "missing 'name' string field";
166 else if (opType == "/node-auto") {
167 result = eop.node_auto(response);
170 // Parsing operations
171 else if (opType == "/code") {
172 auto it = j.find("diameterJson");
174 result = eop.code(response, it->dump(4)); // get the object as string (always indentation = 4)
176 response += "missing 'diameterJson' object field";
178 else if (opType == "/decode") {
179 auto it = j.find("diameterHex");
181 result = eop.decode(response, *it);
183 response += "missing 'diameterHex' string field";
185 else if (opType == "/loadmsg") {
186 auto it = j.find("diameterJson");
188 result = eop.loadmsg(response, it->dump(4)); // get the object as string (always indentation = 4)
190 response += "missing 'diameterJson' object field";
194 else if (opType == "/services") {
195 auto it = j.find("servicesJson");
197 result = eop.services(response, it->dump(4)); // get the object as string (always indentation = 4)
200 response += "missing 'servicesJson' object field";
202 else if (opType == "/diameterServerSessions") {
203 auto it = j.find("sessions");
205 result = eop.diameterServerSessions(response, it->get<int>());
207 response += "missing 'session' integer field";
209 else if (opType == "/change-dir") {
210 auto it = j.find("directory");
211 std::string directory = (it != j.end()) ? *it : ""; // default is: restore initial directory
212 result = eop.change_dir(response, directory);
215 // Client sessions visibility
216 else if (opType == "/visibility") {
217 auto it = j.find("action");
219 std::string action = *it;
221 it = j.find("addressPort");
222 std::string addressPort = (it != j.end()) ? *it : "";
224 it = j.find("socket");
225 int socket = (it != j.end()) ? it->get<int>() : -1;
227 result = eop.visibility(response, action, addressPort, socket);
230 response += "missing 'action' string field";
234 else if (opType == "/collect") {
235 result = eop.collect(response);
237 else if (opType == "/context") {
238 auto it = j.find("targetFile");
239 std::string targetFile = (it != j.end()) ? *it : "";
240 result = eop.context(response, targetFile);
242 else if (opType == "/forceCountersRecord") {
243 result = eop.forceCountersRecord(response);
245 else if (opType == "/log-statistics-samples") {
246 auto it = j.find("list");
248 result = eop.log_statistics_samples(response, *it);
250 response += "missing 'list' string field";
254 else if ((opType == "/sendmsg2e")||(opType == "/sendmsg2c")) {
255 auto it = j.find("diameterJson");
257 if (opType == "/sendmsg2e")
258 result = eop.sendmsg2e(response, it->dump(4)); // get the object as string (always indentation = 4)
260 result = eop.sendmsg2c(response, it->dump(4)); // get the object as string (always indentation = 4)
262 response += "missing 'diameterJson' object field";
264 else if ((opType == "/answermsg2e")||(opType == "/answermsg2c")) {
265 auto itJ = j.find("diameterJson");
266 auto itA = j.find("action");
267 bool hasJ = (itJ != j.end());
268 bool hasA = (itA != j.end());
270 if (hasJ != hasA) { // XOR
272 if (opType == "/answermsg2e") {
274 result = eop.answermsg2e(response, itJ->dump(4)); // get the object as string (always indentation = 4)
276 result = eop.answermsg2e_action(response, *itA);
280 result = eop.answermsg2c(response, itJ->dump(4)); // get the object as string (always indentation = 4)
282 result = eop.answermsg2c_action(response, *itA);
286 response += "missing 'diameterJson' object or 'action' string field (only one accepted)";
288 else if ((opType == "/sendhex2e")||(opType == "/sendhex2c")) {
289 auto it = j.find("diameterHex");
291 if (opType == "/sendhex2e")
292 result = eop.sendhex2e(response, *it);
294 result = eop.sendhex2c(response, *it);
296 response += "missing 'diameterHex' string field";
300 // test_id__<command>
301 else if (opType == "/testid-description") {
302 auto it = j.find("description");
304 result = eop.test_id__description(response, atoi(param1.c_str()), *it);
306 response += "missing 'description' string field";
308 else if (opType == "/testid-ip-limit") {
309 auto it = j.find("amount");
310 int amount = (it != j.end()) ? it->get<int>() : 1;
311 result = eop.test_id__ip_limit(response, atoi(param1.c_str()), amount);
313 else if (opType == "/testid-timeout") {
314 auto it = j.find("msecs");
316 result = eop.test_id__timeout(response, atoi(param1.c_str()), it->get<int>());
318 response += "missing 'msecs' integer field";
320 else if ((opType == "/testid-sendmsg2e")||(opType == "/testid-sendmsg2c")) {
321 auto it = j.find("diameterJson");
324 auto itS = j.find("stepNumber");
325 int stepNumber = (itS != j.end()) ? itS->get<int>() : -1;
327 if (opType == "/testid-sendmsg2e")
328 result = eop.test_id__sendmsg2e(response, atoi(param1.c_str()), it->dump(4), stepNumber); // get the object as string (always indentation = 4)
330 result = eop.test_id__sendmsg2c(response, atoi(param1.c_str()), it->dump(4), stepNumber); // get the object as string (always indentation = 4)
333 response += "missing 'diameterJson' object field";
335 else if (opType == "/testid-delay") {
336 auto it = j.find("msecs");
338 result = eop.test_id__delay(response, atoi(param1.c_str()), it->get<int>());
340 response += "missing 'msecs' integer field";
342 else if (opType == "/testid-sh-command") {
343 auto it = j.find("script");
345 result = eop.test_id__sh_command(response, atoi(param1.c_str()), *it);
347 response += "missing 'script' string field";
349 else if ((opType == "/testid-waitfe-hex")||(opType == "/testid-waitfc-hex")) {
350 auto it = j.find("hex");
353 auto itS = j.find("strict");
354 bool strict = (itS != j.end()) ? (*itS == "true") : false;
356 if (opType == "/testid-waitfe-hex")
357 result = eop.test_id__waitfe_hex(response, atoi(param1.c_str()), *it, strict);
359 result = eop.test_id__waitfc_hex(response, atoi(param1.c_str()), *it, strict);
362 response += "missing 'hex' string field";
364 else if ((opType == "/testid-waitfe-msg")||(opType == "/testid-waitfc-msg")) {
365 auto it = j.find("diameterJson");
368 auto itS = j.find("strict");
369 bool strict = (itS != j.end()) ? (*itS == "true") : false;
371 if (opType == "/testid-waitfe-msg")
372 result = eop.test_id__waitfe_msg(response, atoi(param1.c_str()), it->dump(4), strict); // get the object as string (always indentation = 4)
374 result = eop.test_id__waitfc_msg(response, atoi(param1.c_str()), it->dump(4), strict); // get the object as string (always indentation = 4)
377 response += "missing 'diameterJson' object field";
379 else if ((opType == "/testid-waitfe")||(opType == "/testid-waitfc")) {
380 auto it = j.find("condition");
382 // [code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]
383 auto it_code = it->find("code");
384 auto it_bitR = it->find("bitR");
385 auto it_hopByHop = it->find("hopByHop");
386 auto it_applicationId = it->find("applicationId");
387 auto it_sessionId = it->find("sessionId");
388 auto it_resultCode = it->find("resultCode");
389 auto it_msisdn = it->find("msisdn");
390 auto it_imsi = it->find("imsi");
391 auto it_serviceContextId = it->find("serviceContextId");
393 std::string condition;
394 condition += (it_code != it->end()) ? *it_code : ""; condition += "|";
395 condition += (it_bitR != it->end()) ? *it_bitR : ""; condition += "|";
396 condition += (it_hopByHop != it->end()) ? *it_hopByHop : ""; condition += "|";
397 condition += (it_applicationId != it->end()) ? *it_applicationId : ""; condition += "|";
398 condition += (it_sessionId != it->end()) ? *it_sessionId : ""; condition += "|";
399 condition += (it_resultCode != it->end()) ? *it_resultCode : ""; condition += "|";
400 condition += (it_msisdn != it->end()) ? *it_msisdn : ""; condition += "|";
401 condition += (it_imsi != it->end()) ? *it_imsi : ""; condition += "|";
402 condition += (it_serviceContextId != it->end()) ? *it_serviceContextId : "";
404 if (opType == "/testid-waitfe")
405 result = eop.test_id__waitfe(response, atoi(param1.c_str()), condition);
407 result = eop.test_id__waitfc(response, atoi(param1.c_str()), condition);
410 response += "missing 'condition' object field";
413 // Testcases execution
415 else if (opType == "/test-ttps") {
416 auto it = j.find("amount");
418 result = eop.test__ttps(response, it->get<int>());
420 response += "missing 'amount' integer field";
422 else if (opType == "/test-next") {
423 auto it = j.find("syncAmount");
424 int syncAmount = (it != j.end()) ? it->get<int>() : 1;
425 result = eop.test__next(response, syncAmount);
427 else if (opType == "/test-ip-limit") {
428 auto it = j.find("amount");
429 int amount = (it != j.end()) ? it->get<int>() : -2; // default is: show current ip-limit and in-progress test cases amount
430 result = eop.test__ip_limit(response, amount);
432 else if (opType == "/test-goto") {
433 auto it = j.find("id");
435 result = eop.test__goto(response, it->get<int>());
437 response += "missing 'id' integer field";
439 else if (opType == "/test-run") {
440 auto it = j.find("id");
442 result = eop.test__run(response, it->get<int>());
444 response += "missing 'id' integer field";
446 else if (opType == "/test-look") {
447 auto it = j.find("id");
448 int id = (it != j.end()) ? it->get<int>() : -1; // default is: current
449 result = eop.test__look(response, id);
451 else if (opType == "/test-state") {
452 auto it = j.find("id");
453 int id = (it != j.end()) ? it->get<int>() : -1; // default is: current
454 result = eop.test__state(response, id);
456 else if (opType == "/test-interact") {
457 auto it = j.find("amount");
460 auto itI = j.find("id");
461 int id = (itI != j.end()) ? itI->get<int>() : -1; // default is: current
463 result = eop.test__interact(response, it->get<int>(), id);
466 response += "missing 'amount' integer field";
468 else if (opType == "/test-reset") {
469 auto it = j.find("type");
472 auto itI = j.find("id");
473 int id = (itI != j.end()) ? itI->get<int>() : -2; // default is: apply to all the tests
475 if ((*it == "soft") || (*it == "hard")) {
476 result = eop.test__reset(response, (*it == "soft"), id);
479 response += "invalid 'type' string field (allowed: soft|hard)";
482 response += "missing 'type' string field";
484 else if (opType == "/test-repeats") {
485 auto it = j.find("amount");
487 result = eop.test__repeats(response, it->get<int>());
489 response += "missing 'amount' integer field";
491 else if (opType == "/test-auto-reset") {
492 auto it = j.find("type");
495 if ((*it == "soft") || (*it == "hard")) {
496 result = eop.test__auto_reset(response, (*it == "soft"));
499 response += "invalid 'type' string field (allowed: soft|hard)";
502 response += "missing 'type' string field";
504 else if (opType == "/test-initialized") {
505 result = eop.test__initialized(response);
507 else if (opType == "/test-finished") {
508 result = eop.test__finished(response);
510 else if (opType == "/test-clear") {
511 result = eop.test__clear(response);
513 else if (opType == "/test-junit") {
514 auto it = j.find("targetFile");
515 std::string targetFile = (it != j.end()) ? *it : ""; // default is: get junit on response instead dumping on file
516 result = eop.test__junit(response, targetFile);
518 else if (opType == "/test-summary-counts") {
519 result = eop.test__summary_counts(response);
521 else if (opType == "/test-summary-states") {
522 result = eop.test__summary_states(response);
524 else if (opType == "/test-summary") {
525 result = eop.test__summary(response);
527 else if (opType == "/test-report") {
528 auto it = j.find("state");
529 std::string state = (it != j.end()) ? *it : "all"; // initialized|in-progress|failed|success|[all]|none
531 auto itA = j.find("action");
532 std::string action = (itA != j.end()) ? *itA : "enable"; // default is: enable
534 if ((action == "enable") || (action == "disable")) {
535 result = eop.test__report(response, state, (action == "enable"));
538 response += "invalid 'action' string field (allowed: enable|disable)";
540 else if ((opType == "/test-report-hex")||(opType == "/test-dump_stdout")) {
542 auto itA = j.find("action");
543 std::string action = (itA != j.end()) ? *itA : "enable"; // default is: enable
545 if ((action == "enable") || (action == "disable")) {
546 bool enable = (action == "enable");
548 if (opType == "/test-report-hex")
549 result = eop.test__report_hex(response, enable);
551 result = eop.test__dump_stdout(response, enable);
554 response += "invalid 'action' string field (allowed: enable|disable)";