BUG-7003: Remove Thread.sleep() calls in tests
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / AddPathAllPathsTest.java
1 /*
2  * Copyright (c) 2016 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.assertEquals;
11 import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkReceivedMessages;
12 import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.waitFutureSuccess;
13
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import java.net.InetSocketAddress;
17 import java.util.List;
18 import java.util.Map;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
21 import org.opendaylight.protocol.bgp.mode.impl.add.all.paths.AllPathSelection;
22 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
32
33 public class AddPathAllPathsTest extends AbstractAddPathTest {
34     /*
35      * All-Paths
36      *                                            ___________________
37      *                                           | ODL BGP 127.0.0.1 |
38      * [peer://127.0.0.2; p1, lp100] --(iBGP)--> |                   | --(RR-client, non add-path) --> [Peer://127.0.0.5; (p1, lp100), (p1, lp1200)]
39      * [peer://127.0.0.3; p1, lp200] --(iBGP)--> |                   |
40      * [peer://127.0.0.4; p1, lp50] --(iBGP)-->  |                   | --(RR-client, add-path) --> [Peer://127.0.0.6; (p1, path-id1, lp100), (p1, path-id2, pl50), (p1, path-id3, pl200), (p1, path-id4, pl20)]
41      * [peer://127.0.0.2; p1, lp20] --(iBGP)-->  |___________________|
42      * p1 = 1.1.1.1/32
43      */
44     @Test
45     public void testUseCase1() throws Exception {
46
47         final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
48         final TablesKey tk = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
49         final Map<TablesKey, PathSelectionMode> pathTables = ImmutableMap.of(tk, new AllPathSelection());
50
51
52         final RIBImpl ribImpl = new RIBImpl(this.clusterSingletonServiceProvider, new RibId("test-rib"), AS_NUMBER, new BgpId(RIB_ID), null, this.ribExtension,
53                 this.dispatcher, this.mappingService.getCodecFactory(), getDomBroker(), tables, pathTables, this.ribExtension.getClassLoadingStrategy(), null);
54
55         ribImpl.instantiateServiceInstance();
56         ribImpl.onGlobalContextUpdated(this.schemaContext);
57
58         waitFutureSuccess(this.dispatcher.createServer(StrictBGPPeerRegistry.GLOBAL, new InetSocketAddress(RIB_ID, PORT)));
59         final BgpParameters nonAddPathParams = createParameter(false);
60         final BgpParameters addPathParams = createParameter(true);
61
62         final BGPSessionImpl session1 = createPeerSession(PEER1, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener());
63         final BGPSessionImpl session2 = createPeerSession(PEER2, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener());
64         final BGPSessionImpl session3 = createPeerSession(PEER3, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener());
65         final SimpleSessionListener listener4 = new SimpleSessionListener();
66         final BGPSessionImpl session4 = createPeerSession(PEER4, PeerRole.RrClient, nonAddPathParams, ribImpl, listener4);
67         final SimpleSessionListener listener5 = new SimpleSessionListener();
68         final BGPSessionImpl session5 = createPeerSession(PEER5, PeerRole.RrClient, addPathParams, ribImpl, listener5);
69         checkPeersPresentOnDataStore(5);
70
71         //the best route
72         sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1);
73         checkReceivedMessages(listener4, 1);
74         checkReceivedMessages(listener5, 1);
75         assertEquals(UPD_100, listener5.getListMsg().get(0));
76
77         final SimpleSessionListener listener6 = new SimpleSessionListener();
78         final BGPSessionImpl session6 = createPeerSession(PEER6, PeerRole.RrClient, nonAddPathParams, ribImpl, listener6);
79         checkPeersPresentOnDataStore(6);
80         checkReceivedMessages(listener6, 1);
81         assertEquals(UPD_NA_100, listener6.getListMsg().get(0));
82         session6.close();
83         checkPeersPresentOnDataStore(5);
84
85         //the second best route
86         sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 2);
87         checkReceivedMessages(listener4, 1);
88         checkReceivedMessages(listener5, 2);
89         assertEquals(UPD_50, listener5.getListMsg().get(1));
90
91         //new best route
92         sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 3);
93         checkReceivedMessages(listener4, 2);
94         checkReceivedMessages(listener5, 3);
95         assertEquals(UPD_200, listener5.getListMsg().get(2));
96
97         //the worst route
98         sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 20, 3);
99         checkReceivedMessages(listener4, 2);
100         checkReceivedMessages(listener5, 4);
101         assertEquals(UPD_200.getAttributes().getLocalPref(), ((Update) listener4.getListMsg().get(1)).getAttributes().getLocalPref());
102         assertEquals(UPD_20, listener5.getListMsg().get(3));
103
104         //withdraw second best route, 1 advertisement(1 withdrawal) for add-path supported, none for non add path
105         sendWithdrawalRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 2);
106         checkReceivedMessages(listener4, 2);
107         checkReceivedMessages(listener5, 5);
108
109         //we advertise again to try new test
110         sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 3);
111         checkReceivedMessages(listener4, 2);
112         checkReceivedMessages(listener5, 6);
113         assertEquals(UPD_200, listener5.getListMsg().get(2));
114
115         //withdraw second best route, 1 advertisement(1 withdrawal) for add-path supported, 1 for non add path (withdrawal)
116         sendWithdrawalRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 2);
117         checkReceivedMessages(listener4, 3);
118         checkReceivedMessages(listener5, 7);
119
120         session1.close();
121         session2.close();
122         session3.close();
123         session4.close();
124         session5.close();
125     }
126 }