Migrate to MD-SAL APIs
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / config / BgpDeployerImplTest.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.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.anyInt;
12 import static org.mockito.Mockito.doAnswer;
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.timeout;
17 import static org.mockito.Mockito.verify;
18 import static org.opendaylight.protocol.bgp.rib.impl.config.AbstractConfig.TABLES_KEY;
19 import static org.opendaylight.protocol.bgp.rib.impl.config.RIBTestsUtil.createGlobalIpv4;
20 import static org.opendaylight.protocol.bgp.rib.impl.config.RIBTestsUtil.createGlobalIpv6;
21 import static org.opendaylight.protocol.bgp.rib.impl.config.RIBTestsUtil.createNeighbors;
22 import static org.opendaylight.protocol.bgp.rib.impl.config.RIBTestsUtil.createNeighborsNoRR;
23 import static org.opendaylight.protocol.util.CheckUtil.checkPresentConfiguration;
24
25 import io.netty.util.concurrent.Future;
26 import java.util.Optional;
27 import java.util.concurrent.ExecutionException;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.opendaylight.mdsal.binding.api.DataBroker;
33 import org.opendaylight.mdsal.binding.api.DataTreeModification;
34 import org.opendaylight.mdsal.binding.api.RpcProviderService;
35 import org.opendaylight.mdsal.binding.api.WriteTransaction;
36 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
37 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
40 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
41 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
42 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
43 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
44 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.BGPRibRoutingPolicyFactoryImpl;
45 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.StatementRegistry;
46 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
47 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
48 import org.opendaylight.protocol.bgp.rib.impl.DefaultRibPoliciesMockTest;
49 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
50 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
51 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
52 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
53 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
54 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Global;
55 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
56 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.NetworkInstances;
57 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstance;
58 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstanceKey;
59 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.Protocols;
60 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
61 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.ProtocolKey;
62 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.BGP;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NetworkInstanceProtocol;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
67 import org.opendaylight.yangtools.concepts.ListenerRegistration;
68 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
69 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
70
71 public class BgpDeployerImplTest extends DefaultRibPoliciesMockTest {
72     private static final BgpTableType TABLE_TYPE = new BgpTableTypeImpl(Ipv4AddressFamily.class,
73             UnicastSubsequentAddressFamily.class);
74     private static final String NETWORK_INSTANCE_NAME = "network-test";
75     private static final KeyedInstanceIdentifier<NetworkInstance, NetworkInstanceKey> NETWORK_II =
76             InstanceIdentifier.create(NetworkInstances.class)
77                     .child(NetworkInstance.class, new NetworkInstanceKey(NETWORK_INSTANCE_NAME));
78     private static final String KEY = "bgp";
79     private static final InstanceIdentifier<Bgp> BGP_II = NETWORK_II.child(Protocols.class)
80             .child(Protocol.class, new ProtocolKey(BGP.class, KEY))
81             .augmentation(NetworkInstanceProtocol.class).child(Bgp.class);
82     private static final InstanceIdentifier<Global> GLOBAL_II = BGP_II.child(Global.class);
83     private static final InstanceIdentifier<Neighbors> NEIGHBORS_II = BGP_II.child(Neighbors.class);
84     private static final int VERIFY_TIMEOUT_MILIS = 5000;
85
86     @Mock
87     private BGPTableTypeRegistryConsumer tableTypeRegistry;
88     @Mock
89     private DataTreeModification<Bgp> modification;
90     @Mock
91     private ListenerRegistration<?> dataTreeRegistration;
92     @Mock
93     private ClusterSingletonServiceProvider singletonServiceProvider;
94     @Mock
95     private BGPDispatcher dispatcher;
96     @Mock
97     private BindingCodecTreeFactory codecFactory;
98     @Mock
99     private DOMSchemaService schemaService;
100     @Mock
101     private RpcProviderService rpcRegistry;
102     @Mock
103     private BGPPeerRegistry peerRegistry;
104     private BgpDeployerImpl deployer;
105
106     @Override
107     @Before
108     public void setUp() throws Exception {
109         super.setUp();
110
111         doReturn("mapping").when(this.tableTypeRegistry).toString();
112         doReturn(Optional.of(TABLE_TYPE)).when(this.tableTypeRegistry).getTableType(any());
113         doReturn(Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
114
115         doNothing().when(this.dataTreeRegistration).close();
116         doReturn("bgpPeer").when(this.modification).toString();
117         final RIBExtensionConsumerContext extension = mock(RIBExtensionConsumerContext.class);
118         doReturn(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy()).when(extension).getClassLoadingStrategy();
119
120         final ClusterSingletonServiceRegistration serviceRegistration = mock(ClusterSingletonServiceRegistration.class);
121         doAnswer(invocationOnMock -> {
122             final ClusterSingletonService service = invocationOnMock.getArgument(0);
123             service.instantiateServiceInstance();
124             return serviceRegistration;
125         }).when(this.singletonServiceProvider).registerClusterSingletonService(any(ClusterSingletonService.class));
126         doNothing().when(serviceRegistration).close();
127
128         schemaService = mock(DOMSchemaService.class);
129         doNothing().when(this.dataTreeRegistration).close();
130
131         doReturn(this.dataTreeRegistration).when(schemaService).registerSchemaContextListener(any());
132
133         final DataBroker dataBroker = getDataBroker();
134         final DOMDataBroker domBroker = getDomBroker();
135         final SimpleRIBExtensionProviderContext ribExtensionContext = new SimpleRIBExtensionProviderContext();
136         final BGPRibRoutingPolicyFactoryImpl policyFactory = new BGPRibRoutingPolicyFactoryImpl(dataBroker,
137             new StatementRegistry());
138         doReturn(this.peerRegistry).when(this.dispatcher).getBGPPeerRegistry();
139         final Future future = Mockito.mock(Future.class);
140         doReturn(true).when(future).cancel(true);
141         doReturn(future).when(this.dispatcher).createReconnectingClient(any(), any(), anyInt(), any());
142         doNothing().when(this.peerRegistry).addPeer(any(), any(), any());
143         doNothing().when(this.peerRegistry).removePeer(any());
144         this.deployer = new BgpDeployerImpl(NETWORK_INSTANCE_NAME, this.singletonServiceProvider, dataBroker,
145                 this.tableTypeRegistry, ribExtensionContext, dispatcher, policyFactory, codecFactory, domBroker,
146                 schemaService, rpcRegistry);
147     }
148
149     @Test
150     public void testDeployerRib() throws Exception {
151         deployer.init();
152         checkPresentConfiguration(getDataBroker(), NETWORK_II);
153         createRib(createGlobalIpv4());
154         verify(this.schemaService, timeout(VERIFY_TIMEOUT_MILIS)).registerSchemaContextListener(any());
155
156
157         //change with same rib already existing
158         createRib(createGlobalIpv4());
159         verify(this.schemaService, timeout(VERIFY_TIMEOUT_MILIS)).registerSchemaContextListener(any());
160
161         //Update for existing rib
162         createRib(createGlobalIpv6());
163         verify(this.dataTreeRegistration, timeout(VERIFY_TIMEOUT_MILIS)).close();
164
165         //Delete for existing rib
166         deleteRib();
167         verify(this.dataTreeRegistration, timeout(VERIFY_TIMEOUT_MILIS).times(2)).close();
168
169         deployer.close();
170     }
171
172     @Test
173     public void testDeployerCreateNeighbor() throws Exception {
174         deployer.init();
175         checkPresentConfiguration(getDataBroker(), NETWORK_II);
176
177         createRib(createGlobalIpv4());
178         createNeighbor(createNeighbors());
179         verify(this.peerRegistry, timeout(VERIFY_TIMEOUT_MILIS).times(1)).addPeer(any(), any(), any());
180
181         //change with same peer already existing
182         createNeighbor(createNeighbors());
183         verify(this.peerRegistry, timeout(VERIFY_TIMEOUT_MILIS).times(1)).addPeer(any(), any(), any());
184         //Update for peer
185         createNeighbor(createNeighborsNoRR());
186         verify(this.peerRegistry, timeout(VERIFY_TIMEOUT_MILIS).times(2)).addPeer(any(), any(), any());
187         verify(this.peerRegistry, timeout(VERIFY_TIMEOUT_MILIS).times(1)).removePeer(any());
188
189         deleteNeighbors();
190         verify(this.peerRegistry, timeout(VERIFY_TIMEOUT_MILIS).times(2)).removePeer(any());
191
192         deployer.close();
193     }
194
195     private void createRib(final Global global) throws ExecutionException, InterruptedException {
196         final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
197         wr.put(LogicalDatastoreType.CONFIGURATION, GLOBAL_II, global, true);
198         wr.commit().get();
199     }
200
201     private void deleteRib() throws ExecutionException, InterruptedException {
202         final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
203         wr.delete(LogicalDatastoreType.CONFIGURATION, BGP_II);
204         wr.commit().get();
205     }
206
207     private void createNeighbor(final Neighbors neighbors) throws ExecutionException, InterruptedException {
208         final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
209         wr.put(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II, neighbors, true);
210         wr.commit().get();
211     }
212
213     private void deleteNeighbors() throws ExecutionException, InterruptedException {
214         final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
215         wr.delete(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II);
216         wr.commit().get();
217     }
218 }