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