Bug 4827: MultiPathSupport utilities 15/37315/5
authorMilos Fabian <milfabia@cisco.com>
Fri, 8 Apr 2016 08:30:52 +0000 (10:30 +0200)
committerMilos Fabian <milfabia@cisco.com>
Fri, 22 Apr 2016 05:39:06 +0000 (05:39 +0000)
Added MultiPathSupportUtil utlity class
can be useful in when processing Add-Path aware
BGP PDUs.

Change-Id: Iec7779986569ec8b202d01c5009ff837c467c5d3
Signed-off-by: Milos Fabian <milfabia@cisco.com>
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MultiPathSupportUtil.java [new file with mode: 0644]
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/MultiPathSupportUtilTest.java [new file with mode: 0644]

diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MultiPathSupportUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MultiPathSupportUtil.java
new file mode 100644 (file)
index 0000000..82571d5
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.parser.spi;
+
+import com.google.common.base.Preconditions;
+import java.util.Optional;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
+
+public final class MultiPathSupportUtil {
+
+    private MultiPathSupportUtil() {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Check is AFI/SAFI is supported by {@link MultiPathSupport} service.
+     * @param constraints Peer specific constraint.
+     * @param afiSafi Required AFI/SAFI
+     * @return True if AFI/SAFI is supported.
+     */
+    public static boolean isTableTypeSupported(@Nullable final PeerSpecificParserConstraint constraints, @Nonnull final BgpTableType afiSafi) {
+        Preconditions.checkNotNull(afiSafi);
+        if (constraints != null) {
+            final Optional<MultiPathSupport> peerConstraint = constraints.getPeerConstraint(MultiPathSupport.class);
+            return peerConstraint.isPresent() && peerConstraint.get().isTableTypeSupported(afiSafi);
+        }
+        return false;
+
+    }
+
+}
diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/MultiPathSupportUtilTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/MultiPathSupportUtilTest.java
new file mode 100644 (file)
index 0000000..8c7e2cc
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.parser.spi;
+
+import java.util.Optional;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
+
+public class MultiPathSupportUtilTest {
+
+    private static final BgpTableType AFI_SAFI = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
+
+    @Mock
+    private PeerSpecificParserConstraint constraints;
+
+    @Mock
+    private MultiPathSupport mpSupport;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test
+    public void testIsTableTypeSupportedPossitive() {
+        Mockito.doReturn(Optional.of(this.mpSupport)).when(this.constraints).getPeerConstraint(Mockito.any());
+        Mockito.doReturn(true).when(this.mpSupport).isTableTypeSupported(Mockito.any());
+        Assert.assertTrue(MultiPathSupportUtil.isTableTypeSupported(this.constraints, AFI_SAFI));
+    }
+
+    @Test
+    public void testIsTableTypeSupportedNegativeTableTypeNotSupported() {
+        Mockito.doReturn(Optional.of(this.mpSupport)).when(this.constraints).getPeerConstraint(Mockito.any());
+        Mockito.doReturn(false).when(this.mpSupport).isTableTypeSupported(Mockito.any());
+        Assert.assertFalse(MultiPathSupportUtil.isTableTypeSupported(this.constraints, AFI_SAFI));
+    }
+
+    @Test
+    public void testIsTableTypeSupportedNegativeMpSupportAbsent() {
+        Mockito.doReturn(Optional.empty()).when(this.constraints).getPeerConstraint(Mockito.any());
+        Assert.assertFalse(MultiPathSupportUtil.isTableTypeSupported(this.constraints, AFI_SAFI));
+    }
+
+    @Test
+    public void testIsTableTypeSupportedNegativeNull() {
+        Assert.assertFalse(MultiPathSupportUtil.isTableTypeSupported(null, AFI_SAFI));
+    }
+
+    @Test(expected=NullPointerException.class)
+    public void testIsTableTypeSupportedNPE() {
+        MultiPathSupportUtil.isTableTypeSupported(null, null);
+    }
+
+}