*/
package org.opendaylight.protocol.pcep.spi;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.BitSet;
import java.util.Collections;
import java.util.List;
-import javax.annotation.Nullable;
+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.rev131007.PcerrBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
-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.pcep.error.object.ErrorObjectBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
-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.pcerr.message.pcerr.message.error.type.RequestCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.request._case.RequestBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.request._case.request.RpsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObject;
+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 static final int COMMON_OBJECT_HEADER_LENGTH = 4;
-
private static final int OT_SF_LENGTH = 4;
- private static final int FLAGS_SF_LENGTH = 4;
+
/*
* offsets of fields inside of multi-field in bits
*/
private static final int OT_SF_OFFSET = 0;
- private static final int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
/*
* flags offsets inside multi-filed
*/
- private static final int P_FLAG_OFFSET = 6;
- private static final int I_FLAG_OFFSET = 7;
+ private static final int PROCESSED = 6;
+ private static final int IGNORED = 7;
private final ObjectRegistry registry;
- private final VendorInformationObjectRegistry viRegistry;
protected AbstractMessageParser(final ObjectRegistry registry) {
- this.registry = Preconditions.checkNotNull(registry);
- this.viRegistry = null;
- }
-
- protected AbstractMessageParser(final ObjectRegistry registry, final VendorInformationObjectRegistry viRegistry) {
- this.registry = Preconditions.checkNotNull(registry);
- this.viRegistry = Preconditions.checkNotNull(viRegistry);
+ this.registry = requireNonNull(registry);
}
/**
* @param object Object to be serialized, may be null
* @param buffer ByteBuf where the object should be serialized
*/
- protected void serializeObject(@Nullable final Object object, final ByteBuf buffer) {
- if (object == null) {
- return;
+ protected void serializeObject(final @Nullable Object object, final ByteBuf buffer) {
+ if (object != null) {
+ this.registry.serializeObject(object, buffer);
}
- 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 + ".");
+ 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 byte[] flagsBytes = { ByteArray.copyBitsRange(flagsByte, FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
- final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
-
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 + ".");
+ throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + bytes.readableBytes()
+ + " Expected: >= " + objLength + ".");
}
// copy bytes for deeper parsing
- final ByteBuf bytesToPass = bytes.slice(bytes.readerIndex(), objLength - COMMON_OBJECT_HEADER_LENGTH);
+ final ByteBuf bytesToPass = bytes.readSlice(objLength - COMMON_OBJECT_HEADER_LENGTH);
- final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
+ final ObjectHeader header = new ObjectHeaderImpl(flags.get(PROCESSED), flags.get(IGNORED));
if (VendorInformationUtil.isVendorInformationObject(objClass, objType)) {
- Preconditions.checkState(this.viRegistry != null);
- final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(bytes.getUnsignedInt(bytes.readerIndex()));
- final Optional<? extends Object> obj = this.viRegistry.parseVendorInformationObject(enterpriseNumber, header, bytesToPass);
+ 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());
}
objs.add(o);
}
}
-
- bytes.readerIndex(bytes.readerIndex() + objLength - COMMON_OBJECT_HEADER_LENGTH);
}
return objs;
}
- public static Message createErrorMsg(final PCEPErrors e, final Optional<Rp> rp) {
+ public static Message createErrorMsg(final PCEPErrors err, final Optional<Rp> rp) {
final PcerrMessageBuilder msgBuilder = new PcerrMessageBuilder();
if (rp.isPresent()) {
- new RequestCaseBuilder().setRequest(new RequestBuilder().setRps(Collections.singletonList(new RpsBuilder().setRp(
- rp.get()).build())).build()).build();
+ msgBuilder.setErrorType(new RequestCaseBuilder().setRequest(new RequestBuilder().setRps(
+ Collections.singletonList(new RpsBuilder().setRp(rp.get()).build())).build()).build());
}
return new PcerrBuilder().setPcerrMessage(
- msgBuilder.setErrors(Arrays.asList(new ErrorsBuilder().setErrorObject(
- new ErrorObjectBuilder().setType(e.getErrorType()).setValue(
- e.getErrorValue()).build()).build())).build()).build();
+ msgBuilder.setErrors(Collections.singletonList(new ErrorsBuilder().setErrorObject(
+ new ErrorObjectBuilder().setType(err.getErrorType()).setValue(
+ err.getErrorValue()).build()).build())).build()).build();
}
- protected abstract Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException;
+ 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 {
- Preconditions.checkNotNull(buffer, "Buffer may not be null");
+ 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);
return validate(objs, errors);
}
- protected final void serializeVendorInformationObjects(final List<VendorInformationObject> viObjects, final ByteBuf buffer) {
- if (viObjects != null && !viObjects.isEmpty()) {
+ protected final void serializeVendorInformationObjects(final List<VendorInformationObject> viObjects,
+ final ByteBuf buffer) {
+ if (viObjects != null) {
for (final VendorInformationObject viObject : viObjects) {
- this.viRegistry.serializeVendorInformationObject(viObject, buffer);
+ this.registry.serializeVendorInformationObject(viObject, buffer);
}
}
}
- protected final List<VendorInformationObject> addVendorInformationObjects(final List<Object> objects) {
+ 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);