Merge "Remove dependency on sal-common-util"
[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.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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class ContextChainHolderImplTest {
47
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;
51     @Mock
52     private StatisticsManager statisticsManager;
53     @Mock
54     private RpcManager rpcManager;
55     @Mock
56     private DeviceManager deviceManager;
57     @Mock
58     private RoleManager roleManager;
59     @Mock
60     private StatisticsContext statisticsContext;
61     @Mock
62     private RpcContext rpcContext;
63     @Mock
64     private DeviceContext deviceContext;
65     @Mock
66     private RoleContext roleContext;
67     @Mock
68     private ConnectionContext connectionContext;
69     @Mock
70     private DeviceInfo deviceInfo;
71     @Mock
72     private ClusterSingletonServiceProvider singletonServicesProvider;
73     @Mock
74     private ExecutorService executorService;
75     @Mock
76     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
77     @Mock
78     private EntityOwnershipService entityOwnershipService;
79     @Mock
80     private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
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
90     @Before
91     public void setUp() throws Exception {
92         Mockito.doAnswer(invocation -> {
93             invocation.getArgumentAt(0, Runnable.class).run();
94             return null;
95         }).when(executorService).submit(Mockito.<Runnable>any());
96
97
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);
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                 executorService,
117                 singletonServicesProvider,
118                 entityOwnershipService,
119                 manager);
120         contextChainHolder.addManager(statisticsManager);
121         contextChainHolder.addManager(rpcManager);
122         contextChainHolder.addManager(deviceManager);
123         contextChainHolder.addManager(roleManager);
124     }
125
126     @Test
127     public void addManager() throws Exception {
128         Assert.assertTrue(contextChainHolder.checkAllManagers());
129     }
130
131     @Test
132     public void createContextChain() throws Exception {
133         contextChainHolder.createContextChain(connectionContext);
134         Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class));
135         Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class));
136         Mockito.verify(roleManager).createContext(Mockito.any(DeviceContext.class));
137         Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class), Mockito.anyBoolean());
138     }
139
140
141     @Test
142     public void reconciliationFrameworkFailure() throws Exception {
143         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
144             .thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
145         contextChainHolder.createContextChain(connectionContext);
146         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
147         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
148         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
149         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
150         Mockito.verify(connectionContext).closeConnection(false);
151     }
152
153     @Test
154     public void reconciliationFrameworkDisconnect() throws Exception {
155         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
156             .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
157         contextChainHolder.createContextChain(connectionContext);
158         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
159         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
160         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
161         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
162         Mockito.verify(connectionContext).closeConnection(false);
163     }
164
165     @Test
166     public void reconciliationFrameworkSuccess() throws Exception {
167         contextChainHolder.createContextChain(connectionContext);
168         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
169             .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
170         contextChainHolder.createContextChain(connectionContext);
171         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
172         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
173         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
174         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
175         Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
176     }
177
178     @Test
179     public void reconciliationFrameworkSuccessButNotSubmit() throws Exception {
180         contextChainHolder.createContextChain(connectionContext);
181         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
182             .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
183         contextChainHolder.createContextChain(connectionContext);
184         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
185         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
186         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
187         contextChainHolder.onNotAbleToStartMastershipMandatory(deviceInfo, "Test reason");
188         Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
189         Mockito.verify(connectionContext).closeConnection(false);
190     }
191
192     @Test
193     public void deviceMastered() throws Exception {
194         registration.close();
195         contextChainHolder.createContextChain(connectionContext);
196         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
197         Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
198         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
199         Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
200         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
201         Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
202         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
203         Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
204         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
205         Assert.assertTrue(contextChainHolder.isAnyDeviceMastered());
206         Assert.assertTrue(contextChainHolder.listOfMasteredDevices().size() == 1);
207     }
208
209     @Test
210     public void deviceConnected() throws Exception {
211         registration.close();
212         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
213                 == ConnectionStatus.MAY_CONTINUE);
214         Short auxiliaryId1 = 1;
215         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(auxiliaryId1);
216         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
217                 == ConnectionStatus.MAY_CONTINUE);
218         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
219         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
220                 == ConnectionStatus.MAY_CONTINUE);
221     }
222
223     @Test
224     public void notToAbleMastership() throws Exception {
225         registration.close();
226         contextChainHolder.deviceConnected(connectionContext);
227         contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true);
228         Mockito.verify(deviceContext).close();
229         Mockito.verify(statisticsContext).close();
230         Mockito.verify(rpcContext).close();
231     }
232
233     @Test
234     public void notAbleToSetSlave() throws Exception {
235         registration.close();
236         contextChainHolder.deviceConnected(connectionContext);
237         contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason");
238         Mockito.verify(deviceContext).close();
239         Mockito.verify(statisticsContext).close();
240         Mockito.verify(rpcContext).close();
241     }
242
243     @Test
244     public void deviceDisconnected() throws Exception {
245         registration.close();
246         contextChainHolder.createContextChain(connectionContext);
247         contextChainHolder.onDeviceDisconnected(connectionContext);
248         Mockito.verify(deviceContext).close();
249         Mockito.verify(statisticsContext).close();
250         Mockito.verify(rpcContext).close();
251     }
252
253     @Test
254     public void onClose() throws Exception {
255         registration.close();
256         contextChainHolder.createContextChain(connectionContext);
257         contextChainHolder.close();
258         Mockito.verify(deviceContext).close();
259         Mockito.verify(statisticsContext).close();
260         Mockito.verify(rpcContext).close();
261     }
262
263     @Test
264     public void ownershipChanged() throws Exception {
265         registration.close();
266         contextChainHolder.createContextChain(connectionContext);
267         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
268         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
269         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
270         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
271         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
272         EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
273                 new Entity(ENTITY_TEST, OPENFLOW_TEST),
274                 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER
275         );
276         contextChainHolder.ownershipChanged(ownershipChange);
277         Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
278     }
279
280     @Test
281     public void ownershipChangedButHasOwner() throws Exception {
282         registration.close();
283         contextChainHolder.createContextChain(connectionContext);
284         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
285         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
286         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
287         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
288         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
289         EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
290                 new Entity(ENTITY_TEST, OPENFLOW_TEST),
291                 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER
292         );
293         contextChainHolder.ownershipChanged(ownershipChange);
294         Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());
295     }
296 }