Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPMessageFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.pcep.impl;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.protocol.framework.DeserializerException;
17 import org.opendaylight.protocol.framework.DocumentedException;
18 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
19 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.MessageHandlerRegistry;
21 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.base.Preconditions;
28 import com.google.common.primitives.UnsignedBytes;
29
30 /**
31  * A PCEP message parser which also does validation.
32  */
33 public final class PCEPMessageFactory implements ProtocolMessageFactory<Message> {
34
35         private final static Logger logger = LoggerFactory.getLogger(PCEPMessageFactory.class);
36
37         private final static int TYPE_SIZE = 1; // bytes
38
39         private final static int LENGTH_SIZE = 2; // bytes
40
41         public final static int COMMON_HEADER_LENGTH = 4; // bytes
42
43         /**
44          * Current supported version of PCEP.
45          */
46         public static final int PCEP_VERSION = 1;
47
48         private static final int VERSION_SF_LENGTH = 3;
49
50         private static final int VER_FLAGS_MF_LENGTH = 1;
51         private static final int TYPE_F_LENGTH = 1;
52         private static final int LENGTH_F_LENGTH = 2;
53
54         private static final int VER_FLAGS_MF_OFFSET = 0;
55         private static final int TYPE_F_OFFSET = VER_FLAGS_MF_LENGTH + VER_FLAGS_MF_OFFSET;
56         private static final int LENGTH_F_OFFSET = TYPE_F_LENGTH + TYPE_F_OFFSET;
57
58         private final MessageHandlerRegistry registry;
59
60         public PCEPMessageFactory(final MessageHandlerRegistry registry) {
61                 this.registry = Preconditions.checkNotNull(registry);
62         }
63
64         @Override
65         public Message parse(final byte[] bytes) throws DeserializerException, DocumentedException {
66                 Preconditions.checkArgument(bytes != null, "Bytes may not be null");
67                 Preconditions.checkArgument(bytes.length != 0, "Bytes may not be empty");
68
69                 logger.trace("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(bytes));
70
71                 final int type = UnsignedBytes.toInt(bytes[1]);
72
73                 final int msgLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, TYPE_SIZE + 1, LENGTH_SIZE));
74
75                 final byte[] msgBody = ByteArray.cutBytes(bytes, TYPE_SIZE + 1 + LENGTH_SIZE);
76
77                 if (msgBody.length != msgLength - COMMON_HEADER_LENGTH) {
78                         throw new DeserializerException("Body size " + msgBody.length + " does not match header size "
79                                         + (msgLength - COMMON_HEADER_LENGTH));
80                 }
81
82                 final List<Message> errors = new ArrayList<>();
83                 Message msg = null;
84
85                 try {
86                         msg = this.registry.getMessageParser(type).parseMessage(msgBody, errors);
87                 } catch (final PCEPDeserializerException e) {
88                         logger.debug("Unexpected deserializer problem", e);
89                         throw new DeserializerException(e.getMessage(), e);
90                 }
91
92                 if (!errors.isEmpty()) {
93                         // FIXME: we have a bunch of error messages, how can we send them back?
94                 }
95
96                 logger.debug("Message was parsed. {}", msg);
97                 return msg;
98         }
99
100         @Override
101         public byte[] put(final Message msg) {
102                 if (msg == null) {
103                         throw new IllegalArgumentException("PCEPMessage is mandatory.");
104                 }
105
106                 final ByteBuf buf = new UnpooledByteBufAllocator(false).buffer();
107
108                 final MessageSerializer serializer = this.registry.getMessageSerializer(msg);
109
110                 serializer.serializeMessage(msg, buf);
111
112                 final byte[] msgBody = new byte[buf.readableBytes()];
113
114                 buf.getBytes(0, msgBody);
115
116                 final byte[] headerBytes = new byte[COMMON_HEADER_LENGTH];
117
118                 // msgVer_Flag
119                 headerBytes[VER_FLAGS_MF_OFFSET] = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
120
121                 // msgType
122                 headerBytes[TYPE_F_OFFSET] = (byte) serializer.getMessageType();
123
124                 // msgLength
125                 System.arraycopy(ByteArray.intToBytes(msgBody.length + COMMON_HEADER_LENGTH), Integer.SIZE / Byte.SIZE - LENGTH_F_LENGTH,
126                                 headerBytes, LENGTH_F_OFFSET, LENGTH_F_LENGTH);
127
128                 final byte[] retBytes = new byte[headerBytes.length + msgBody.length];
129
130                 ByteArray.copyWhole(headerBytes, retBytes, 0);
131                 ByteArray.copyWhole(msgBody, retBytes, COMMON_HEADER_LENGTH);
132
133                 return retBytes;
134         }
135 }