BUG-6747: Race condition on peer connection
[bgpcep.git] / bgp / path-selection-mode / src / test / java / org / opendaylight / protocol / bgp / mode / impl / base / BaseRouteEntryTest.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.mode.impl.base;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
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 class BaseRouteEntryTest extends AbstractRouteEntryTest {
23
24     private BaseSimpleRouteEntry testBARE;
25
26     @Before
27     public void setUp() {
28         super.setUp();
29     }
30
31     @Test
32     public void testBaseSimpleRouteEntry() throws Exception {
33         this.testBARE = new BaseSimpleRouteEntry();
34         testWriteEmptyBestPath();
35         testAddRouteSelectBestAndWriteOnDS();
36         testRewriteSameRoute();
37         testInitializePeerWithExistentRoute();
38         testRemoveRoute();
39     }
40
41     private void testRemoveRoute() {
42         Map<YangInstanceIdentifier, Long> yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
43         assertEquals(8, (long) yiiCount.size());
44         assertEquals(1, (long) yiiCount.get(this.routePaYii));
45         this.testBARE.removeRoute(ROUTER_ID, REMOTE_PATH_ID);
46         this.testBARE.selectBest(AS);
47         this.testBARE.updateRoute(TABLES_KEY, this.peerPT, LOC_RIB_TARGET, this.ribSupport, this.tx, ROUTE_ID_PA);
48         yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
49         assertFalse(yiiCount.containsKey(this.routePaYii));
50         assertFalse(yiiCount.containsKey(this.routeAddRiboutAttYii));
51     }
52
53     private void testInitializePeerWithExistentRoute() {
54         this.testBARE.writeRoute(PEER_ID, ROUTE_ID_PA, PEER_YII2, this.peg, TABLES_KEY, this.peerPT, this.ribSupport, this.tx);
55         assertEquals(8, this.yIIChanges.size());
56         final Map<YangInstanceIdentifier, Long> yiiCount = this.yIIChanges.stream()
57             .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
58         assertEquals(1, (long) yiiCount.get(this.routeRiboutYiiPeer2));
59         assertEquals(1, (long) yiiCount.get(this.routeRiboutAttYiiPeer2));
60     }
61
62     private void testRewriteSameRoute() {
63         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(), this.attributes);
64         assertEquals(1, this.testBARE.getOffsets().size());
65         assertFalse(this.testBARE.selectBest(AS));
66     }
67
68     private void testAddRouteSelectBestAndWriteOnDS() {
69         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(), this.attributes);
70         assertFalse(this.testBARE.getOffsets().isEmpty());
71         this.testBARE.selectBest(AS);
72         this.testBARE.updateRoute(TABLES_KEY, this.peerPT, LOC_RIB_TARGET, this.ribSupport, this.tx, ROUTE_ID_PA);
73         Map<YangInstanceIdentifier, Long> yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
74         assertEquals(3, yiiCount.size());
75         assertEquals(1, (long) yiiCount.get(this.routePaYii));
76         assertEquals(1, (long) yiiCount.get(this.routeRiboutYii));
77         assertEquals(1, (long) yiiCount.get(this.routeRiboutAttYii));
78         this.testBARE.updateRoute(TABLES_KEY, this.peerPT, LOC_RIB_TARGET, this.ribSupport, this.tx, ROUTE_ID_PA_ADD_PATH);
79         yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
80         assertEquals(6, yiiCount.size());
81         assertEquals(1, (long) yiiCount.get(this.routePaAddPathYii));
82         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutYii));
83         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutAttYii));
84     }
85
86     private void testWriteEmptyBestPath() {
87         this.testBARE.writeRoute(PEER_ID, ROUTE_ID_PA, PEER_YII2, this.peg, TABLES_KEY, this.peerPT, this.ribSupport, this.tx);
88         assertEquals(0, this.yIIChanges.size());
89     }
90 }