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.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;
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;
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;
87 private BGPTableTypeRegistryConsumer tableTypeRegistry;
89 private DataTreeModification<Bgp> modification;
91 private ListenerRegistration<?> dataTreeRegistration;
93 private ClusterSingletonServiceProvider singletonServiceProvider;
95 private BGPDispatcher dispatcher;
97 private BindingCodecTreeFactory codecFactory;
99 private DOMSchemaService schemaService;
101 private RpcProviderService rpcRegistry;
103 private BGPPeerRegistry peerRegistry;
104 private BgpDeployerImpl deployer;
108 public void setUp() throws Exception {
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());
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();
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();
128 schemaService = mock(DOMSchemaService.class);
129 doNothing().when(this.dataTreeRegistration).close();
131 doReturn(this.dataTreeRegistration).when(schemaService).registerSchemaContextListener(any());
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);
150 public void testDeployerRib() throws Exception {
152 checkPresentConfiguration(getDataBroker(), NETWORK_II);
153 createRib(createGlobalIpv4());
154 verify(this.schemaService, timeout(VERIFY_TIMEOUT_MILIS)).registerSchemaContextListener(any());
157 //change with same rib already existing
158 createRib(createGlobalIpv4());
159 verify(this.schemaService, timeout(VERIFY_TIMEOUT_MILIS)).registerSchemaContextListener(any());
161 //Update for existing rib
162 createRib(createGlobalIpv6());
163 verify(this.dataTreeRegistration, timeout(VERIFY_TIMEOUT_MILIS)).close();
165 //Delete for existing rib
167 verify(this.dataTreeRegistration, timeout(VERIFY_TIMEOUT_MILIS).times(2)).close();
173 public void testDeployerCreateNeighbor() throws Exception {
175 checkPresentConfiguration(getDataBroker(), NETWORK_II);
177 createRib(createGlobalIpv4());
178 createNeighbor(createNeighbors());
179 verify(this.peerRegistry, timeout(VERIFY_TIMEOUT_MILIS).times(1)).addPeer(any(), any(), any());
181 //change with same peer already existing
182 createNeighbor(createNeighbors());
183 verify(this.peerRegistry, timeout(VERIFY_TIMEOUT_MILIS).times(1)).addPeer(any(), any(), any());
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());
190 verify(this.peerRegistry, timeout(VERIFY_TIMEOUT_MILIS).times(2)).removePeer(any());
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);
201 private void deleteRib() throws ExecutionException, InterruptedException {
202 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
203 wr.delete(LogicalDatastoreType.CONFIGURATION, BGP_II);
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);
213 private void deleteNeighbors() throws ExecutionException, InterruptedException {
214 final WriteTransaction wr = getDataBroker().newWriteOnlyTransaction();
215 wr.delete(LogicalDatastoreType.CONFIGURATION, NEIGHBORS_II);