2994e5ff7a8b7692681b7df3a16ee0ceb0b6d142
[anna.git] / example / diameter / tme / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // http://redmine.teslayout.com/projects/anna-suite
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     *  Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 // Standard
38 #include <iostream>
39 #include <fstream>
40
41 // STL
42 #include <string>
43
44 #include <anna/core/core.hpp>
45 #include <anna/xml/xml.hpp>
46 #include <anna/diameter/stack/Engine.hpp>
47 #include <anna/time/functions.hpp>
48 #include <anna/time/Date.hpp>
49
50 #include <anna/diameter/codec/tme/Engine.hpp>
51 #include <anna/diameter/codec/tme/Message.hpp>
52
53
54 typedef anna::diameter::codec::tme::Message tmeMessage;
55 typedef anna::diameter::codec::tme::Avp tmeAvp;
56
57
58 void _exit(const std::string & msg) {
59   std::cout << std::endl << msg << std::endl;
60   exit(-1);
61 }
62
63
64 std::string exec_cmd(const char* cmd) {
65   FILE* pipe = popen(cmd, "r");
66
67   if(!pipe) return "";
68
69   char buffer[128];
70   std::string result = "";
71
72   while(!feof(pipe)) {
73     if(fgets(buffer, 128, pipe) != NULL)
74       result += buffer;
75   }
76
77   pclose(pipe);
78   return result;
79 }
80
81 int main(int argc, char** argv) {
82   Logger::setLevel(Logger::Debug);
83   Logger::initialize("tme", new TraceWriter("file.trace", 2048000));
84   anna::time::functions::initialize(); // before application instantiation (it have a anna::time object)
85   anna::time::functions::setControlPoint(); // start control point (application lifetime)
86   anna::diameter::stack::Engine & stackEngine = anna::diameter::stack::Engine::instantiate();
87   anna::diameter::stack::Dictionary *dictionary;
88   anna::diameter::codec::tme::Engine *codecEngine = new anna::diameter::codec::tme::Engine();
89   std::string exec = argv[0];
90   std::string param = argv[1] ? argv[1] : "";
91
92   if(argc < 2) {
93     std::string msg = "Usage: "; msg += exec; msg += " <xml directory>";
94     msg += "\n     xml directory: contains the xml files needed, which are:";
95     msg += "\n";
96     msg += "\n                       avps_ietf.xml";
97     msg += "\n                       avps_etsi.xml";
98     msg += "\n                       avps_tgpp.xml";
99     msg += "\n                       avps_tme.xml";
100     msg += "\n                       commands_dccaOCS-CS_HuaweiNGIN_de-es.xml";
101     msg += "\n";
102     msg += "\n     For example:   "; msg += exec; msg += " ";
103     std::string projectRoot = exec_cmd("git rev-parse --show-toplevel");
104
105     if(projectRoot != "") {
106       //projectRoot.resize(projectRoot.size()-1);
107       projectRoot.erase(projectRoot.size() - 1);
108       msg += projectRoot;
109       msg += "/";
110     }
111
112     msg += "source/diameter/stack/setups";
113     _exit(msg);
114   }
115
116   try {
117     dictionary = stackEngine.createDictionary(0 /* general unique stack id */);
118     dictionary->allowUpdates();
119     dictionary->load(param + "/avps_ietf.xml");
120     dictionary->load(param + "/avps_etsi.xml");
121     dictionary->load(param + "/avps_tgpp.xml");
122     dictionary->load(param + "/avps_tme.xml");
123     dictionary->load(param + "/commands_dccaOCS-CS_HuaweiNGIN_de-es.xml");
124     codecEngine->setDictionary(dictionary);
125     // Trace:
126     LOGINFORMATION(Logger::information(stackEngine.asString(), ANNA_FILE_LOCATION));
127     LOGDEBUG(Logger::debug(dictionary->asString(), ANNA_FILE_LOCATION));
128     LOGDEBUG(Logger::debug(codecEngine->asString(), ANNA_FILE_LOCATION));
129   } catch(anna::RuntimeException &ex) {
130     ex.trace();
131     std::cout << ex.getText() << std::endl;
132   }
133
134   // Build CCA
135   tmeMessage cca;
136   cca.setId("Credit-Control-Answer");
137   tmeAvp *sid = (tmeAvp*)cca.addAvp("Session-Id");
138   tmeAvp *ohst = (tmeAvp*)cca.addAvp("Origin-Host");
139   tmeAvp *orlm = (tmeAvp*)cca.addAvp("Origin-Realm");
140   sid->getUTF8String()->setValue("grump.example.com:33041;23432;893;0AF3B81");
141   ohst->getDiameterIdentity()->setValue("c0-10-70-238-74-w5p1vepg1.vepc.ericsson.se");
142   orlm->getDiameterIdentity()->setValue("vepc.ericsson.se");
143   int inRange = 65535;
144   int outRange = inRange + 1;
145   //((tmeAvp*)cca.addAvp("CS-Result-Code"))->getUnsigned16()->setValue(inRange);
146   ((tmeAvp*)cca.addAvp("CS-Result-Code"))->getUnsigned16()->setValue(outRange); // WILL BE 0 !!
147   /*
148      <command name="Credit-Control-Answer" code="272" type="Answer">
149         <avprule id="Session-Id" type="Fixed"/>
150         <avprule id="Result-Code" type="Mandatory"/>
151         <avprule id="CS-Result-Code" type="Optional"/>
152         <avprule id="CS-Dialogue-Handling" type="Optional"/>
153         <avprule id="Origin-Host" type="Mandatory"/>
154         <avprule id="Origin-Realm" type="Mandatory"/>
155         <avprule id="Auth-Application-Id" type="Mandatory"/>
156         <avprule id="CC-Request-Type" type="Mandatory"/>
157         <avprule id="CC-Request-Number" type="Mandatory"/>
158         <avprule id="Granted-Service-Unit" type="Optional"/>
159         <avprule id="Cost-Information" type="Optional"/>
160         <avprule id="Low-Balance-Indication" type="Optional"/>
161         <avprule id="Remaining-Balance" type="Optional"/>
162         <avprule id="Credit-Control-Failure-Handling" type="Optional"/>
163         <avprule id="CC-Correlation-Id" type="Optional"/>
164         <avprule id="Redirect-Host" type="Optional" qual="*"/>
165         <avprule id="Redirect-Host-Usage" type="Optional"/>
166         <avprule id="Redirect-Max-Cache-Time" type="Optional"/>
167         <avprule id="Proxy-Info" type="Optional" qual="*"/>
168         <avprule id="Route-Record" type="Optional" qual="*"/>
169         <avprule id="Failed-AVP" type="Optional" qual="*"/>
170         <avprule id="Service-Information" type="Optional"/>
171         <avprule id="AVP" type="Optional" qual="*"/>
172      </command>
173   */
174   LOGDEBUG(Logger::debug(cca.asXMLString(), ANNA_FILE_LOCATION));
175   _exit("Open 'file.trace' in order to see traces");
176 }
177