Merge "BUG-130 : introduced XROSubobjectUtil."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROExplicitExclusionRouteSubobjectParser.java
index 07c9826184773c81d135ef194a1b320d38c891b0..56148546b6dbbded30ab2b1835dfa11040dbd353 100644 (file)
@@ -9,14 +9,20 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 
 import java.util.List;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
+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.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.pcep.types.rev131005.explicit.route.object.ero.Subobjects;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.Exrs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
 
 import com.google.common.collect.Lists;
 
@@ -24,11 +30,8 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
 
        public static final int TYPE = 33;
 
-       private static final int RESERVED = 2;
-
        private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
        private static final int SUB_LENGTH_F_LENGTH = 1;
-       private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH;
 
        private static final int TYPE_FLAG_F_OFFSET = 0;
        private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
@@ -47,18 +50,38 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
                }
                final SubobjectsBuilder builder = new SubobjectsBuilder();
                builder.setLoose(loose);
-               // builder.setSubobjectType( )
+               final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> list = parseSubobjects(buffer);
+               final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs> exrss = Lists.newArrayList();
+               for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects s : list) {
+                       final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder();
+                       b.setAttribute(s.getAttribute());
+                       b.setMandatory(s.isMandatory());
+                       b.setSubobjectType(s.getSubobjectType());
+                       exrss.add(b.build());
+               }
+               builder.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build());
                return builder.build();
        }
 
        @Override
        public byte[] serializeSubobject(final Subobjects subobject) {
-
-               // return PCEPXROSubobjectParser.put(subobject.getXroSubobjets());
-               return new byte[0];
+               if (!(subobject.getSubobjectType() instanceof ExrsCase)) {
+                       throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
+                                       + ". Needed Exrs.");
+               }
+               final Exrs e = ((ExrsCase) subobject.getSubobjectType()).getExrs();
+               final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> list = Lists.newArrayList();
+               for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs ex : e.getExrs()) {
+                       final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectsBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectsBuilder();
+                       b.setAttribute(ex.getAttribute());
+                       b.setMandatory(ex.isMandatory());
+                       b.setSubobjectType(ex.getSubobjectType());
+                       list.add(b.build());
+               }
+               return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), serializeSubobject(list));
        }
 
-       private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects> parseSubobjects(
+       private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> parseSubobjects(
                        final byte[] bytes) throws PCEPDeserializerException {
                if (bytes == null) {
                        throw new IllegalArgumentException("Byte array is mandatory.");
@@ -70,14 +93,14 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
                int length;
                int offset = 0;
 
-               final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects> subs = Lists.newArrayList();
+               final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> subs = Lists.newArrayList();
 
                while (offset < bytes.length) {
 
                        length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
 
-                       type = bytes[offset + TYPE_FLAG_F_OFFSET];
-
+                       final boolean mandatory = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
+                       type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
                        if (length > bytes.length - offset) {
                                throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
                                                + (bytes.length - offset));
@@ -86,38 +109,27 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
                        soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
                        System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
 
-                       final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects sub = this.registry.getSubobjectParser(
-                                       type).parseSubobject(soContentsBytes, false);
+                       final XROSubobjectParser parser = this.registry.getSubobjectParser(type);
 
-                       subs.add(sub);
+                       subs.add(parser.parseSubobject(soContentsBytes, mandatory));
 
                        offset += length;
                }
                return subs;
        }
 
-       private final byte[] serializeSubobject(
-                       final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects> subobjects) {
+       private byte[] serializeSubobject(
+                       final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects> subobjects) {
 
                final List<byte[]> result = Lists.newArrayList();
 
                int finalLength = 0;
 
-               for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects subobject : subobjects) {
-
-                       final XROSubobjectSerializer serializer = this.registry.getSubobjectSerializer(subobject);
-
-                       final byte[] valueBytes = serializer.serializeSubobject(subobject);
-
-                       final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length];
-
-                       final byte typeBytes = (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - 1)[0]);
-                       final byte lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - 1)[0];
+               for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects subobject : subobjects) {
 
-                       bytes[0] = typeBytes;
-                       bytes[1] = lengthBytes;
-                       System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
+                       final XROSubobjectSerializer serializer = this.registry.getSubobjectSerializer(subobject.getSubobjectType());
 
+                       final byte[] bytes = serializer.serializeSubobject(subobject);
                        finalLength += bytes.length;
                        result.add(bytes);
                }