Migrate deprecated submit() to commit() for BGP/BMP
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / ParserToSalTest.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.rib.impl;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyInt;
14 import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.AS_NUMBER;
15 import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.BGP_ID;
16 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
17
18 import com.google.common.collect.Collections2;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Lists;
21 import com.google.common.eventbus.EventBus;
22 import io.netty.util.concurrent.GlobalEventExecutor;
23 import java.net.InetSocketAddress;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
35 import org.opendaylight.protocol.bgp.inet.RIBActivator;
36 import org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectionModeFactory;
37 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
38 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
40 import org.opendaylight.protocol.bgp.rib.mock.BGPMock;
41 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBExtensionProviderActivator;
42 import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker;
43 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
44 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
45 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
46 import org.opendaylight.protocol.concepts.KeyMapping;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61
62 @Ignore
63 public class ParserToSalTest extends DefaultRibPoliciesMockTest {
64
65     private static final String TEST_RIB_ID = "testRib";
66     private static final TablesKey TABLE_KEY
67             = new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
68     private static final InstanceIdentifier<BgpRib> BGP_IID = InstanceIdentifier.create(BgpRib.class);
69     private BGPMock mock;
70     private AbstractRIBExtensionProviderActivator baseact, lsact;
71     private RIBExtensionProviderContext ext1, ext2;
72     private final IpAddress localAddress = new IpAddress(new Ipv4Address("127.0.0.1"));
73
74     @Mock
75     private BGPDispatcher dispatcher;
76     private BGPPeerTracker peerTracker = new BGPPeerTrackerImpl();
77     private CodecsRegistryImpl codecsRegistry;
78
79     @Before
80     public void setUp() throws Exception {
81         super.setUp();
82         final String hexMessages = "/bgp_hex.txt";
83         final List<byte[]> bgpMessages = HexDumpBGPFileParser
84                 .parseMessages(ParserToSalTest.class.getResourceAsStream(hexMessages));
85         this.mock = new BGPMock(new EventBus("test"), ServiceLoaderBGPExtensionProviderContext
86                 .getSingletonInstance().getMessageRegistry(), Lists.newArrayList(fixMessages(bgpMessages)));
87
88         Mockito.doReturn(GlobalEventExecutor.INSTANCE.newSucceededFuture(null)).when(this.dispatcher)
89                 .createReconnectingClient(any(InetSocketAddress.class), any(InetSocketAddress.class),
90                         anyInt(), any(KeyMapping.class));
91
92         this.ext1 = new SimpleRIBExtensionProviderContext();
93         this.ext2 = new SimpleRIBExtensionProviderContext();
94         this.baseact = new RIBActivator();
95         this.lsact = new org.opendaylight.protocol.bgp.linkstate.impl.RIBActivator();
96
97         this.baseact.startRIBExtensionProvider(this.ext1, this.mappingService);
98         this.lsact.startRIBExtensionProvider(this.ext2, this.mappingService);
99         this.codecsRegistry = CodecsRegistryImpl.create(this.bindingCodecTreeFactory,
100                 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
101     }
102
103     @After
104     public void tearDown() {
105         this.lsact.close();
106         this.baseact.close();
107     }
108
109     @Test
110     public void testWithLinkstate() throws ReadFailedException {
111         final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(LinkstateAddressFamily.class,
112                 LinkstateSubsequentAddressFamily.class));
113
114         final RIBImpl rib = new RIBImpl(new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, this.ext2, this.dispatcher,
115                 this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies, this.peerTracker, tables,
116                 Collections.singletonMap(TABLE_KEY, BasePathSelectionModeFactory
117                         .createBestPathSelectionStrategy(this.peerTracker)));
118         rib.instantiateServiceInstance();
119         assertTablesExists(tables);
120         rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
121         final BGPPeer peer = new BGPPeer(this.localAddress, rib, PeerRole.Ibgp, null, Collections.emptySet(),
122                 Collections.emptySet());
123         peer.instantiateServiceInstance();
124         final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
125         reg.close();
126     }
127
128     @Test
129     public void testWithoutLinkstate() throws ReadFailedException {
130         final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(Ipv4AddressFamily.class,
131                 UnicastSubsequentAddressFamily.class));
132         final RIBImpl rib = new RIBImpl(new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, this.ext1, this.dispatcher,
133                 this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies, this.peerTracker, tables,
134                 Collections.singletonMap(TABLE_KEY, BasePathSelectionModeFactory
135                         .createBestPathSelectionStrategy(this.peerTracker)));
136         rib.instantiateServiceInstance();
137         rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
138         assertTablesExists(tables);
139         final BGPPeer peer = new BGPPeer(this.localAddress, rib, PeerRole.Ibgp, null, Collections.emptySet(),
140                 Collections.emptySet());
141         peer.instantiateServiceInstance();
142         final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
143         reg.close();
144     }
145
146     private static Collection<byte[]> fixMessages(final Collection<byte[]> bgpMessages) {
147         return Collections2.transform(bgpMessages, input -> {
148             final byte[] ret = new byte[input.length + 1];
149             // ff
150             ret[0] = -1;
151             System.arraycopy(input, 0, ret, 1, input.length);
152             return ret;
153         });
154     }
155
156     private void assertTablesExists(final List<BgpTableType> expectedTables) throws ReadFailedException {
157         readDataOperational(getDataBroker(), BGP_IID, bgpRib -> {
158             final List<Tables> tables = bgpRib.getRib().get(0).getLocRib().getTables();
159             assertFalse(tables.isEmpty());
160
161             for (final BgpTableType tableType : expectedTables) {
162                 boolean found = false;
163                 for (final Tables table : tables) {
164                     if (table.getAfi().equals(tableType.getAfi()) && table.getSafi().equals(tableType.getSafi())) {
165                         found = true;
166                         assertTrue(Boolean.valueOf(true).equals(table.getAttributes().isUptodate()));
167                     }
168                 }
169                 assertTrue(found);
170             }
171             return bgpRib;
172         });
173     }
174 }