BUG-113: switch BGP to proper activators
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / AbstractMessageRegistry.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.bgp.parser.spi.pojo;
9
10 import java.util.Arrays;
11
12 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
13 import org.opendaylight.protocol.bgp.parser.BGPError;
14 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
15 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
16 import org.opendaylight.protocol.framework.DeserializerException;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yangtools.yang.binding.Notification;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.common.primitives.UnsignedBytes;
23
24 abstract class AbstractMessageRegistry implements MessageRegistry {
25         private final static Logger logger = LoggerFactory.getLogger(AbstractMessageRegistry.class);
26
27         protected abstract Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException;
28         protected abstract byte[] serializeMessageImpl(final Notification message);
29
30         @Override
31         public final Notification parseMessage(final byte[] bytes) throws BGPDocumentedException, DeserializerException {
32                 if (bytes == null) {
33                         throw new IllegalArgumentException("Array of bytes is mandatory.");
34                 }
35                 if (bytes.length < MessageUtil.COMMON_HEADER_LENGTH) {
36                         throw new IllegalArgumentException("Too few bytes in passed array. Passed: " + bytes.length + ". Expected: >= "
37                                         + MessageUtil.COMMON_HEADER_LENGTH + ".");
38                 }
39                 /*
40                  * byte array starts with message length
41                  */
42                 // final byte[] ones = new byte[MARKER_LENGTH];
43                 // Arrays.fill(ones, (byte)0xff);
44                 // if (Arrays.equals(bytes, ones))
45                 // throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
46                 final byte[] bs = ByteArray.cutBytes(bytes, MessageUtil.MARKER_LENGTH);
47                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(bs, 0, MessageUtil.LENGTH_FIELD_LENGTH));
48                 final int messageType = UnsignedBytes.toInt(bs[MessageUtil.LENGTH_FIELD_LENGTH]);
49
50                 final byte[] msgBody = ByteArray.cutBytes(bs, MessageUtil.LENGTH_FIELD_LENGTH + MessageUtil.TYPE_FIELD_LENGTH);
51
52                 if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
53                         throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
54                 }
55                 if (msgBody.length != messageLength - MessageUtil.COMMON_HEADER_LENGTH) {
56                         throw new DeserializerException("Size doesn't match size specified in header. Passed: " + msgBody.length + "; Expected: "
57                                         + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
58                 }
59
60                 logger.debug("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(msgBody));
61
62                 final Notification msg = parseBody(messageType, msgBody, messageLength);
63                 if (msg == null) {
64                         throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { bs[MessageUtil.LENGTH_FIELD_LENGTH] });
65                 }
66
67                 return msg;
68         }
69
70         @Override
71         public final byte[] serializeMessage(final Notification message) {
72                 if (message == null) {
73                         throw new IllegalArgumentException("BGPMessage is mandatory.");
74                 }
75
76                 logger.trace("Serializing {}", message);
77
78                 final byte[] ret = serializeMessageImpl(message);
79                 if (ret == null) {
80                         throw new IllegalArgumentException("Unknown instance of BGPMessage. Passed " + message.getClass());
81                 }
82
83                 logger.trace("Serialized BGP message {}.", Arrays.toString(ret));
84                 return ret;
85         }
86 }