2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.pcep.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
14 import java.util.ArrayDeque;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.Queue;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
21 import org.opendaylight.protocol.util.BitArray;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.PcerrBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObjectBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.PcerrMessageBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.ErrorsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.error.type.RequestCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.error.type.request._case.RequestBuilder;
33 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;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.rp.object.Rp;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
36 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
38 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
39 private static final int COMMON_OBJECT_HEADER_LENGTH = 4;
40 private static final int OT_SF_LENGTH = 4;
43 * offsets of fields inside of multi-field in bits
45 private static final int OT_SF_OFFSET = 0;
47 * flags offsets inside multi-filed
49 private static final int PROCESSED = 6;
50 private static final int IGNORED = 7;
52 private final ObjectRegistry registry;
54 protected AbstractMessageParser(final ObjectRegistry registry) {
55 this.registry = requireNonNull(registry);
59 * Calls registry to pick up specific object serializer for given object.
60 * Checks if the object is not null.
61 * @param object Object to be serialized, may be null
62 * @param buffer ByteBuf where the object should be serialized
64 protected void serializeObject(final @Nullable Object object, final ByteBuf buffer) {
66 this.registry.serializeObject(object, buffer);
70 private Queue<Object> parseObjects(final ByteBuf bytes) throws PCEPDeserializerException {
71 final Queue<Object> objs = new ArrayDeque<>();
72 while (bytes.isReadable()) {
73 if (bytes.readableBytes() < COMMON_OBJECT_HEADER_LENGTH) {
74 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + bytes.readableBytes()
75 + " Expected: >= " + COMMON_OBJECT_HEADER_LENGTH + ".");
77 final int objClass = bytes.readUnsignedByte();
79 final byte flagsByte = bytes.readByte();
80 final BitArray flags = BitArray.valueOf(flagsByte);
81 final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(flagsByte, OT_SF_OFFSET, OT_SF_LENGTH));
82 final int objLength = bytes.readUnsignedShort();
84 if (bytes.readableBytes() < objLength - COMMON_OBJECT_HEADER_LENGTH) {
85 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + bytes.readableBytes()
86 + " Expected: >= " + objLength + ".");
88 // copy bytes for deeper parsing
89 final ByteBuf bytesToPass = bytes.readSlice(objLength - COMMON_OBJECT_HEADER_LENGTH);
91 final ObjectHeader header = new ObjectHeaderImpl(flags.get(PROCESSED), flags.get(IGNORED));
93 if (VendorInformationUtil.isVendorInformationObject(objClass, objType)) {
94 final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(ByteBufUtils.readUint32(bytesToPass));
95 this.registry.parseVendorInformationObject(enterpriseNumber, header, bytesToPass).ifPresent(objs::add);
97 // parseObject is required to return null for P=0 errored objects
98 final Object o = this.registry.parseObject(objClass, objType, header, bytesToPass);
108 public static Message createErrorMsg(final PCEPErrors err, final Optional<Rp> optRp) {
109 final PcerrMessageBuilder msgBuilder = new PcerrMessageBuilder();
110 optRp.ifPresent(rp -> {
111 msgBuilder.setErrorType(new RequestCaseBuilder()
112 .setRequest(new RequestBuilder().setRps(List.of(new RpsBuilder().setRp(rp).build())).build())
115 return new PcerrBuilder()
116 .setPcerrMessage(msgBuilder.setErrors(List.of(new ErrorsBuilder()
117 .setErrorObject(new ErrorObjectBuilder()
118 .setType(err.getErrorType())
119 .setValue(err.getErrorValue())
126 protected abstract Message validate(Queue<Object> objects, List<Message> errors)
127 throws PCEPDeserializerException;
130 public final Message parseMessage(final ByteBuf buffer, final List<Message> errors)
131 throws PCEPDeserializerException {
132 // Parse objects first
133 final Queue<Object> objs = parseObjects(requireNonNull(buffer, "Buffer may not be null"));
136 return validate(objs, errors);
139 protected final void serializeVendorInformationObjects(final List<VendorInformationObject> viObjects,
140 final ByteBuf buffer) {
141 if (viObjects != null) {
142 for (final VendorInformationObject viObject : viObjects) {
143 this.registry.serializeVendorInformationObject(viObject, buffer);
148 protected static List<VendorInformationObject> addVendorInformationObjects(final Queue<Object> objects) {
149 final List<VendorInformationObject> vendorInfo = new ArrayList<>();
150 for (Object obj = objects.peek(); obj instanceof VendorInformationObject; obj = objects.peek()) {
151 vendorInfo.add((VendorInformationObject) obj);