092bb4a16aa3d0c5eaf787256a2f9f442bf73ed0
[bgpcep.git] / bgp / path-selection-mode / src / test / java / org / opendaylight / protocol / bgp / mode / impl / add / n / 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.n.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 final static long N_PATHS = 2;
24     private SimpleRouteEntry testBARE;
25
26     @Before
27     public void setUp() {
28         super.setUp();
29     }
30
31     @Test
32     public void testSimpleRouteEntry() throws Exception {
33         this.testBARE = (SimpleRouteEntry) new AddPathBestNPathSelection(N_PATHS).createRouteEntry(false);
34         testWriteEmptyBestPath();
35         testAddRouteSelectBestAndWriteOnDS();
36         testRewriteSameRoute();
37         testInitializePeerWithExistentRoute();
38         testRemoveRoute();
39     }
40
41     @Test(expected = NullPointerException.class)
42     public void testAddRouteSelectBestAndWriteOnDSs() {
43         /** Add non Add Path Route **/
44         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(), this.attributes);
45     }
46
47     private void testWriteEmptyBestPath() {
48         this.testBARE.writeRoute(PEER_ID, ROUTE_ID_PA, PEER_YII2, this.peg, TABLES_KEY, this.peerPT, this.ribSupport, this.discCache, this.tx);
49         assertEquals(0, this.yIIChanges.size());
50     }
51
52     private void testAddRouteSelectBestAndWriteOnDS() {
53         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(), this.attributes);
54         assertFalse(this.testBARE.isEmpty());
55         assertTrue(this.testBARE.selectBest(AS));
56         /** Add AddPath Route **/
57         this.testBARE.updateRoute(TABLES_KEY, this.peerPT, LOC_RIB_TARGET, this.ribSupport, this.discCache, this.tx, ROUTE_ID_PA_ADD_PATH);
58         Map<YangInstanceIdentifier, Long> yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
59         assertEquals(3, yiiCount.size());
60         assertEquals(1, (long) yiiCount.get(this.routePaAddPathYii));
61         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutYii));
62         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutAttYii));
63     }
64
65     private void testRewriteSameRoute() {
66         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(), this.attributes);
67         assertFalse(this.testBARE.selectBest(AS));
68     }
69
70     private void testInitializePeerWithExistentRoute() {
71         assertEquals(3, this.yIIChanges.size());
72         this.testBARE.writeRoute(PEER_ID, ROUTE_ID_PA_ADD_PATH, PEER_YII2, this.peg, TABLES_KEY, this.peerPT, this.ribSupport, this.discCache, this.tx);
73         assertEquals(5, this.yIIChanges.size());
74         final Map<YangInstanceIdentifier, Long> yiiCount = this.yIIChanges.stream()
75             .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
76         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutYiiPeer2));
77         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutYiiPeer2));
78     }
79
80     private void testRemoveRoute() {
81         Map<YangInstanceIdentifier, Long> yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
82         assertEquals(5, yiiCount.size());
83         assertEquals(1, (long) yiiCount.get(this.routePaAddPathYii));
84         assertTrue(this.testBARE.removeRoute(ROUTER_ID, REMOTE_PATH_ID));
85         assertTrue(this.testBARE.selectBest(AS));
86         this.testBARE.updateRoute(TABLES_KEY, this.peerPT, LOC_RIB_TARGET, this.ribSupport, this.discCache, this.tx, ROUTE_ID_PA_ADD_PATH);
87         yiiCount = this.yIIChanges.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
88         assertEquals(2, yiiCount.size());
89         assertFalse(yiiCount.containsKey(this.routePaAddPathYii));
90         assertFalse(yiiCount.containsKey(this.routeAddRiboutYii));
91     }
92 }