1 package org.opendaylight.protocol.pcep.impl.message;
3 import java.util.Arrays;
4 import java.util.BitSet;
7 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
8 import org.opendaylight.protocol.pcep.spi.MessageParser;
9 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
10 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
11 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
12 import org.opendaylight.protocol.pcep.spi.ObjectParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
14 import org.opendaylight.protocol.pcep.spi.PCEPErrorMapping;
15 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcerrMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.Lists;
28 import com.google.common.primitives.UnsignedBytes;
30 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
32 private static final int COMMON_OBJECT_HEADER_LENGTH = 4;
34 private static final int OC_F_LENGTH = 1;
35 private static final int OT_FLAGS_MF_LENGTH = 1;
36 private static final int OBJ_LENGTH_F_LENGTH = 2;
38 private static final int OC_F_OFFSET = 0;
39 private static final int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
40 private static final int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
42 private static final int OT_SF_LENGTH = 4;
43 private static final int FLAGS_SF_LENGTH = 4;
46 * offsets of fields inside of multi-field in bits
48 private static final int OT_SF_OFFSET = 0;
49 private static final int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
52 * flags offsets inside multi-filed
54 private static final int P_FLAG_OFFSET = 6;
55 private static final int I_FLAG_OFFSET = 7;
57 private final ObjectHandlerRegistry registry;
59 protected AbstractMessageParser(final ObjectHandlerRegistry registry) {
60 this.registry = Preconditions.checkNotNull(registry);
63 protected byte[] serializeObject(final Object object) {
65 throw new IllegalArgumentException("Null object passed.");
68 final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
70 final byte[] valueBytes = serializer.serializeObject(object);
72 final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
75 retBytes[OC_F_OFFSET] = UnsignedBytes.checkedCast(serializer.getObjectClass());
77 // objType_flags multi-field
78 retBytes[OT_FLAGS_MF_OFFSET] = UnsignedBytes.checkedCast(serializer.getObjectType() << (Byte.SIZE - OT_SF_LENGTH));
79 if (object.isProcessingRule() != null && object.isProcessingRule()) {
80 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
82 if (object.isIgnore() != null && object.isIgnore()) {
83 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
87 System.arraycopy(ByteArray.intToBytes(valueBytes.length + COMMON_OBJECT_HEADER_LENGTH), Integer.SIZE / Byte.SIZE
88 - OBJ_LENGTH_F_LENGTH, retBytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
90 ByteArray.copyWhole(valueBytes, retBytes, COMMON_OBJECT_HEADER_LENGTH);
94 private final List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException {
96 final List<Object> objs = Lists.newArrayList();
97 while (bytes.length - offset > 0) {
98 if (bytes.length - offset < COMMON_OBJECT_HEADER_LENGTH) {
99 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
100 + COMMON_OBJECT_HEADER_LENGTH + ".");
103 final int objClass = UnsignedBytes.toInt(bytes[offset]);
105 offset += OC_F_LENGTH;
107 final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[offset], OT_SF_OFFSET, OT_SF_LENGTH));
109 final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[offset], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
111 final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
113 offset += OT_FLAGS_MF_LENGTH;
115 final int objLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset, OBJ_LENGTH_F_LENGTH));
117 if (bytes.length - offset < objLength - COMMON_OBJECT_HEADER_LENGTH) {
118 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
122 offset += OBJ_LENGTH_F_LENGTH;
124 // copy bytes for deeper parsing
125 final byte[] bytesToPass = ByteArray.subByte(bytes, offset, objLength - COMMON_OBJECT_HEADER_LENGTH);
127 offset += objLength - COMMON_OBJECT_HEADER_LENGTH;
129 final ObjectParser parser = Preconditions.checkNotNull(this.registry.getObjectParser(objClass, objType));
130 final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
132 // parseObject is required to return null for P=0 errored objects
133 final Object o = parser.parseObject(header, bytesToPass);
142 public static PcerrMessage createErrorMsg(final PCEPErrors e) {
143 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
144 return new PcerrBuilder().setPcerrMessage(
145 new PcerrMessageBuilder().setErrors(
146 Arrays.asList(new ErrorsBuilder().setErrorObject(
147 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).type).setValue(
148 maping.getFromErrorsEnum(e).value).build()).build())).build()).build();
151 abstract protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException;
154 public final Message parseMessage(final byte[] buffer, final List<Message> errors) throws PCEPDeserializerException {
155 Preconditions.checkNotNull(buffer, "Buffer may not be null");
157 // Parse objects first
158 final List<Object> objs = parseObjects(buffer);
161 return validate(objs, errors);