1 package org.opendaylight.protocol.pcep.impl.message;
3 import java.util.Arrays;
4 import java.util.BitSet;
7 import org.opendaylight.protocol.pcep.spi.MessageParser;
8 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
9 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
10 import org.opendaylight.protocol.pcep.spi.ObjectParser;
11 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
12 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.PCEPErrorMapping;
14 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.RequestBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.request.RpsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Lists;
29 import com.google.common.primitives.UnsignedBytes;
31 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
33 private static final int COMMON_OBJECT_HEADER_LENGTH = 4;
35 private static final int OC_F_LENGTH = 1;
36 private static final int OT_FLAGS_MF_LENGTH = 1;
37 private static final int OBJ_LENGTH_F_LENGTH = 2;
39 private static final int OC_F_OFFSET = 0;
40 private static final int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
41 private static final int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
43 private static final int OT_SF_LENGTH = 4;
44 private static final int FLAGS_SF_LENGTH = 4;
47 * offsets of fields inside of multi-field in bits
49 private static final int OT_SF_OFFSET = 0;
50 private static final int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
53 * flags offsets inside multi-filed
55 private static final int P_FLAG_OFFSET = 6;
56 private static final int I_FLAG_OFFSET = 7;
58 private final ObjectHandlerRegistry registry;
60 protected AbstractMessageParser(final ObjectHandlerRegistry registry) {
61 this.registry = Preconditions.checkNotNull(registry);
64 protected byte[] serializeObject(final Object object) {
66 throw new IllegalArgumentException("Null object passed.");
69 final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
71 final byte[] valueBytes = serializer.serializeObject(object);
73 final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
76 retBytes[OC_F_OFFSET] = UnsignedBytes.checkedCast(serializer.getObjectClass());
78 // objType_flags multi-field
79 retBytes[OT_FLAGS_MF_OFFSET] = UnsignedBytes.checkedCast(serializer.getObjectType() << (Byte.SIZE - OT_SF_LENGTH));
80 if (object.isProcessingRule() != null && object.isProcessingRule()) {
81 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
83 if (object.isIgnore() != null && object.isIgnore()) {
84 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
88 System.arraycopy(ByteArray.intToBytes(valueBytes.length + COMMON_OBJECT_HEADER_LENGTH, OBJ_LENGTH_F_LENGTH), 0, retBytes,
89 OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
91 ByteArray.copyWhole(valueBytes, retBytes, COMMON_OBJECT_HEADER_LENGTH);
95 private final List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException {
97 final List<Object> objs = Lists.newArrayList();
98 while (bytes.length - offset > 0) {
99 if (bytes.length - offset < COMMON_OBJECT_HEADER_LENGTH) {
100 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
101 + COMMON_OBJECT_HEADER_LENGTH + ".");
104 final int objClass = UnsignedBytes.toInt(bytes[offset]);
106 offset += OC_F_LENGTH;
108 final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[offset], OT_SF_OFFSET, OT_SF_LENGTH));
110 final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[offset], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
112 final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
114 offset += OT_FLAGS_MF_LENGTH;
116 final int objLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset, OBJ_LENGTH_F_LENGTH));
118 if (bytes.length - offset < objLength - COMMON_OBJECT_HEADER_LENGTH) {
119 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
123 offset += OBJ_LENGTH_F_LENGTH;
125 // copy bytes for deeper parsing
126 final byte[] bytesToPass = ByteArray.subByte(bytes, offset, objLength - COMMON_OBJECT_HEADER_LENGTH);
128 offset += objLength - COMMON_OBJECT_HEADER_LENGTH;
130 final ObjectParser parser = Preconditions.checkNotNull(this.registry.getObjectParser(objClass, objType));
131 final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
133 // parseObject is required to return null for P=0 errored objects
134 final Object o = parser.parseObject(header, bytesToPass);
143 public static Message createErrorMsg(final PCEPErrors e) {
144 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
145 return new PcerrBuilder().setPcerrMessage(
146 new PcerrMessageBuilder().setErrors(
147 Arrays.asList(new ErrorsBuilder().setErrorObject(
148 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).type).setValue(
149 maping.getFromErrorsEnum(e).value).build()).build())).build()).build();
152 public static Message createErrorMsg(final PCEPErrors e, final Rp rp) {
153 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
154 return new PcerrBuilder().setPcerrMessage(
155 new PcerrMessageBuilder().setErrorType(
156 new RequestBuilder().setRps(Lists.newArrayList(new RpsBuilder().setRp(rp).build())).build()).setErrors(
157 Arrays.asList(new ErrorsBuilder().setErrorObject(
158 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).type).setValue(
159 maping.getFromErrorsEnum(e).value).build()).build())).build()).build();
162 abstract protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException;
165 public final Message parseMessage(final byte[] buffer, final List<Message> errors) throws PCEPDeserializerException {
166 Preconditions.checkNotNull(buffer, "Buffer may not be null");
168 // Parse objects first
169 final List<Object> objs = parseObjects(buffer);
172 return validate(objs, errors);