BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROUnnumberedInterfaceSubobjectParser.java
index f2f96e0471b986691b0811135779655d71153ad1..ada6e0dee599872b37f4ca46780c0a16f122a962 100644 (file)
@@ -8,15 +8,24 @@
 package org.opendaylight.protocol.pcep.impl.subobject;
 
 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
-import org.opendaylight.protocol.pcep.subobject.XROUnnumberedInterfaceSubobject;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
 import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.SubobjectsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedBuilder;
+
+import com.google.common.primitives.UnsignedInts;
 
 /**
- * Parser for {@link org.opendaylight.protocol.pcep.subobject.XROUnnumberedInterfaceSubobject
- * XROUnnumberedInterfaceSubobject}
+ * Parser for {@link UnnumberedSubobject}
  */
-public class XROUnnumberedInterfaceSubobjectParser {
+public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
+
+       public static final int TYPE = 4;
+
        public static final int ATTRIBUTE_LENGTH = 1;
        public static final int ROUTER_ID_NUMBER_LENGTH = 4;
        public static final int INTERFACE_ID_NUMBER_LENGTH = 4;
@@ -27,38 +36,44 @@ public class XROUnnumberedInterfaceSubobjectParser {
 
        public static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
 
-       public static XROUnnumberedInterfaceSubobject parse(final byte[] soContentsBytes, final boolean mandatory)
-                       throws PCEPDeserializerException {
-               if (soContentsBytes == null || soContentsBytes.length == 0)
+       @Override
+       public Subobjects parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
+               if (buffer == null || buffer.length == 0)
                        throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
-               if (soContentsBytes.length != CONTENT_LENGTH)
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: "
+               if (buffer.length != CONTENT_LENGTH)
+                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
                                        + CONTENT_LENGTH + ".");
 
-               // return new XROUnnumberedInterfaceSubobject(new IPv4Address(
-               // ByteArray.subByte(soContentsBytes, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)), new
-               // UnnumberedInterfaceIdentifier(
-               // UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(soContentsBytes, INTERFACE_ID_NUMBER_OFFSET,
-               // INTERFACE_ID_NUMBER_LENGTH)))),
-               // mandatory, XROSubobjectAttributeMapping.getInstance().getFromAttributeIdentifier((short)
-               // (soContentsBytes[ATTRIBUTE_OFFSET] & 0xFF)));
-               return null;
+               final SubobjectsBuilder builder = new SubobjectsBuilder();
+               final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
+               ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
+               ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
+                               INTERFACE_ID_NUMBER_LENGTH))));
+               builder.setSubobjectType(ubuilder.build());
+               builder.setMandatory(mandatory);
+               builder.setAttribute(Attribute.forValue(buffer[ATTRIBUTE_OFFSET] & 0xFF));
+               return builder.build();
        }
 
-       public static byte[] put(final ExcludeRouteSubobject objToSerialize) {
-               if (!(objToSerialize instanceof XROUnnumberedInterfaceSubobject))
-                       throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + objToSerialize.getClass()
-                                       + ". Needed XROUnnumberedInterfaceSubobject.");
+       @Override
+       public byte[] serializeSubobject(final Subobjects subobject) {
+               if (!(subobject.getSubobjectType() instanceof UnnumberedSubobject))
+                       throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
+                                       + ". Needed UnnumberedSubobject.");
 
                byte[] retBytes;
                retBytes = new byte[CONTENT_LENGTH];
-               final XROUnnumberedInterfaceSubobject specObj = (XROUnnumberedInterfaceSubobject) objToSerialize;
-
-               retBytes[ATTRIBUTE_OFFSET] = (byte) XROSubobjectAttributeMapping.getInstance().getFromAttributeEnum(specObj.getAttribute());
-               ByteArray.copyWhole(specObj.getRouterID().getValue().getBytes(), retBytes, ROUTER_ID_NUMBER_OFFSET);
-               System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceID().getInterfaceId()), Long.SIZE / Byte.SIZE
-                               - INTERFACE_ID_NUMBER_LENGTH, retBytes, INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
+               final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
 
+               retBytes[ATTRIBUTE_OFFSET] = (byte) subobject.getAttribute().getIntValue();
+               ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId()), retBytes, ROUTER_ID_NUMBER_OFFSET);
+               System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
+                               INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
                return retBytes;
        }
+
+       @Override
+       public int getType() {
+               return TYPE;
+       }
 }