Merge "Fix codestyle"
[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 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;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class ContextChainHolderImplTest {
46
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;
50     @Mock
51     private StatisticsManager statisticsManager;
52     @Mock
53     private RpcManager rpcManager;
54     @Mock
55     private DeviceManager deviceManager;
56     @Mock
57     private RoleManager roleManager;
58     @Mock
59     private StatisticsContext statisticsContext;
60     @Mock
61     private RpcContext rpcContext;
62     @Mock
63     private DeviceContext deviceContext;
64     @Mock
65     private RoleContext roleContext;
66     @Mock
67     private ConnectionContext connectionContext;
68     @Mock
69     private DeviceInfo deviceInfo;
70     @Mock
71     private ClusterSingletonServiceProvider singletonServicesProvider;
72     @Mock
73     private ExecutorService executorService;
74     @Mock
75     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
76     @Mock
77     private EntityOwnershipService entityOwnershipService;
78     @Mock
79     private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
80     @Mock
81     private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
82     @Mock
83     private FeaturesReply featuresReply;
84
85     private ContextChainHolderImpl contextChainHolder;
86     private ReconciliationFrameworkRegistration registration;
87     private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
88
89     @Before
90     public void setUp() throws Exception {
91         Mockito.doAnswer(invocation -> {
92             invocation.getArgumentAt(0, Runnable.class).run();
93             return null;
94         }).when(executorService).submit(Mockito.<Runnable>any());
95
96
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);
104
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);
111
112         registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
113
114         contextChainHolder = new ContextChainHolderImpl(
115                 executorService,
116                 singletonServicesProvider,
117                 entityOwnershipService,
118                 manager);
119         contextChainHolder.addManager(statisticsManager);
120         contextChainHolder.addManager(rpcManager);
121         contextChainHolder.addManager(deviceManager);
122         contextChainHolder.addManager(roleManager);
123     }
124
125     @Test
126     public void addManager() throws Exception {
127         Assert.assertTrue(contextChainHolder.checkAllManagers());
128     }
129
130     @Test
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());
137     }
138
139
140     @Test
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);
150     }
151
152     @Test
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);
162     }
163
164     @Test
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);
175     }
176
177     @Test
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);
189     }
190
191     @Test
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);
206     }
207
208     @Test
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);
220     }
221
222     @Test
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();
230     }
231
232     @Test
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();
240     }
241
242     @Test
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();
250     }
251
252     @Test
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();
260     }
261
262     @Test
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),
273                 true,
274                 false,
275                 false
276         );
277         contextChainHolder.ownershipChanged(ownershipChange);
278         Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
279     }
280
281     @Test
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),
292                 true,
293                 false,
294                 true
295         );
296         contextChainHolder.ownershipChanged(ownershipChange);
297         Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());
298     }
299 }