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
9 package org.opendaylight.protocol.bgp.rib.impl.config;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
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.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.controller.md.sal.binding.api.DataTreeModification;
32 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
35 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
36 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
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.rev171207.BgpTableType;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.Protocol1;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.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(Protocol1.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);
82 private BlueprintContainer blueprintContainer;
84 private BundleContext bundleContext;
86 private BGPTableTypeRegistryConsumer tableTypeRegistry;
88 private DataTreeModification<Bgp> modification;
90 private ListenerRegistration<?> dataTreeRegistration;
92 private ServiceRegistration<?> registration;
94 private ClusterSingletonServiceProvider singletonServiceProvider;
95 private BgpDeployerImpl deployer;
98 public void setUp() throws Exception {
101 doReturn("mapping").when(this.tableTypeRegistry).toString();
102 doReturn(Optional.of(TABLE_TYPE)).when(this.tableTypeRegistry).getTableType(any());
103 doReturn(Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
104 doNothing().when(this.registration).unregister();
105 doReturn(this.registration).when(this.bundleContext).registerService(eq(InstanceType.RIB.getServices()),
106 any(), any(Dictionary.class));
107 doReturn(this.registration).when(this.bundleContext).registerService(eq(InstanceType.PEER.getServices()),
108 any(), any(Dictionary.class));
110 doNothing().when(this.dataTreeRegistration).close();
111 doReturn("bgpPeer").when(this.modification).toString();
112 final RIBExtensionConsumerContext extension = mock(RIBExtensionConsumerContext.class);
113 doReturn(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy()).when(extension).getClassLoadingStrategy();
115 final ClusterSingletonServiceRegistration serviceRegistration = mock(ClusterSingletonServiceRegistration.class);
116 doReturn(serviceRegistration).when(this.singletonServiceProvider).registerClusterSingletonService(any());
117 doNothing().when(serviceRegistration).close();
119 final DOMSchemaService schemaService = mock(DOMSchemaService.class);
120 doNothing().when(this.dataTreeRegistration).close();
122 doReturn(this.dataTreeRegistration).when(schemaService).registerSchemaContextListener(any());
124 final RibImpl ribImpl = new RibImpl(extension, mock(BGPDispatcher.class), this.policyProvider,
125 mock(BindingCodecTreeFactory.class), getDomBroker(), schemaService, this.mappingService);
126 doReturn(ribImpl).when(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
128 doReturn(new BgpPeer(mock(RpcProviderRegistry.class)))
129 .when(this.blueprintContainer).getComponentInstance(eq("bgpPeer"));
131 this.deployer = new BgpDeployerImpl(NETWORK_INSTANCE_NAME, this.singletonServiceProvider,
132 this.blueprintContainer, this.bundleContext, getDataBroker(), this.tableTypeRegistry);
136 public void testDeployerRib() throws Exception {
138 checkPresentConfiguration(getDataBroker(), NETWORK_II);
139 createRib(createGlobalIpv4());
141 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
142 verify(this.bundleContext).registerService(eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
144 //change with same rib already existing
145 createRib(createGlobalIpv4());
146 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
147 verify(this.bundleContext).registerService(eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
149 //Update for existing rib
150 createRib(createGlobalIpv6());
152 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
153 verify(this.bundleContext, times(2)).registerService(eq(InstanceType.RIB.getServices()),
154 any(), any(Dictionary.class));
155 verify(this.dataTreeRegistration).close();
156 verify(this.registration).unregister();
158 //Delete for existing rib
161 verify(this.blueprintContainer).getComponentInstance(eq("ribImpl"));
162 verify(this.bundleContext, times(2))
163 .registerService(eq(InstanceType.RIB.getServices()), any(), any(Dictionary.class));
164 verify(this.dataTreeRegistration, times(2)).close();
165 verify(this.registration, times(2)).unregister();
174 public void testDeployerCreateNeighbor() throws Exception {
176 checkPresentConfiguration(getDataBroker(), NETWORK_II);
178 createRib(createGlobalIpv4());
179 createNeighbor(createNeighbors());
180 verify(this.blueprintContainer).getComponentInstance(eq("bgpPeer"));
181 verify(this.bundleContext).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());
193 verify(this.blueprintContainer).getComponentInstance(eq("bgpPeer"));
194 verify(this.bundleContext, times(2))
195 .registerService(eq(InstanceType.PEER.getServices()), any(BgpPeer.class), any(Dictionary.class));
196 verify(this.registration).unregister();
199 //Delete existing Peer
200 verify(this.bundleContext, times(2))
201 .registerService(eq(InstanceType.PEER.getServices()), any(BgpPeer.class), any(Dictionary.class));
202 verify(this.registration, times(2)).unregister();
207 private void createRib(final Global global) throws ExecutionException, InterruptedException {
208 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
209 wr.put(LogicalDatastoreType.CONFIGURATION, GLOBAL_II, global, true);
213 private void deleteRib() throws ExecutionException, InterruptedException {
214 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
215 wr.delete(LogicalDatastoreType.CONFIGURATION, BGP_II);
219 private void createNeighbor(final Neighbors neighbors) throws ExecutionException, InterruptedException {
220 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
221 wr.put(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II, neighbors, true);
225 private void deleteNeighbors() throws ExecutionException, InterruptedException {
226 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
227 wr.delete(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II);