Migrate to use yangtools' ByteBufUtils
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractMessageParser.java
index bb431c72498ecc79be859df24f3357c2c11409ee..28252d038c6f71b717d4501f2c3de6f1be2217f0 100644 (file)
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.spi;
 
-import java.util.Arrays;
-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.PCEPErrors;
-import org.opendaylight.protocol.pcep.UnknownObject;
-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 static java.util.Objects.requireNonNull;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
 import com.google.common.primitives.UnsignedBytes;
+import io.netty.buffer.ByteBuf;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.protocol.util.BitArray;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.PcerrBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObjectBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.PcerrMessageBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.ErrorsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.error.type.RequestCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.error.type.request._case.RequestBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.error.type.request._case.request.RpsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.rp.object.Rp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
+import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 
 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
-
-       private final static int COMMON_OBJECT_HEADER_LENGTH = 4;
-
-       private final static int OC_F_LENGTH = 1;
-       private final static int OT_FLAGS_MF_LENGTH = 1; // multi-field
-       private final static int OBJ_LENGTH_F_LENGTH = 2;
-
-       private final static int OC_F_OFFSET = 0;
-       private final static int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
-       private final static int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
-
-       private final static int OT_SF_LENGTH = 4;
-       private final static int FLAGS_SF_LENGTH = 4;
-
-       /*
-        * offsets of fields inside of multi-field in bits
-        */
-       private final static int OT_SF_OFFSET = 0;
-       private final static int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
-
-       /*
-        * flags offsets inside multi-filed
-        */
-       private final static int P_FLAG_OFFSET = 6;
-       private final static int I_FLAG_OFFSET = 7;
-
-       private final ObjectHandlerRegistry registry;
-
-       protected AbstractMessageParser(final ObjectHandlerRegistry registry) {
-               this.registry = Preconditions.checkNotNull(registry);
-       }
-
-       protected byte[] serializeObject(final Object object) {
-               if (object == null) {
-                       throw new IllegalArgumentException("Null object passed.");
-               }
-
-               final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
-
-               final byte[] valueBytes = serializer.serializeObject(object);
-
-               final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
-
-               // objClass
-               retBytes[OC_F_OFFSET] = (byte) serializer.getObjectClass();
-
-               // objType_flags multi-field
-               retBytes[OT_FLAGS_MF_OFFSET] = (byte) (serializer.getObjectType() << (Byte.SIZE - OT_SF_LENGTH));
-               if (object.isProcessingRule()) {
-                       retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
-               }
-               if (object.isIgnore()) {
-                       retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
-               }
-
-               // objLength
-               System.arraycopy(ByteArray.intToBytes(valueBytes.length), Integer.SIZE / Byte.SIZE - OBJ_LENGTH_F_LENGTH, retBytes,
-                               OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
-
-               System.arraycopy(valueBytes, 0, retBytes, COMMON_OBJECT_HEADER_LENGTH, valueBytes.length);
-
-               return retBytes;
-       }
-
-       protected List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
-               int offset = 0;
-               final List<Object> objs = Lists.newArrayList();
-               while (bytes.length - offset > 0) {
-                       if (bytes.length - offset < COMMON_OBJECT_HEADER_LENGTH) {
-                               throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
-                                               + COMMON_OBJECT_HEADER_LENGTH + ".");
-                       }
-
-                       final int objClass = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OC_F_OFFSET, OC_F_OFFSET + OC_F_LENGTH));
-
-                       final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], OT_SF_OFFSET, OT_SF_LENGTH));
-
-                       final int objLength = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_OFFSET
-                                       + OBJ_LENGTH_F_LENGTH));
-
-                       final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
-
-                       final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
-
-                       if (bytes.length - offset < objLength) {
-                               throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
-                                               + objLength + ".");
-                       }
-
-                       // copy bytes for deeper parsing
-                       final byte[] bytesToPass = ByteArray.subByte(bytes, offset + COMMON_OBJECT_HEADER_LENGTH, objLength
-                                       - COMMON_OBJECT_HEADER_LENGTH);
-
-                       offset += objLength;
-
-                       final ObjectParser parser = this.registry.getObjectParser(objClass, objType);
-
-                       final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
-
-                       try {
-                               objs.add(parser.parseObject(header, bytesToPass));
-                       } catch (final PCEPDocumentedException e) {
-                               if (e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_CLASS | e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_TYPE
-                                               | e.getError() == PCEPErrors.NOT_SUPPORTED_OBJ_CLASS | e.getError() == PCEPErrors.NOT_SUPPORTED_OBJ_TYPE) {
-                                       objs.add(new UnknownObject(e.getError()));
-                               } else {
-                                       throw e;
-                               }
-                       }
-               }
-               return objs;
-       }
+    private static final int COMMON_OBJECT_HEADER_LENGTH = 4;
+    private static final int OT_SF_LENGTH = 4;
+
+    /*
+     * offsets of fields inside of multi-field in bits
+     */
+    private static final int OT_SF_OFFSET = 0;
+    /*
+     * flags offsets inside multi-filed
+     */
+    private static final int PROCESSED = 6;
+    private static final int IGNORED = 7;
+
+    private final ObjectRegistry registry;
+
+    protected AbstractMessageParser(final ObjectRegistry registry) {
+        this.registry = requireNonNull(registry);
+    }
+
+    /**
+     * Calls registry to pick up specific object serializer for given object.
+     * Checks if the object is not null.
+     * @param object Object to be serialized, may be null
+     * @param buffer ByteBuf where the object should be serialized
+     */
+    protected void serializeObject(final @Nullable Object object, final ByteBuf buffer) {
+        if (object != null) {
+            this.registry.serializeObject(object, buffer);
+        }
+    }
+
+    private List<Object> parseObjects(final ByteBuf bytes) throws PCEPDeserializerException {
+        final List<Object> objs = new ArrayList<>();
+        while (bytes.isReadable()) {
+            if (bytes.readableBytes() < COMMON_OBJECT_HEADER_LENGTH) {
+                throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + bytes.readableBytes()
+                    + " Expected: >= " + COMMON_OBJECT_HEADER_LENGTH + ".");
+            }
+            final int objClass = bytes.readUnsignedByte();
+
+            final byte flagsByte = bytes.readByte();
+            final BitArray flags = BitArray.valueOf(flagsByte);
+            final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(flagsByte, OT_SF_OFFSET, OT_SF_LENGTH));
+            final int objLength = bytes.readUnsignedShort();
+
+            if (bytes.readableBytes() < objLength - COMMON_OBJECT_HEADER_LENGTH) {
+                throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + bytes.readableBytes()
+                    + " Expected: >= " + objLength + ".");
+            }
+            // copy bytes for deeper parsing
+            final ByteBuf bytesToPass = bytes.readSlice(objLength - COMMON_OBJECT_HEADER_LENGTH);
+
+            final ObjectHeader header = new ObjectHeaderImpl(flags.get(PROCESSED), flags.get(IGNORED));
+
+            if (VendorInformationUtil.isVendorInformationObject(objClass, objType)) {
+                final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(ByteBufUtils.readUint32(bytesToPass));
+                final Optional<? extends Object> obj = this.registry.parseVendorInformationObject(enterpriseNumber,
+                    header, bytesToPass);
+                if (obj.isPresent()) {
+                    objs.add(obj.get());
+                }
+            } else {
+                // parseObject is required to return null for P=0 errored objects
+                final Object o = this.registry.parseObject(objClass, objType, header, bytesToPass);
+                if (o != null) {
+                    objs.add(o);
+                }
+            }
+        }
+
+        return objs;
+    }
+
+    public static Message createErrorMsg(final PCEPErrors err, final Optional<Rp> rp) {
+        final PcerrMessageBuilder msgBuilder = new PcerrMessageBuilder();
+        if (rp.isPresent()) {
+            msgBuilder.setErrorType(new RequestCaseBuilder().setRequest(new RequestBuilder().setRps(
+                Collections.singletonList(new RpsBuilder().setRp(rp.get()).build())).build()).build());
+        }
+        return new PcerrBuilder().setPcerrMessage(
+                msgBuilder.setErrors(Collections.singletonList(new ErrorsBuilder().setErrorObject(
+                    new ErrorObjectBuilder().setType(err.getErrorType()).setValue(
+                        err.getErrorValue()).build()).build())).build()).build();
+    }
+
+    protected abstract Message validate(List<Object> objects, List<Message> errors) throws PCEPDeserializerException;
+
+    @Override
+    public final Message parseMessage(final ByteBuf buffer, final List<Message> errors)
+            throws PCEPDeserializerException {
+        requireNonNull(buffer, "Buffer may not be null");
+
+        // Parse objects first
+        final List<Object> objs = parseObjects(buffer);
+
+        // Run validation
+        return validate(objs, errors);
+    }
+
+    protected final void serializeVendorInformationObjects(final List<VendorInformationObject> viObjects,
+            final ByteBuf buffer) {
+        if (viObjects != null) {
+            for (final VendorInformationObject viObject : viObjects) {
+                this.registry.serializeVendorInformationObject(viObject, buffer);
+            }
+        }
+    }
+
+    protected static List<VendorInformationObject> addVendorInformationObjects(final List<Object> objects) {
+        final List<VendorInformationObject> vendorInfo = new ArrayList<>();
+        while (!objects.isEmpty() && objects.get(0) instanceof VendorInformationObject) {
+            final VendorInformationObject viObject = (VendorInformationObject) objects.get(0);
+            vendorInfo.add(viObject);
+            objects.remove(0);
+        }
+        return vendorInfo;
+    }
 }