Switch statements should end with a default case.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPErrorObjectParser.java
index 05e9b4f0a9ed582d655645220506788d1901bfa2..62497fc151dfafae234e35dacc0644281498aa2d 100644 (file)
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPDocumentedException;
-import org.opendaylight.protocol.pcep.impl.Util;
-import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
-import org.opendaylight.protocol.util.ByteArray;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
+
+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;
+import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
+import org.opendaylight.protocol.pcep.spi.ObjectUtil;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.PCEPErrors;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
+import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
 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.PcepErrorObject;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ReqMissingTlv;
 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.pcep.error.object.Tlvs;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.TlvsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.tlvs.ReqMissingBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.Tlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.TlvsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
 
 /**
- * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPErrorObject PCEPErrorObject}
+ * Parser for {@link ErrorObject}
  */
-public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorsBuilder> {
-
-       public static final int CLASS = 13;
-
-       public static final int TYPE = 1;
-
-       public static final int FLAGS_F_LENGTH = 1;
-       public static final int ET_F_LENGTH = 1;
-       public static final int EV_F_LENGTH = 1;
-
-       public static final int FLAGS_F_OFFSET = 1; // added reserved field of size 1 byte
-       public static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-       public static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
-       public static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
-
-       public PCEPErrorObjectParser(final TlvHandlerRegistry tlvReg) {
-               super(tlvReg);
-       }
-
-       @Override
-       public PcepErrorObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
-       PCEPDocumentedException {
-               if (bytes == null) {
-                       throw new IllegalArgumentException("Array of bytes is mandatory.");
-               }
-
-               final ErrorsBuilder builder = new ErrorsBuilder();
-
-               parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
-
-               builder.setIgnore(header.isIgnore());
-               builder.setProcessingRule(header.isProcessingRule());
-
-               builder.setType((short) (bytes[ET_F_OFFSET] & 0xFF));
-               builder.setValue((short) (bytes[EV_F_OFFSET] & 0xFF));
-
-               return builder.build();
-       }
-
-       @Override
-       public void addTlv(final ErrorsBuilder builder, final Tlv tlv) {
-               if (tlv instanceof ReqMissingTlv && builder.getType() == 7) {
-                       builder.setTlvs(new TlvsBuilder().setReqMissing(
-                                       new ReqMissingBuilder().setRequestId(((ReqMissingTlv) tlv).getRequestId()).build()).build());
-               }
-       }
-
-       @Override
-       public byte[] serializeObject(final Object object) {
-               if (!(object instanceof PcepErrorObject)) {
-                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
-               }
-
-               final PcepErrorObject errObj = (PcepErrorObject) object;
-
-               final byte[] tlvs = serializeTlvs(((Errors) errObj).getTlvs());
-               int tlvsLength = 0;
-               if (tlvs != null) {
-                       tlvsLength = tlvs.length;
-               }
-               final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
-               if (tlvs != null) {
-                       ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-               }
-
-               retBytes[ET_F_OFFSET] = ByteArray.shortToBytes(errObj.getType())[1];
-               retBytes[EV_F_OFFSET] = ByteArray.shortToBytes(errObj.getValue())[1];
-
-               return retBytes;
-       }
-
-       public byte[] serializeTlvs(final Tlvs tlvs) {
-               if (tlvs.getReqMissing() != null) {
-                       return serializeTlv(new ReqMissingBuilder().setRequestId(tlvs.getReqMissing().getRequestId()).build());
-               }
-               return null;
-       }
-
-       @Override
-       public int getObjectType() {
-               return TYPE;
-       }
-
-       @Override
-       public int getObjectClass() {
-               return CLASS;
-       }
+public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObjectBuilder> {
+
+    public static final int CLASS = 13;
+
+    public static final int TYPE = 1;
+
+    private static final int FLAGS_F_LENGTH = 1;
+
+    private static final int RESERVED  = 1;
+
+    public PCEPErrorObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
+        super(tlvReg, viTlvReg);
+    }
+
+    @Override
+    public ErrorObject 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.");
+        final ErrorObjectBuilder builder = new ErrorObjectBuilder();
+        builder.setIgnore(header.isIgnore());
+        builder.setProcessingRule(header.isProcessingRule());
+        bytes.readerIndex(bytes.readerIndex() + FLAGS_F_LENGTH + RESERVED);
+        builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
+        builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
+        parseTlvs(builder, bytes.slice());
+        return builder.build();
+    }
+
+    @Override
+    public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
+        if (tlv instanceof ReqMissing && builder.getType() == PCEPErrors.SYNC_PATH_COMP_REQ_MISSING.getErrorType()) {
+            builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
+        }
+    }
+
+    @Override
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof ErrorObject, "Wrong instance of PCEPObject. Passed %s. Needed ErrorObject.", object.getClass());
+        final ErrorObject errObj = (ErrorObject) object;
+        final ByteBuf body = Unpooled.buffer();
+        body.writeZero(FLAGS_F_LENGTH + RESERVED);
+        Preconditions.checkArgument(errObj.getType() != null, "Type is mandatory.");
+        writeUnsignedByte(errObj.getType(), body);
+        Preconditions.checkArgument(errObj.getValue() != null, "Value is mandatory.");
+        writeUnsignedByte(errObj.getValue(), body);
+        serializeTlvs(errObj.getTlvs(), body);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
+    }
+
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
+        if (tlvs == null) {
+            return;
+        }
+        if (tlvs.getReqMissing() != null) {
+            serializeTlv(tlvs.getReqMissing(), body);
+        }
+        serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
+    }
+
+    @Override
+    protected final void addVendorInformationTlvs(final ErrorObjectBuilder builder, final List<VendorInformationTlv> tlvs) {
+        if (!tlvs.isEmpty()) {
+            builder.setTlvs(new TlvsBuilder(builder.getTlvs()).setVendorInformationTlv(tlvs).build());
+        }
+    }
 }