Update MRI projects for Aluminium
[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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages;
12 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
13
14 import com.google.common.collect.ImmutableMap;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import java.net.InetSocketAddress;
18 import java.util.Map;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
23 import org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectionModeFactory;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
31
32 public class AddPathBasePathsTest extends AbstractAddPathTest {
33     private RIBImpl ribImpl;
34     private Channel serverChannel;
35
36     @Override
37     @Before
38     public void setUp() throws Exception {
39         super.setUp();
40         final TablesKey tk = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
41         final Map<TablesKey, PathSelectionMode> pathTables = ImmutableMap.of(tk,
42             BasePathSelectionModeFactory.createBestPathSelectionStrategy());
43
44         this.ribImpl = new RIBImpl(this.tableRegistry, new RibId("test-rib"), AS_NUMBER, new BgpId(RIB_ID),
45                 this.ribExtension,
46                 this.serverDispatcher, this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies,
47                 TABLES_TYPE, pathTables);
48         this.ribImpl.instantiateServiceInstance();
49         final ChannelFuture channelFuture = this.serverDispatcher.createServer(
50             new InetSocketAddress(RIB_ID, PORT.toJava()));
51         waitFutureSuccess(channelFuture);
52         this.serverChannel = channelFuture.channel();
53     }
54
55     @Override
56     @After
57     public void tearDown() throws Exception {
58         waitFutureSuccess(this.serverChannel.close());
59         super.tearDown();
60     }
61
62     /*
63      * Base-Paths
64      *                                            ___________________
65      *                                           | ODL BGP 127.0.0.1 |
66      * [peer://127.0.0.2; p1, lp100] --(iBGP)--> |                   | --(RR-client, non add-path) -->
67      * [peer://127.0.0.3; p1, lp200] --(iBGP)--> |                   |    [Peer://127.0.0.5; (p1, lp100), (p1, lp1200)]
68      * [peer://127.0.0.4; p1, lp50] --(iBGP)-->  |                   | --(eBgp, non add-path) -->
69      * [peer://127.0.0.2; p1, lp20] --(iBGP)-->  |___________________|    [Peer://127.0.0.6; (p1, path-id1, lp100),
70      * p1 = 1.1.1.1/32                                                      (p1, path-id2, pl50), (p1, path-id3, pl200)]
71      */
72     @Test
73     public void testUseCase1() throws Exception {
74         final BgpParameters nonAddPathParams = createParameter(false);
75
76         configurePeer(this.tableRegistry, PEER1, this.ribImpl, nonAddPathParams, PeerRole.Ibgp, this.serverRegistry);
77         final BGPSessionImpl session1 = createPeerSession(PEER1, nonAddPathParams, new SimpleSessionListener());
78
79         configurePeer(this.tableRegistry, PEER2, this.ribImpl, nonAddPathParams, PeerRole.Ibgp, this.serverRegistry);
80         final BGPSessionImpl session2 = createPeerSession(PEER2, nonAddPathParams, new SimpleSessionListener());
81
82         configurePeer(this.tableRegistry, PEER3, this.ribImpl, nonAddPathParams, PeerRole.Ibgp, this.serverRegistry);
83         final BGPSessionImpl session3 = createPeerSession(PEER3,nonAddPathParams, new SimpleSessionListener());
84
85         final SimpleSessionListener listener4 = new SimpleSessionListener();
86         configurePeer(this.tableRegistry, PEER4, this.ribImpl, nonAddPathParams, PeerRole.RrClient,
87                 this.serverRegistry);
88         final BGPSessionImpl session4 = createPeerSession(PEER4, nonAddPathParams, listener4);
89
90         final SimpleSessionListener listener5 = new SimpleSessionListener();
91         configurePeer(this.tableRegistry, PEER5, this.ribImpl, nonAddPathParams, PeerRole.Ebgp, this.serverRegistry);
92         final BGPSessionImpl session5 = createPeerSession(PEER5, nonAddPathParams, listener5);
93         checkPeersPresentOnDataStore(5);
94
95         //new best route so far
96         sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1);
97         checkReceivedMessages(listener4, 2);
98         checkReceivedMessages(listener5, 2);
99         assertEquals(UPD_NA_100, listener4.getListMsg().get(1));
100         assertEquals(UPD_NA_100_EBGP, listener5.getListMsg().get(1));
101
102         //the second best route
103         sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 100, 1);
104         checkReceivedMessages(listener4, 2);
105         checkReceivedMessages(listener5, 2);
106
107         //new best route
108         sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 1);
109         checkReceivedMessages(listener4, 3);
110         checkReceivedMessages(listener5, 3);
111         assertEquals(UPD_NA_200, listener4.getListMsg().get(2));
112         assertEquals(UPD_NA_200_EBGP, listener5.getListMsg().get(2));
113
114         final SimpleSessionListener listener6 = new SimpleSessionListener();
115         configurePeer(this.tableRegistry, PEER6, this.ribImpl, nonAddPathParams, PeerRole.RrClient,
116                 this.serverRegistry);
117         final BGPSessionImpl session6 = createPeerSession(PEER6, nonAddPathParams, listener6);
118
119         checkPeersPresentOnDataStore(6);
120         checkReceivedMessages(listener6, 2);
121         assertEquals(UPD_NA_200, listener6.getListMsg().get(1));
122         session6.close();
123         checkPeersPresentOnDataStore(5);
124
125         //best route updated to be the worse one
126         sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 20, 1);
127         checkReceivedMessages(listener4, 4);
128         checkReceivedMessages(listener5, 4);
129         assertEquals(UPD_NA_100, listener4.getListMsg().get(3));
130         assertEquals(UPD_NA_100_EBGP, listener5.getListMsg().get(3));
131
132         //Remove second best, no advertisement should be done
133         sendWithdrawalRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 1);
134         checkReceivedMessages(listener4, 4);
135         checkReceivedMessages(listener5, 4);
136
137         //Remove best, 1 advertisement
138         sendWithdrawalRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1);
139         checkReceivedMessages(listener4, 5);
140         checkReceivedMessages(listener5, 5);
141
142         //Remove best, 1 withdrawal
143         sendWithdrawalRouteAndCheckIsOnLocRib(session3, PREFIX1, 20, 0);
144         checkReceivedMessages(listener4, 6);
145         checkReceivedMessages(listener5, 6);
146
147         session1.close();
148         session2.close();
149         session3.close();
150         session4.close();
151         session5.close();
152     }
153 }