2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.lifecycle;
10 import com.google.common.util.concurrent.Futures;
11 import io.netty.util.HashedWheelTimer;
12 import java.util.concurrent.ExecutorService;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
26 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
27 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
32 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
33 import org.opendaylight.openflowplugin.api.openflow.lifecycle.OwnershipChangeListener;
34 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
35 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
36 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration;
37 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
38 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
39 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
40 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
41 import org.opendaylight.openflowplugin.impl.mastership.MastershipChangeServiceManagerImpl;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
44 import org.opendaylight.yangtools.yang.common.RpcError;
46 @RunWith(MockitoJUnitRunner.class)
47 public class ContextChainHolderImplTest {
49 private static final String ENTITY_TEST = "EntityTest";
50 private static final String OPENFLOW_TEST = "openflow:test";
52 private HashedWheelTimer timer;
54 private StatisticsManager statisticsManager;
56 private RpcManager rpcManager;
58 private DeviceManager deviceManager;
60 private StatisticsContext statisticsContext;
62 private RpcContext rpcContext;
64 private DeviceContext deviceContext;
66 private ConnectionContext connectionContext;
68 private DeviceInfo deviceInfo;
70 private ClusterSingletonServiceProvider singletonServicesProvider;
72 private ExecutorService executorService;
74 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
76 private EntityOwnershipService entityOwnershipService;
78 private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
80 private OwnershipChangeListener ownershipChangeListener;
82 private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
84 private FeaturesReply featuresReply;
86 private ContextChainHolderImpl contextChainHolder;
87 private ReconciliationFrameworkRegistration registration;
88 private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
89 private final Short AUXILIARY_ID = 0;
92 public void setUp() throws Exception {
93 Mockito.doAnswer(invocation -> {
94 invocation.getArgumentAt(0, Runnable.class).run();
96 }).when(executorService).submit(Mockito.<Runnable>any());
99 Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
100 Mockito.when(deviceManager.createContext(connectionContext)).thenReturn(deviceContext);
101 Mockito.when(rpcManager.createContext(deviceContext)).thenReturn(rpcContext);
102 Mockito.when(statisticsManager.createContext(deviceContext)).thenReturn(statisticsContext);
103 Mockito.when(deviceContext.makeDeviceSlave()).thenReturn(Futures.immediateFuture(null));
104 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
106 Mockito.when(singletonServicesProvider.registerClusterSingletonService(Mockito.any()))
107 .thenReturn(clusterSingletonServiceRegistration);
108 Mockito.when(entityOwnershipService.registerListener(Mockito.any(), Mockito.any()))
109 .thenReturn(entityOwnershipListenerRegistration);
110 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
111 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
113 registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
115 contextChainHolder = new ContextChainHolderImpl(
118 singletonServicesProvider,
119 entityOwnershipService,
122 contextChainHolder.addManager(statisticsManager);
123 contextChainHolder.addManager(rpcManager);
124 contextChainHolder.addManager(deviceManager);
128 public void addManager() throws Exception {
129 Assert.assertTrue(contextChainHolder.checkAllManagers());
133 public void createContextChain() throws Exception {
134 contextChainHolder.createContextChain(connectionContext);
135 Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class));
136 Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class));
137 Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class));
142 public void reconciliationFrameworkFailure() throws Exception {
143 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
144 contextChainHolder.createContextChain(connectionContext);
145 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
146 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
147 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
148 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
149 Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
153 public void reconciliationFrameworkDisconnect() throws Exception {
154 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
155 contextChainHolder.createContextChain(connectionContext);
156 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
157 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
158 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
159 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
160 Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
164 public void reconciliationFrameworkSuccess() throws Exception {
165 contextChainHolder.createContextChain(connectionContext);
166 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
167 Mockito.when(statisticsContext.initialSubmitAfterReconciliation()).thenReturn(true);
168 contextChainHolder.createContextChain(connectionContext);
169 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
170 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
171 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
172 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
173 Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
177 public void reconciliationFrameworkSuccessButNotSubmit() throws Exception {
178 contextChainHolder.createContextChain(connectionContext);
179 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
180 Mockito.when(statisticsContext.initialSubmitAfterReconciliation()).thenReturn(false);
181 contextChainHolder.createContextChain(connectionContext);
182 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
183 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
184 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
185 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
186 Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
190 public void deviceMastered() throws Exception {
191 registration.close();
192 contextChainHolder.createContextChain(connectionContext);
193 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
194 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
195 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
196 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
197 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
198 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
199 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
200 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
201 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
202 Assert.assertTrue(contextChainHolder.isAnyDeviceMastered());
203 Assert.assertTrue(contextChainHolder.listOfMasteredDevices().size() == 1);
207 public void deviceConnected() throws Exception {
208 registration.close();
209 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
210 == ConnectionStatus.MAY_CONTINUE);
211 Short AUXILIARY_ID_1 = 1;
212 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID_1);
213 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
214 == ConnectionStatus.MAY_CONTINUE);
215 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
216 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
217 == ConnectionStatus.ALREADY_CONNECTED);
221 public void notToAbleMastership() throws Exception {
222 registration.close();
223 contextChainHolder.deviceConnected(connectionContext);
224 contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true);
225 Mockito.verify(deviceContext).close();
226 Mockito.verify(statisticsContext).close();
227 Mockito.verify(rpcContext).close();
231 public void notAbleToSetSlave() throws Exception {
232 registration.close();
233 contextChainHolder.deviceConnected(connectionContext);
234 contextChainHolder.onSlaveRoleNotAcquired(deviceInfo);
235 Mockito.verify(deviceContext).close();
236 Mockito.verify(statisticsContext).close();
237 Mockito.verify(rpcContext).close();
241 public void deviceDisconnected() throws Exception {
242 registration.close();
243 contextChainHolder.createContextChain(connectionContext);
244 contextChainHolder.onDeviceDisconnected(connectionContext);
245 Mockito.verify(deviceContext).close();
246 Mockito.verify(statisticsContext).close();
247 Mockito.verify(rpcContext).close();
251 public void onClose() throws Exception {
252 registration.close();
253 contextChainHolder.createContextChain(connectionContext);
254 contextChainHolder.close();
255 Mockito.verify(deviceContext).close();
256 Mockito.verify(statisticsContext).close();
257 Mockito.verify(rpcContext).close();
261 public void ownershipChanged() throws Exception {
262 registration.close();
263 contextChainHolder.createContextChain(connectionContext);
264 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
265 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
266 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
267 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
268 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
269 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
270 new Entity(ENTITY_TEST, OPENFLOW_TEST),
275 contextChainHolder.ownershipChanged(ownershipChange);
276 Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
280 public void ownershipChangedButHasOwner() throws Exception {
281 registration.close();
282 contextChainHolder.createContextChain(connectionContext);
283 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
284 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
285 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
286 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
287 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
288 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
289 new Entity(ENTITY_TEST, OPENFLOW_TEST),
294 contextChainHolder.ownershipChanged(ownershipChange);
295 Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());