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;
16 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.PolicyPropertyInfo.PolicyPropertyId;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
21 * This is a policy object instance, used to provide an abstraction
22 * 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}, {@link PolicyPropertyInfo.PropertyType},
31 * {@link PolicyPropertyInfo.PropertyCardinality}
35 public static class PropertyKey {
37 private final PolicyPropertyId propId;
38 private final PolicyPropertyInfo.PropertyType type;
39 private final PolicyPropertyInfo.PropertyCardinality cardinality;
41 public PropertyKey(PolicyPropertyId propId, PolicyPropertyInfo.PropertyType type,
42 PolicyPropertyInfo.PropertyCardinality cardinality) {
45 this.cardinality = cardinality;
49 public int hashCode() {
52 result = prime * result + ((cardinality == null) ? 0 : cardinality.hashCode());
53 result = prime * result + ((propId == null) ? 0 : propId.hashCode());
54 result = prime * result + ((type == null) ? 0 : type.hashCode());
59 public boolean equals(Object obj) {
64 if (getClass() != obj.getClass())
66 PropertyKey other = (PropertyKey) obj;
67 if (cardinality != other.cardinality)
70 if (other.propId != null)
72 } else if (!propId.equals(other.propId))
74 if (type != other.type)
82 * Class that contains a value held by the {@link PolicyObjectInstance}.
83 * The value can be scalar or vector in nature.
87 public static class Value {
90 private List<Object> vl;
91 private PolicyPropertyInfo.PropertyType type;
92 private PolicyPropertyInfo.PropertyCardinality cardinality;
94 public void setPropertyType(PolicyPropertyInfo.PropertyType type) {
98 public void setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality cardinality) {
99 this.cardinality = cardinality;
102 public void setValue(Object v) {
106 public void setValue(List<Object> vl) {
110 public Object getValue(int index) {
111 if (this.cardinality == PolicyPropertyInfo.PropertyCardinality.VECTOR) {
112 return this.vl.get(index);
118 public PolicyPropertyInfo.PropertyType getType() {
124 * Class used as a reference to another PolicyObject.
128 public static class PolicyReference {
130 private final Uri uri;
131 private final long classId;
133 public PolicyReference(long classId, Uri uri) {
135 this.classId = classId;
138 public Uri getUri() {
142 public long getClassId() {
147 public int hashCode() {
148 final int prime = 31;
150 result = prime * result + (int) (classId ^ (classId >>> 32));
151 result = prime * result + ((uri == null) ? 0 : uri.hashCode());
156 public boolean equals(Object obj) {
161 if (getClass() != obj.getClass())
163 PolicyReference other = (PolicyReference) obj;
164 if (classId != other.classId)
167 if (other.uri != null)
169 } else if (!uri.equals(other.uri))
176 private final long classId;
178 private Uri parentUri;
179 private String parentSubject;
180 private String parentRelation;
181 private final List<Uri> children = new ArrayList<>();
182 private final Map<PropertyKey, Value> propertyMap = new HashMap<>();
184 private PolicyPropertyInfo.PropertyType normalize(PolicyPropertyInfo.PropertyType type) {
190 return PolicyPropertyInfo.PropertyType.U64;
196 public PolicyObjectInstance(long classId) {
197 this.classId = classId;
201 * Get the class ID for this object instance.
203 * @return the class ID
205 public long getClassId() {
209 public Uri getUri() {
213 public void setUri(Uri uri) {
217 public void addChild(Uri childUri) {
218 children.add(childUri);
221 public List<Uri> getChildren() {
222 return this.children;
225 public void setParent(Uri uri) {
226 this.parentUri = uri;
229 public Uri getParent() {
230 return this.parentUri;
233 public void setParentSubject(String subject) {
234 this.parentSubject = subject;
237 public String getParentSubject() {
238 return this.parentSubject;
241 public void setParentRelation(String relation) {
242 this.parentRelation = relation;
245 public String getParentRelation() {
246 return this.parentRelation;
250 * Check whether the given property is set. If the property is
251 * vector-valued, this will return false if the vector is zero length
256 * @return true if set, false if not set or zero length vector
258 public boolean isSet(PolicyPropertyInfo.PolicyPropertyId propId, PolicyPropertyInfo.PropertyType type,
259 PolicyPropertyInfo.PropertyCardinality cardinality) {
260 type = normalize(type);
261 PropertyKey key = new PropertyKey(propId, type, cardinality);
262 return propertyMap.containsKey(key);
266 * Unset the given property. If it's a vector, the vector is
267 * emptied. If its a scalar, the scalar is unset.
272 * @return true if the property was alread set before
274 public boolean unset(PolicyPropertyInfo.PolicyPropertyId propId, PolicyPropertyInfo.PropertyType type,
275 PolicyPropertyInfo.PropertyCardinality cardinality) {
276 type = normalize(type);
277 PropertyKey key = new PropertyKey(propId, type, cardinality);
278 Value v = propertyMap.remove(key);
287 * Get the unsigned 64-bit valued property for prop_name.
290 * @return null if not present or {@link BigInteger}
292 public BigInteger getUint64(PolicyPropertyInfo.PolicyPropertyId id) {
293 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.U64,
294 PolicyPropertyInfo.PropertyCardinality.SCALAR);
295 return (BigInteger) propertyMap.get(key).getValue(0);
299 * For a vector-valued 64-bit unsigned property, get the specified
300 * property value at the specified index
304 * @return the property value
306 public BigInteger getUint64(PolicyPropertyInfo.PolicyPropertyId id, int index) {
307 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.U64,
308 PolicyPropertyInfo.PropertyCardinality.VECTOR);
309 return (BigInteger) propertyMap.get(key).getValue(index);
313 * Get the number of unsigned 64-bit values for the specified
317 * @return the number of elements
319 public int getUint64Size(PolicyPropertyInfo.PolicyPropertyId id) {
320 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.U64,
321 PolicyPropertyInfo.PropertyCardinality.VECTOR);
322 Value v = propertyMap.get(key);
327 * Get the signed 64-bit valued property for prop_name.
330 * @return the property value
332 public long getInt64(PolicyPropertyInfo.PolicyPropertyId id) {
333 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.S64,
334 PolicyPropertyInfo.PropertyCardinality.SCALAR);
335 return (long) propertyMap.get(key).getValue(0);
339 * For a vector-valued 64-bit signed property, get the specified
340 * property value at the specified index
344 * @return the property value
346 public long getInt64(PolicyPropertyInfo.PolicyPropertyId id, int index) {
347 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.S64,
348 PolicyPropertyInfo.PropertyCardinality.VECTOR);
349 return (long) propertyMap.get(key).getValue(index);
353 * Get the number of signed 64-bit values for the specified
357 * @return the number of elements
359 public int getInt64Size(PolicyPropertyInfo.PolicyPropertyId id) {
360 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.S64,
361 PolicyPropertyInfo.PropertyCardinality.VECTOR);
362 Value v = propertyMap.get(key);
367 * Get the string-valued property for prop_name.
370 * @return the property value
372 public String getString(PolicyPropertyInfo.PolicyPropertyId id) {
373 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.STRING,
374 PolicyPropertyInfo.PropertyCardinality.SCALAR);
375 return (String) propertyMap.get(key).getValue(0);
379 * For a vector-valued string property, get the specified property
380 * value at the specified index
384 * @return the property value
386 public String getString(PolicyPropertyInfo.PolicyPropertyId id, int index) {
387 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.STRING,
388 PolicyPropertyInfo.PropertyCardinality.VECTOR);
389 return (String) propertyMap.get(key).getValue(index);
393 * Get the number of string values for the specified property
396 * @return the number of elements
398 public int getStringSize(PolicyPropertyInfo.PolicyPropertyId id) {
399 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.STRING,
400 PolicyPropertyInfo.PropertyCardinality.VECTOR);
401 Value v = propertyMap.get(key);
406 * Get the reference-valued property for prop_name.
409 * @return the property value
411 public PolicyReference getReference(PolicyPropertyInfo.PolicyPropertyId id) {
412 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.REFERENCE,
413 PolicyPropertyInfo.PropertyCardinality.SCALAR);
414 return (PolicyReference) propertyMap.get(key).getValue(0);
418 * For a vector-valued reference property, get the specified property
419 * value at the specified index
423 * @return the property value
425 public PolicyReference getReference(PolicyPropertyInfo.PolicyPropertyId id, int index) {
426 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.REFERENCE,
427 PolicyPropertyInfo.PropertyCardinality.VECTOR);
428 return (PolicyReference) propertyMap.get(key).getValue(index);
432 * Get the number of reference values for the specified property
435 * @return the number of elements
437 public int getReferenceSize(PolicyPropertyInfo.PolicyPropertyId id) {
438 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.REFERENCE,
439 PolicyPropertyInfo.PropertyCardinality.VECTOR);
440 Value v = propertyMap.get(key);
445 * Get the MAC-address-valued property for prop_name.
448 * @return the property value
450 public MacAddress getMacAddress(PolicyPropertyInfo.PolicyPropertyId id) {
451 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.MAC,
452 PolicyPropertyInfo.PropertyCardinality.SCALAR);
453 return (MacAddress) propertyMap.get(key).getValue(0);
457 * For a vector-valued MAC address property, get the specified
458 * property value at the specified index
462 * @return the property value
464 public MacAddress getMacAddress(PolicyPropertyInfo.PolicyPropertyId id, int index) {
465 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.MAC,
466 PolicyPropertyInfo.PropertyCardinality.VECTOR);
467 return (MacAddress) propertyMap.get(key).getValue(index);
471 * Get the number of MAC address values for the specified
475 * @return the number of elements
477 public int getMacAddressSize(PolicyPropertyInfo.PolicyPropertyId id) {
478 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.MAC,
479 PolicyPropertyInfo.PropertyCardinality.VECTOR);
480 Value v = propertyMap.get(key);
486 * Set the uint64-valued parameter to the specified value.
491 public void setUint64(PolicyPropertyInfo.PolicyPropertyId id, BigInteger bi) {
492 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.U64,
493 PolicyPropertyInfo.PropertyCardinality.SCALAR);
494 Value v = propertyMap.get(key);
497 propertyMap.put(key, v);
499 v.setPropertyType(PolicyPropertyInfo.PropertyType.U64);
500 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
505 * Set the uint64-vector-valued parameter to the specified value.
510 public void setUint64(PolicyPropertyInfo.PolicyPropertyId id, List<BigInteger> bil) {
511 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.U64,
512 PolicyPropertyInfo.PropertyCardinality.VECTOR);
513 Value v = propertyMap.get(key);
516 propertyMap.put(key, v);
518 v.setPropertyType(PolicyPropertyInfo.PropertyType.U64);
519 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
525 * Set the int64-valued parameter to the specified value.
530 public void setInt64(PolicyPropertyInfo.PolicyPropertyId id, long li) {
531 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.S64,
532 PolicyPropertyInfo.PropertyCardinality.SCALAR);
533 Value v = propertyMap.get(key);
536 propertyMap.put(key, v);
538 v.setPropertyType(PolicyPropertyInfo.PropertyType.S64);
539 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
545 * Set the int64-vector-valued parameter to the specified value.
550 public void setInt64(PolicyPropertyInfo.PolicyPropertyId id, List<Long> ll) {
551 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.S64,
552 PolicyPropertyInfo.PropertyCardinality.VECTOR);
553 Value v = propertyMap.get(key);
556 propertyMap.put(key, v);
558 v.setPropertyType(PolicyPropertyInfo.PropertyType.S64);
559 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
565 * Set the string-valued parameter to the specified value.
570 public void setString(PolicyPropertyInfo.PolicyPropertyId id, String s) {
571 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.STRING,
572 PolicyPropertyInfo.PropertyCardinality.SCALAR);
573 Value v = propertyMap.get(key);
576 propertyMap.put(key, v);
578 v.setPropertyType(PolicyPropertyInfo.PropertyType.STRING);
579 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
585 * Set the string-vector-valued parameter to the specified vector.
590 public void setString(PolicyPropertyInfo.PolicyPropertyId id, List<String> sl) {
591 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.STRING,
592 PolicyPropertyInfo.PropertyCardinality.VECTOR);
593 Value v = propertyMap.get(key);
596 propertyMap.put(key, v);
598 v.setPropertyType(PolicyPropertyInfo.PropertyType.STRING);
599 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
605 * Set the reference-valued parameter to the specified value.
610 public void setReference(PolicyPropertyInfo.PolicyPropertyId id, PolicyReference pr) {
611 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.REFERENCE,
612 PolicyPropertyInfo.PropertyCardinality.SCALAR);
613 Value v = propertyMap.get(key);
616 propertyMap.put(key, v);
618 v.setPropertyType(PolicyPropertyInfo.PropertyType.REFERENCE);
619 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
625 * Set the reference-vector-valued parameter to the specified
631 public void setReference(PolicyPropertyInfo.PolicyPropertyId id, List<PolicyReference> prl) {
632 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.REFERENCE,
633 PolicyPropertyInfo.PropertyCardinality.VECTOR);
634 Value v = propertyMap.get(key);
637 propertyMap.put(key, v);
639 v.setPropertyType(PolicyPropertyInfo.PropertyType.REFERENCE);
640 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
646 * Set the MAC address-valued parameter to the specified value.
651 public void setMacAddress(PolicyPropertyInfo.PolicyPropertyId id, MacAddress mac) {
652 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.MAC,
653 PolicyPropertyInfo.PropertyCardinality.SCALAR);
654 Value v = propertyMap.get(key);
657 propertyMap.put(key, v);
659 v.setPropertyType(PolicyPropertyInfo.PropertyType.MAC);
660 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
666 * Set the MAC address-vector-valued parameter to the specified value.
671 public void setMacAddress(PolicyPropertyInfo.PolicyPropertyId id, List<MacAddress> macList) {
672 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.MAC,
673 PolicyPropertyInfo.PropertyCardinality.VECTOR);
674 Value v = propertyMap.get(key);
677 propertyMap.put(key, v);
679 v.setPropertyType(PolicyPropertyInfo.PropertyType.MAC);
680 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
686 * Add a value to a the specified unsigned 64-bit vector.
691 public void addUint64(PolicyPropertyInfo.PolicyPropertyId id, BigInteger bi) {
692 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.U64,
693 PolicyPropertyInfo.PropertyCardinality.VECTOR);
694 Value v = propertyMap.get(key);
697 v.setPropertyType(PolicyPropertyInfo.PropertyType.U64);
698 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
699 List<BigInteger> bil = new ArrayList<BigInteger>();
701 propertyMap.put(key, v);
707 * Add a value to a the specified signed 64-bit vector.
712 public void addInt64(PolicyPropertyInfo.PolicyPropertyId id, long li) {
713 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.S64,
714 PolicyPropertyInfo.PropertyCardinality.VECTOR);
715 Value v = propertyMap.get(key);
718 v.setPropertyType(PolicyPropertyInfo.PropertyType.U64);
719 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
720 List<Long> ll = new ArrayList<Long>();
722 propertyMap.put(key, v);
728 * Add a value to a the specified string vector.
733 public void addString(PolicyPropertyInfo.PolicyPropertyId id, String s) {
734 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.STRING,
735 PolicyPropertyInfo.PropertyCardinality.VECTOR);
736 Value v = propertyMap.get(key);
739 v.setPropertyType(PolicyPropertyInfo.PropertyType.STRING);
740 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
741 List<String> sl = new ArrayList<String>();
743 propertyMap.put(key, v);
749 * Add a value to a the specified reference vector.
754 public void addReference(PolicyPropertyInfo.PolicyPropertyId id, PolicyReference pr) {
755 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.REFERENCE,
756 PolicyPropertyInfo.PropertyCardinality.VECTOR);
757 Value v = propertyMap.get(key);
760 v.setPropertyType(PolicyPropertyInfo.PropertyType.REFERENCE);
761 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
762 List<PolicyReference> prl = new ArrayList<PolicyReference>();
764 propertyMap.put(key, v);
771 * Add a value to a the specified MAC address vector.
776 public void addMacAddress(PolicyPropertyInfo.PolicyPropertyId id, MacAddress mac) {
777 PropertyKey key = new PropertyKey(id, PolicyPropertyInfo.PropertyType.MAC,
778 PolicyPropertyInfo.PropertyCardinality.VECTOR);
779 Value v = propertyMap.get(key);
782 v.setPropertyType(PolicyPropertyInfo.PropertyType.MAC);
783 v.setPropertyCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
784 List<MacAddress> ml = new ArrayList<MacAddress>();
786 propertyMap.put(key, v);