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