Bug 611 - BGP Update message serialization
[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.primitives.UnsignedBytes;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufUtil;
13 import java.util.Arrays;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPError;
16 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
17 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Parser for BGPNotification message.
28  */
29 public final class BGPNotificationMessageParser implements MessageParser, MessageSerializer {
30
31     public static final int TYPE = 3;
32
33     private static final Logger LOG = LoggerFactory.getLogger(BGPNotificationMessageParser.class);
34
35     private static final int ERROR_SIZE = 2;
36
37     /**
38      * Serializes BGP Notification message.
39      *
40      * @param msg to be serialized
41      * @return BGP Notification message converted to byte array
42      */
43     @Override
44     public void serializeMessage(final Notification msg, ByteBuf bytes) {
45         if (msg == null) {
46             throw new IllegalArgumentException("BGP Notification message cannot be null");
47         }
48
49         final Notify ntf = (Notify) msg;
50         LOG.trace("Started serializing Notification message: {}", ntf);
51
52         final byte[] msgBody = (ntf.getData() == null) ? new byte[ERROR_SIZE] : new byte[ERROR_SIZE + ntf.getData().length];
53
54         msgBody[0] = UnsignedBytes.checkedCast(ntf.getErrorCode());
55
56         msgBody[1] = UnsignedBytes.checkedCast(ntf.getErrorSubcode());
57
58         if (ntf.getData() != null) {
59             System.arraycopy(ntf.getData(), 0, msgBody, ERROR_SIZE, ntf.getData().length);
60         }
61
62         bytes.writeBytes(MessageUtil.formatMessage(TYPE, msgBody));
63         LOG.trace("Notification message serialized to: {}", ByteBufUtil.hexDump(bytes));
64     }
65
66     /**
67      * Parses BGP Notification message to bytes.
68      *
69      * @param body byte array to be parsed
70      * @return BGPNotification message
71      * @throws BGPDocumentedException
72      */
73     @Override
74     public Notify parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
75         if (body == null) {
76             throw new IllegalArgumentException("Byte array cannot be null.");
77         }
78         LOG.trace("Started parsing of notification message: {}", Arrays.toString(ByteArray.getAllBytes(body)));
79
80         if (body.readableBytes() < ERROR_SIZE) {
81             throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
82         }
83         final int errorCode = UnsignedBytes.toInt(body.readByte());
84         final int errorSubcode = UnsignedBytes.toInt(body.readByte());
85
86         byte[] data = null;
87         if (body.readableBytes() != 0) {
88             data = ByteArray.readAllBytes(body);
89         }
90         final NotifyBuilder builder = new NotifyBuilder().setErrorCode((short) errorCode).setErrorSubcode((short) errorSubcode);
91         if (data != null) {
92             builder.setData(data);
93         }
94         LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", BGPError.forValue(errorCode, errorSubcode),
95                 Arrays.toString(data));
96         return builder.build();
97     }
98 }