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