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.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.openflowplugin.rf.state.rev170713.ResultState;
45 @RunWith(MockitoJUnitRunner.class)
46 public class ContextChainHolderImplTest {
48 private static final String ENTITY_TEST = "EntityTest";
49 private static final String OPENFLOW_TEST = "openflow:test";
50 private static final Short AUXILIARY_ID = 0;
52 private StatisticsManager statisticsManager;
54 private RpcManager rpcManager;
56 private DeviceManager deviceManager;
58 private RoleManager roleManager;
60 private StatisticsContext statisticsContext;
62 private RpcContext rpcContext;
64 private DeviceContext deviceContext;
66 private RoleContext roleContext;
68 private ConnectionContext connectionContext;
70 private DeviceInfo deviceInfo;
72 private ClusterSingletonServiceProvider singletonServicesProvider;
74 private ExecutorService executorService;
76 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
78 private EntityOwnershipService entityOwnershipService;
80 private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
82 private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
84 private FeaturesReply featuresReply;
86 private ContextChainHolderImpl contextChainHolder;
87 private ReconciliationFrameworkRegistration registration;
88 private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
91 public void setUp() throws Exception {
93 Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
94 Mockito.when(deviceManager.createContext(connectionContext)).thenReturn(deviceContext);
95 Mockito.when(rpcManager.createContext(deviceContext)).thenReturn(rpcContext);
96 Mockito.when(roleManager.createContext(deviceContext)).thenReturn(roleContext);
97 Mockito.when(statisticsManager.createContext(Mockito.eq(deviceContext), Mockito.anyBoolean()))
98 .thenReturn(statisticsContext);
99 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
101 Mockito.when(singletonServicesProvider.registerClusterSingletonService(Mockito.any()))
102 .thenReturn(clusterSingletonServiceRegistration);
103 Mockito.when(entityOwnershipService.registerListener(Mockito.any(), Mockito.any()))
104 .thenReturn(entityOwnershipListenerRegistration);
105 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
106 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
108 registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
110 contextChainHolder = new ContextChainHolderImpl(
112 singletonServicesProvider,
113 entityOwnershipService,
115 contextChainHolder.addManager(statisticsManager);
116 contextChainHolder.addManager(rpcManager);
117 contextChainHolder.addManager(deviceManager);
118 contextChainHolder.addManager(roleManager);
122 public void addManager() throws Exception {
123 Assert.assertTrue(contextChainHolder.checkAllManagers());
127 public void createContextChain() throws Exception {
128 contextChainHolder.createContextChain(connectionContext);
129 Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class));
130 Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class));
131 Mockito.verify(roleManager).createContext(Mockito.any(DeviceContext.class));
132 Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class), Mockito.anyBoolean());
137 public void reconciliationFrameworkFailure() throws Exception {
138 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
139 .thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
140 contextChainHolder.createContextChain(connectionContext);
141 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
142 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
143 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
144 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
145 Mockito.verify(connectionContext).closeConnection(false);
149 public void reconciliationFrameworkDisconnect() throws Exception {
150 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
151 .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
152 contextChainHolder.createContextChain(connectionContext);
153 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
154 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
155 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
156 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
157 Mockito.verify(connectionContext).closeConnection(false);
161 public void reconciliationFrameworkSuccess() throws Exception {
162 contextChainHolder.createContextChain(connectionContext);
163 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
164 .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
165 contextChainHolder.createContextChain(connectionContext);
166 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
167 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
168 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
169 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
170 Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
174 public void reconciliationFrameworkSuccessButNotSubmit() throws Exception {
175 contextChainHolder.createContextChain(connectionContext);
176 contextChainHolder.createContextChain(connectionContext);
177 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
178 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
179 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
180 contextChainHolder.onNotAbleToStartMastershipMandatory(deviceInfo, "Test reason");
181 Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
182 Mockito.verify(connectionContext).closeConnection(false);
186 public void deviceMastered() throws Exception {
187 registration.close();
188 contextChainHolder.createContextChain(connectionContext);
189 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
190 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
191 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
192 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
193 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
194 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
195 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
196 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
197 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
198 Assert.assertTrue(contextChainHolder.isAnyDeviceMastered());
199 Assert.assertTrue(contextChainHolder.listOfMasteredDevices().size() == 1);
203 public void deviceConnected() throws Exception {
204 registration.close();
205 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
206 == ConnectionStatus.MAY_CONTINUE);
207 Short auxiliaryId1 = 1;
208 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(auxiliaryId1);
209 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
210 == ConnectionStatus.MAY_CONTINUE);
211 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
212 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
213 == ConnectionStatus.MAY_CONTINUE);
217 public void notToAbleMastership() throws Exception {
218 registration.close();
219 contextChainHolder.deviceConnected(connectionContext);
220 contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true);
221 Mockito.verify(deviceContext).close();
222 Mockito.verify(statisticsContext).close();
223 Mockito.verify(rpcContext).close();
227 public void notAbleToSetSlave() throws Exception {
228 registration.close();
229 contextChainHolder.deviceConnected(connectionContext);
230 contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason");
231 Mockito.verify(deviceContext).close();
232 Mockito.verify(statisticsContext).close();
233 Mockito.verify(rpcContext).close();
237 public void deviceDisconnected() throws Exception {
238 registration.close();
239 contextChainHolder.createContextChain(connectionContext);
240 contextChainHolder.onDeviceDisconnected(connectionContext);
241 Mockito.verify(deviceContext).close();
242 Mockito.verify(statisticsContext).close();
243 Mockito.verify(rpcContext).close();
247 public void onClose() throws Exception {
248 registration.close();
249 contextChainHolder.createContextChain(connectionContext);
250 contextChainHolder.close();
251 Mockito.verify(deviceContext).close();
252 Mockito.verify(statisticsContext).close();
253 Mockito.verify(rpcContext).close();
257 public void ownershipChanged() throws Exception {
258 registration.close();
259 contextChainHolder.createContextChain(connectionContext);
260 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
261 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
262 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
263 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
264 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
265 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
266 new Entity(ENTITY_TEST, OPENFLOW_TEST),
267 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER
269 contextChainHolder.ownershipChanged(ownershipChange);
270 Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
274 public void ownershipChangedButHasOwner() throws Exception {
275 registration.close();
276 contextChainHolder.createContextChain(connectionContext);
277 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
278 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
279 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
280 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
281 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
282 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
283 new Entity(ENTITY_TEST, OPENFLOW_TEST),
284 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER
286 contextChainHolder.ownershipChanged(ownershipChange);
287 Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());