2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl.config;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.timeout;
16 import static org.mockito.Mockito.times;
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;
25 import java.util.Dictionary;
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.opendaylight.mdsal.binding.api.DataTreeModification;
32 import org.opendaylight.mdsal.binding.api.RpcProviderService;
33 import org.opendaylight.mdsal.binding.api.WriteTransaction;
34 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
35 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
38 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
39 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
40 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
41 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
42 import org.opendaylight.protocol.bgp.rib.impl.DefaultRibPoliciesMockTest;
43 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
44 import org.opendaylight.protocol.bgp.rib.impl.spi.InstanceType;
45 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
46 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
47 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Global;
48 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
49 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.NetworkInstances;
50 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstance;
51 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstanceKey;
52 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.Protocols;
53 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
54 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.ProtocolKey;
55 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.BGP;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NetworkInstanceProtocol;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
60 import org.opendaylight.yangtools.concepts.ListenerRegistration;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
63 import org.osgi.framework.BundleContext;
64 import org.osgi.framework.ServiceRegistration;
65 import org.osgi.service.blueprint.container.BlueprintContainer;
67 public class BgpDeployerImplTest extends DefaultRibPoliciesMockTest {
68 private static final BgpTableType TABLE_TYPE = new BgpTableTypeImpl(Ipv4AddressFamily.class,
69 UnicastSubsequentAddressFamily.class);
70 private static final String NETWORK_INSTANCE_NAME = "network-test";
71 private static final KeyedInstanceIdentifier<NetworkInstance, NetworkInstanceKey> NETWORK_II =
72 InstanceIdentifier.create(NetworkInstances.class)
73 .child(NetworkInstance.class, new NetworkInstanceKey(NETWORK_INSTANCE_NAME));
74 private static final String KEY = "bgp";
75 private static final InstanceIdentifier<Bgp> BGP_II = NETWORK_II.child(Protocols.class)
76 .child(Protocol.class, new ProtocolKey(BGP.class, KEY))
77 .augmentation(NetworkInstanceProtocol.class).child(Bgp.class);
78 private static final InstanceIdentifier<Global> GLOBAL_II = BGP_II.child(Global.class);
79 private static final InstanceIdentifier<Neighbors> NEIGHBORS_II = BGP_II.child(Neighbors.class);
80 private static final int VERIFY_TIMEOUT_MILIS = 5000;
83 private BlueprintContainer blueprintContainer;
85 private BundleContext bundleContext;
87 private BGPTableTypeRegistryConsumer tableTypeRegistry;
89 private DataTreeModification<Bgp> modification;
91 private ListenerRegistration<?> dataTreeRegistration;
93 private ServiceRegistration<?> registration;
95 private ClusterSingletonServiceProvider singletonServiceProvider;
96 private BgpDeployerImpl deployer;
100 public void setUp() throws Exception {
103 doReturn("mapping").when(this.tableTypeRegistry).toString();
104 doReturn(Optional.of(TABLE_TYPE)).when(this.tableTypeRegistry).getTableType(any());
105 doReturn(Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
106 doNothing().when(this.registration).unregister();
107 doReturn(this.registration).when(this.bundleContext).registerService(eq(InstanceType.RIB.getServices()),
108 any(), any(Dictionary.class));
109 doReturn(this.registration).when(this.bundleContext).registerService(eq(InstanceType.PEER.getServices()),
110 any(), any(Dictionary.class));
112 doNothing().when(this.dataTreeRegistration).close();
113 doReturn("bgpPeer").when(this.modification).toString();
114 final RIBExtensionConsumerContext extension = mock(RIBExtensionConsumerContext.class);
115 doReturn(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy()).when(extension).getClassLoadingStrategy();
117 final ClusterSingletonServiceRegistration serviceRegistration = mock(ClusterSingletonServiceRegistration.class);
118 doReturn(serviceRegistration).when(this.singletonServiceProvider).registerClusterSingletonService(any());
119 doNothing().when(serviceRegistration).close();
121 schemaService = mock(DOMSchemaService.class);
122 doNothing().when(this.dataTreeRegistration).close();
124 doReturn(this.dataTreeRegistration).when(schemaService).registerSchemaContextListener(any());
126 final RibImpl ribImpl = new RibImpl(extension, mock(BGPDispatcher.class), this.policyProvider,
127 mock(BindingCodecTreeFactory.class), getDomBroker(), getDataBroker(), schemaService);
128 doReturn(ribImpl).when(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
130 doReturn(new BgpPeer(mock(RpcProviderService.class))).when(this.blueprintContainer)
131 .getComponentInstance(eq("bgpPeer"));
133 this.deployer = new BgpDeployerImpl(NETWORK_INSTANCE_NAME, this.singletonServiceProvider,
134 this.blueprintContainer, this.bundleContext, getDataBroker(), this.tableTypeRegistry);
138 public void testDeployerRib() throws Exception {
140 checkPresentConfiguration(getDataBroker(), NETWORK_II);
141 createRib(createGlobalIpv4());
143 verify(this.blueprintContainer, timeout(VERIFY_TIMEOUT_MILIS)).getComponentInstance(eq("ribImpl"));
144 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS)).registerService(eq(InstanceType.RIB.getServices()),
145 any(), any(Dictionary.class));
147 //change with same rib already existing
148 createRib(createGlobalIpv4());
149 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
150 verify(this.bundleContext).registerService(eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
152 //Update for existing rib
153 createRib(createGlobalIpv6());
155 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
156 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS).times(2)).registerService(
157 eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
158 verify(this.dataTreeRegistration).close();
159 verify(this.registration).unregister();
161 //Delete for existing rib
164 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
165 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS).times(2))
166 .registerService(eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
167 verify(this.dataTreeRegistration, timeout(VERIFY_TIMEOUT_MILIS).times(2)).close();
168 verify(this.registration, timeout(VERIFY_TIMEOUT_MILIS).times(2)).unregister();
174 public void testDeployerCreateNeighbor() throws Exception {
176 checkPresentConfiguration(getDataBroker(), NETWORK_II);
178 createRib(createGlobalIpv4());
179 createNeighbor(createNeighbors());
180 verify(this.blueprintContainer, timeout(VERIFY_TIMEOUT_MILIS)).getComponentInstance(eq("bgpPeer"));
181 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS)).registerService(eq(InstanceType.PEER.getServices()),
182 any(BgpPeer.class), any(Dictionary.class));
184 //change with same peer already existing
185 createNeighbor(createNeighbors());
186 verify(this.blueprintContainer).getComponentInstance(eq("bgpPeer"));
187 verify(this.bundleContext).registerService(eq(InstanceType.PEER.getServices()),
188 any(BgpPeer.class), any(Dictionary.class));
191 createNeighbor(createNeighborsNoRR());
192 verify(this.blueprintContainer).getComponentInstance(eq("bgpPeer"));
193 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS).times(2))
194 .registerService(eq(InstanceType.PEER.getServices()), any(BgpPeer.class), any(Dictionary.class));
195 verify(this.registration).unregister();
198 //Delete existing Peer
199 verify(this.bundleContext, times(2))
200 .registerService(eq(InstanceType.PEER.getServices()), any(BgpPeer.class), any(Dictionary.class));
201 verify(this.registration, timeout(VERIFY_TIMEOUT_MILIS).times(2)).unregister();
206 private void createRib(final Global global) throws ExecutionException, InterruptedException {
207 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
208 wr.put(LogicalDatastoreType.CONFIGURATION, GLOBAL_II, global, true);
212 private void deleteRib() throws ExecutionException, InterruptedException {
213 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
214 wr.delete(LogicalDatastoreType.CONFIGURATION, BGP_II);
218 private void createNeighbor(final Neighbors neighbors) throws ExecutionException, InterruptedException {
219 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
220 wr.put(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II, neighbors, true);
224 private void deleteNeighbors() throws ExecutionException, InterruptedException {
225 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
226 wr.delete(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II);