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.impl.message;
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.MessageParser;
15 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
16 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Notify;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.NotifyBuilder;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.common.primitives.UnsignedBytes;
27 * Parser for BGPNotification message.
29 public final class BGPNotificationMessageParser implements MessageParser, MessageSerializer {
30 public static final int TYPE = 3;
32 private static final Logger logger = LoggerFactory.getLogger(BGPNotificationMessageParser.class);
34 private static final int ERROR_SIZE = 2;
37 * Serializes BGP Notification message.
39 * @param msg to be serialized
40 * @return BGP Notification message converted to byte array
43 public byte[] serializeMessage(final Notification msg) {
45 throw new IllegalArgumentException("BGP Notification message cannot be null");
48 final Notify ntf = (Notify) msg;
49 logger.trace("Started serializing Notification message: {}", ntf);
51 final byte[] msgBody = (ntf.getData() == null) ? new byte[ERROR_SIZE] : new byte[ERROR_SIZE + ntf.getData().length];
53 msgBody[0] = ByteArray.intToBytes(ntf.getErrorCode())[Integer.SIZE / Byte.SIZE - 1];
55 msgBody[1] = ByteArray.intToBytes(ntf.getErrorSubcode())[Integer.SIZE / Byte.SIZE - 1];
57 if (ntf.getData() != null) {
58 System.arraycopy(ntf.getData(), 0, msgBody, ERROR_SIZE, ntf.getData().length);
61 final byte[] ret = MessageUtil.formatMessage(TYPE, msgBody);
62 logger.trace("Notification message serialized to: {}", Arrays.toString(ret));
67 * Parses BGP Notification message to bytes.
69 * @param body byte array to be parsed
70 * @return BGPNotification message
71 * @throws BGPDocumentedException
74 public Notify parseMessageBody(final byte[] body, final int messageLength) throws BGPDocumentedException {
76 throw new IllegalArgumentException("Byte array cannot be null.");
78 logger.trace("Started parsing of notification message: {}", Arrays.toString(body));
80 if (body.length < ERROR_SIZE) {
81 throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
83 final int errorCode = UnsignedBytes.toInt(body[0]);
84 final int errorSubcode = UnsignedBytes.toInt(body[1]);
87 if (body.length > ERROR_SIZE) {
88 data = ByteArray.subByte(body, ERROR_SIZE, body.length - ERROR_SIZE);
90 logger.trace("Notification message was parsed: err = {}, data = {}.", BGPError.forValue(errorCode, errorSubcode),
91 Arrays.toString(data));
92 final NotifyBuilder builder = new NotifyBuilder().setErrorCode((short) errorCode).setErrorSubcode((short) errorSubcode);
94 builder.setData(data);
96 return builder.build();