Add decoder constraint based on treat-as-withdrawn configuration
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / ParserToSalTest.java
old mode 100644 (file)
new mode 100755 (executable)
index ef0ea5c..45d9fc3
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.AS_NUMBER;
+import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.BGP_ID;
+import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
 
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Throwables;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import com.google.common.eventbus.EventBus;
 import io.netty.util.concurrent.GlobalEventExecutor;
-import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
-import java.util.concurrent.ExecutionException;
-import javax.annotation.Nullable;
 import org.junit.After;
+import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
-import org.opendaylight.controller.md.sal.binding.test.DataBrokerTestCustomizer;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.core.api.model.SchemaService;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.protocol.bgp.inet.RIBActivator;
+import org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectionModeFactory;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
-import org.opendaylight.protocol.bgp.rib.impl.BGPPeer;
-import org.opendaylight.protocol.bgp.rib.impl.RIBActivator;
-import org.opendaylight.protocol.bgp.rib.impl.RIBImpl;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
-import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.mock.BGPMock;
 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBExtensionProviderActivator;
 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
-import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
-import org.opendaylight.tcpmd5.api.KeyMapping;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
-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.rib.rev130925.BgpRib;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
-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;
-import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeFactory;
+import org.opendaylight.protocol.concepts.KeyMapping;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-public class ParserToSalTest extends AbstractDataBrokerTest {
+@Ignore
+public class ParserToSalTest extends DefaultRibPoliciesMockTest {
 
     private static final String TEST_RIB_ID = "testRib";
-
-    private final String hex_messages = "/bgp_hex.txt";
-
+    private static final TablesKey TABLE_KEY
+            = new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
+    private static final InstanceIdentifier<BgpRib> BGP_IID = InstanceIdentifier.create(BgpRib.class);
+    private final IpAddress localAddress = new IpAddress(new Ipv4Address("127.0.0.1"));
     private BGPMock mock;
     private AbstractRIBExtensionProviderActivator baseact, lsact;
-    private RIBExtensionProviderContext ext;
-
-    @Mock
-    BGPDispatcher dispatcher;
-
-    @Mock
-    ReconnectStrategyFactory tcpStrategyFactory;
-
+    private RIBExtensionProviderContext ext1, ext2;
     @Mock
-    ReconnectStrategyFactory sessionStrategy;
-
-    BindingCodecTreeFactory codecFactory;
-
-    private SchemaService schemaService;
-
-
-    @Override
-    protected DataBrokerTestCustomizer createDataBrokerTestCustomizer() {
-        DataBrokerTestCustomizer customizer = super.createDataBrokerTestCustomizer();
-        codecFactory = customizer.getBindingToNormalized();
-        schemaService = customizer.getSchemaService();
-        return customizer;
-    }
-
-    @Override
-    protected void setupWithDataBroker(DataBroker dataBroker) {
-        MockitoAnnotations.initMocks(this);
-        final List<byte[]> bgpMessages;
-        try {
-            bgpMessages = HexDumpBGPFileParser.parseMessages(ParserToSalTest.class.getResourceAsStream(this.hex_messages));
-        } catch (IOException e) {
-            throw Throwables.propagate(e);
-        }
-        this.mock = new BGPMock(new EventBus("test"), ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry(), Lists.newArrayList(fixMessages(bgpMessages)));
-
-        Mockito.doReturn(GlobalEventExecutor.INSTANCE.newSucceededFuture(null)).when(this.dispatcher).createReconnectingClient(
-                Mockito.any(InetSocketAddress.class), Mockito.any(AsNumber.class),
-                Mockito.any(BGPPeerRegistry.class), Mockito.eq(this.tcpStrategyFactory), Mockito.eq(this.sessionStrategy),
-                Mockito.any(KeyMapping.class));
-
-        this.ext = new SimpleRIBExtensionProviderContext();
+    private BGPDispatcher dispatcher;
+    private CodecsRegistryImpl codecsRegistry;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        final String hexMessages = "/bgp_hex.txt";
+        final List<byte[]> bgpMessages = HexDumpBGPFileParser
+                .parseMessages(ParserToSalTest.class.getResourceAsStream(hexMessages));
+        this.mock = new BGPMock(new EventBus("test"), ServiceLoaderBGPExtensionProviderContext
+                .getSingletonInstance().getMessageRegistry(), Lists.newArrayList(fixMessages(bgpMessages)));
+
+        Mockito.doReturn(GlobalEventExecutor.INSTANCE.newSucceededFuture(null)).when(this.dispatcher)
+                .createReconnectingClient(any(InetSocketAddress.class), any(InetSocketAddress.class),
+                        anyInt(), any(KeyMapping.class));
+
+        this.ext1 = new SimpleRIBExtensionProviderContext();
+        this.ext2 = new SimpleRIBExtensionProviderContext();
         this.baseact = new RIBActivator();
-        this.lsact = new org.opendaylight.protocol.bgp.linkstate.RIBActivator();
+        this.lsact = new org.opendaylight.protocol.bgp.linkstate.impl.RIBActivator();
 
-        this.baseact.startRIBExtensionProvider(this.ext);
-        this.lsact.startRIBExtensionProvider(this.ext);
+        this.baseact.startRIBExtensionProvider(this.ext1, this.mappingService);
+        this.lsact.startRIBExtensionProvider(this.ext2, this.mappingService);
+        this.codecsRegistry = CodecsRegistryImpl.create(this.bindingCodecTreeFactory,
+                GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
     }
 
     @After
@@ -129,67 +104,69 @@ public class ParserToSalTest extends AbstractDataBrokerTest {
         this.baseact.close();
     }
 
-    private void runTestWithTables(final List<BgpTableType> tables) {
-        final RIBImpl rib = new RIBImpl(new RibId(TEST_RIB_ID), new AsNumber(72L), new Ipv4Address("127.0.0.1"), null, this.ext, this.dispatcher, this.tcpStrategyFactory, this.codecFactory, this.sessionStrategy, getDataBroker(), getDomBroker(), tables, GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
-        rib.onGlobalContextUpdated(schemaService.getGlobalContext());
-        final BGPPeer peer = new BGPPeer("peer-" + this.mock.toString(), rib);
-
-        final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
-        reg.close();
-    }
-
-    // FIXME: Re-enable once linkstate RIB is introduced.
     @Test
-    @Ignore
-    public void testWithLinkstate() throws InterruptedException, ExecutionException {
-        final List<BgpTableType> tables = ImmutableList.of(
-                (BgpTableType) new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class),
-                new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
-        runTestWithTables(tables);
+    public void testWithLinkstate() throws ReadFailedException {
+        final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(LinkstateAddressFamily.class,
+                LinkstateSubsequentAddressFamily.class));
+
+        final RIBImpl rib = new RIBImpl(this.tableRegistry, new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, this.ext2,
+                this.dispatcher, this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies,
+                tables, Collections.singletonMap(TABLE_KEY, BasePathSelectionModeFactory
+                .createBestPathSelectionStrategy()));
+        rib.instantiateServiceInstance();
         assertTablesExists(tables);
+        rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
+        final BGPPeer peer = AbstractAddPathTest.configurePeer(this.tableRegistry, this.localAddress.getIpv4Address(),
+                rib, null, PeerRole.Ibgp, new StrictBGPPeerRegistry());
+        peer.instantiateServiceInstance();
+        final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
+        reg.close();
     }
 
     @Test
-    public void testWithoutLinkstate() throws InterruptedException, ExecutionException {
-        final List<BgpTableType> tables = ImmutableList.of((BgpTableType) new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
-        runTestWithTables(tables);
+    public void testWithoutLinkstate() throws ReadFailedException {
+        final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(Ipv4AddressFamily.class,
+                UnicastSubsequentAddressFamily.class));
+        final RIBImpl rib = new RIBImpl(this.tableRegistry, new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, this.ext1,
+                this.dispatcher, this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies,
+                tables, Collections.singletonMap(TABLE_KEY,
+                BasePathSelectionModeFactory.createBestPathSelectionStrategy()));
+        rib.instantiateServiceInstance();
+        rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
         assertTablesExists(tables);
+        final BGPPeer peer = AbstractAddPathTest.configurePeer(this.tableRegistry, this.localAddress.getIpv4Address(),
+                rib, null, PeerRole.Ibgp, new StrictBGPPeerRegistry());
+        peer.instantiateServiceInstance();
+        final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
+        reg.close();
     }
 
-    private Collection<byte[]> fixMessages(final Collection<byte[]> bgpMessages) {
-        return Collections2.transform(bgpMessages, new Function<byte[], byte[]>() {
-
-            @Nullable
-            @Override
-            public byte[] apply(@Nullable final byte[] input) {
-                final byte[] ret = new byte[input.length + 1];
-                // ff
-                ret[0] = -1;
-                for (int i = 0; i < input.length; i++) {
-                    ret[i + 1] = input[i];
-                }
-                return ret;
-            }
+    private static Collection<byte[]> fixMessages(final Collection<byte[]> bgpMessages) {
+        return Collections2.transform(bgpMessages, input -> {
+            final byte[] ret = new byte[input.length + 1];
+            // ff
+            ret[0] = -1;
+            System.arraycopy(input, 0, ret, 1, input.length);
+            return ret;
         });
     }
 
-    private void assertTablesExists(final List<BgpTableType> expectedTables) throws InterruptedException, ExecutionException {
-        final Optional<LocRib> lockRib = getLocRibTable();
-        assertTrue(lockRib.isPresent());
-        final List<Tables> tables = lockRib.get().getTables();
-        assertFalse(tables.isEmpty());
-        for (final BgpTableType tableType : expectedTables) {
-            boolean found = false;
-            for (final Tables table : tables) {
-                if(table.getAfi().equals(tableType.getAfi()) && table.getSafi().equals(tableType.getSafi())) {
-                    found = true;
+    private void assertTablesExists(final List<BgpTableType> expectedTables) throws ReadFailedException {
+        readDataOperational(getDataBroker(), BGP_IID, bgpRib -> {
+            final List<Tables> tables = bgpRib.getRib().get(0).getLocRib().getTables();
+            assertFalse(tables.isEmpty());
+
+            for (final BgpTableType tableType : expectedTables) {
+                boolean found = false;
+                for (final Tables table : tables) {
+                    if (table.getAfi().equals(tableType.getAfi()) && table.getSafi().equals(tableType.getSafi())) {
+                        found = true;
+                        assertEquals(Boolean.TRUE, table.getAttributes().isUptodate());
+                    }
                 }
+                assertTrue(found);
             }
-            assertTrue(found);
-        }
-    }
-
-    private Optional<LocRib> getLocRibTable() throws InterruptedException, ExecutionException {
-        return getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(new RibId(TEST_RIB_ID))).child(LocRib.class).build()).get();
+            return bgpRib;
+        });
     }
 }