Fix SubIdStatement/ObjectIdentifier ranges
[yangtools.git] / yang / rfc6643-model-api / src / main / java / org / opendaylight / yangtools / rfc6643 / model / api / ObjectIdentifier.java
index c97a12379ca1bc8bc5124915175dadb46b8d427a..806b237a1ba3ce87685f476b33952c8f55a2eca1 100644 (file)
@@ -19,6 +19,7 @@ import java.util.stream.IntStream;
 import org.checkerframework.checker.regex.qual.Regex;
 import org.opendaylight.yangtools.concepts.Identifier;
 import org.opendaylight.yangtools.concepts.WritableObject;
+import org.opendaylight.yangtools.yang.common.Uint32;
 
 /**
  * An OID, or ObjectIdentifier, as defined by ITU and ISO/IEC.
@@ -32,20 +33,21 @@ public final class ObjectIdentifier implements Identifier, WritableObject {
     private static final Pattern CHECK_OID_PATTERN = Pattern.compile(CHECK_OID_REGEX);
     private static final Pattern SPLIT_PATTERN = Pattern.compile(".", Pattern.LITERAL);
 
-    private final int[] components;
+    private final int[] subIdentifiers;
 
-    private ObjectIdentifier(final int[] components) {
-        this.components = components;
+    private ObjectIdentifier(final int[] subIdentifiers) {
+        this.subIdentifiers = subIdentifiers;
     }
 
     /**
-     * Create an {@link ObjectIdentifier} from its integer components.
+     * Create an {@link ObjectIdentifier} from its integer components. Each sub-identifier is interpreted as an unsigned
+     * integer.
      *
-     * @param components OID items
+     * @param subIdentifiers OID sub-identifiers
      * @return An ObjectIdentifier.
      */
-    public static ObjectIdentifier forComponents(final int... components) {
-        return new ObjectIdentifier(components.clone());
+    public static ObjectIdentifier of(final int... subIdentifiers) {
+        return new ObjectIdentifier(subIdentifiers.clone());
     }
 
     /**
@@ -58,13 +60,13 @@ public final class ObjectIdentifier implements Identifier, WritableObject {
         return new ObjectIdentifier(parseObjectId(str));
     }
 
-    public int[] getComponents() {
+    public int[] getSubIdentifiers() {
         // Always make a defensive copy
-        return components.clone();
+        return subIdentifiers.clone();
     }
 
-    public IntStream streamComponents() {
-        return Arrays.stream(components);
+    public IntStream streamSubIdentifiers() {
+        return Arrays.stream(subIdentifiers);
     }
 
     /**
@@ -76,8 +78,8 @@ public final class ObjectIdentifier implements Identifier, WritableObject {
      * @throws IOException If an I/O error is reported
      */
     public static ObjectIdentifier readFrom(final DataInput in) throws IOException {
-        final int count = in.readInt();
-        checkArgument(count >= 0, "Illegal item count");
+        final int count = in.readUnsignedByte();
+        checkArgument(count >= 0 && count <= 128, "Illegal item count %s", count);
 
         final int[] oid = new int[count];
         for (int index = 0; index < count; ++index) {
@@ -89,29 +91,29 @@ public final class ObjectIdentifier implements Identifier, WritableObject {
 
     @Override
     public void writeTo(final DataOutput out) throws IOException {
-        out.writeInt(components.length);
-        for (int i : components) {
+        out.writeByte(subIdentifiers.length);
+        for (int i : subIdentifiers) {
             out.writeInt(i);
         }
     }
 
     @Override
     public int hashCode() {
-        return Arrays.hashCode(components);
+        return Arrays.hashCode(subIdentifiers);
     }
 
     @Override
     public boolean equals(final Object obj) {
         return this == obj || obj instanceof ObjectIdentifier
-                && Arrays.equals(components, ((ObjectIdentifier) obj).components);
+                && Arrays.equals(subIdentifiers, ((ObjectIdentifier) obj).subIdentifiers);
     }
 
     @Override
     public String toString() {
         StringBuilder stringBuilder = new StringBuilder();
-        stringBuilder.append(components[0]);
-        for (int index = 1; index < components.length; index++) {
-            stringBuilder.append('.').append(components[index]);
+        stringBuilder.append(subIdentifiers[0]);
+        for (int index = 1; index < subIdentifiers.length; index++) {
+            stringBuilder.append('.').append(subIdentifiers[index]);
         }
         return stringBuilder.toString();
     }
@@ -120,9 +122,11 @@ public final class ObjectIdentifier implements Identifier, WritableObject {
         checkArgument(CHECK_OID_PATTERN.matcher(objectId).matches(), "Wrong format for OID: '%s'", objectId);
 
         final String[] splitOid = SPLIT_PATTERN.split(objectId);
+        checkArgument(splitOid.length <= 128, "Object Identifier can have at most 128 sub-identifiers");
+
         final int[] oid = new int[splitOid.length];
         for (int index = 0; index < splitOid.length; index ++) {
-            oid[index] = Integer.parseInt(splitOid[index]);
+            oid[index] = Uint32.valueOf(splitOid[index]).intValue();
         }
         return oid;
     }