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