BUG-6747: Race condition on peer connection
[bgpcep.git] / bgp / path-selection-mode / src / test / java / org / opendaylight / protocol / bgp / mode / impl / add / all / paths / SimpleRouteEntryTest.java
1 /*
2  * Copyright (c) 2015 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.mode.impl.add.all.paths;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.Map;
15 import java.util.function.Function;
16 import java.util.stream.Collectors;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.mode.impl.AbstractRouteEntryTest;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21
22 public final class SimpleRouteEntryTest extends AbstractRouteEntryTest {
23     private SimpleRouteEntry testBARE;
24
25     @Before
26     public void setUp() {
27         super.setUp();
28     }
29
30     @Test
31     public void testSimpleRouteEntry() throws Exception {
32         this.testBARE = (SimpleRouteEntry) new AllPathSelection().createRouteEntry(false);
33         testAddRouteSelectBestAndWriteOnDS();
34         testRemoveRoute();
35     }
36
37     private void testAddRouteSelectBestAndWriteOnDS() {
38         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(), this.attributes);
39         assertFalse(this.testBARE.isEmpty());
40         assertTrue(this.testBARE.selectBest(AS));
41         /** Add AddPath Route **/
42         this.testBARE.updateRoute(TABLES_KEY, this.peerPT, LOC_RIB_TARGET, this.ribSupport, this.tx, ROUTE_ID_PA_ADD_PATH);
43         Map<YangInstanceIdentifier, Long> yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
44         assertEquals(3, yiiCount.size());
45         assertEquals(1, (long) yiiCount.get(this.routePaAddPathYii));
46         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutYii));
47         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutAttYii));
48     }
49
50     private void testRemoveRoute() {
51         Map<YangInstanceIdentifier, Long> yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
52         assertEquals(3, yiiCount.size());
53         assertEquals(1, (long) yiiCount.get(this.routePaAddPathYii));
54         assertTrue(this.testBARE.removeRoute(ROUTER_ID, REMOTE_PATH_ID));
55         assertTrue(this.testBARE.selectBest(AS));
56         this.testBARE.updateRoute(TABLES_KEY, this.peerPT, LOC_RIB_TARGET, this.ribSupport, this.tx, ROUTE_ID_PA_ADD_PATH);
57         yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
58         assertEquals(0, yiiCount.size());
59         assertFalse(yiiCount.containsKey(this.routePaAddPathYii));
60         assertFalse(yiiCount.containsKey(this.routeAddRiboutYii));
61     }
62 }