BUG-6647 Increase code coverage and clean up I
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / MessageUtil.java
old mode 100644 (file)
new mode 100755 (executable)
index 35aedd1..a2e3ef3
@@ -9,10 +9,15 @@ package org.opendaylight.protocol.bgp.parser.spi;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.primitives.UnsignedBytes;
-
 import io.netty.buffer.ByteBuf;
-
 import java.util.Arrays;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
 
 public final class MessageUtil {
 
@@ -27,13 +32,14 @@ public final class MessageUtil {
     }
 
     private MessageUtil() {
+        throw new UnsupportedOperationException();
     }
 
     /**
      * Adds header to message value.
      *
      * @param type of the message
-     * @param value message value
+     * @param body message body
      * @param buffer ByteBuf where the message will be copied with its header
      */
     public static void formatMessage(final int type, final ByteBuf body, final ByteBuf buffer) {
@@ -42,4 +48,47 @@ public final class MessageUtil {
         buffer.writeByte(type);
         buffer.writeBytes(body);
     }
+
+    /**
+     * Check for NLRI attribute in Update message
+     *
+     * @param message Update message
+     * @return true if any prefix or MP-REACH-NLRI attribute is present, false otherwise
+     */
+    public static boolean isAnyNlriPresent(final Update message) {
+        if (message == null || message.getAttributes() == null) {
+            return false;
+        }
+        final Nlri nlri = message.getNlri();
+        return (nlri != null && nlri.getNlri() != null && !nlri.getNlri().isEmpty())
+            || (getMpReachNlri(message.getAttributes()) != null);
+    }
+
+    /**
+     * Finds MP-REACH-NLRI in Update message attributes
+     *
+     * @param attrs Update message attributes
+     * @return MP-REACH-NLRI if present in the attributes, null otherwise
+     */
+    public static MpReachNlri getMpReachNlri(final Attributes attrs) {
+        if (attrs != null && attrs.getAugmentation(Attributes1.class) != null) {
+            return attrs.getAugmentation(Attributes1.class).getMpReachNlri();
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Finds MP-UNREACH-NLRI in Update message attributes
+     *
+     * @param attrs Update message attributes
+     * @return MP-UNREACH-NLRI if present in the attributes, null otherwise
+     */
+    public static MpUnreachNlri getMpUnreachNlri(final Attributes attrs) {
+        if (attrs != null && attrs.getAugmentation(Attributes2.class) != null) {
+            return attrs.getAugmentation(Attributes2.class).getMpUnreachNlri();
+        } else {
+            return null;
+        }
+    }
 }