2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.groupbasedpolicy.renderer.opflex.mit;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
15 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.PolicyPropertyInfo.PolicyPropertyId;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 * This is a policy object instance, used to provide an abstraction
21 * between OpFlex messaging and the back-end policy.
26 public class PolicyObjectInstance {
29 * An object used as a key for the property hash map. It
30 * consists of the tuple: { {@link PolicyPropertyId},
31 * {@link PolicyPropertyInfo.PropertyType},
32 * {@link PolicyPropertyInfo.Propertycardinality} }
37 public static class PropertyKey {
38 private final PolicyPropertyId propId;
39 private final PolicyPropertyInfo.PropertyType type;
40 private final PolicyPropertyInfo.PropertyCardinality cardinality;
42 public PropertyKey(PolicyPropertyId propId, PolicyPropertyInfo.PropertyType type,
43 PolicyPropertyInfo.PropertyCardinality cardinality) {
46 this.cardinality = cardinality;
50 public int hashCode() {
53 result = prime * result
54 + ((cardinality == null) ? 0 : cardinality.hashCode());
55 result = prime * result
56 + ((propId == null) ? 0 : propId.hashCode());
57 result = prime * result + ((type == null) ? 0 : type.hashCode());
62 public boolean equals(Object obj) {
67 if (getClass() != obj.getClass())
69 PropertyKey other = (PropertyKey) obj;
70 if (cardinality != other.cardinality)
73 if (other.propId != null)
75 } else if (!propId.equals(other.propId))
77 if (type != other.type)
85 * Class that contains a value held by the {@link PolicyObjectInstance}.
86 * The value can be scalar or vector in nature.
91 public static class Value {
93 private List<Object> vl;
94 private PolicyPropertyInfo.PropertyType type;
95 private PolicyPropertyInfo.PropertyCardinality cardinality;
96 public void setPropertyType(PolicyPropertyInfo.PropertyType type) {
100 public void setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality cardinality) {
101 this.cardinality = cardinality;
104 public void setValue(Object v) {
108 public void setValue(List<Object> vl) {
112 public Object getValue(int index) {
113 if (this.cardinality == PolicyPropertyInfo.PropertyCardinality.VECTOR) {
114 return this.vl.get(index);
121 public PolicyPropertyInfo.PropertyType getType() {
127 * Class used as a reference to another PolicyObject.
132 public static class PolicyReference {
133 private final Uri uri;
134 private final long classId;
136 public PolicyReference(long classId, Uri uri) {
138 this.classId = classId;
141 public Uri getUri() {
145 public long getClassId() {
150 public int hashCode() {
151 final int prime = 31;
153 result = prime * result + (int) (classId ^ (classId >>> 32));
154 result = prime * result + ((uri == null) ? 0 : uri.hashCode());
159 public boolean equals(Object obj) {
164 if (getClass() != obj.getClass())
166 PolicyReference other = (PolicyReference) obj;
167 if (classId != other.classId)
170 if (other.uri != null)
172 } else if (!uri.equals(other.uri))
179 private long classId;
181 private Uri parentUri;
182 private String parentSubject;
183 private String parentRelation;
184 private List<Uri> children;
185 private HashMap<PropertyKey, Value> propertyMap;
187 private PolicyPropertyInfo.PropertyType normalize(PolicyPropertyInfo.PropertyType type) {
193 return PolicyPropertyInfo.PropertyType.U64;
199 public PolicyObjectInstance(long classId) {
200 this.classId = classId;
201 this.children = new ArrayList<Uri>();
202 this.propertyMap = new HashMap<PropertyKey, Value>();
206 * Get the class ID for this object instance.
208 * @return the class ID
210 public long getClassId() {
214 public Uri getUri() {
218 public void setUri(Uri uri) {
222 public void addChild(Uri childUri) {
223 children.add(childUri);
226 public List<Uri> getChildren() {
227 return this.children;
230 public void setParent(Uri uri) {
231 this.parentUri = uri;
234 public Uri getParent() {
235 return this.parentUri;
238 public void setParentSubject(String subject) {
239 this.parentSubject = subject;
242 public String getParentSubject() {
243 return this.parentSubject;
246 public void setParentRelation(String relation) {
247 this.parentRelation = relation;
250 public String getParentRelation() {
251 return this.parentRelation;
255 * Check whether the given property is set. If the property is
256 * vector-valued, this will return false if the vector is zero length
261 * @return true if set, false if not set or zero length vector
263 public boolean isSet(PolicyPropertyInfo.PolicyPropertyId propId,
264 PolicyPropertyInfo.PropertyType type,
265 PolicyPropertyInfo.PropertyCardinality cardinality) {
266 type = normalize(type);
267 PropertyKey key = new PropertyKey(propId, type, cardinality);
268 return propertyMap.containsKey(key);
272 * Unset the given property. If it's a vector, the vector is
273 * emptied. If its a scalar, the scalar is unset.
278 * @return true if the property was alread set before
280 public boolean unset(PolicyPropertyInfo.PolicyPropertyId propId,
281 PolicyPropertyInfo.PropertyType type,
282 PolicyPropertyInfo.PropertyCardinality cardinality) {
283 type = normalize(type);
284 PropertyKey key = new PropertyKey(propId, type, cardinality);
285 Value v = propertyMap.remove(key);
286 if (v == null) return false;
293 * Get the unsigned 64-bit valued property for prop_name.
296 * @return null if not present or {@link BigInteger}
298 public BigInteger getUint64(PolicyPropertyInfo.PolicyPropertyId id) {
301 PolicyPropertyInfo.PropertyType.U64,
302 PolicyPropertyInfo.PropertyCardinality.SCALAR);
303 return (BigInteger)propertyMap.get(key).getValue(0);
307 * For a vector-valued 64-bit unsigned property, get the specified
308 * property value at the specified index
312 * @return the property value
314 public BigInteger getUint64(PolicyPropertyInfo.PolicyPropertyId id, int index) {
317 PolicyPropertyInfo.PropertyType.U64,
318 PolicyPropertyInfo.PropertyCardinality.VECTOR);
319 return (BigInteger)propertyMap.get(key).getValue(index);
323 * Get the number of unsigned 64-bit values for the specified
327 * @return the number of elements
329 public int getUint64Size(PolicyPropertyInfo.PolicyPropertyId id) {
332 PolicyPropertyInfo.PropertyType.U64,
333 PolicyPropertyInfo.PropertyCardinality.VECTOR);
334 Value v = propertyMap.get(key);
339 * Get the signed 64-bit valued property for prop_name.
342 * @return the property value
344 public long getInt64(PolicyPropertyInfo.PolicyPropertyId id) {
347 PolicyPropertyInfo.PropertyType.S64,
348 PolicyPropertyInfo.PropertyCardinality.SCALAR);
349 return (long)propertyMap.get(key).getValue(0);
353 * For a vector-valued 64-bit signed property, get the specified
354 * property value at the specified index
358 * @return the property value
360 public long getInt64(PolicyPropertyInfo.PolicyPropertyId id, int index) {
363 PolicyPropertyInfo.PropertyType.S64,
364 PolicyPropertyInfo.PropertyCardinality.VECTOR);
365 return (long)propertyMap.get(key).getValue(index);
369 * Get the number of signed 64-bit values for the specified
373 * @return the number of elements
375 public int getInt64Size(PolicyPropertyInfo.PolicyPropertyId id) {
378 PolicyPropertyInfo.PropertyType.S64,
379 PolicyPropertyInfo.PropertyCardinality.VECTOR);
380 Value v = propertyMap.get(key);
385 * Get the string-valued property for prop_name.
388 * @return the property value
390 public String getString(PolicyPropertyInfo.PolicyPropertyId id) {
393 PolicyPropertyInfo.PropertyType.STRING,
394 PolicyPropertyInfo.PropertyCardinality.SCALAR);
395 return (String)propertyMap.get(key).getValue(0);
399 * For a vector-valued string property, get the specified property
400 * value at the specified index
404 * @return the property value
406 public String getString(PolicyPropertyInfo.PolicyPropertyId id, int index) {
409 PolicyPropertyInfo.PropertyType.STRING,
410 PolicyPropertyInfo.PropertyCardinality.VECTOR);
411 return (String)propertyMap.get(key).getValue(index);
415 * Get the number of string values for the specified property
418 * @return the number of elements
420 public int getStringSize(PolicyPropertyInfo.PolicyPropertyId id) {
423 PolicyPropertyInfo.PropertyType.STRING,
424 PolicyPropertyInfo.PropertyCardinality.VECTOR);
425 Value v = propertyMap.get(key);
430 * Get the reference-valued property for prop_name.
432 * @return the property value
434 public PolicyReference getReference(PolicyPropertyInfo.PolicyPropertyId id) {
437 PolicyPropertyInfo.PropertyType.REFERENCE,
438 PolicyPropertyInfo.PropertyCardinality.SCALAR);
439 return (PolicyReference)propertyMap.get(key).getValue(0);
443 * For a vector-valued reference property, get the specified property
444 * value at the specified index
448 * @return the property value
450 public PolicyReference getReference(PolicyPropertyInfo.PolicyPropertyId id, int index) {
453 PolicyPropertyInfo.PropertyType.REFERENCE,
454 PolicyPropertyInfo.PropertyCardinality.VECTOR);
455 return (PolicyReference)propertyMap.get(key).getValue(index);
459 * Get the number of reference values for the specified property
462 * @return the number of elements
464 public int getReferenceSize(PolicyPropertyInfo.PolicyPropertyId id) {
467 PolicyPropertyInfo.PropertyType.REFERENCE,
468 PolicyPropertyInfo.PropertyCardinality.VECTOR);
469 Value v = propertyMap.get(key);
474 * Get the MAC-address-valued property for prop_name.
477 * @return the property value
479 public MacAddress getMacAddress(PolicyPropertyInfo.PolicyPropertyId id) {
482 PolicyPropertyInfo.PropertyType.MAC,
483 PolicyPropertyInfo.PropertyCardinality.SCALAR);
484 return (MacAddress)propertyMap.get(key).getValue(0);
488 * For a vector-valued MAC address property, get the specified
489 * property value at the specified index
493 * @return the property value
495 public MacAddress getMacAddress(PolicyPropertyInfo.PolicyPropertyId id, int index) {
498 PolicyPropertyInfo.PropertyType.MAC,
499 PolicyPropertyInfo.PropertyCardinality.VECTOR);
500 return (MacAddress)propertyMap.get(key).getValue(index);
504 * Get the number of MAC address values for the specified
508 * @return the number of elements
510 public int getMacAddressSize(PolicyPropertyInfo.PolicyPropertyId id) {
513 PolicyPropertyInfo.PropertyType.MAC,
514 PolicyPropertyInfo.PropertyCardinality.VECTOR);
515 Value v = propertyMap.get(key);
521 * Set the uint64-valued parameter to the specified value.
526 public void setUint64(PolicyPropertyInfo.PolicyPropertyId id, BigInteger bi) {
529 PolicyPropertyInfo.PropertyType.U64,
530 PolicyPropertyInfo.PropertyCardinality.SCALAR);
531 Value v = propertyMap.get(key);
532 if (v == null) v = new Value(); propertyMap.put(key, v);
534 v.setPropertyType(PolicyPropertyInfo.PropertyType.U64);
535 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
540 * Set the uint64-vector-valued parameter to the specified value.
545 public void setUint64(PolicyPropertyInfo.PolicyPropertyId id, List<BigInteger> bil) {
548 PolicyPropertyInfo.PropertyType.U64,
549 PolicyPropertyInfo.PropertyCardinality.VECTOR);
550 Value v = propertyMap.get(key);
551 if (v == null) v = new Value(); propertyMap.put(key, v);
553 v.setPropertyType(PolicyPropertyInfo.PropertyType.U64);
554 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
560 * Set the int64-valued parameter to the specified value.
565 public void setInt64(PolicyPropertyInfo.PolicyPropertyId id, long li) {
568 PolicyPropertyInfo.PropertyType.S64,
569 PolicyPropertyInfo.PropertyCardinality.SCALAR);
570 Value v = propertyMap.get(key);
571 if (v == null) v = new Value(); propertyMap.put(key, v);
573 v.setPropertyType(PolicyPropertyInfo.PropertyType.S64);
574 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
580 * Set the int64-vector-valued parameter to the specified value.
585 public void setInt64(PolicyPropertyInfo.PolicyPropertyId id, List<Long> ll) {
588 PolicyPropertyInfo.PropertyType.S64,
589 PolicyPropertyInfo.PropertyCardinality.VECTOR);
590 Value v = propertyMap.get(key);
591 if (v == null) v = new Value(); propertyMap.put(key, v);
593 v.setPropertyType(PolicyPropertyInfo.PropertyType.S64);
594 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
600 * Set the string-valued parameter to the specified value.
605 public void setString(PolicyPropertyInfo.PolicyPropertyId id, String s) {
608 PolicyPropertyInfo.PropertyType.STRING,
609 PolicyPropertyInfo.PropertyCardinality.SCALAR);
610 Value v = propertyMap.get(key);
611 if (v == null) v = new Value(); propertyMap.put(key, v);
613 v.setPropertyType(PolicyPropertyInfo.PropertyType.STRING);
614 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
620 * Set the string-vector-valued parameter to the specified vector.
625 public void setString(PolicyPropertyInfo.PolicyPropertyId id, List<String> sl) {
628 PolicyPropertyInfo.PropertyType.STRING,
629 PolicyPropertyInfo.PropertyCardinality.VECTOR);
630 Value v = propertyMap.get(key);
631 if (v == null) v = new Value(); propertyMap.put(key, v);
633 v.setPropertyType(PolicyPropertyInfo.PropertyType.STRING);
634 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
640 * Set the reference-valued parameter to the specified value.
645 public void setReference(PolicyPropertyInfo.PolicyPropertyId id, PolicyReference pr) {
648 PolicyPropertyInfo.PropertyType.REFERENCE,
649 PolicyPropertyInfo.PropertyCardinality.SCALAR);
650 Value v = propertyMap.get(key);
651 if (v == null) v = new Value(); propertyMap.put(key, v);
653 v.setPropertyType(PolicyPropertyInfo.PropertyType.REFERENCE);
654 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
660 * Set the reference-vector-valued parameter to the specified
666 public void setReference(PolicyPropertyInfo.PolicyPropertyId id, List<PolicyReference> prl) {
669 PolicyPropertyInfo.PropertyType.REFERENCE,
670 PolicyPropertyInfo.PropertyCardinality.VECTOR);
671 Value v = propertyMap.get(key);
672 if (v == null) v = new Value(); propertyMap.put(key, v);
674 v.setPropertyType(PolicyPropertyInfo.PropertyType.REFERENCE);
675 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
681 * Set the MAC address-valued parameter to the specified value.
686 public void setMacAddress(PolicyPropertyInfo.PolicyPropertyId id, MacAddress mac) {
689 PolicyPropertyInfo.PropertyType.MAC,
690 PolicyPropertyInfo.PropertyCardinality.SCALAR);
691 Value v = propertyMap.get(key);
692 if (v == null) v = new Value(); propertyMap.put(key, v);
694 v.setPropertyType(PolicyPropertyInfo.PropertyType.MAC);
695 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
701 * Set the MAC address-vector-valued parameter to the specified value.
706 public void setMacAddress(PolicyPropertyInfo.PolicyPropertyId id, List<MacAddress> macList) {
709 PolicyPropertyInfo.PropertyType.MAC,
710 PolicyPropertyInfo.PropertyCardinality.VECTOR);
711 Value v = propertyMap.get(key);
712 if (v == null) v = new Value(); propertyMap.put(key, v);
714 v.setPropertyType(PolicyPropertyInfo.PropertyType.MAC);
715 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
721 * Add a value to a the specified unsigned 64-bit vector.
726 public void addUint64(PolicyPropertyInfo.PolicyPropertyId id, BigInteger bi) {
729 PolicyPropertyInfo.PropertyType.U64,
730 PolicyPropertyInfo.PropertyCardinality.VECTOR);
731 Value v = propertyMap.get(key);
734 v.setPropertyType(PolicyPropertyInfo.PropertyType.U64);
735 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
736 List<BigInteger> bil = new ArrayList<BigInteger>();
738 propertyMap.put(key, v);
744 * Add a value to a the specified signed 64-bit vector.
749 public void addInt64(PolicyPropertyInfo.PolicyPropertyId id, long li) {
752 PolicyPropertyInfo.PropertyType.S64,
753 PolicyPropertyInfo.PropertyCardinality.VECTOR);
754 Value v = propertyMap.get(key);
757 v.setPropertyType(PolicyPropertyInfo.PropertyType.U64);
758 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
759 List<Long> ll = new ArrayList<Long>();
761 propertyMap.put(key, v);
767 * Add a value to a the specified string vector.
772 public void addString(PolicyPropertyInfo.PolicyPropertyId id, String s) {
775 PolicyPropertyInfo.PropertyType.STRING,
776 PolicyPropertyInfo.PropertyCardinality.VECTOR);
777 Value v = propertyMap.get(key);
780 v.setPropertyType(PolicyPropertyInfo.PropertyType.STRING);
781 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
782 List<String> sl = new ArrayList<String>();
784 propertyMap.put(key, v);
790 * Add a value to a the specified reference vector.
795 public void addReference(PolicyPropertyInfo.PolicyPropertyId id, PolicyReference pr) {
798 PolicyPropertyInfo.PropertyType.REFERENCE,
799 PolicyPropertyInfo.PropertyCardinality.VECTOR);
800 Value v = propertyMap.get(key);
803 v.setPropertyType(PolicyPropertyInfo.PropertyType.REFERENCE);
804 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
805 List<PolicyReference> prl = new ArrayList<PolicyReference>();
807 propertyMap.put(key, v);
814 * Add a value to a the specified MAC address vector.
819 public void addMacAddress(PolicyPropertyInfo.PolicyPropertyId id, MacAddress mac) {
822 PolicyPropertyInfo.PropertyType.MAC,
823 PolicyPropertyInfo.PropertyCardinality.VECTOR);
824 Value v = propertyMap.get(key);
827 v.setPropertyType(PolicyPropertyInfo.PropertyType.MAC);
828 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
829 List<MacAddress> ml = new ArrayList<MacAddress>();
831 propertyMap.put(key, v);