Updated license
[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 // https://bitbucket.org/testillano/anna
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 Google Inc. 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   if (!pipe) return "";
67   char buffer[128];
68   std::string result = "";
69   while(!feof(pipe)) {
70     if(fgets(buffer, 128, pipe) != NULL)
71       result += buffer;
72   }
73   pclose(pipe);
74   return result;
75 }
76
77 int main(int argc, char** argv) {
78   Logger::setLevel(Logger::Debug);
79   Logger::initialize("tme", new TraceWriter("file.trace", 2048000));
80   anna::time::functions::initialize(); // before application instantiation (it have a anna::time object)
81   anna::time::functions::setControlPoint(); // start control point (application lifetime)
82   anna::diameter::stack::Engine & stackEngine = anna::diameter::stack::Engine::instantiate();
83   anna::diameter::stack::Dictionary *dictionary;
84   anna::diameter::codec::tme::Engine *codecEngine = new anna::diameter::codec::tme::Engine();
85   std::string exec = argv[0];
86   std::string param = argv[1] ? argv[1] : "";
87
88   if (param == "") {
89     std::string msg = "Use: "; msg += exec; msg += " <xml directory>";
90     msg += "\n     xml directory: contains the xml files needed, which are:";
91     msg += "\n";
92     msg += "\n                       avps_ietf.xml";
93     msg += "\n                       avps_etsi.xml";
94     msg += "\n                       avps_tgpp.xml";
95     msg += "\n                       avps_tme.xml";
96     msg += "\n                       commands_dccaOCS-CS_HuaweiNGIN_de-es.xml";
97     msg += "\n";
98     msg += "\n     For example:   "; msg += exec; msg += " ";
99     std::string projectRoot = exec_cmd("git rev-parse --show-toplevel");
100     if (projectRoot != "") {
101       //projectRoot.resize(projectRoot.size()-1);
102       projectRoot.erase(projectRoot.size()-1);
103       msg += projectRoot;
104       msg += "/";
105     }
106
107     msg += "source/diameter/stack/setups";
108
109     _exit(msg);
110   }
111
112   try {
113     dictionary = stackEngine.createDictionary(0 /* general unique stack id */);
114     dictionary->allowUpdates();
115     dictionary->load(param + "/avps_ietf.xml");
116     dictionary->load(param + "/avps_etsi.xml");
117     dictionary->load(param + "/avps_tgpp.xml");
118     dictionary->load(param + "/avps_tme.xml");
119     dictionary->load(param + "/commands_dccaOCS-CS_HuaweiNGIN_de-es.xml");
120     codecEngine->setDictionary(dictionary);
121
122     // Trace:
123     LOGINFORMATION(Logger::information(stackEngine.asString(), ANNA_FILE_LOCATION));
124     LOGDEBUG(Logger::debug(dictionary->asString(), ANNA_FILE_LOCATION));
125     LOGDEBUG(Logger::debug(codecEngine->asString(), ANNA_FILE_LOCATION));
126
127   } catch (anna::RuntimeException &ex) {
128     ex.trace();
129     std::cout << ex.getText() << std::endl;
130   }
131
132   // Build CCA
133   tmeMessage cca;
134   cca.setId("Credit-Control-Answer");
135
136   tmeAvp *sid = (tmeAvp*)cca.addAvp("Session-Id");
137   tmeAvp *ohst = (tmeAvp*)cca.addAvp("Origin-Host");
138   tmeAvp *orlm = (tmeAvp*)cca.addAvp("Origin-Realm");
139
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 /*
149    <command name="Credit-Control-Answer" code="272" type="Answer">
150       <avprule id="Session-Id" type="Fixed"/>
151       <avprule id="Result-Code" type="Mandatory"/>
152       <avprule id="CS-Result-Code" type="Optional"/>
153       <avprule id="CS-Dialogue-Handling" type="Optional"/>
154       <avprule id="Origin-Host" type="Mandatory"/>
155       <avprule id="Origin-Realm" type="Mandatory"/>
156       <avprule id="Auth-Application-Id" type="Mandatory"/>
157       <avprule id="CC-Request-Type" type="Mandatory"/>
158       <avprule id="CC-Request-Number" type="Mandatory"/>
159       <avprule id="Granted-Service-Unit" type="Optional"/>
160       <avprule id="Cost-Information" type="Optional"/>
161       <avprule id="Low-Balance-Indication" type="Optional"/>
162       <avprule id="Remaining-Balance" type="Optional"/>
163       <avprule id="Credit-Control-Failure-Handling" type="Optional"/>
164       <avprule id="CC-Correlation-Id" type="Optional"/>
165       <avprule id="Redirect-Host" type="Optional" qual="*"/>
166       <avprule id="Redirect-Host-Usage" type="Optional"/>
167       <avprule id="Redirect-Max-Cache-Time" type="Optional"/>
168       <avprule id="Proxy-Info" type="Optional" qual="*"/>
169       <avprule id="Route-Record" type="Optional" qual="*"/>
170       <avprule id="Failed-AVP" type="Optional" qual="*"/>
171       <avprule id="Service-Information" type="Optional"/>
172       <avprule id="AVP" type="Optional" qual="*"/>
173    </command>
174 */
175    LOGDEBUG(Logger::debug(cca.asXMLString(), ANNA_FILE_LOCATION));
176
177    _exit("Open 'file.trace' in order to see traces");
178 }
179