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 java.util.concurrent.ExecutorService;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
30 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
31 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
32 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
33 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration;
34 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
35 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
36 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
37 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
39 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
40 import org.opendaylight.openflowplugin.impl.mastership.MastershipChangeServiceManagerImpl;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
44 @RunWith(MockitoJUnitRunner.class)
45 public class ContextChainHolderImplTest {
47 private static final String ENTITY_TEST = "EntityTest";
48 private static final String OPENFLOW_TEST = "openflow:test";
49 private static final Short AUXILIARY_ID = 0;
51 private StatisticsManager statisticsManager;
53 private RpcManager rpcManager;
55 private DeviceManager deviceManager;
57 private RoleManager roleManager;
59 private StatisticsContext statisticsContext;
61 private RpcContext rpcContext;
63 private DeviceContext deviceContext;
65 private RoleContext roleContext;
67 private ConnectionContext connectionContext;
69 private DeviceInfo deviceInfo;
71 private ClusterSingletonServiceProvider singletonServicesProvider;
73 private ExecutorService executorService;
75 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
77 private EntityOwnershipService entityOwnershipService;
79 private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
81 private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
83 private FeaturesReply featuresReply;
85 private ContextChainHolderImpl contextChainHolder;
86 private ReconciliationFrameworkRegistration registration;
87 private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
90 public void setUp() throws Exception {
91 Mockito.doAnswer(invocation -> {
92 invocation.getArgumentAt(0, Runnable.class).run();
94 }).when(executorService).submit(Mockito.<Runnable>any());
97 Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
98 Mockito.when(deviceManager.createContext(connectionContext)).thenReturn(deviceContext);
99 Mockito.when(rpcManager.createContext(deviceContext)).thenReturn(rpcContext);
100 Mockito.when(roleManager.createContext(deviceContext)).thenReturn(roleContext);
101 Mockito.when(statisticsManager.createContext(Mockito.eq(deviceContext), Mockito.anyBoolean()))
102 .thenReturn(statisticsContext);
103 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
105 Mockito.when(singletonServicesProvider.registerClusterSingletonService(Mockito.any()))
106 .thenReturn(clusterSingletonServiceRegistration);
107 Mockito.when(entityOwnershipService.registerListener(Mockito.any(), Mockito.any()))
108 .thenReturn(entityOwnershipListenerRegistration);
109 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
110 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
112 registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
114 contextChainHolder = new ContextChainHolderImpl(
116 singletonServicesProvider,
117 entityOwnershipService,
119 contextChainHolder.addManager(statisticsManager);
120 contextChainHolder.addManager(rpcManager);
121 contextChainHolder.addManager(deviceManager);
122 contextChainHolder.addManager(roleManager);
126 public void addManager() throws Exception {
127 Assert.assertTrue(contextChainHolder.checkAllManagers());
131 public void createContextChain() throws Exception {
132 contextChainHolder.createContextChain(connectionContext);
133 Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class));
134 Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class));
135 Mockito.verify(roleManager).createContext(Mockito.any(DeviceContext.class));
136 Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class), Mockito.anyBoolean());
141 public void reconciliationFrameworkFailure() throws Exception {
142 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
143 .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(connectionContext).closeConnection(false);
153 public void reconciliationFrameworkDisconnect() throws Exception {
154 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
155 .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
156 contextChainHolder.createContextChain(connectionContext);
157 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
158 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
159 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
160 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
161 Mockito.verify(connectionContext).closeConnection(false);
165 public void reconciliationFrameworkSuccess() throws Exception {
166 contextChainHolder.createContextChain(connectionContext);
167 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
168 .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
169 contextChainHolder.createContextChain(connectionContext);
170 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
171 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
172 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
173 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
174 Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
178 public void reconciliationFrameworkSuccessButNotSubmit() throws Exception {
179 contextChainHolder.createContextChain(connectionContext);
180 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
181 .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
182 contextChainHolder.createContextChain(connectionContext);
183 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
184 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
185 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
186 contextChainHolder.onNotAbleToStartMastershipMandatory(deviceInfo, "Test reason");
187 Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
188 Mockito.verify(connectionContext).closeConnection(false);
192 public void deviceMastered() throws Exception {
193 registration.close();
194 contextChainHolder.createContextChain(connectionContext);
195 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
196 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
197 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
198 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
199 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
200 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
201 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
202 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
203 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
204 Assert.assertTrue(contextChainHolder.isAnyDeviceMastered());
205 Assert.assertTrue(contextChainHolder.listOfMasteredDevices().size() == 1);
209 public void deviceConnected() throws Exception {
210 registration.close();
211 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
212 == ConnectionStatus.MAY_CONTINUE);
213 Short auxiliaryId1 = 1;
214 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(auxiliaryId1);
215 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
216 == ConnectionStatus.MAY_CONTINUE);
217 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
218 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
219 == ConnectionStatus.MAY_CONTINUE);
223 public void notToAbleMastership() throws Exception {
224 registration.close();
225 contextChainHolder.deviceConnected(connectionContext);
226 contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true);
227 Mockito.verify(deviceContext).close();
228 Mockito.verify(statisticsContext).close();
229 Mockito.verify(rpcContext).close();
233 public void notAbleToSetSlave() throws Exception {
234 registration.close();
235 contextChainHolder.deviceConnected(connectionContext);
236 contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason");
237 Mockito.verify(deviceContext).close();
238 Mockito.verify(statisticsContext).close();
239 Mockito.verify(rpcContext).close();
243 public void deviceDisconnected() throws Exception {
244 registration.close();
245 contextChainHolder.createContextChain(connectionContext);
246 contextChainHolder.onDeviceDisconnected(connectionContext);
247 Mockito.verify(deviceContext).close();
248 Mockito.verify(statisticsContext).close();
249 Mockito.verify(rpcContext).close();
253 public void onClose() throws Exception {
254 registration.close();
255 contextChainHolder.createContextChain(connectionContext);
256 contextChainHolder.close();
257 Mockito.verify(deviceContext).close();
258 Mockito.verify(statisticsContext).close();
259 Mockito.verify(rpcContext).close();
263 public void ownershipChanged() throws Exception {
264 registration.close();
265 contextChainHolder.createContextChain(connectionContext);
266 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
267 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
268 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
269 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
270 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
271 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
272 new Entity(ENTITY_TEST, OPENFLOW_TEST),
277 contextChainHolder.ownershipChanged(ownershipChange);
278 Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
282 public void ownershipChangedButHasOwner() throws Exception {
283 registration.close();
284 contextChainHolder.createContextChain(connectionContext);
285 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
286 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
287 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
288 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
289 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
290 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
291 new Entity(ENTITY_TEST, OPENFLOW_TEST),
296 contextChainHolder.ownershipChanged(ownershipChange);
297 Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());