Merge "BUG-130 : introduced XROSubobjectUtil."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROExplicitExclusionRouteSubobjectParser.java
index aca27205e81eea828dc5d7bdcf18bb113191f6e9..56148546b6dbbded30ab2b1835dfa11040dbd353 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 
 import java.util.List;
 
+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;
@@ -18,12 +19,12 @@ 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.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;
-import com.google.common.primitives.UnsignedBytes;
 
 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
 
@@ -31,7 +32,6 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
 
        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;
@@ -65,11 +65,11 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
 
        @Override
        public byte[] serializeSubobject(final Subobjects subobject) {
-               if (!(subobject.getSubobjectType() instanceof Exrs)) {
+               if (!(subobject.getSubobjectType() instanceof ExrsCase)) {
                        throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
                                        + ". Needed Exrs.");
                }
-               final Exrs e = (Exrs) subobject.getSubobjectType();
+               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();
@@ -78,7 +78,7 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
                        b.setSubobjectType(ex.getSubobjectType());
                        list.add(b.build());
                }
-               return serializeSubobject(list);
+               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.xro.Subobjects> parseSubobjects(
@@ -129,18 +129,7 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
 
                        final XROSubobjectSerializer serializer = this.registry.getSubobjectSerializer(subobject.getSubobjectType());
 
-                       final byte[] valueBytes = serializer.serializeSubobject(subobject);
-
-                       final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length];
-
-                       final byte typeBytes = (byte) (UnsignedBytes.checkedCast(serializer.getType()) | (subobject.isMandatory() ? 1 << 7 : 0));
-
-                       final byte lengthBytes = UnsignedBytes.checkedCast(valueBytes.length);
-
-                       bytes[0] = typeBytes;
-                       bytes[1] = lengthBytes;
-                       System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
-
+                       final byte[] bytes = serializer.serializeSubobject(subobject);
                        finalLength += bytes.length;
                        result.add(bytes);
                }