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