1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 //------------------------------------------------------------------------------
38 //-------------------------------------------------------- included header files
39 //------------------------------------------------------------------------------
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>
51 #include <anna/config/defines.hpp>
52 #include <anna/core/RuntimeException.hpp>
53 #include <anna/xml/xml.hpp>
55 anna_assign_enum(anna::diameter::stack::AvpRule::Presence) = { "Fixed", "Mandatory", "Optional", NULL /* list end indicator */};
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(), "*");
64 if((q != "") && (asterisk == NULL))
65 throw anna::RuntimeException("Non-empty qualifier must contain '*'", ANNA_FILE_LOCATION);
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);
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();
89 //------------------------------------------------------------------------------
90 //-------------------------------------------------------- AvpRule::getQualMin()
91 //------------------------------------------------------------------------------
92 int anna::diameter::stack::AvpRule::getQualMin(void) const throw() {
94 if(isFixed() || isMandatory()) return 1;
96 if(isOptional()) return 0;
100 const char * c_qual = a_qual.c_str();
101 int asterisk_pos = strstr(c_qual, "*") - c_qual;
104 if(asterisk_pos == 0) return 0;
107 std::string min = a_qual.substr(0, asterisk_pos); // 'x'
108 return (atoi(min.c_str()));
113 //------------------------------------------------------------------------------
114 //-------------------------------------------------------- AvpRule::getQualMax()
115 //------------------------------------------------------------------------------
116 int anna::diameter::stack::AvpRule::getQualMax(void) const throw() {
117 if(a_qual == "") return 1;
120 const char * c_qual = a_qual.c_str();
121 int asterisk_pos = strstr(c_qual, "*") - c_qual;
124 if(asterisk_pos == (a_qual.size() - 1)) return -1; // inf
127 std::string max = a_qual.substr(asterisk_pos + 1, a_qual.size() - asterisk_pos - 1); // 'y'
128 return (atoi(max.c_str()));
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);
141 std::string s_open, s_close;
143 if(isFixed()) { s_open = "<"; s_close = ">"; }
145 if(isMandatory()) { s_open = "{"; s_close = "}"; }
147 if(isOptional()) { s_open = "["; s_close = "]"; }
153 if(!showPair) return trace;
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();
162 for(int k = 0; k < NumberOfDots; k++) trace += ".";
164 trace += anna::diameter::functions::avpIdAsPairString(avp->getId());
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);
181 if(isFixed()) type = "Fixed";
182 else if(isMandatory()) type = "Mandatory";
183 else if(isOptional()) type = "Optional";
185 result->createAttribute("type", type);
187 if(a_qual != "") result->createAttribute("qual", a_qual);