Clean up code based on sonar exam
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / MessageUtil.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.spi;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import java.util.Arrays;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
21
22 public final class MessageUtil {
23
24     @VisibleForTesting
25     public static final int MARKER_LENGTH = 16;
26     @VisibleForTesting
27     public static final int COMMON_HEADER_LENGTH = 19;
28     private static final byte[] MARKER = new byte[MARKER_LENGTH];
29
30     static {
31         Arrays.fill(MARKER, 0, MARKER_LENGTH, UnsignedBytes.MAX_VALUE);
32     }
33
34     private MessageUtil() {
35         throw new UnsupportedOperationException();
36     }
37
38     /**
39      * Adds header to message value.
40      *
41      * @param type of the message
42      * @param body message body
43      * @param buffer ByteBuf where the message will be copied with its header
44      */
45     public static void formatMessage(final int type, final ByteBuf body, final ByteBuf buffer) {
46         buffer.writeBytes(MARKER);
47         buffer.writeShort(body.writerIndex() + COMMON_HEADER_LENGTH);
48         buffer.writeByte(type);
49         buffer.writeBytes(body);
50     }
51
52     /**
53      * Check for NLRI attribute in Update message
54      *
55      * @param message Update message
56      * @return true if any prefix or MP-REACH-NLRI attribute is present, false otherwise
57      */
58     public static boolean isAnyNlriPresent(final Update message) {
59         if (message == null || message.getAttributes() == null) {
60             return false;
61         }
62         final Nlri nlri = message.getNlri();
63         return (nlri != null && nlri.getNlri() != null && !nlri.getNlri().isEmpty())
64             || (getMpReachNlri(message.getAttributes()) != null);
65     }
66
67     /**
68      * Finds MP-REACH-NLRI in Update message attributes
69      *
70      * @param attrs Update message attributes
71      * @return MP-REACH-NLRI if present in the attributes, null otherwise
72      */
73     public static MpReachNlri getMpReachNlri(final Attributes attrs) {
74         if (attrs != null && attrs.getAugmentation(Attributes1.class) != null) {
75             return attrs.getAugmentation(Attributes1.class).getMpReachNlri();
76         } else {
77             return null;
78         }
79     }
80
81     /**
82      * Finds MP-UNREACH-NLRI in Update message attributes
83      *
84      * @param attrs Update message attributes
85      * @return MP-UNREACH-NLRI if present in the attributes, null otherwise
86      */
87     public static MpUnreachNlri getMpUnreachNlri(final Attributes attrs) {
88         if (attrs != null && attrs.getAugmentation(Attributes2.class) != null) {
89             return attrs.getAugmentation(Attributes2.class).getMpUnreachNlri();
90         } else {
91             return null;
92         }
93     }
94 }