Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPNotificationMessageParser.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.impl.message;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
29
30 /**
31  * Parser for BGPNotification message.
32  */
33 public final class BGPNotificationMessageParser implements MessageParser, MessageSerializer {
34
35     private static final Logger LOG = LoggerFactory.getLogger(BGPNotificationMessageParser.class);
36
37     public static final int TYPE = 3;
38
39     private static final int ERROR_SIZE = 2;
40
41     /**
42      * Serializes BGP Notification message.
43      *
44      * @param msg to be serialized
45      * @param bytes ByteBuf where the message will be serialized
46      */
47     @Override
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;
51
52         final ByteBuf msgBody = Unpooled.buffer()
53                 .writeByte(ntf.getErrorCode().toJava())
54                 .writeByte(ntf.getErrorSubcode().toJava());
55         final byte[] data = ntf.getData();
56         if (data != null) {
57             msgBody.writeBytes(data);
58         }
59         if (LOG.isTraceEnabled()) {
60             LOG.trace("Notification message serialized to: {}", ByteBufUtil.hexDump(msgBody));
61         }
62         MessageUtil.formatMessage(TYPE, msgBody, bytes);
63     }
64
65     /**
66      * Parses BGP Notification message to bytes.
67      *
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
72      */
73     @Override
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);
79         }
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));
86         }
87
88         final Notify result = builder.build();
89         final BGPError err = BGPError.forValue(errorCode, errorSubcode);
90
91         if (LOG.isDebugEnabled()) {
92             LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", err,
93                 Arrays.toString(result.getData()));
94         }
95         return result;
96     }
97 }