Update MRI projects for Aluminium
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / config / RibImplTest.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.config;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17
18 import com.google.common.collect.ImmutableClassToInstanceMap;
19 import com.google.common.collect.ImmutableSet;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
28 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
30 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
31 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base.AfiSafisBuilder;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Global;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.GlobalBuilder;
37 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalAddPathsConfigBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.common.Uint8;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
51 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
52 import org.osgi.framework.ServiceRegistration;
53
54 public class RibImplTest extends AbstractConfig {
55     private static final List<AfiSafi> AFISAFIS = new ArrayList<>();
56     private static final BgpTableType TABLE_TYPE = new BgpTableTypeImpl(Ipv4AddressFamily.class,
57             UnicastSubsequentAddressFamily.class);
58     private static final BgpId BGP_ID = new BgpId(new Ipv4AddressNoZone("127.0.0.1"));
59
60     static {
61         AFISAFIS.add(new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class)
62                 .addAugmentation(new GlobalAddPathsConfigBuilder().setReceive(true).setSendMax(Uint8.ZERO).build())
63                 .build());
64     }
65
66     @Mock
67     private RIBExtensionConsumerContext extension;
68     @Mock
69     private CodecsRegistry codecsRegistry;
70     @Mock
71     private DOMDataBroker domDataBroker;
72     @Mock
73     private RIBSupport<?, ?, ?, ?> ribSupport;
74     @Mock
75     private ServiceRegistration<?> serviceRegistration;
76
77     @Override
78     @Before
79     public void setUp() throws Exception {
80         super.setUp();
81
82         doReturn(this.ribSupport).when(this.extension).getRIBSupport(any(TablesKey.class));
83         final NodeIdentifier nii = new NodeIdentifier(QName.create("", "test").intern());
84         doReturn(nii).when(this.ribSupport).routeAttributesIdentifier();
85         doReturn(ImmutableSet.of()).when(this.ribSupport).cacheableAttributeObjects();
86         final MapEntryNode emptyTable = mock(MapEntryNode.class);
87         doReturn(emptyTable).when(this.ribSupport).emptyTable();
88         final NodeIdentifierWithPredicates niie = NodeIdentifierWithPredicates.of(Rib.QNAME,
89                 QName.create("", "test").intern(), "t");
90         doReturn(niie).when(emptyTable).getIdentifier();
91         doReturn(QName.create("", "test").intern()).when(emptyTable).getNodeType();
92         doReturn(this.domTx).when(this.domDataBroker).createMergingTransactionChain(any());
93         final DOMDataTreeChangeService dOMDataTreeChangeService = mock(DOMDataTreeChangeService.class);
94         doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dOMDataTreeChangeService))
95                 .when(this.domDataBroker).getExtensions();
96         doReturn(mock(ListenerRegistration.class)).when(dOMDataTreeChangeService)
97                 .registerDataTreeChangeListener(any(), any());
98         doNothing().when(this.serviceRegistration).unregister();
99     }
100
101     @Test
102     public void testRibImpl() {
103         final RibImpl ribImpl = new RibImpl(
104                 this.extension,
105                 this.dispatcher,
106                 this.policyProvider,
107                 this.codecsRegistry,
108                 this.domDataBroker,
109                 getDataBroker());
110         ribImpl.setServiceRegistration(this.serviceRegistration);
111         ribImpl.start(createGlobal(), "rib-test", this.tableTypeRegistry);
112         verify(this.domDataBroker).getExtensions();
113         assertEquals("RIBImpl{bgpId=Ipv4Address{_value=127.0.0.1}, localTables=[BgpTableTypeImpl ["
114                 + "getAfi()=interface org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types."
115                 + "rev200120.Ipv4AddressFamily, "
116                 + "getSafi()=interface org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types."
117                 + "rev200120.UnicastSubsequentAddressFamily]]}", ribImpl.toString());
118         assertEquals(Collections.singleton(new TablesKey(Ipv4AddressFamily.class,
119                 UnicastSubsequentAddressFamily.class)), ribImpl.getLocalTablesKeys());
120         assertNotNull(ribImpl.getService());
121         assertNotNull(ribImpl.getInstanceIdentifier());
122         assertEquals(AS, ribImpl.getLocalAs());
123         assertEquals(BGP_ID, ribImpl.getBgpIdentifier());
124         assertEquals(Collections.singleton(TABLE_TYPE), ribImpl.getLocalTables());
125         assertEquals(this.dispatcher, ribImpl.getDispatcher());
126         assertEquals(this.extension, ribImpl.getRibExtensions());
127         assertNotNull(ribImpl.getRibSupportContext());
128         assertNotNull(ribImpl.getCodecsRegistry());
129
130         ribImpl.close();
131         verify(this.serviceRegistration).unregister();
132     }
133
134     private static Global createGlobal() {
135         return new GlobalBuilder()
136                 .setAfiSafis(new AfiSafisBuilder().setAfiSafi(AFISAFIS).build())
137                 .setConfig(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base
138                         .ConfigBuilder().setAs(AS).setRouterId(BGP_ID).build()).build();
139     }
140 }