2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.spi.pojo;
10 import java.util.Arrays;
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;
22 import com.google.common.primitives.UnsignedBytes;
24 abstract class AbstractMessageRegistry implements MessageRegistry {
25 private final static Logger logger = LoggerFactory.getLogger(AbstractMessageRegistry.class);
27 protected abstract Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException;
28 protected abstract byte[] serializeMessageImpl(final Notification message);
31 public final Notification parseMessage(final byte[] bytes) throws BGPDocumentedException, DeserializerException {
33 throw new IllegalArgumentException("Array of bytes is mandatory.");
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 + ".");
40 * byte array starts with message length
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]);
50 final byte[] msgBody = ByteArray.cutBytes(bs, MessageUtil.LENGTH_FIELD_LENGTH + MessageUtil.TYPE_FIELD_LENGTH);
52 if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
53 throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
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) + ". ");
60 logger.debug("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(msgBody));
62 final Notification msg = parseBody(messageType, msgBody, messageLength);
64 throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { bs[MessageUtil.LENGTH_FIELD_LENGTH] });
71 public final byte[] serializeMessage(final Notification message) {
72 if (message == null) {
73 throw new IllegalArgumentException("BGPMessage is mandatory.");
76 logger.trace("Serializing {}", message);
78 final byte[] ret = serializeMessageImpl(message);
80 throw new IllegalArgumentException("Unknown instance of BGPMessage. Passed " + message.getClass());
83 logger.trace("Serialized BGP message {}.", Arrays.toString(ret));