a479edc6179de37372aca6e857e654bfe735c9ff
[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 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.rev171207.Notify;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.NotifyBuilder;
23 import org.opendaylight.yangtools.yang.binding.Notification;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Parser for BGPNotification message.
29  */
30 public final class BGPNotificationMessageParser implements MessageParser, MessageSerializer {
31
32     private static final Logger LOG = LoggerFactory.getLogger(BGPNotificationMessageParser.class);
33
34     public static final int TYPE = 3;
35
36     private static final int ERROR_SIZE = 2;
37
38     /**
39      * Serializes BGP Notification message.
40      *
41      * @param msg to be serialized
42      * @param bytes ByteBuf where the message will be serialized
43      */
44     @Override
45     public void serializeMessage(final Notification msg, final ByteBuf bytes) {
46         Preconditions.checkArgument(msg instanceof Notify, "Message needs to be of type Notify");
47         final Notify ntf = (Notify) msg;
48
49         final ByteBuf msgBody = Unpooled.buffer();
50         msgBody.writeByte(ntf.getErrorCode());
51         msgBody.writeByte(ntf.getErrorSubcode());
52         final byte[] data = ntf.getData();
53         if (data != null) {
54             msgBody.writeBytes(data);
55         }
56         LOG.trace("Notification message serialized to: {}", ByteBufUtil.hexDump(msgBody));
57         MessageUtil.formatMessage(TYPE, msgBody, bytes);
58     }
59
60     /**
61      * Parses BGP Notification message to bytes.
62      *
63      * @param body ByteBuf to be parsed
64      * @param messageLength the length of the message
65      * @return {@link Notify} which represents BGP notification message
66      * @throws BGPDocumentedException if parsing goes wrong
67      */
68     @Override
69     public Notify parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
70         Preconditions.checkArgument(body != null, "Buffer cannot be null.");
71         if (body.readableBytes() < ERROR_SIZE) {
72             throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
73         }
74         final int errorCode = body.readUnsignedByte();
75         final int errorSubcode = body.readUnsignedByte();
76         final NotifyBuilder builder = new NotifyBuilder().setErrorCode((short) errorCode).setErrorSubcode((short) errorSubcode);
77         if (body.isReadable()) {
78             builder.setData(ByteArray.readAllBytes(body));
79         }
80         LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", BGPError.forValue(errorCode, errorSubcode),
81             Arrays.toString(builder.getData()));
82         return builder.build();
83     }
84 }