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.common.api.LogicalDatastoreType;
35 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
36 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
37 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
38 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
39 import org.opendaylight.protocol.bgp.rib.impl.DefaultRibPoliciesMockTest;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.InstanceType;
43 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
44 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
45 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Global;
46 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
47 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.NetworkInstances;
48 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstance;
49 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstanceKey;
50 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.Protocols;
51 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
52 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.ProtocolKey;
53 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.BGP;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NetworkInstanceProtocol;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
60 import org.osgi.framework.BundleContext;
61 import org.osgi.framework.ServiceRegistration;
62 import org.osgi.service.blueprint.container.BlueprintContainer;
64 public class BgpDeployerImplTest extends DefaultRibPoliciesMockTest {
65 private static final BgpTableType TABLE_TYPE = new BgpTableTypeImpl(Ipv4AddressFamily.class,
66 UnicastSubsequentAddressFamily.class);
67 private static final String NETWORK_INSTANCE_NAME = "network-test";
68 private static final KeyedInstanceIdentifier<NetworkInstance, NetworkInstanceKey> NETWORK_II =
69 InstanceIdentifier.create(NetworkInstances.class)
70 .child(NetworkInstance.class, new NetworkInstanceKey(NETWORK_INSTANCE_NAME));
71 private static final String KEY = "bgp";
72 private static final InstanceIdentifier<Bgp> BGP_II = NETWORK_II.child(Protocols.class)
73 .child(Protocol.class, new ProtocolKey(BGP.class, KEY))
74 .augmentation(NetworkInstanceProtocol.class).child(Bgp.class);
75 private static final InstanceIdentifier<Global> GLOBAL_II = BGP_II.child(Global.class);
76 private static final InstanceIdentifier<Neighbors> NEIGHBORS_II = BGP_II.child(Neighbors.class);
77 private static final int VERIFY_TIMEOUT_MILIS = 5000;
80 private BlueprintContainer blueprintContainer;
82 private BundleContext bundleContext;
84 private BGPTableTypeRegistryConsumer tableTypeRegistry;
86 private DataTreeModification<Bgp> modification;
88 private ServiceRegistration<?> registration;
90 private ClusterSingletonServiceProvider singletonServiceProvider;
91 private BgpDeployerImpl deployer;
95 public void setUp() throws Exception {
98 doReturn("mapping").when(this.tableTypeRegistry).toString();
99 doReturn(Optional.of(TABLE_TYPE)).when(this.tableTypeRegistry).getTableType(any());
100 doReturn(Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
101 doNothing().when(this.registration).unregister();
102 doReturn(this.registration).when(this.bundleContext).registerService(eq(InstanceType.RIB.getServices()),
103 any(), any(Dictionary.class));
104 doReturn(this.registration).when(this.bundleContext).registerService(eq(InstanceType.PEER.getServices()),
105 any(), any(Dictionary.class));
107 doReturn("bgpPeer").when(this.modification).toString();
108 final RIBExtensionConsumerContext extension = mock(RIBExtensionConsumerContext.class);
110 final ClusterSingletonServiceRegistration serviceRegistration = mock(ClusterSingletonServiceRegistration.class);
111 doReturn(serviceRegistration).when(this.singletonServiceProvider).registerClusterSingletonService(any());
112 doNothing().when(serviceRegistration).close();
114 final RibImpl ribImpl = new RibImpl(extension, mock(BGPDispatcher.class), this.policyProvider,
115 mock(CodecsRegistry.class), getDomBroker(), getDataBroker());
116 doReturn(ribImpl).when(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
118 doReturn(new BgpPeer(mock(RpcProviderService.class))).when(this.blueprintContainer)
119 .getComponentInstance(eq("bgpPeer"));
121 this.deployer = new BgpDeployerImpl(NETWORK_INSTANCE_NAME, this.singletonServiceProvider,
122 this.blueprintContainer, this.bundleContext, getDataBroker(), this.tableTypeRegistry);
126 public void testDeployerRib() throws Exception {
128 checkPresentConfiguration(getDataBroker(), NETWORK_II);
129 createRib(createGlobalIpv4());
131 verify(this.blueprintContainer, timeout(VERIFY_TIMEOUT_MILIS)).getComponentInstance(eq("ribImpl"));
132 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS)).registerService(eq(InstanceType.RIB.getServices()),
133 any(), any(Dictionary.class));
135 //change with same rib already existing
136 createRib(createGlobalIpv4());
137 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
138 verify(this.bundleContext).registerService(eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
140 //Update for existing rib
141 createRib(createGlobalIpv6());
143 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
144 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS).times(2)).registerService(
145 eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
146 verify(this.registration).unregister();
148 //Delete for existing rib
151 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
152 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS).times(2))
153 .registerService(eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
154 verify(this.registration, timeout(VERIFY_TIMEOUT_MILIS).times(2)).unregister();
160 public void testDeployerCreateNeighbor() throws Exception {
162 checkPresentConfiguration(getDataBroker(), NETWORK_II);
164 createRib(createGlobalIpv4());
165 createNeighbor(createNeighbors());
166 verify(this.blueprintContainer, timeout(VERIFY_TIMEOUT_MILIS)).getComponentInstance(eq("bgpPeer"));
167 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS)).registerService(eq(InstanceType.PEER.getServices()),
168 any(BgpPeer.class), any(Dictionary.class));
170 //change with same peer already existing
171 createNeighbor(createNeighbors());
172 verify(this.blueprintContainer).getComponentInstance(eq("bgpPeer"));
173 verify(this.bundleContext).registerService(eq(InstanceType.PEER.getServices()),
174 any(BgpPeer.class), any(Dictionary.class));
177 createNeighbor(createNeighborsNoRR());
178 verify(this.blueprintContainer).getComponentInstance(eq("bgpPeer"));
179 verify(this.bundleContext, timeout(VERIFY_TIMEOUT_MILIS).times(2))
180 .registerService(eq(InstanceType.PEER.getServices()), any(BgpPeer.class), any(Dictionary.class));
181 verify(this.registration).unregister();
184 //Delete existing Peer
185 verify(this.bundleContext, times(2))
186 .registerService(eq(InstanceType.PEER.getServices()), any(BgpPeer.class), any(Dictionary.class));
187 verify(this.registration, timeout(VERIFY_TIMEOUT_MILIS).times(2)).unregister();
192 private void createRib(final Global global) throws ExecutionException, InterruptedException {
193 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
194 wr.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, GLOBAL_II, global);
198 private void deleteRib() throws ExecutionException, InterruptedException {
199 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
200 wr.delete(LogicalDatastoreType.CONFIGURATION, BGP_II);
204 private void createNeighbor(final Neighbors neighbors) throws ExecutionException, InterruptedException {
205 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
206 wr.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II, neighbors);
210 private void deleteNeighbors() throws ExecutionException, InterruptedException {
211 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
212 wr.delete(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II);