dfe08e01cc0a1ae8dfe4dec2e37c7442e7fb1f4d
[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.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.lifecycle.OwnershipChangeListener;
33 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
34 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
35 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration;
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     @Mock
50     private HashedWheelTimer timer;
51     @Mock
52     private StatisticsManager statisticsManager;
53     @Mock
54     private RpcManager rpcManager;
55     @Mock
56     private DeviceManager deviceManager;
57     @Mock
58     private StatisticsContext statisticsContext;
59     @Mock
60     private RpcContext rpcContext;
61     @Mock
62     private DeviceContext deviceContext;
63     @Mock
64     private ConnectionContext connectionContext;
65     @Mock
66     private DeviceInfo deviceInfo;
67     @Mock
68     private ClusterSingletonServiceProvider singletonServicesProvider;
69     @Mock
70     private ExecutorService executorService;
71     @Mock
72     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
73     @Mock
74     private EntityOwnershipService entityOwnershipService;
75     @Mock
76     private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
77     @Mock
78     private OwnershipChangeListener ownershipChangeListener;
79     @Mock
80     private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
81     @Mock
82     private FeaturesReply featuresReply;
83
84     private ContextChainHolderImpl contextChainHolder;
85     private ReconciliationFrameworkRegistration registration;
86     private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
87     private final Short AUXILIARY_ID = 0;
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(statisticsManager.createContext(deviceContext)).thenReturn(statisticsContext);
101         Mockito.when(deviceContext.makeDeviceSlave()).thenReturn(Futures.immediateFuture(null));
102         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
103
104         Mockito.when(singletonServicesProvider.registerClusterSingletonService(Mockito.any()))
105                 .thenReturn(clusterSingletonServiceRegistration);
106         Mockito.when(entityOwnershipService.registerListener(Mockito.any(), Mockito.any()))
107                 .thenReturn(entityOwnershipListenerRegistration);
108         Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
109         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
110
111         registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
112
113         contextChainHolder = new ContextChainHolderImpl(
114                 timer,
115                 executorService,
116                 singletonServicesProvider,
117                 entityOwnershipService,
118                 manager
119         );
120         contextChainHolder.addManager(statisticsManager);
121         contextChainHolder.addManager(rpcManager);
122         contextChainHolder.addManager(deviceManager);
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(statisticsManager).createContext(Mockito.any(DeviceContext.class));
136     }
137
138
139     @Test
140     public void reconciliationFrameworkFailure() throws Exception {
141         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
142         contextChainHolder.createContextChain(connectionContext);
143         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
144         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
145         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
146         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
147         Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
148     }
149
150     @Test
151     public void reconciliationFrameworkDisconnect() throws Exception {
152         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
153         contextChainHolder.createContextChain(connectionContext);
154         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
155         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
156         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
157         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
158         Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
159     }
160
161     @Test
162     public void reconciliationFrameworkSuccess() throws Exception {
163         contextChainHolder.createContextChain(connectionContext);
164         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
165         Mockito.when(statisticsContext.initialSubmitAfterReconciliation()).thenReturn(true);
166         contextChainHolder.createContextChain(connectionContext);
167         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
168         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
169         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
170         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
171         Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
172     }
173
174     @Test
175     public void reconciliationFrameworkSuccessButNotSubmit() throws Exception {
176         contextChainHolder.createContextChain(connectionContext);
177         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
178         Mockito.when(statisticsContext.initialSubmitAfterReconciliation()).thenReturn(false);
179         contextChainHolder.createContextChain(connectionContext);
180         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
181         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
182         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
183         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
184         Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
185     }
186
187     @Test
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);
202     }
203
204     @Test
205     public void deviceConnected() throws Exception {
206         registration.close();
207         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
208                 == ConnectionStatus.MAY_CONTINUE);
209         Short AUXILIARY_ID_1 = 1;
210         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID_1);
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.ALREADY_CONNECTED);
216     }
217
218     @Test
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();
226     }
227
228     @Test
229     public void notAbleToSetSlave() throws Exception {
230         registration.close();
231         contextChainHolder.deviceConnected(connectionContext);
232         contextChainHolder.onSlaveRoleNotAcquired(deviceInfo);
233         Mockito.verify(deviceContext).close();
234         Mockito.verify(statisticsContext).close();
235         Mockito.verify(rpcContext).close();
236     }
237
238     @Test
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();
246     }
247
248     @Test
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();
256     }
257
258     @Test
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                 true,
270                 false,
271                 false
272         );
273         contextChainHolder.ownershipChanged(ownershipChange);
274         Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
275     }
276     @Test
277
278     public void ownershipChangedButHasOwner() throws Exception {
279         registration.close();
280         contextChainHolder.createContextChain(connectionContext);
281         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
282         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
283         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
284         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
285         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
286         EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
287                 new Entity(ENTITY_TEST, OPENFLOW_TEST),
288                 true,
289                 false,
290                 true
291         );
292         contextChainHolder.ownershipChanged(ownershipChange);
293         Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());
294     }
295 }