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