Removed checkstyle warnings.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / AbstractXROWithSubobjectsParser.java
index 89309c90608ef8554e7aa2b4fab827814adf4044..25d8bdc3712c44cfdd6abd802290a875d3129bfb 100644 (file)
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.primitives.UnsignedBytes;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+
+import java.util.ArrayList;
 import java.util.List;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.ObjectParser;
 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
-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.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
-
 public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
 
-       private static final Logger logger = LoggerFactory.getLogger(AbstractXROWithSubobjectsParser.class);
-
-       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;
-
-       public static final int TYPE_FLAG_F_OFFSET = 0;
-       public static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
-       public static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
-
-       protected static final int PADDED_TO = 4;
-
-       private final XROSubobjectHandlerRegistry subobjReg;
-
-       protected AbstractXROWithSubobjectsParser(final XROSubobjectHandlerRegistry subobjReg) {
-               this.subobjReg = Preconditions.checkNotNull(subobjReg);
-       }
-
-       protected List<Subobjects> parseSubobjects(final byte[] bytes) throws PCEPDeserializerException {
-               if (bytes == null) {
-                       throw new IllegalArgumentException("Byte array is mandatory.");
-               }
-
-               int type;
-
-               byte[] soContentsBytes;
-               int length;
-               int offset = 0;
-
-               final List<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];
-
-                       if (length > bytes.length - offset) {
-                               throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
-                                               + (bytes.length - offset));
-                       }
-
-                       soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
-                       System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
-
-                       logger.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
-                       final Subobjects sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes, false);
-                       logger.debug("Subobject was parsed. {}", sub);
-
-                       subs.add(sub);
-
-                       offset += length;
-               }
-               return subs;
-       }
-
-       protected final byte[] serializeSubobject(final List<Subobjects> subobjects) {
-
-               final List<byte[]> result = Lists.newArrayList();
-
-               int finalLength = 0;
-
-               for (final Subobjects subobject : subobjects) {
-
-                       final XROSubobjectSerializer serializer = this.subobjReg.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];
-
-                       bytes[0] = typeBytes;
-                       bytes[1] = lengthBytes;
-                       System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
-
-                       finalLength += bytes.length;
-                       result.add(bytes);
-               }
-
-               final byte[] resultBytes = new byte[finalLength];
-               int byteOffset = 0;
-               for (final byte[] b : result) {
-                       System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
-                       byteOffset += b.length;
-               }
-               return resultBytes;
-       }
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractXROWithSubobjectsParser.class);
+
+    private static final int HEADER_LENGTH = 2;
+
+    private final XROSubobjectRegistry subobjReg;
+
+    protected AbstractXROWithSubobjectsParser(final XROSubobjectRegistry subobjReg) {
+        this.subobjReg = Preconditions.checkNotNull(subobjReg);
+    }
+
+    protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
+        Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+        final List<Subobject> subs = new ArrayList<>();
+        while (buffer.isReadable()) {
+            final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false;
+            int type = UnsignedBytes.checkedCast((buffer.readByte() & 0xff) & ~(1 << 7));
+            int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH;
+            if (length > buffer.readableBytes()) {
+                throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
+                        + buffer.readableBytes());
+            }
+            LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
+            final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), mandatory);
+            if (sub == null) {
+                LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
+            } else {
+                LOG.debug("Subobject was parsed. {}", sub);
+                subs.add(sub);
+            }
+            buffer.readerIndex(buffer.readerIndex() + length);
+        }
+        return subs;
+    }
+
+    protected final byte[] serializeSubobject(final List<Subobject> subobjects) {
+        final List<byte[]> result = Lists.newArrayList();
+        int finalLength = 0;
+        for (final Subobject subobject : subobjects) {
+            final byte[] bytes = this.subobjReg.serializeSubobject(subobject);
+            if (bytes == null) {
+                LOG.warn("Could not find serializer for subobject type: {}. Skipping subobject.", subobject.getSubobjectType());
+            } else {
+                finalLength += bytes.length;
+                result.add(bytes);
+            }
+        }
+        final byte[] resultBytes = new byte[finalLength];
+        int byteOffset = 0;
+        for (final byte[] b : result) {
+            System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
+            byteOffset += b.length;
+        }
+        return resultBytes;
+    }
 }