Rework PCEPDeserializerException and move it into SPI
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPSvecObjectParser.java
index c3f6b142761d30b2856a3af69e274c82ec7bb79b..666af449dfb47a28fd5dfa6134dd8dd1445dd97f 100644 (file)
@@ -10,22 +10,20 @@ package org.opendaylight.protocol.pcep.impl.object;
 import java.util.BitSet;
 import java.util.List;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPDocumentedException;
-import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SvecObject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.SvecBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.Svec;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.SvecBuilder;
 
 import com.google.common.collect.Lists;
 
 /**
- * Parser for {@link SvecObject}
+ * Parser for {@link Svec}
  */
 public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuilder> {
 
@@ -36,53 +34,49 @@ public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuild
        /*
         * field lengths in bytes
         */
-       public static final int FLAGS_F_LENGTH = 3;
-       public static final int REQ_LIST_ITEM_LENGTH = 4;
+       private static final int FLAGS_F_LENGTH = 3;
+       private static final int REQ_LIST_ITEM_LENGTH = 4;
 
        /*
         * fields offsets in bytes
         */
-       public static final int FLAGS_F_OFFSET = 1; // aded reserved field of size 1
-       public static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
+       private static final int FLAGS_F_OFFSET = 1;
+       private static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
 
        /*
         * flags offsets inside flags field in bits
         */
-       public static final int S_FLAG_OFFSET = 21;
-       public static final int N_FLAG_OFFSET = 22;
-       public static final int L_FLAG_OFFSET = 23;
+       private static final int S_FLAG_OFFSET = 21;
+       private static final int N_FLAG_OFFSET = 22;
+       private static final int L_FLAG_OFFSET = 23;
 
        /*
         * min size in bytes
         */
-       public static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
+       private static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
 
        public PCEPSvecObjectParser(final TlvHandlerRegistry tlvReg) {
                super(tlvReg);
        }
 
        @Override
-       public SvecObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
+       public Svec parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
                if (bytes == null || bytes.length == 0) {
                        throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
                }
-
                if (bytes.length < MIN_SIZE) {
                        throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
                                        + ".");
                }
-
                final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
                final List<RequestId> requestIDs = Lists.newArrayList();
 
                for (int i = REQ_ID_LIST_OFFSET; i < bytes.length; i += REQ_LIST_ITEM_LENGTH) {
                        requestIDs.add(new RequestId(ByteArray.bytesToLong(ByteArray.subByte(bytes, i, REQ_LIST_ITEM_LENGTH))));
                }
-
                if (requestIDs.isEmpty()) {
                        throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
                }
-
                final SvecBuilder builder = new SvecBuilder();
 
                builder.setIgnore(header.isIgnore());
@@ -102,11 +96,11 @@ public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuild
 
        @Override
        public byte[] serializeObject(final Object object) {
-               if (!(object instanceof SvecObject)) {
+               if (!(object instanceof Svec)) {
                        throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
                }
 
-               final SvecObject svecObj = (SvecObject) object;
+               final Svec svecObj = (Svec) object;
                final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
                final List<RequestId> requestIDs = svecObj.getRequestsIds();
                final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
@@ -120,9 +114,7 @@ public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuild
                        System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue()), 4, retBytes, REQ_LIST_ITEM_LENGTH * i
                                        + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
                }
-
                assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
-
                return retBytes;
        }