1 package org.opendaylight.protocol.pcep.impl.message;
3 import java.util.Arrays;
4 import java.util.BitSet;
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;
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Lists;
31 import com.google.common.primitives.UnsignedBytes;
33 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
34 private static final Logger LOG = LoggerFactory.getLogger(AbstractMessageParser.class);
36 private static final int COMMON_OBJECT_HEADER_LENGTH = 4;
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;
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;
46 private static final int OT_SF_LENGTH = 4;
47 private static final int FLAGS_SF_LENGTH = 4;
50 * offsets of fields inside of multi-field in bits
52 private static final int OT_SF_OFFSET = 0;
53 private static final int FLAGS_SF_OFFSET = OT_SF_OFFSET + OT_SF_LENGTH;
56 * flags offsets inside multi-filed
58 private static final int P_FLAG_OFFSET = 6;
59 private static final int I_FLAG_OFFSET = 7;
61 private final ObjectHandlerRegistry registry;
63 protected AbstractMessageParser(final ObjectHandlerRegistry registry) {
64 this.registry = Preconditions.checkNotNull(registry);
67 protected byte[] serializeObject(final Object object) {
69 throw new IllegalArgumentException("Null object passed.");
72 final ObjectSerializer serializer = this.registry.getObjectSerializer(object);
74 final byte[] valueBytes = serializer.serializeObject(object);
76 final byte[] retBytes = new byte[COMMON_OBJECT_HEADER_LENGTH + valueBytes.length];
79 retBytes[OC_F_OFFSET] = UnsignedBytes.checkedCast(serializer.getObjectClass());
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;
86 if (object.isIgnore() != null && object.isIgnore()) {
87 retBytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
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);
94 ByteArray.copyWhole(valueBytes, retBytes, COMMON_OBJECT_HEADER_LENGTH);
98 protected final List<Object> parseObjects(final byte[] bytes) throws PCEPDeserializerException {
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 + ".");
107 final int objClass = UnsignedBytes.toInt(bytes[offset]);
109 offset += OC_F_LENGTH;
111 final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(bytes[offset], OT_SF_OFFSET, OT_SF_LENGTH));
113 final byte[] flagsBytes = { ByteArray.copyBitsRange(bytes[offset], FLAGS_SF_OFFSET, FLAGS_SF_LENGTH) };
115 final BitSet flags = ByteArray.bytesToBitSet(flagsBytes);
117 offset += OT_FLAGS_MF_LENGTH;
119 final int objLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset, OBJ_LENGTH_F_LENGTH));
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: >= "
126 offset += OBJ_LENGTH_F_LENGTH;
128 // copy bytes for deeper parsing
129 final byte[] bytesToPass = ByteArray.subByte(bytes, offset, objLength - COMMON_OBJECT_HEADER_LENGTH);
131 offset += objLength - COMMON_OBJECT_HEADER_LENGTH;
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));
136 // parseObject is required to return null for P=0 errored objects
137 final Object o = parser.parseObject(header, bytesToPass);
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();
155 // FIXME: remove PCEPDocumentedException
156 abstract protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException, PCEPDocumentedException;
159 public final Message parseMessage(final byte[] buffer, final List<Message> errors) throws PCEPDeserializerException {
160 Preconditions.checkNotNull(buffer, "Buffer may not be null");
162 // Parse objects first
163 final List<Object> objs = parseObjects(buffer);
167 return validate(objs, errors);
168 } catch (PCEPDocumentedException e) {
169 LOG.info("Message failed to validate", e);
170 errors.add(createErrorMsg(e.getError()));