Remove deprecated CheckedFuture.
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainHolderImplTest.java
1 /**
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.lifecycle;
9
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;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class ContextChainHolderImplTest {
48
49     private static final String ENTITY_TEST = "EntityTest";
50     private static final String OPENFLOW_TEST = "openflow:test";
51     @Mock
52     private HashedWheelTimer timer;
53     @Mock
54     private StatisticsManager statisticsManager;
55     @Mock
56     private RpcManager rpcManager;
57     @Mock
58     private DeviceManager deviceManager;
59     @Mock
60     private StatisticsContext statisticsContext;
61     @Mock
62     private RpcContext rpcContext;
63     @Mock
64     private DeviceContext deviceContext;
65     @Mock
66     private ConnectionContext connectionContext;
67     @Mock
68     private DeviceInfo deviceInfo;
69     @Mock
70     private ClusterSingletonServiceProvider singletonServicesProvider;
71     @Mock
72     private ExecutorService executorService;
73     @Mock
74     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
75     @Mock
76     private EntityOwnershipService entityOwnershipService;
77     @Mock
78     private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
79     @Mock
80     private OwnershipChangeListener ownershipChangeListener;
81     @Mock
82     private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
83     @Mock
84     private FeaturesReply featuresReply;
85
86     private ContextChainHolderImpl contextChainHolder;
87     private ReconciliationFrameworkRegistration registration;
88     private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
89     private final Short AUXILIARY_ID = 0;
90
91     @Before
92     public void setUp() throws Exception {
93         Mockito.doAnswer(invocation -> {
94             invocation.getArgumentAt(0, Runnable.class).run();
95             return null;
96         }).when(executorService).submit(Mockito.<Runnable>any());
97
98
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);
105
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
113         registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
114
115         contextChainHolder = new ContextChainHolderImpl(
116                 timer,
117                 executorService,
118                 singletonServicesProvider,
119                 entityOwnershipService,
120                 manager
121         );
122         contextChainHolder.addManager(statisticsManager);
123         contextChainHolder.addManager(rpcManager);
124         contextChainHolder.addManager(deviceManager);
125     }
126
127     @Test
128     public void addManager() throws Exception {
129         Assert.assertTrue(contextChainHolder.checkAllManagers());
130     }
131
132     @Test
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));
138     }
139
140
141     @Test
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);
150     }
151
152     @Test
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);
161     }
162
163     @Test
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);
174     }
175
176     @Test
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);
187     }
188
189     @Test
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);
204     }
205
206     @Test
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);
218     }
219
220     @Test
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();
228     }
229
230     @Test
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();
238     }
239
240     @Test
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();
248     }
249
250     @Test
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();
258     }
259
260     @Test
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),
271                 true,
272                 false,
273                 false
274         );
275         contextChainHolder.ownershipChanged(ownershipChange);
276         Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
277     }
278
279     @Test
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),
290                 true,
291                 false,
292                 true
293         );
294         contextChainHolder.ownershipChanged(ownershipChange);
295         Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());
296     }
297 }