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