Bump upstreams to 2022.09
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / AddPathNPathsTest.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 io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import java.net.InetSocketAddress;
17 import java.util.Map;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
22 import org.opendaylight.protocol.bgp.mode.impl.add.n.paths.AddPathBestNPathSelection;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
30
31 public class AddPathNPathsTest extends AbstractAddPathTest {
32     private RIBImpl ribImpl;
33     private Channel serverChannel;
34
35     @Override
36     @Before
37     public void setUp() throws Exception {
38         super.setUp();
39         final TablesKey tk = new TablesKey(Ipv4AddressFamily.VALUE, UnicastSubsequentAddressFamily.VALUE);
40         final Map<TablesKey, PathSelectionMode> pathTables = Map.of(tk, new AddPathBestNPathSelection(2));
41
42         ribImpl = new RIBImpl(tableRegistry, new RibId("test-rib"), AS_NUMBER, new BgpId(RIB_ID), ribExtension,
43                 serverDispatcher, codecsRegistry, getDomBroker(), policies, TABLES_TYPE, pathTables);
44
45         ribImpl.instantiateServiceInstance();
46         final ChannelFuture channelFuture = serverDispatcher.createServer(
47             new InetSocketAddress(RIB_ID, PORT.toJava()));
48         waitFutureSuccess(channelFuture);
49         serverChannel = channelFuture.channel();
50     }
51
52     @Override
53     @After
54     public void tearDown() throws Exception {
55         waitFutureSuccess(serverChannel.close());
56         super.tearDown();
57     }
58
59     /*
60      * N-Paths
61      *                                            ___________________
62      *                                           | ODL BGP 127.0.0.1 |
63      * [peer://127.0.0.2; p1, lp100] --(iBGP)--> |                   | --(RR-client, non add-path) -->
64      * [peer://127.0.0.3; p1, lp200] --(iBGP)--> |                   |     [Peer://127.0.0.5; (p1, lp100), (p1, lp1200)]
65      * [peer://127.0.0.4; p1, lp50] --(iBGP)-->  |                   | --(RR-client, add-path) -->
66      * [peer://127.0.0.2; p1, lp20] --(iBGP)-->  |___________________|     [Peer://127.0.0.6; (p1, path-id1, lp100),
67      * p1 = 1.1.1.1/32                                                      (p1, path-id2, pl50), (p1, path-id3, pl200)]
68      */
69     @Test
70     public void testUseCase1() throws Exception {
71         final BgpParameters nonAddPathParams = createParameter(false);
72         final BgpParameters addPathParams = createParameter(true);
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, addPathParams, PeerRole.RrClient, serverRegistry);
90         final BGPSessionImpl session5 = createPeerSession(PEER5, addPathParams, 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_100, listener5.getListMsg().get(1));
98
99         final SimpleSessionListener listener6 = new SimpleSessionListener();
100         configurePeer(tableRegistry, PEER6, ribImpl, nonAddPathParams, PeerRole.RrClient,
101                 serverRegistry);
102         final BGPSessionImpl session6 = createPeerSession(PEER6, nonAddPathParams, listener6);
103         checkPeersPresentOnDataStore(6);
104         checkReceivedMessages(listener6, 2);
105         assertEquals(UPD_NA_100, listener6.getListMsg().get(1));
106         session6.close();
107         checkPeersPresentOnDataStore(5);
108
109         //the second best route
110         sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 2);
111         checkReceivedMessages(listener4, 2);
112         checkReceivedMessages(listener5, 3);
113         assertEquals(UPD_50, listener5.getListMsg().get(2));
114
115         //new best route
116         sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 2);
117         checkReceivedMessages(listener4, 3);
118         checkReceivedMessages(listener5, 4);
119         assertEquals(UPD_200, listener5.getListMsg().get(3));
120
121         //the worst prefix, no changes
122         sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 20, 2);
123         checkReceivedMessages(listener4, 3);
124         checkReceivedMessages(listener5, 4);
125
126         //withdraw second best route, 2 advertisement (1 withdrawal) for add-path supported, none for non add path
127         sendWithdrawalRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 2);
128         checkReceivedMessages(listener4, 3);
129         checkReceivedMessages(listener5, 6);
130
131         //we advertise again to try new test
132         sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 2);
133         checkReceivedMessages(listener4, 3);
134         checkReceivedMessages(listener5, 7);
135
136         //withdraw second best route, 2 advertisement (1 withdrawal) for add-path supported,
137         // 1 withdrawal for non add path
138         sendWithdrawalRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 2);
139         checkReceivedMessages(listener4, 4);
140         checkReceivedMessages(listener5, 9);
141
142         session1.close();
143         session2.close();
144         session3.close();
145         session4.close();
146         session5.close();
147     }
148 }