1 package org.opendaylight.protocol.pcep.spi;
3 import java.util.Arrays;
4 import java.util.BitSet;
7 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
8 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
9 import org.opendaylight.protocol.pcep.PCEPErrors;
10 import org.opendaylight.protocol.pcep.UnknownObject;
11 import org.opendaylight.protocol.util.ByteArray;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
15 import com.google.common.collect.Lists;
16 import com.google.common.primitives.UnsignedBytes;
18 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
20 private final static int COMMON_OBJECT_HEADER_LENGTH = 4;
22 private final static int OC_F_LENGTH = 1;
23 private final static int OT_FLAGS_MF_LENGTH = 1; // multi-field
24 private final static int OBJ_LENGTH_F_LENGTH = 2;
26 private final static int OC_F_OFFSET = 0;
27 private final static int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
28 private final static int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
30 private final static int OT_SF_LENGTH = 4;
31 private final static int FLAGS_SF_LENGTH = 4;
34 * offsets of fields inside of multi-field in bits
36 private final static int OT_SF_OFFSET = 0;
37 private final static int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
40 * flags offsets inside multi-filed
42 private final static int P_FLAG_OFFSET = 6;
43 private final static int I_FLAG_OFFSET = 7;
45 private final HandlerRegistry registry;
47 protected AbstractMessageParser(final HandlerRegistry registry) {
48 this.registry = registry;
51 protected byte[] serializeObject(final Object object) {
53 throw new IllegalArgumentException("Null object passed.");
55 final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
57 final byte[] valueBytes = serializer.serializeObject(object);
59 final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
62 retBytes[OC_F_OFFSET] = (byte) serializer.getObjectClass();
64 // objType_flags multi-field
65 retBytes[OT_FLAGS_MF_OFFSET] = (byte) (serializer.getObjectType() << (Byte.SIZE - OT_SF_LENGTH));
66 if (object.isProcessingRule())
67 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
68 if (object.isIgnore())
69 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
72 System.arraycopy(ByteArray.intToBytes(valueBytes.length), Integer.SIZE / Byte.SIZE - OBJ_LENGTH_F_LENGTH, retBytes,
73 OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
75 System.arraycopy(valueBytes, 0, retBytes, COMMON_OBJECT_HEADER_LENGTH, valueBytes.length);
80 protected List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
82 final List<Object> objs = Lists.newArrayList();
83 while (bytes.length - offset > 0) {
84 if (bytes.length - offset < COMMON_OBJECT_HEADER_LENGTH)
85 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
86 + COMMON_OBJECT_HEADER_LENGTH + ".");
88 final int objClass = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OC_F_OFFSET, OC_F_OFFSET + OC_F_LENGTH));
90 final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], OT_SF_OFFSET, OT_SF_LENGTH));
92 final int objLength = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_OFFSET
93 + OBJ_LENGTH_F_LENGTH));
95 final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
97 final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
99 if (bytes.length - offset < objLength)
100 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
103 // copy bytes for deeper parsing
104 final byte[] bytesToPass = ByteArray.subByte(bytes, offset + COMMON_OBJECT_HEADER_LENGTH, objLength
105 - COMMON_OBJECT_HEADER_LENGTH);
109 final ObjectParser parser = this.registry.getObjectParser(objClass, objType);
111 final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
114 objs.add(parser.parseObject(header, bytesToPass));
115 } catch (final PCEPDocumentedException e) {
116 if (e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_CLASS | e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_TYPE
117 | e.getError() == PCEPErrors.NOT_SUPPORTED_OBJ_CLASS | e.getError() == PCEPErrors.NOT_SUPPORTED_OBJ_TYPE) {
118 objs.add(new UnknownObject(e.getError()));