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