Migrate to use yangtools' ByteBufUtils
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPRequestParameterObjectParser.java
index 14b59ab184fdf45b354164fee7691356dc178e6a..a17688d5cf9202d665575cd890e82ae0469a7709 100644 (file)
@@ -5,13 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.pcep.parser.object;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
 
-import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import java.util.List;
@@ -32,12 +30,14 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.rp.object.rp.Tlvs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.rp.object.rp.TlvsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint8;
+import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 
 /**
- * Parser for {@link Rp}
+ * Parser for {@link Rp}.
  */
 public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
-
     private static final int CLASS = 2;
     private static final int TYPE = 1;
 
@@ -54,14 +54,12 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
     /*
      * offsets of subfields inside multi-field in bits
      */
-
     private static final int FLAGS_SF_OFFSET = 0;
     private static final int PRI_SF_OFFSET = FLAGS_SF_OFFSET + FLAGS_SF_LENGTH;
 
     /*
      * flags offsets inside flags sub-field in bits
      */
-
     private static final int O_FLAG_OFFSET = 26;
     private static final int B_FLAG_OFFSET = 27;
     private static final int R_FLAG_OFFSET = 28;
@@ -84,9 +82,7 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
      * RFC6006 flags
      */
     private static final int F_FLAG_OFFSET = 18;
-
     private static final int N_FLAG_OFFSET = 19;
-
     private static final int E_FLAG_OFFSET = 20;
 
     public PCEPRequestParameterObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
@@ -95,35 +91,35 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
 
     @Override
     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
-        Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+        checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
         final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE);
-
-        final RpBuilder builder = new RpBuilder();
-        builder.setIgnore(header.isIgnore());
-        builder.setProcessingRule(header.isProcessingRule());
+        final Uint32 reqId = ByteBufUtils.readUint32(bytes);
+        final TlvsBuilder tlvsBuilder = new TlvsBuilder();
+        parseTlvs(tlvsBuilder, bytes.slice());
+        final RpBuilder builder = new RpBuilder()
+                .setIgnore(header.isIgnore())
+                .setProcessingRule(header.isProcessingRule())
+                .setFragmentation(flags.get(F_FLAG_OFFSET))
+                .setP2mp(flags.get(N_FLAG_OFFSET))
+                .setEroCompression(flags.get(E_FLAG_OFFSET))
+                .setMakeBeforeBreak(flags.get(M_FLAG_OFFSET))
+                .setOrder(flags.get(D_FLAG_OFFSET))
+                .setPathKey(flags.get(P_FLAG_OFFSET))
+                .setSupplyOf(flags.get(S_FLAG_OFFSET))
+                .setLoose(flags.get(O_FLAG_OFFSET))
+                .setBiDirectional(flags.get(B_FLAG_OFFSET))
+                .setReoptimization(flags.get(R_FLAG_OFFSET))
+                .setRequestId(new RequestId(reqId))
+                .setTlvs(tlvsBuilder.build());
 
         short priority = 0;
         priority |= flags.get(PRI_SF_OFFSET + 2) ? 1 : 0;
         priority |= (flags.get(PRI_SF_OFFSET + 1) ? 1 : 0) << 1;
         priority |= (flags.get(PRI_SF_OFFSET) ? 1 : 0) << 2;
         if (priority != 0) {
-            builder.setPriority(priority);
+            builder.setPriority(Uint8.valueOf(priority));
         }
-        builder.setFragmentation(flags.get(F_FLAG_OFFSET));
-        builder.setP2mp(flags.get(N_FLAG_OFFSET));
-        builder.setEroCompression(flags.get(E_FLAG_OFFSET));
-        builder.setMakeBeforeBreak(flags.get(M_FLAG_OFFSET));
-        builder.setOrder(flags.get(D_FLAG_OFFSET));
-        builder.setPathKey(flags.get(P_FLAG_OFFSET));
-        builder.setSupplyOf(flags.get(S_FLAG_OFFSET));
-        builder.setLoose(flags.get(O_FLAG_OFFSET));
-        builder.setBiDirectional(flags.get(B_FLAG_OFFSET));
-        builder.setReoptimization(flags.get(R_FLAG_OFFSET));
-
-        builder.setRequestId(new RequestId(bytes.readUnsignedInt()));
-        final TlvsBuilder tlvsBuilder = new TlvsBuilder();
-        parseTlvs(tlvsBuilder, bytes.slice());
-        builder.setTlvs(tlvsBuilder.build());
+
         return builder.build();
     }
 
@@ -139,7 +135,8 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
 
     @Override
     public void serializeObject(final Object object, final ByteBuf buffer) {
-        Preconditions.checkArgument(object instanceof Rp, "Wrong instance of PCEPObject. Passed %s. Needed RPObject.", object.getClass());
+        checkArgument(object instanceof Rp, "Wrong instance of PCEPObject. Passed %s. Needed RPObject.",
+            object.getClass());
         final ByteBuf body = Unpooled.buffer();
         final Rp rpObj = (Rp) object;
         final BitArray flags = new BitArray(FLAGS_SIZE);
@@ -155,11 +152,11 @@ public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsPars
         flags.set(E_FLAG_OFFSET, rpObj.isEroCompression());
         final byte[] res = flags.array();
         if (rpObj.getPriority() != null) {
-            final byte p = UnsignedBytes.checkedCast(rpObj.getPriority());
-            res[res.length -1] = (byte) (res[res.length -1] | p);
+            final byte p = rpObj.getPriority().byteValue();
+            res[res.length - 1] = (byte) (res[res.length - 1] | p);
         }
         body.writeBytes(res);
-        Preconditions.checkArgument(rpObj.getRequestId() != null, "RequestId is mandatory");
+        checkArgument(rpObj.getRequestId() != null, "RequestId is mandatory");
         writeUnsignedInt(rpObj.getRequestId().getValue(), body);
         serializeTlvs(rpObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);