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.junit.MockitoJUnitRunner;
19 import org.opendaylight.mdsal.eos.binding.api.Entity;
20 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
21 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
22 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
23 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
31 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
32 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
33 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
34 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration;
35 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
36 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
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.openflow.provider.config.rev160510.NonZeroUint32Type;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
46 import org.opendaylight.yangtools.yang.common.Uint8;
48 @RunWith(MockitoJUnitRunner.class)
49 public class ContextChainHolderImplTest {
51 private static final String ENTITY_TEST = "EntityTest";
52 private static final String OPENFLOW_TEST = "openflow:test";
53 private static final Uint8 AUXILIARY_ID = Uint8.ZERO;
54 private static final Long DEVICE_DATASTORE_REMOVAL_DELAY = 500L;
56 private StatisticsManager statisticsManager;
58 private RpcManager rpcManager;
60 private DeviceManager deviceManager;
62 private RoleManager roleManager;
64 private StatisticsContext statisticsContext;
66 private RpcContext rpcContext;
68 private DeviceContext deviceContext;
70 private RoleContext roleContext;
72 private ConnectionContext connectionContext;
74 private DeviceInfo deviceInfo;
76 private ClusterSingletonServiceProvider singletonServicesProvider;
78 private ExecutorService executorService;
80 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
82 private EntityOwnershipService entityOwnershipService;
84 private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
86 private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
88 private FeaturesReply featuresReply;
90 private OpenflowProviderConfig config;
92 private ContextChainHolderImpl contextChainHolder;
93 private ReconciliationFrameworkRegistration registration;
94 private final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
97 public void setUp() throws Exception {
98 Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
99 Mockito.when(deviceManager.createContext(connectionContext)).thenReturn(deviceContext);
100 Mockito.when(rpcManager.createContext(deviceContext)).thenReturn(rpcContext);
101 Mockito.when(roleManager.createContext(deviceContext)).thenReturn(roleContext);
102 Mockito.when(statisticsManager.createContext(Mockito.eq(deviceContext), Mockito.anyBoolean()))
103 .thenReturn(statisticsContext);
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);
112 Mockito.when(config.getDeviceDatastoreRemovalDelay())
113 .thenReturn(new NonZeroUint32Type(DEVICE_DATASTORE_REMOVAL_DELAY));
115 registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
117 contextChainHolder = new ContextChainHolderImpl(
119 singletonServicesProvider,
120 entityOwnershipService,
123 contextChainHolder.addManager(statisticsManager);
124 contextChainHolder.addManager(rpcManager);
125 contextChainHolder.addManager(deviceManager);
126 contextChainHolder.addManager(roleManager);
130 public void addManager() {
131 Assert.assertTrue(contextChainHolder.checkAllManagers());
135 public void createContextChain() {
136 contextChainHolder.createContextChain(connectionContext);
137 Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class));
138 Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class));
139 Mockito.verify(roleManager).createContext(Mockito.any(DeviceContext.class));
140 Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class), Mockito.anyBoolean());
145 public void reconciliationFrameworkFailure() {
146 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
147 .thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
148 contextChainHolder.createContextChain(connectionContext);
149 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
150 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
151 Mockito.verify(connectionContext).closeConnection(false);
155 public void reconciliationFrameworkDisconnect() {
156 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
157 .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
158 contextChainHolder.createContextChain(connectionContext);
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() {
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.RPC_REGISTRATION);
171 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
172 Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
176 public void reconciliationFrameworkSuccessButNotSubmit() {
177 contextChainHolder.createContextChain(connectionContext);
178 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
179 .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
180 // TODO when if (future != null) check in MastershipChangeServiceManagerImpl's becomeSlaveOrDisconnect() is rm
181 // Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
182 // .thenReturn(Futures.immediateFuture(null));
183 contextChainHolder.createContextChain(connectionContext);
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.RPC_REGISTRATION);
196 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
197 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
198 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
199 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
200 Assert.assertTrue(contextChainHolder.isAnyDeviceMastered());
201 Assert.assertTrue(contextChainHolder.listOfMasteredDevices().size() == 1);
205 public void deviceConnected() throws Exception {
206 registration.close();
207 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
208 == ConnectionStatus.MAY_CONTINUE);
209 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(Uint8.ONE);
210 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
211 == ConnectionStatus.MAY_CONTINUE);
212 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
213 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
214 == ConnectionStatus.MAY_CONTINUE);
218 public void notToAbleMastership() throws Exception {
219 registration.close();
220 contextChainHolder.deviceConnected(connectionContext);
221 contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true);
222 Mockito.verify(deviceContext).close();
223 Mockito.verify(statisticsContext).close();
224 Mockito.verify(rpcContext).close();
228 public void notAbleToSetSlave() throws Exception {
229 registration.close();
230 contextChainHolder.deviceConnected(connectionContext);
231 contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason");
232 Mockito.verify(deviceContext).close();
233 Mockito.verify(statisticsContext).close();
234 Mockito.verify(rpcContext).close();
238 public void deviceDisconnected() throws Exception {
239 registration.close();
240 contextChainHolder.createContextChain(connectionContext);
241 contextChainHolder.onDeviceDisconnected(connectionContext);
242 Mockito.verify(deviceContext).close();
243 Mockito.verify(statisticsContext).close();
244 Mockito.verify(rpcContext).close();
248 public void onClose() throws Exception {
249 registration.close();
250 contextChainHolder.createContextChain(connectionContext);
251 contextChainHolder.close();
252 Mockito.verify(deviceContext).close();
253 Mockito.verify(statisticsContext).close();
254 Mockito.verify(rpcContext).close();
258 public void ownershipChanged() throws Exception {
259 registration.close();
260 contextChainHolder.createContextChain(connectionContext);
261 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
262 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
263 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
264 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
265 new Entity(ENTITY_TEST, OPENFLOW_TEST),
266 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER
268 contextChainHolder.ownershipChanged(ownershipChange);
269 Mockito.verify(deviceManager, Mockito.timeout(1000)).removeDeviceFromOperationalDS(Mockito.any());
273 public void ownershipChangedButHasOwner() throws Exception {
274 registration.close();
275 contextChainHolder.createContextChain(connectionContext);
276 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
277 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
278 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
279 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
280 new Entity(ENTITY_TEST, OPENFLOW_TEST),
281 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER
283 contextChainHolder.ownershipChanged(ownershipChange);
284 Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());