Sonar issues fix
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROSRLGSubobjectParser.java
index 306ff013684295b0a82b694949b1ea7766252848..9cbd321e03f9d6ffb4a92c3f3c4b1f48bcd32516 100644 (file)
@@ -7,67 +7,57 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 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.exclude.route.object.Subobjects;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.SubobjectsBuilder;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgSubobject;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
 
 /**
- * Parser for {@link SrlgSubobject}
+ * Parser for {@link SrlgCase}
  */
 public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
 
-       public static final int TYPE = 34;
-
-       public static final int SRLG_ID_NUMBER_LENGTH = 4;
-       public static final int ATTRIBUTE_LENGTH = 1;
-
-       public static final int SRLG_ID_NUMBER_OFFSET = 0;
-       public static final int ATTRIBUTE_OFFSET = SRLG_ID_NUMBER_OFFSET + SRLG_ID_NUMBER_LENGTH + 1; // added reserved
-                                                                                                                                                                                                       // field of size 1
-
-       public static final int CONTENT_LENGTH = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
-
-       @Override
-       public Subobjects parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
-               if (buffer == null || buffer.length == 0)
-                       throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
-               if (buffer.length != CONTENT_LENGTH)
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
-                                       + CONTENT_LENGTH + ".");
-
-               final SubobjectsBuilder builder = new SubobjectsBuilder();
-               builder.setMandatory(mandatory);
-               builder.setAttribute(Attribute.Srlg);
-               builder.setSubobjectType(new SrlgBuilder().setSrlgId(
-                               new SrlgId(ByteArray.bytesToLong(ByteArray.subByte(buffer, SRLG_ID_NUMBER_OFFSET, SRLG_ID_NUMBER_LENGTH)))).build());
-               return builder.build();
-       }
-
-       @Override
-       public byte[] serializeSubobject(final Subobjects subobject) {
-               if (!(subobject.getSubobjectType() instanceof SrlgSubobject))
-                       throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
-                                       + ". Needed SrlgSubobject.");
-
-               byte[] retBytes;
-               retBytes = new byte[CONTENT_LENGTH];
-               final SrlgSubobject specObj = (SrlgSubobject) subobject.getSubobjectType();
-
-               ByteArray.copyWhole(ByteArray.longToBytes(specObj.getSrlgId().getValue()), retBytes, SRLG_ID_NUMBER_OFFSET);
-               retBytes[ATTRIBUTE_OFFSET] = (byte) subobject.getAttribute().getIntValue();
-
-               return retBytes;
-       }
-
-       @Override
-       public int getType() {
-               return TYPE;
-       }
+    public static final int TYPE = 34;
+
+    private static final int CONTENT_LENGTH = 5;
+
+    @Override
+    public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
+        Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+        if (buffer.readableBytes() != CONTENT_LENGTH) {
+            throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
+                    + CONTENT_LENGTH + ".");
+        }
+        final SubobjectBuilder builder = new SubobjectBuilder();
+        builder.setMandatory(mandatory);
+        builder.setAttribute(Attribute.Srlg);
+        builder.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(buffer.readUnsignedInt())).build()).build());
+        return builder.build();
+    }
+
+    @Override
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrlgCase, "Unknown subobject instance. Passed %s. Needed SrlgCase.", subobject.getSubobjectType().getClass());
+        final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
+        final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+        Preconditions.checkArgument(specObj.getSrlgId() != null, "SrlgId is mandatory.");
+        writeUnsignedInt(specObj.getSrlgId().getValue(), body);
+        Preconditions.checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
+        writeUnsignedByte((short) subobject.getAttribute().getIntValue(), body);
+        XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
+    }
 }