BUG-47 : PCEP migration to generated DTOs.
[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.collect.Lists;
16 import com.google.common.primitives.UnsignedBytes;
17
18 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
19
20         private final static int COMMON_OBJECT_HEADER_LENGTH = 4;
21
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;
25
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;
29
30         private final static int OT_SF_LENGTH = 4;
31         private final static int FLAGS_SF_LENGTH = 4;
32
33         /*
34          * offsets of fields inside of multi-field in bits
35          */
36         private final static int OT_SF_OFFSET = 0;
37         private final static int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
38
39         /*
40          * flags offsets inside multi-filed
41          */
42         private final static int P_FLAG_OFFSET = 6;
43         private final static int I_FLAG_OFFSET = 7;
44
45         private final HandlerRegistry registry;
46
47         protected AbstractMessageParser(final HandlerRegistry registry) {
48                 this.registry = registry;
49         }
50
51         protected byte[] serializeObject(final Object object) {
52                 if (object == null)
53                         throw new IllegalArgumentException("Null object passed.");
54
55                 final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
56
57                 final byte[] valueBytes = serializer.serializeObject(object);
58
59                 final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
60
61                 // objClass
62                 retBytes[OC_F_OFFSET] = (byte) serializer.getObjectClass();
63
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;
70
71                 // objLength
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);
74
75                 System.arraycopy(valueBytes, 0, retBytes, COMMON_OBJECT_HEADER_LENGTH, valueBytes.length);
76
77                 return retBytes;
78         }
79
80         protected List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
81                 int offset = 0;
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 + ".");
87
88                         final int objClass = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OC_F_OFFSET, OC_F_OFFSET + OC_F_LENGTH));
89
90                         final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], OT_SF_OFFSET, OT_SF_LENGTH));
91
92                         final int objLength = ByteArray.bytesToInt(Arrays.copyOfRange(bytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_OFFSET
93                                         + OBJ_LENGTH_F_LENGTH));
94
95                         final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[OT_FLAGS_MF_OFFSET], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
96
97                         final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
98
99                         if (bytes.length - offset < objLength)
100                                 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
101                                                 + objLength + ".");
102
103                         // copy bytes for deeper parsing
104                         final byte[] bytesToPass = ByteArray.subByte(bytes, offset + COMMON_OBJECT_HEADER_LENGTH, objLength
105                                         - COMMON_OBJECT_HEADER_LENGTH);
106
107                         offset += objLength;
108
109                         final ObjectParser parser = this.registry.getObjectParser(objClass, objType);
110
111                         final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
112
113                         try {
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()));
119                                 } else
120                                         throw e;
121                         }
122                 }
123                 return objs;
124         }
125 }