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 final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
91 public void setUp() throws Exception {
92 Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
93 Mockito.when(deviceManager.createContext(connectionContext)).thenReturn(deviceContext);
94 Mockito.when(rpcManager.createContext(deviceContext)).thenReturn(rpcContext);
95 Mockito.when(roleManager.createContext(deviceContext)).thenReturn(roleContext);
96 Mockito.when(statisticsManager.createContext(Mockito.eq(deviceContext), Mockito.anyBoolean()))
97 .thenReturn(statisticsContext);
98 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
100 Mockito.when(singletonServicesProvider.registerClusterSingletonService(Mockito.any()))
101 .thenReturn(clusterSingletonServiceRegistration);
102 Mockito.when(entityOwnershipService.registerListener(Mockito.any(), Mockito.any()))
103 .thenReturn(entityOwnershipListenerRegistration);
104 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
105 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
107 registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
109 contextChainHolder = new ContextChainHolderImpl(
111 singletonServicesProvider,
112 entityOwnershipService,
114 contextChainHolder.addManager(statisticsManager);
115 contextChainHolder.addManager(rpcManager);
116 contextChainHolder.addManager(deviceManager);
117 contextChainHolder.addManager(roleManager);
121 public void addManager() throws Exception {
122 Assert.assertTrue(contextChainHolder.checkAllManagers());
126 public void createContextChain() throws Exception {
127 contextChainHolder.createContextChain(connectionContext);
128 Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class));
129 Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class));
130 Mockito.verify(roleManager).createContext(Mockito.any(DeviceContext.class));
131 Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class), Mockito.anyBoolean());
136 public void reconciliationFrameworkFailure() throws Exception {
137 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
138 .thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
139 contextChainHolder.createContextChain(connectionContext);
140 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
141 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
142 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
143 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
144 Mockito.verify(connectionContext).closeConnection(false);
148 public void reconciliationFrameworkDisconnect() throws Exception {
149 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
150 .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
151 contextChainHolder.createContextChain(connectionContext);
152 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
153 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
154 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
155 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
156 Mockito.verify(connectionContext).closeConnection(false);
160 public void reconciliationFrameworkSuccess() throws Exception {
161 contextChainHolder.createContextChain(connectionContext);
162 Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
163 .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
164 contextChainHolder.createContextChain(connectionContext);
165 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
166 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
167 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
168 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
169 Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
173 public void reconciliationFrameworkSuccessButNotSubmit() throws Exception {
174 contextChainHolder.createContextChain(connectionContext);
175 // TODO when if (future != null) check in MastershipChangeServiceManagerImpl's becomeSlaveOrDisconnect() is rm
176 // Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
177 // .thenReturn(Futures.immediateFuture(null));
178 contextChainHolder.createContextChain(connectionContext);
179 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
180 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
181 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
182 contextChainHolder.onNotAbleToStartMastershipMandatory(deviceInfo, "Test reason");
183 Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
184 Mockito.verify(connectionContext).closeConnection(false);
188 public void deviceMastered() throws Exception {
189 registration.close();
190 contextChainHolder.createContextChain(connectionContext);
191 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
192 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
193 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
194 Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
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 Short auxiliaryId1 = 1;
210 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(auxiliaryId1);
211 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
212 == ConnectionStatus.MAY_CONTINUE);
213 Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
214 Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
215 == ConnectionStatus.MAY_CONTINUE);
219 public void notToAbleMastership() throws Exception {
220 registration.close();
221 contextChainHolder.deviceConnected(connectionContext);
222 contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true);
223 Mockito.verify(deviceContext).close();
224 Mockito.verify(statisticsContext).close();
225 Mockito.verify(rpcContext).close();
229 public void notAbleToSetSlave() throws Exception {
230 registration.close();
231 contextChainHolder.deviceConnected(connectionContext);
232 contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason");
233 Mockito.verify(deviceContext).close();
234 Mockito.verify(statisticsContext).close();
235 Mockito.verify(rpcContext).close();
239 public void deviceDisconnected() throws Exception {
240 registration.close();
241 contextChainHolder.createContextChain(connectionContext);
242 contextChainHolder.onDeviceDisconnected(connectionContext);
243 Mockito.verify(deviceContext).close();
244 Mockito.verify(statisticsContext).close();
245 Mockito.verify(rpcContext).close();
249 public void onClose() throws Exception {
250 registration.close();
251 contextChainHolder.createContextChain(connectionContext);
252 contextChainHolder.close();
253 Mockito.verify(deviceContext).close();
254 Mockito.verify(statisticsContext).close();
255 Mockito.verify(rpcContext).close();
259 public void ownershipChanged() throws Exception {
260 registration.close();
261 contextChainHolder.createContextChain(connectionContext);
262 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
263 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
264 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
265 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
266 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
267 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
268 new Entity(ENTITY_TEST, OPENFLOW_TEST),
269 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER
271 contextChainHolder.ownershipChanged(ownershipChange);
272 Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
276 public void ownershipChangedButHasOwner() throws Exception {
277 registration.close();
278 contextChainHolder.createContextChain(connectionContext);
279 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
280 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
281 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
282 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
283 contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
284 EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
285 new Entity(ENTITY_TEST, OPENFLOW_TEST),
286 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER
288 contextChainHolder.ownershipChanged(ownershipChange);
289 Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());