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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufUtil;
13 import io.netty.buffer.Unpooled;
14 import java.util.Arrays;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPError;
17 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
18 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
23 import org.opendaylight.yangtools.yang.binding.Notification;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Parser for BGPNotification message.
30 public final class BGPNotificationMessageParser implements MessageParser, MessageSerializer {
32 public static final int TYPE = 3;
34 private static final Logger LOG = LoggerFactory.getLogger(BGPNotificationMessageParser.class);
36 private static final int ERROR_SIZE = 2;
39 * Serializes BGP Notification message.
41 * @param msg to be serialized
42 * @param bytes ByteBuf where the message will be serialized
45 public void serializeMessage(final Notification msg, final ByteBuf bytes) {
46 Preconditions.checkArgument(msg instanceof Notify, "BGP Notification message cannot be null");
47 final Notify ntf = (Notify) msg;
48 LOG.trace("Started serializing Notification message: {}", ntf);
50 final byte[] data = ntf.getData();
51 final ByteBuf msgBody = Unpooled.buffer();
52 msgBody.writeByte(ntf.getErrorCode());
53 msgBody.writeByte(ntf.getErrorSubcode());
55 msgBody.writeBytes(data);
57 LOG.trace("Notification message serialized to: {}", ByteBufUtil.hexDump(bytes));
58 MessageUtil.formatMessage(TYPE, msgBody, bytes);
62 * Parses BGP Notification message to bytes.
64 * @param body ByteBuf to be parsed
65 * @param messageLength the length of the message
66 * @return {@link Notify} which represents BGP notification message
67 * @throws BGPDocumentedException if parsing goes wrong
70 public Notify parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
71 Preconditions.checkArgument(body != null, "Byte buffer cannot be null.");
72 LOG.trace("Started parsing of notification message: {}", ByteBufUtil.hexDump(body));
73 if (body.readableBytes() < ERROR_SIZE) {
74 throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
76 final int errorCode = body.readUnsignedByte();
77 final int errorSubcode = body.readUnsignedByte();
78 final NotifyBuilder builder = new NotifyBuilder().setErrorCode((short) errorCode).setErrorSubcode((short) errorSubcode);
79 if (body.isReadable()) {
80 builder.setData(ByteArray.readAllBytes(body));
82 LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", BGPError.forValue(errorCode, errorSubcode),
83 Arrays.toString(builder.getData()));
84 return builder.build();