Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / AbstractMessageParser.java
1 package org.opendaylight.protocol.pcep.impl.message;
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.PCEPErrorMapping;
10 import org.opendaylight.protocol.pcep.PCEPErrors;
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.message.rev131007.PcerrBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcerrMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Lists;
31 import com.google.common.primitives.UnsignedBytes;
32
33 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
34         private static final Logger LOG = LoggerFactory.getLogger(AbstractMessageParser.class);
35
36         private static final int COMMON_OBJECT_HEADER_LENGTH = 4;
37
38         private static final int OC_F_LENGTH = 1;
39         private static final int OT_FLAGS_MF_LENGTH = 1;
40         private static final int OBJ_LENGTH_F_LENGTH = 2;
41
42         private static final int OC_F_OFFSET = 0;
43         private static final int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
44         private static final int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
45
46         private static final int OT_SF_LENGTH = 4;
47         private static final int FLAGS_SF_LENGTH = 4;
48
49         /*
50          * offsets of fields inside of multi-field in bits
51          */
52         private static final int OT_SF_OFFSET = 0;
53         private static final int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
54
55         /*
56          * flags offsets inside multi-filed
57          */
58         private static final int P_FLAG_OFFSET = 6;
59         private static final int I_FLAG_OFFSET = 7;
60
61         private final ObjectHandlerRegistry registry;
62
63         protected AbstractMessageParser(final ObjectHandlerRegistry registry) {
64                 this.registry = Preconditions.checkNotNull(registry);
65         }
66
67         protected byte[] serializeObject(final Object object) {
68                 if (object == null) {
69                         throw new IllegalArgumentException("Null object passed.");
70                 }
71
72                 final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
73
74                 final byte[] valueBytes = serializer.serializeObject(object);
75
76                 final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
77
78                 // objClass
79                 retBytes[OC_F_OFFSET] = UnsignedBytes.checkedCast(serializer.getObjectClass());
80
81                 // objType_flags multi-field
82                 retBytes[OT_FLAGS_MF_OFFSET] = UnsignedBytes.checkedCast(serializer.getObjectType() << (Byte.SIZE - OT_SF_LENGTH));
83                 if (object.isProcessingRule() != null && object.isProcessingRule()) {
84                         retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
85                 }
86                 if (object.isIgnore() != null && object.isIgnore()) {
87                         retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
88                 }
89
90                 // objLength
91                 System.arraycopy(ByteArray.intToBytes(valueBytes.length + COMMON_OBJECT_HEADER_LENGTH), Integer.SIZE / Byte.SIZE
92                                 - OBJ_LENGTH_F_LENGTH, retBytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
93
94                 ByteArray.copyWhole(valueBytes, retBytes, COMMON_OBJECT_HEADER_LENGTH);
95                 return retBytes;
96         }
97
98         protected final List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException {
99                 int offset = 0;
100                 final List<Object> objs = Lists.newArrayList();
101                 while (bytes.length - offset > 0) {
102                         if (bytes.length - offset < COMMON_OBJECT_HEADER_LENGTH) {
103                                 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
104                                                 + COMMON_OBJECT_HEADER_LENGTH + ".");
105                         }
106
107                         final int objClass = UnsignedBytes.toInt(bytes[offset]);
108
109                         offset += OC_F_LENGTH;
110
111                         final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[offset], OT_SF_OFFSET, OT_SF_LENGTH));
112
113                         final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[offset], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
114
115                         final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
116
117                         offset += OT_FLAGS_MF_LENGTH;
118
119                         final int objLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset, OBJ_LENGTH_F_LENGTH));
120
121                         if (bytes.length - offset < objLength - COMMON_OBJECT_HEADER_LENGTH) {
122                                 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + (bytes.length - offset) + " Expected: >= "
123                                                 + objLength + ".");
124                         }
125
126                         offset += OBJ_LENGTH_F_LENGTH;
127
128                         // copy bytes for deeper parsing
129                         final byte[] bytesToPass = ByteArray.subByte(bytes, offset, objLength - COMMON_OBJECT_HEADER_LENGTH);
130
131                         offset += objLength - COMMON_OBJECT_HEADER_LENGTH;
132
133                         final ObjectParser parser = Preconditions.checkNotNull(this.registry.getObjectParser(objClass, objType));
134                         final ObjectHeader header = new ObjectHeaderImpl(flags.get(P_FLAG_OFFSET), flags.get(I_FLAG_OFFSET));
135
136                         // parseObject is required to return null for P=0 errored objects
137                         final Object o = parser.parseObject(header, bytesToPass);
138                         if (o != null) {
139                                 objs.add(o);
140                         }
141                 }
142
143                 return objs;
144         }
145
146         public static PcerrMessage createErrorMsg(final PCEPErrors e) {
147                 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
148                 return new PcerrBuilder().setPcerrMessage(
149                                 new PcerrMessageBuilder().setErrors(
150                                                 Arrays.asList(new ErrorsBuilder().setErrorObject(
151                                                                 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).type).setValue(
152                                                                                 maping.getFromErrorsEnum(e).value).build()).build())).build()).build();
153         }
154
155         // FIXME: remove PCEPDocumentedException
156         abstract protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException, PCEPDocumentedException;
157
158         @Override
159         public final Message parseMessage(final byte[] buffer, final List<Message> errors) throws PCEPDeserializerException {
160                 Preconditions.checkNotNull(buffer, "Buffer may not be null");
161
162                 // Parse objects first
163                 final List<Object> objs = parseObjects(buffer);
164
165                 // Run validation
166                 try {
167                         return validate(objs, errors);
168                 } catch (PCEPDocumentedException e) {
169                         LOG.info("Message failed to validate", e);
170                         errors.add(createErrorMsg(e.getError()));
171                         return null;
172                 }
173         }
174 }