BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROIpPrefixSubobjectParser.java
index 85ca26e0549e47c2b51ab2c1756599165a22e4f4..097c8dd315184abd49f59698b853380773a09fe4 100644 (file)
@@ -10,11 +10,12 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.concepts.Ipv6Util;
 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.SubobjectParser;
-import org.opendaylight.protocol.pcep.spi.SubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.CSubobject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixBuilder;
 
@@ -23,16 +24,18 @@ import com.google.common.primitives.UnsignedBytes;
 /**
  * Parser for {@link IpPrefixSubobject}
  */
-public class EROIpPrefixSubobjectParser implements SubobjectParser, SubobjectSerializer {
+public class EROIpPrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
 
-       public static final int TYPE = 32;
+       public static final int TYPE = 1;
+
+       public static final int TYPE6 = 2;
 
        public static final int IP4_F_LENGTH = 4;
        public static final int PREFIX4_F_LENGTH = 1;
 
        public static final int PREFIX4_F_OFFSET = IP4_F_LENGTH;
 
-       public static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1; //added reserved field of size 1 byte
+       public static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1; // added reserved field of size 1
 
        public static final int IP_F_LENGTH = 16;
        public static final int PREFIX_F_LENGTH = 1;
@@ -40,50 +43,62 @@ public class EROIpPrefixSubobjectParser implements SubobjectParser, SubobjectSer
        public static final int IP_F_OFFSET = 0;
        public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
 
-       public static final int CONTENT_LENGTH = PREFIX_F_OFFSET + PREFIX_F_LENGTH + 1; // added reserved field of size 1 byte
+       public static final int CONTENT_LENGTH = PREFIX_F_OFFSET + PREFIX_F_LENGTH + 1; // added reserved field of size 1
+                                                                                                                                                                       // byte
 
        @Override
-       public IpPrefixSubobject parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
+       public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
                if (buffer == null || buffer.length == 0) {
                        throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
                }
 
+               final SubobjectsBuilder builder = new SubobjectsBuilder();
+               builder.setLoose(loose);
+
                if (buffer.length == CONTENT4_LENGTH) {
                        final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
-                       return new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP4_F_LENGTH), length))).build();
+                       builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
+                                       new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP4_F_LENGTH), length))).build());
+                       return builder.build();
                } else if (buffer.length == CONTENT_LENGTH) {
                        final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
-                       return new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP_F_LENGTH), length))).build();
+                       builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
+                                       new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP_F_LENGTH), length))).build());
+                       return builder.build();
                } else {
                        throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
                }
        }
 
        @Override
-       public byte[] serializeSubobject(final CSubobject subobject) {
-               if (!(subobject instanceof IpPrefixSubobject)) {
-                       throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getClass()
+       public byte[] serializeSubobject(final Subobjects subobject) {
+               if (!(subobject.getSubobjectType() instanceof IpPrefixSubobject)) {
+                       throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
                                        + ". Needed IpPrefixSubobject.");
                }
-
-               final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject;
+               final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject.getSubobjectType();
                final IpPrefix prefix = specObj.getIpPrefix();
 
                if (prefix.getIpv4Prefix() != null) {
                        final byte[] retBytes = new byte[CONTENT4_LENGTH];
                        ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, 0);
+                       retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
                        return retBytes;
                } else if (prefix.getIpv6Prefix() != null) {
-                       final byte[] retBytes = new byte[CONTENT4_LENGTH];
-                       ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, 0);
+                       final byte[] retBytes = new byte[CONTENT_LENGTH];
+                       ByteArray.copyWhole(prefix.getIpv6Prefix().getValue().getBytes(), retBytes, 0);
+                       retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
                        return retBytes;
-                       //retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(prefix.getLength())[Integer.SIZE / Byte.SIZE - 1];
                }
-               return new byte[0];
+               throw new IllegalArgumentException("No valid IpPrefix");
        }
 
        @Override
        public int getType() {
                return TYPE;
        }
+
+       public int getType6() {
+               return TYPE6;
+       }
 }