PageRenderTime 1853ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/diameter/core/mux/jar/src/main/java/org/mobicents/diameter/dictionary/AvpRepresentation.java

http://mobicents.googlecode.com/
Java | 355 lines | 224 code | 55 blank | 76 comment | 44 complexity | 43996ddb57711257f050a96117071d12 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.diameter.dictionary;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.jdiameter.common.impl.validation.AvpRepresentationImpl;
  27. /**
  28. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  29. * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski </a>
  30. */
  31. public class AvpRepresentation {
  32. /**
  33. * <pre>
  34. * Represents multiplicity of AVP:
  35. * 0 The AVP MUST NOT be present in the message.
  36. * </pre>
  37. */
  38. public final static String _MP_NOT_ALLOWED = "0";
  39. /**
  40. * <pre>
  41. * Represents multiplicity of AVP:
  42. * 0+ Zero or more instances of the AVP MAY be present in the
  43. * message.
  44. * </pre>
  45. */
  46. public final static String _MP_ZERO_OR_MORE = "0+";
  47. /**
  48. * <pre>
  49. * Represents multiplicity of AVP:
  50. * 0-1 Zero or one instance of the AVP MAY be present in the
  51. * message. It is considered an error if there are more than
  52. * one instance of the AVP.
  53. * </pre>
  54. */
  55. public final static String _MP_ZERO_OR_ONE = "0-1";
  56. /**
  57. * <pre>
  58. * Represents multiplicity of AVP:
  59. * 1 One instance of the AVP MUST be present in the message.
  60. * message.
  61. * </pre>
  62. */
  63. public final static String _MP_ONE = "1";
  64. /**
  65. * <pre>
  66. * Represents multiplicity of AVP:
  67. * 1+ At least one instance of the AVP MUST be present in the
  68. * message.
  69. * </pre>
  70. */
  71. public final static String _MP_ONE_AND_MORE = "1+";
  72. public final static String _DEFAULT_MANDATORY = "may";
  73. public final static String _DEFAULT_PROTECTED = "may";
  74. public final static String _DEFAULT_VENDOR = "mustnot";
  75. public final static int _FIX_POSITION_INDEX = -1;
  76. public enum Rule {
  77. must, may, mustnot, shouldnot
  78. };
  79. public enum Type {
  80. OctetString, Integer32, Integer64, Unsigned32, Unsigned64, Float32, Float64, Grouped, Address, Time, UTF8String, DiameterIdentity, DiameterURI, Enumerated, IPFilterRule, QoSFilterRule
  81. };
  82. protected String description;
  83. protected boolean mayEncrypt;
  84. protected boolean _protected;
  85. protected boolean _mandatory;
  86. protected String ruleMandatory;
  87. protected String ruleProtected;
  88. protected String ruleVendorBit;
  89. protected String type; // String, in case user defines his own type
  90. // ususally this will be -1, as only SessionId has fixed position
  91. private int positionIndex = _FIX_POSITION_INDEX;
  92. protected int code = -1;
  93. protected long vendor = 0;
  94. protected boolean allowed = true;
  95. protected String multiplicityIndicator = "0";
  96. protected String name = "Some-AVP";
  97. protected boolean grouped = false;
  98. protected List<AvpRepresentation> children = new ArrayList<AvpRepresentation>();
  99. protected boolean weak = false;
  100. /**
  101. * @param code
  102. * @param vendor
  103. */
  104. public AvpRepresentation(int code, long vendor) {
  105. super();
  106. this.code = code;
  107. this.vendor = vendor;
  108. }
  109. AvpRepresentation(AvpRepresentationImpl clone) {
  110. this(-1, clone.getCode(), clone.getVendorId(), clone.getMultiplicityIndicator(), clone.getName());
  111. this.allowed = clone.isAllowed();
  112. this.grouped = clone.isGrouped();
  113. this.positionIndex = clone.getPositionIndex();
  114. this.weak = clone.isWeak();
  115. this._mandatory = clone.isMandatory();
  116. this._protected = clone.isProtected();
  117. this.description = clone.getDescription();
  118. this.mayEncrypt = clone.isMayEncrypt();
  119. this.ruleMandatory = clone.getRuleMandatory();
  120. this.ruleProtected = clone.getRuleProtected();
  121. this.ruleVendorBit = clone.getRuleVendorBit();
  122. this.type = clone.getType();
  123. if (this.multiplicityIndicator.equals(_MP_NOT_ALLOWED))
  124. this.allowed = false;
  125. // copy others.
  126. if (isGrouped()) {
  127. for (Object o : clone.getChildren()) {
  128. AvpRepresentationImpl avpImpl = (AvpRepresentationImpl) o;
  129. this.children.add(new AvpRepresentation(avpImpl));
  130. }
  131. this.children = Collections.unmodifiableList(this.children);
  132. }
  133. }
  134. AvpRepresentation(int positionIndex, int code, long vendor, String multiplicityIndicator, String name) {
  135. super();
  136. this.positionIndex = positionIndex;
  137. this.code = code;
  138. this.vendor = vendor;
  139. this.multiplicityIndicator = multiplicityIndicator;
  140. this.name = name;
  141. if (this.multiplicityIndicator.equals(_MP_NOT_ALLOWED))
  142. this.allowed = false;
  143. }
  144. public boolean isPositionFixed() {
  145. return this.positionIndex == _FIX_POSITION_INDEX;
  146. }
  147. public void markFixPosition(int index) {
  148. this.positionIndex = index;
  149. }
  150. public static int get_FIX_POSITION_INDEX() {
  151. return _FIX_POSITION_INDEX;
  152. }
  153. public int getPositionIndex() {
  154. return positionIndex;
  155. }
  156. public int getCode() {
  157. return code;
  158. }
  159. public long getVendorId() {
  160. return vendor;
  161. }
  162. public boolean isAllowed() {
  163. return allowed;
  164. }
  165. public boolean isAllowed(int avpCode, long vendorId) {
  166. if (this.isGrouped()) {
  167. // make better get ?
  168. for (AvpRepresentation rep : this.children) {
  169. if (rep.getCode() == avpCode && rep.getVendorId() == vendorId) {
  170. return rep.isAllowed();
  171. } else {
  172. continue;
  173. }
  174. }
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. }
  180. public boolean isAllowed(int avpCode) {
  181. return this.isAllowed(avpCode, 0L);
  182. }
  183. public String getMultiplicityIndicator() {
  184. return multiplicityIndicator;
  185. }
  186. public String getName() {
  187. return name;
  188. }
  189. public boolean isGrouped() {
  190. return grouped;
  191. }
  192. public List<AvpRepresentation> getChildren() {
  193. return children;
  194. }
  195. public boolean isWeak() {
  196. return weak;
  197. }
  198. public String getDescription() {
  199. return description;
  200. }
  201. public boolean isMayEncrypt() {
  202. return mayEncrypt;
  203. }
  204. public String getRuleMandatory() {
  205. return ruleMandatory;
  206. }
  207. public int getRuleMandatoryAsInt() {
  208. return Rule.valueOf(ruleMandatory).ordinal();
  209. }
  210. public String getRuleProtected() {
  211. return ruleProtected;
  212. }
  213. public int getRuleProtectedAsInt() {
  214. return Rule.valueOf(ruleProtected).ordinal();
  215. }
  216. public String getRuleVendorBit() {
  217. return ruleVendorBit;
  218. }
  219. public int getRuleVendorBitAsInt() {
  220. return Rule.valueOf(ruleVendorBit).ordinal();
  221. }
  222. public String getType() {
  223. return type;
  224. }
  225. public boolean isProtected() {
  226. return _protected;
  227. }
  228. public boolean isMandatory() {
  229. return _mandatory;
  230. }
  231. public boolean isCountValidForMultiplicity(int avpCount) {
  232. // This covers not_allowed
  233. if (!allowed) {
  234. if (avpCount == 0) {
  235. return true;
  236. }
  237. } else {
  238. if (this.multiplicityIndicator.equals(_MP_ZERO_OR_MORE)) {
  239. if (avpCount >= 0)
  240. return true;
  241. } else if (this.multiplicityIndicator.equals(_MP_ZERO_OR_ONE)) {
  242. if ((avpCount == 0) || (avpCount == 1))
  243. return true;
  244. } else if (this.multiplicityIndicator.equals(_MP_ONE)) {
  245. if (avpCount == 1) {
  246. return true;
  247. }
  248. } else if (this.multiplicityIndicator.equals(_MP_ONE_AND_MORE)) {
  249. if (avpCount >= 1) {
  250. return true;
  251. }
  252. }
  253. }
  254. // if we did not return, we are screwed.
  255. return false;
  256. }
  257. // public String toString() {
  258. // return this.getName() + "@" + hashCode() + " Name[" + getName() +
  259. // "] Code[" + getCode() + "] Vendor[" + getVendorId() + "] MLP[" +
  260. // getMultiplicityIndicator() + "] Allowed[" + isAllowed() + "] ";
  261. // }
  262. public String toString() {
  263. StringBuffer sb = new StringBuffer();
  264. sb.append("name: ").append(this.getName()).append(", code: ").append(this.getCode()).append(", vendor: ").append(this.getVendorId()).append(", weak: ")
  265. .append(this.isWeak()).append(", grouped: ").append(this.isGrouped()).append(", type: ").append(this.getType()).append(", multiplicity: ")
  266. .append(this.getMultiplicityIndicator());
  267. if (this.isGrouped()) {
  268. for (AvpRepresentation child : this.getChildren()) {
  269. String childStr = child.toString().replace("\n", "\n---");
  270. sb.append("\n---" + childStr);
  271. }
  272. }
  273. return sb.toString();
  274. }
  275. @Override
  276. public int hashCode() {
  277. // code+vendor is enough by AVP def
  278. final int prime = 31;
  279. int result = 1;
  280. result = prime * result + code;
  281. result = prime * result + (int) (vendor ^ (vendor >>> 32));
  282. return result;
  283. }
  284. @Override
  285. public boolean equals(Object obj) {
  286. if (this == obj)
  287. return true;
  288. if (obj == null)
  289. return false;
  290. if (getClass() != obj.getClass())
  291. return false;
  292. // code+vendor is enough by AVP def
  293. AvpRepresentation other = (AvpRepresentation) obj;
  294. if (code != other.code)
  295. return false;
  296. if (vendor != other.vendor)
  297. return false;
  298. return true;
  299. }
  300. }