Bump versions to 0.21.8-SNAPSHOT
[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, 1);
97         assertEquals(UPD_NA_100, listener4.getListMsg().get(1));
98
99         //the second best route
100         sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 100, 1);
101         checkReceivedMessages(listener4, 2);
102         checkReceivedMessages(listener5, 1);
103
104         //new best route
105         sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 1);
106         checkReceivedMessages(listener4, 3);
107         checkReceivedMessages(listener5, 1);
108         assertEquals(UPD_NA_200, listener4.getListMsg().get(2));
109
110         final SimpleSessionListener listener6 = new SimpleSessionListener();
111         configurePeer(tableRegistry, PEER6, ribImpl, nonAddPathParams, PeerRole.RrClient,
112                 serverRegistry);
113         final BGPSessionImpl session6 = createPeerSession(PEER6, nonAddPathParams, listener6);
114
115         checkPeersPresentOnDataStore(6);
116         checkReceivedMessages(listener6, 2);
117         assertEquals(UPD_NA_200, listener6.getListMsg().get(1));
118         session6.close();
119         checkPeersPresentOnDataStore(5);
120
121         //best route updated to be the worse one
122         sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 20, 1);
123         checkReceivedMessages(listener4, 4);
124         checkReceivedMessages(listener5, 1);
125         assertEquals(UPD_NA_100, listener4.getListMsg().get(3));
126
127         //Remove second best, no advertisement should be done
128         sendWithdrawalRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 1);
129         checkReceivedMessages(listener4, 4);
130         checkReceivedMessages(listener5, 1);
131
132         //Remove best, 1 advertisement
133         sendWithdrawalRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1);
134         checkReceivedMessages(listener4, 5);
135         checkReceivedMessages(listener5, 1);
136
137         //Remove best, 1 withdrawal
138         sendWithdrawalRouteAndCheckIsOnLocRib(session3, PREFIX1, 20, 0);
139         checkReceivedMessages(listener4, 6);
140         checkReceivedMessages(listener5, 1);
141
142         session1.close();
143         session2.close();
144         session3.close();
145         session4.close();
146         session5.close();
147     }
148 }