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 static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufUtil;
14 import io.netty.buffer.Unpooled;
15 import java.util.Arrays;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
21 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26 import org.opendaylight.yangtools.yang.common.Uint8;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Parser for BGPNotification message.
33 public final class BGPNotificationMessageParser implements MessageParser, MessageSerializer {
35 private static final Logger LOG = LoggerFactory.getLogger(BGPNotificationMessageParser.class);
37 public static final int TYPE = 3;
39 private static final int ERROR_SIZE = 2;
42 * Serializes BGP Notification message.
44 * @param msg to be serialized
45 * @param bytes ByteBuf where the message will be serialized
48 public void serializeMessage(final Notification<?> msg, final ByteBuf bytes) {
49 checkArgument(msg instanceof Notify, "Message needs to be of type Notify");
50 final Notify ntf = (Notify) msg;
52 final ByteBuf msgBody = Unpooled.buffer()
53 .writeByte(ntf.getErrorCode().toJava())
54 .writeByte(ntf.getErrorSubcode().toJava());
55 final byte[] data = ntf.getData();
57 msgBody.writeBytes(data);
59 if (LOG.isTraceEnabled()) {
60 LOG.trace("Notification message serialized to: {}", ByteBufUtil.hexDump(msgBody));
62 MessageUtil.formatMessage(TYPE, msgBody, bytes);
66 * Parses BGP Notification message to bytes.
68 * @param body ByteBuf to be parsed
69 * @param messageLength the length of the message
70 * @return {@link Notify} which represents BGP notification message
71 * @throws BGPDocumentedException if parsing goes wrong
74 public Notify parseMessageBody(final ByteBuf body, final int messageLength,
75 final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
76 checkArgument(body != null, "Buffer cannot be null.");
77 if (body.readableBytes() < ERROR_SIZE) {
78 throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
80 final Uint8 errorCode = Uint8.valueOf(body.readUnsignedByte());
81 final Uint8 errorSubcode = Uint8.valueOf(body.readUnsignedByte());
82 final NotifyBuilder builder = new NotifyBuilder()
83 .setErrorCode(errorCode).setErrorSubcode(errorSubcode);
84 if (body.isReadable()) {
85 builder.setData(ByteArray.readAllBytes(body));
88 final Notify result = builder.build();
89 final BGPError err = BGPError.forValue(errorCode, errorSubcode);
91 if (LOG.isDebugEnabled()) {
92 LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", err,
93 Arrays.toString(result.getData()));