BUG-47: more subobject models
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractMessageParser.java
1 package org.opendaylight.protocol.pcep.spi;
2
3 import java.util.Arrays;
4 import java.util.BitSet;
5 import java.util.List;
6
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;
14
15 import com.google.common.base.Preconditions;
16 import com.google.common.collect.Lists;
17 import com.google.common.primitives.UnsignedBytes;
18
19 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
20
21         private final static int COMMON_OBJECT_HEADER_LENGTH = 4;
22
23         private final static int OC_F_LENGTH = 1;
24         private final static int OT_FLAGS_MF_LENGTH = 1; // multi-field
25         private final static int OBJ_LENGTH_F_LENGTH = 2;
26
27         private final static int OC_F_OFFSET = 0;
28         private final static int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
29         private final static int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
30
31         private final static int OT_SF_LENGTH = 4;
32         private final static int FLAGS_SF_LENGTH = 4;
33
34         /*
35          * offsets of fields inside of multi-field in bits
36          */
37         private final static int OT_SF_OFFSET = 0;
38         private final static int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
39
40         /*
41          * flags offsets inside multi-filed
42          */
43         private final static int P_FLAG_OFFSET = 6;
44         private final static int I_FLAG_OFFSET = 7;
45
46         private final ObjectHandlerRegistry registry;
47
48         protected AbstractMessageParser(final ObjectHandlerRegistry registry) {
49                 this.registry = Preconditions.checkNotNull(registry);
50         }
51
52         protected byte[] serializeObject(final Object object) {
53                 if (object == null) {
54                         throw new IllegalArgumentException("Null object passed.");
55                 }
56
57                 final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
58
59                 final byte[] valueBytes = serializer.serializeObject(object);
60
61                 final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
62
63                 // objClass
64                 retBytes[OC_F_OFFSET] = (byte) serializer.getObjectClass();
65
66                 // objType_flags multi-field
67                 retBytes[OT_FLAGS_MF_OFFSET] = (byte) (serializer.getObjectType() << (Byte.SIZE - OT_SF_LENGTH));
68                 if (object.isProcessingRule()) {
69                         retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
70                 }
71                 if (object.isIgnore()) {
72                         retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
73                 }
74
75                 // objLength
76                 System.arraycopy(ByteArray.intToBytes(valueBytes.length), Integer.SIZE / Byte.SIZE - OBJ_LENGTH_F_LENGTH, retBytes,
77                                 OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
78
79                 System.arraycopy(valueBytes, 0, retBytes, COMMON_OBJECT_HEADER_LENGTH, valueBytes.length);
80
81                 return retBytes;
82         }
83
84         protected List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
85                 int offset = 0;
86                 final List<Object> objs = Lists.newArrayList();
87                 while (bytes.length - offset > 0) {
88                         if (bytes.length - offset < COMMON_OBJECT_HEADER_LENGTH) {
89                                 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
90                                                 + COMMON_OBJECT_HEADER_LENGTH + ".");
91                         }
92
93                         final int objClass = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OC_F_OFFSET, OC_F_OFFSET + OC_F_LENGTH));
94
95                         final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], OT_SF_OFFSET, OT_SF_LENGTH));
96
97                         final int objLength = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_OFFSET
98                                         + OBJ_LENGTH_F_LENGTH));
99
100                         final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
101
102                         final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
103
104                         if (bytes.length - offset < objLength) {
105                                 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
106                                                 + objLength + ".");
107                         }
108
109                         // copy bytes for deeper parsing
110                         final byte[] bytesToPass = ByteArray.subByte(bytes, offset + COMMON_OBJECT_HEADER_LENGTH, objLength
111                                         - COMMON_OBJECT_HEADER_LENGTH);
112
113                         offset += objLength;
114
115                         final ObjectParser parser = this.registry.getObjectParser(objClass, objType);
116
117                         final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
118
119                         try {
120                                 objs.add(parser.parseObject(header, bytesToPass));
121                         } catch (final PCEPDocumentedException e) {
122                                 if (e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_CLASS | e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_TYPE
123                                                 | e.getError() == PCEPErrors.NOT_SUPPORTED_OBJ_CLASS | e.getError() == PCEPErrors.NOT_SUPPORTED_OBJ_TYPE) {
124                                         objs.add(new UnknownObject(e.getError()));
125                                 } else {
126                                         throw e;
127                                 }
128                         }
129                 }
130                 return objs;
131         }
132 }