1 package org.opendaylight.protocol.pcep.impl;
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.pcep.spi.MessageParser;
12 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
13 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
15 import org.opendaylight.protocol.pcep.spi.ObjectParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
17 import org.opendaylight.protocol.util.ByteArray;
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;
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Lists;
23 import com.google.common.primitives.UnsignedBytes;
25 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
27 private final static int COMMON_OBJECT_HEADER_LENGTH = 4;
29 private final static int OC_F_LENGTH = 1;
30 private final static int OT_FLAGS_MF_LENGTH = 1; // multi-field
31 private final static int OBJ_LENGTH_F_LENGTH = 2;
33 private final static int OC_F_OFFSET = 0;
34 private final static int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
35 private final static int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
37 private final static int OT_SF_LENGTH = 4;
38 private final static int FLAGS_SF_LENGTH = 4;
41 * offsets of fields inside of multi-field in bits
43 private final static int OT_SF_OFFSET = 0;
44 private final static int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
47 * flags offsets inside multi-filed
49 private final static int P_FLAG_OFFSET = 6;
50 private final static int I_FLAG_OFFSET = 7;
52 private final ObjectHandlerRegistry registry;
54 protected AbstractMessageParser(final ObjectHandlerRegistry registry) {
55 this.registry = Preconditions.checkNotNull(registry);
58 protected byte[] serializeObject(final Object object) {
60 throw new IllegalArgumentException("Null object passed.");
63 final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
65 final byte[] valueBytes = serializer.serializeObject(object);
67 final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
70 retBytes[OC_F_OFFSET] = (byte) serializer.getObjectClass();
72 // objType_flags multi-field
73 retBytes[OT_FLAGS_MF_OFFSET] = (byte) (serializer.getObjectType() << (Byte.SIZE - OT_SF_LENGTH));
74 if (object.isProcessingRule()) {
75 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
77 if (object.isIgnore()) {
78 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
82 System.arraycopy(ByteArray.intToBytes(valueBytes.length), Integer.SIZE / Byte.SIZE - OBJ_LENGTH_F_LENGTH, retBytes,
83 OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
85 System.arraycopy(valueBytes, 0, retBytes, COMMON_OBJECT_HEADER_LENGTH, valueBytes.length);
90 protected List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
92 final List<Object> objs = Lists.newArrayList();
93 while (bytes.length - offset > 0) {
94 if (bytes.length - offset < COMMON_OBJECT_HEADER_LENGTH) {
95 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
96 + COMMON_OBJECT_HEADER_LENGTH + ".");
99 final int objClass = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OC_F_OFFSET, OC_F_OFFSET + OC_F_LENGTH));
101 final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], OT_SF_OFFSET, OT_SF_LENGTH));
103 final int objLength = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_OFFSET
104 + OBJ_LENGTH_F_LENGTH));
106 final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
108 final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
110 if (bytes.length - offset < objLength) {
111 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
115 // copy bytes for deeper parsing
116 final byte[] bytesToPass = ByteArray.subByte(bytes, offset + COMMON_OBJECT_HEADER_LENGTH, objLength
117 - COMMON_OBJECT_HEADER_LENGTH);
121 final ObjectParser parser = this.registry.getObjectParser(objClass, objType);
123 final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
126 objs.add(parser.parseObject(header, bytesToPass));
127 } catch (final PCEPDocumentedException e) {
128 if (e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_CLASS | e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_TYPE
129 | e.getError() == PCEPErrors.NOT_SUPPORTED_OBJ_CLASS | e.getError() == PCEPErrors.NOT_SUPPORTED_OBJ_TYPE) {
130 objs.add(new UnknownObject(e.getError()));