Bump upstreams to 2022.09
[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.VALUE, UnicastSubsequentAddressFamily.VALUE);
41         final Map<TablesKey, PathSelectionMode> pathTables = ImmutableMap.of(tk,
42             BasePathSelectionModeFactory.createBestPathSelectionStrategy());
43
44         ribImpl = new RIBImpl(tableRegistry, new RibId("test-rib"), AS_NUMBER, new BgpId(RIB_ID), ribExtension,
45                 serverDispatcher, codecsRegistry, getDomBroker(), policies, TABLES_TYPE, pathTables);
46         ribImpl.instantiateServiceInstance();
47         final ChannelFuture channelFuture = serverDispatcher.createServer(
48             new InetSocketAddress(RIB_ID, PORT.toJava()));
49         waitFutureSuccess(channelFuture);
50         serverChannel = channelFuture.channel();
51     }
52
53     @Override
54     @After
55     public void tearDown() throws Exception {
56         waitFutureSuccess(serverChannel.close());
57         super.tearDown();
58     }
59
60     /*
61      * Base-Paths
62      *                                            ___________________
63      *                                           | ODL BGP 127.0.0.1 |
64      * [peer://127.0.0.2; p1, lp100] --(iBGP)--> |                   | --(RR-client, non add-path) -->
65      * [peer://127.0.0.3; p1, lp200] --(iBGP)--> |                   |    [Peer://127.0.0.5; (p1, lp100), (p1, lp1200)]
66      * [peer://127.0.0.4; p1, lp50] --(iBGP)-->  |                   | --(eBgp, non add-path) -->
67      * [peer://127.0.0.2; p1, lp20] --(iBGP)-->  |___________________|    [Peer://127.0.0.6; (p1, path-id1, lp100),
68      * p1 = 1.1.1.1/32                                                      (p1, path-id2, pl50), (p1, path-id3, pl200)]
69      */
70     @Test
71     public void testUseCase1() throws Exception {
72         final BgpParameters nonAddPathParams = createParameter(false);
73
74         configurePeer(tableRegistry, PEER1, ribImpl, nonAddPathParams, PeerRole.Ibgp, serverRegistry);
75         final BGPSessionImpl session1 = createPeerSession(PEER1, nonAddPathParams, new SimpleSessionListener());
76
77         configurePeer(tableRegistry, PEER2, ribImpl, nonAddPathParams, PeerRole.Ibgp, serverRegistry);
78         final BGPSessionImpl session2 = createPeerSession(PEER2, nonAddPathParams, new SimpleSessionListener());
79
80         configurePeer(tableRegistry, PEER3, ribImpl, nonAddPathParams, PeerRole.Ibgp, serverRegistry);
81         final BGPSessionImpl session3 = createPeerSession(PEER3,nonAddPathParams, new SimpleSessionListener());
82
83         final SimpleSessionListener listener4 = new SimpleSessionListener();
84         configurePeer(tableRegistry, PEER4, ribImpl, nonAddPathParams, PeerRole.RrClient,
85                 serverRegistry);
86         final BGPSessionImpl session4 = createPeerSession(PEER4, nonAddPathParams, listener4);
87
88         final SimpleSessionListener listener5 = new SimpleSessionListener();
89         configurePeer(tableRegistry, PEER5, ribImpl, nonAddPathParams, PeerRole.Ebgp, serverRegistry);
90         final BGPSessionImpl session5 = createPeerSession(PEER5, nonAddPathParams, listener5);
91         checkPeersPresentOnDataStore(5);
92
93         //new best route so far
94         sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1);
95         checkReceivedMessages(listener4, 2);
96         checkReceivedMessages(listener5, 2);
97         assertEquals(UPD_NA_100, listener4.getListMsg().get(1));
98         assertEquals(UPD_NA_100_EBGP, listener5.getListMsg().get(1));
99
100         //the second best route
101         sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 100, 1);
102         checkReceivedMessages(listener4, 2);
103         checkReceivedMessages(listener5, 2);
104
105         //new best route
106         sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 1);
107         checkReceivedMessages(listener4, 3);
108         checkReceivedMessages(listener5, 3);
109         assertEquals(UPD_NA_200, listener4.getListMsg().get(2));
110         assertEquals(UPD_NA_200_EBGP, listener5.getListMsg().get(2));
111
112         final SimpleSessionListener listener6 = new SimpleSessionListener();
113         configurePeer(tableRegistry, PEER6, ribImpl, nonAddPathParams, PeerRole.RrClient,
114                 serverRegistry);
115         final BGPSessionImpl session6 = createPeerSession(PEER6, nonAddPathParams, listener6);
116
117         checkPeersPresentOnDataStore(6);
118         checkReceivedMessages(listener6, 2);
119         assertEquals(UPD_NA_200, listener6.getListMsg().get(1));
120         session6.close();
121         checkPeersPresentOnDataStore(5);
122
123         //best route updated to be the worse one
124         sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 20, 1);
125         checkReceivedMessages(listener4, 4);
126         checkReceivedMessages(listener5, 4);
127         assertEquals(UPD_NA_100, listener4.getListMsg().get(3));
128         assertEquals(UPD_NA_100_EBGP, listener5.getListMsg().get(3));
129
130         //Remove second best, no advertisement should be done
131         sendWithdrawalRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 1);
132         checkReceivedMessages(listener4, 4);
133         checkReceivedMessages(listener5, 4);
134
135         //Remove best, 1 advertisement
136         sendWithdrawalRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1);
137         checkReceivedMessages(listener4, 5);
138         checkReceivedMessages(listener5, 5);
139
140         //Remove best, 1 withdrawal
141         sendWithdrawalRouteAndCheckIsOnLocRib(session3, PREFIX1, 20, 0);
142         checkReceivedMessages(listener4, 6);
143         checkReceivedMessages(listener5, 6);
144
145         session1.close();
146         session2.close();
147         session3.close();
148         session4.close();
149         session5.close();
150     }
151 }