Skip oracle and database build by mean cmake variable
[anna.git] / example / diameter / rxSimpleTest / MyDiameterEntity.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 // Project
10 #include <anna/core/core.hpp>
11 #include <anna/diameter/functions.hpp>
12 #include <anna/time/functions.hpp>
13 #include <anna/diameter.comm/Response.hpp>
14 #include <anna/diameter.comm/ClientSession.hpp>
15 #include <anna/diameter.comm/OriginHost.hpp>
16 //#include <anna/diameter/helpers/base/functions.hpp>
17 #include <anna/diameter/helpers/nas/defines.hpp>
18
19 // Process
20 #include <MyDiameterEngine.hpp>
21 #include <MyDiameterEntity.hpp>
22 #include <MyLocalServer.hpp>
23 #include <rxSimpleTest.hpp>
24 #include <anna/testing/TestManager.hpp>
25
26
27 void MyDiameterEntity::eventRequestRetransmission(const anna::diameter::comm::ClientSession* clientSession, anna::diameter::comm::Message *request) throw() {
28
29   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventRequestRetransmission", ANNA_FILE_LOCATION));
30
31   // Base class:
32   Entity::eventRequestRetransmission(clientSession, request); // warning trace
33
34   // Performance stats:
35   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
36   anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
37   // CommandId:
38   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(request->getBody());
39   LOGDEBUG
40   (
41     std::string msg = "Request retransmitted: ";
42     msg += anna::diameter::functions::commandIdAsPairString(cid);
43     msg += " | DiameterServer: ";
44     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
45     msg += " | EventTime: ";
46     msg += anna::time::functions::currentTimeAsString();
47     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
48   );
49 }
50
51
52 void MyDiameterEntity::eventRequest(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
53 throw(anna::RuntimeException) {
54   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventRequest", ANNA_FILE_LOCATION));
55   // Performance stats:
56   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
57   anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
58
59   // CommandId:
60   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
61   LOGDEBUG
62   (
63     std::string msg = "Request received: ";
64     msg += anna::diameter::functions::commandIdAsPairString(cid);
65     msg += " | DiameterServer: ";
66     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
67     msg += " | EventTime: ";
68     msg += anna::time::functions::currentTimeAsString();
69     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
70   );
71 }
72
73 void MyDiameterEntity::eventResponse(const anna::diameter::comm::Response &response)
74 throw(anna::RuntimeException) {
75   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventResponse", ANNA_FILE_LOCATION));
76   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
77   anna::diameter::comm::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
78   anna::diameter::comm::ClassCode::_v code = response.getClassCode();
79   anna::diameter::comm::Response::ResultCode::_v result = response.getResultCode();
80   anna::diameter::comm::Message* request = const_cast<anna::diameter::comm::Message*>(response.getRequest());
81   const anna::DataBlock* message = response.getMessage();
82   const anna::diameter::comm::ClientSession *clientSession_c = static_cast<const anna::diameter::comm::ClientSession *>(response.getSession());
83   anna::diameter::comm::ClientSession *clientSession = const_cast<anna::diameter::comm::ClientSession *>(clientSession_c);
84   bool contextExpired = (result == anna::diameter::comm::Response::ResultCode::Timeout);
85   bool isUnavailable = (result == anna::diameter::comm::Response::ResultCode::DiameterUnavailable);
86   bool isOK = (result == anna::diameter::comm::Response::ResultCode::Success);
87
88   // CommandId:
89   anna::diameter::CommandId request_cid = request->getCommandId();
90   LOGDEBUG
91   (
92     std::string msg = "Response received for original diameter request: ";
93     msg += anna::diameter::functions::commandIdAsPairString(request_cid);
94     msg += " | Response: ";
95     msg += response.asString();
96     msg += " | DiameterServer: ";
97     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
98     msg += " | EventTime: ";
99     msg += anna::time::functions::currentTimeAsString();
100     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
101   );
102
103   if(isUnavailable) {
104     //if (isApplicationMessage)
105     LOGWARNING(anna::Logger::warning("Diameter entity unavailable for Diameter Request", ANNA_FILE_LOCATION));
106   }
107
108   if(contextExpired) {
109     //if (isApplicationMessage)
110     LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the entity", ANNA_FILE_LOCATION));
111   }
112
113   if(isOK) {
114     LOGDEBUG(
115       std::string msg = "Received response for diameter message:  ";
116       msg += anna::diameter::functions::commandIdAsPairString(request_cid);
117       anna::Logger::debug(msg, ANNA_FILE_LOCATION);
118     );
119
120     if(request_cid == anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) {
121       // Build AAR and send it:
122       anna::diameter::codec::Message aar;
123
124       // From file would be:
125       //aar.loadXML("./aar.xml");
126       aar.setApplicationId(anna::diameter::helpers::APPID__3GPP_Rx); // 16777236
127       aar.setId("AA-Request");
128
129       // Session-Id
130       Avp *sessionId = aar.addAvp("Session-Id");
131       sessionId->getUTF8String()->setValue("ocs3;1332774430;1;1332774430");
132
133       // Auth-Application-Id
134       Avp *authApplicationId = aar.addAvp("Auth-Application-Id");
135       authApplicationId->getUnsigned32()->setValue(16777236);
136
137       // Origin-Host
138       Avp *originHost = aar.addAvp("Origin-Host");
139       originHost->getDiameterIdentity()->setValue("thehost.theOriginRealm.com");
140
141       // Origin-Realm
142       Avp *originRealm = aar.addAvp("Origin-Realm");
143       originRealm->getDiameterIdentity()->setValue("theOriginRealm.com");
144
145       // Destination-Realm
146       Avp *destinationRealm = aar.addAvp("Destination-Realm");
147       destinationRealm->getDiameterIdentity()->setValue("theDestinationRealm.com");
148
149       // Suppported features:
150       Avp *supportedFeatures = aar.addAvp("Supported-Features");
151       supportedFeatures->addAvp("Vendor-Id")->getUnsigned32()->setValue(10415);
152       supportedFeatures->addAvp("Feature-List-ID")->getUnsigned32()->setValue(1);
153       supportedFeatures->addAvp("Feature-List")->getUnsigned32()->setValue(3);
154
155       anna::diameter::comm::Message *msg;
156
157       try {
158         msg = my_node->createCommMessage();
159         msg->setBody(aar.code());
160         /* response = NULL =*/clientSession->send(msg);
161       } catch(anna::RuntimeException &ex) {
162         ex.trace();
163       }
164
165       // release msg
166       my_node->releaseCommMessage(msg);
167     }
168     else if(request_cid == anna::diameter::helpers::nas::COMMANDID__AA_Request) {
169       // Decode
170       anna::diameter::codec::Message aaa;
171       try { aaa.decode(*message); } catch(anna::RuntimeException &ex) { ex.trace(); }
172
173       Avp *resultCode = aaa.getAvp("Result-Code");
174       int rc = resultCode->getUnsigned32()->getValue();
175
176       if (rc != 2001) exit(1);
177       else exit(0);
178     }
179   }
180 }
181
182 void MyDiameterEntity::eventUnknownResponse(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
183 throw(anna::RuntimeException) {
184   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventUnknownResponse", ANNA_FILE_LOCATION));
185   // Performance stats:
186   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
187   anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
188   // CommandId:
189   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
190   LOGDEBUG
191   (
192     std::string msg = "Out-of-context response received from entity: ";
193     msg += anna::diameter::functions::commandIdAsPairString(cid);
194     msg += " | DiameterServer: ";
195     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
196     msg += " | EventTime: ";
197     msg += anna::time::functions::currentTimeAsString();
198     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
199   );
200 }
201
202 void MyDiameterEntity::eventDPA(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
203 throw(anna::RuntimeException) {
204   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventDPA", ANNA_FILE_LOCATION));
205   // Performance stats:
206   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
207   anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
208   // CommandId:
209   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
210   LOGDEBUG
211   (
212     std::string msg = "Disconnect-Peer-Answer received from entity: ";
213     msg += anna::diameter::functions::commandIdAsPairString(cid);
214     msg += " | DiameterServer: ";
215     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
216     msg += " | EventTime: ";
217     msg += anna::time::functions::currentTimeAsString();
218     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
219   );
220 }
221