Updated license
[anna.git] / source / diameter / stack / AvpRule.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 //------------------------------------------------------------------------------
38 //-------------------------------------------------------- included header files
39 //------------------------------------------------------------------------------
40
41 // Standard
42 #include <stdlib.h>
43
44 // Local
45 #include <anna/diameter/stack/AvpRule.hpp>
46 #include <anna/diameter/stack/Avp.hpp>
47 #include <anna/diameter/stack/Format.hpp>
48 #include <anna/diameter/functions.hpp>
49 #include <anna/diameter/stack/Dictionary.hpp>
50
51 #include <anna/config/defines.hpp>
52 #include <anna/core/RuntimeException.hpp>
53 #include <anna/xml/xml.hpp>
54
55 anna_assign_enum(anna::diameter::stack::AvpRule::Presence) = { "Fixed", "Mandatory", "Optional", NULL /* list end indicator */};
56
57
58 //------------------------------------------------------------------------------
59 //----------------------------------------------------------- AvpRule::setQual()
60 //------------------------------------------------------------------------------
61 void anna::diameter::stack::AvpRule::setQual(const std::string & q) throw(anna::RuntimeException) {
62   const char *asterisk = strstr(q.c_str(), "*");
63
64   if((q != "") && (asterisk == NULL))
65     throw anna::RuntimeException("Non-empty qualifier must contain '*'", ANNA_FILE_LOCATION);
66
67   a_qual = q;
68 }
69
70
71 //------------------------------------------------------------------------------
72 //------------------------------------------------------------- AvpRule::getId()
73 //------------------------------------------------------------------------------
74 anna::diameter::AvpId anna::diameter::stack::AvpRule::getId(void) const throw() {
75   const Avp * avp = a_dictionary->getAvp(a_avpName);
76   return avp->getId();
77 }
78
79 //------------------------------------------------------------------------------
80 //------------------------------------------------------------- AvpRule::isAny()
81 //------------------------------------------------------------------------------
82 bool anna::diameter::stack::AvpRule::isAny(void) const throw() {
83   const Avp * avp = a_dictionary->getAvp(a_avpName);
84   const Format * format = a_dictionary->getFormat(avp->getFormatName());
85   return format->isAny();
86 }
87
88
89 //------------------------------------------------------------------------------
90 //-------------------------------------------------------- AvpRule::getQualMin()
91 //------------------------------------------------------------------------------
92 int anna::diameter::stack::AvpRule::getQualMin(void) const throw() {
93   if(a_qual == "") {
94     if(isFixed() || isMandatory()) return 1;
95
96     if(isOptional()) return 0;
97   }
98
99   // Asterisk location
100   const char * c_qual = a_qual.c_str();
101   int asterisk_pos = strstr(c_qual, "*") - c_qual;
102
103   // '*', '*y'
104   if(asterisk_pos == 0) return 0;
105
106   // 'x*', 'x*y'
107   std::string min = a_qual.substr(0, asterisk_pos);  // 'x'
108   return (atoi(min.c_str()));
109 }
110
111
112
113 //------------------------------------------------------------------------------
114 //-------------------------------------------------------- AvpRule::getQualMax()
115 //------------------------------------------------------------------------------
116 int anna::diameter::stack::AvpRule::getQualMax(void) const throw() {
117   if(a_qual == "") return 1;
118
119   // Asterisk location
120   const char * c_qual = a_qual.c_str();
121   int asterisk_pos = strstr(c_qual, "*") - c_qual;
122
123   // '*', 'x*'
124   if(asterisk_pos == (a_qual.size() - 1)) return -1;  // inf
125
126   // '*y', 'x*y'
127   std::string max = a_qual.substr(asterisk_pos + 1, a_qual.size() - asterisk_pos - 1);  // 'y'
128   return (atoi(max.c_str()));
129 }
130
131
132 //------------------------------------------------------------------------------
133 //---------------------------------------------------------- AvpRule::asString()
134 //------------------------------------------------------------------------------
135 std::string anna::diameter::stack::AvpRule::asString(bool showPair) const throw() {
136   std::string trace = "No Avp rule defined";
137   const Avp * avp = a_dictionary->getAvp(a_avpName);
138
139   if(avp) {
140     trace = a_qual;
141     std::string s_open, s_close;
142
143     if(isFixed()) { s_open = "<"; s_close = ">"; }
144
145     if(isMandatory()) { s_open = "{"; s_close = "}"; }
146
147     if(isOptional()) { s_open = "["; s_close = "]"; }
148
149     trace += s_open;
150     trace += a_avpName;
151     trace += s_close;
152
153     if(!showPair) return trace;
154
155     // Avoid ambiguous descriptions:
156     int qualSize = a_qual.size();
157     int tabSize = strlen(DICTIONARY_AVPRULE_TAB);
158     int NumberOfDots =   /* qual add */ ((qualSize > tabSize) ? tabSize : qualSize) +
159                                         /* max expected avp description size */   48 -
160                                         /* current trace length */                trace.size();
161
162     for(register int k = 0; k < NumberOfDots; k++) trace += ".";
163
164     trace += anna::diameter::functions::avpIdAsPairString(avp->getId());
165   }
166
167   return (trace);
168 }
169
170
171 //------------------------------------------------------------------------------
172 //------------------------------------------------------------- AvpRule::asXML()
173 //------------------------------------------------------------------------------
174 anna::xml::Node* anna::diameter::stack::AvpRule::asXML(anna::xml::Node* parent) const throw() {
175 //   <!ELEMENT avprule EMPTY>
176 //   <!ATTLIST avprule id CDATA #REQUIRED type (Fixed | Mandatory | Optional) #REQUIRED qual CDATA #IMPLIED>
177   anna::xml::Node* result = parent->createChild("avprule");
178   result->createAttribute("id", a_avpName);
179   std::string type;
180
181   if(isFixed()) type = "Fixed";
182   else if(isMandatory()) type = "Mandatory";
183   else if(isOptional()) type = "Optional";
184
185   result->createAttribute("type", type);
186
187   if(a_qual != "") result->createAttribute("qual", a_qual);
188
189   return result;
190 }