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