Merge "OPNFLWPLUG-1027 : Topology manager writes link information everytime topology...
[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 final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
89
90     @Before
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);
99
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);
106
107         registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
108
109         contextChainHolder = new ContextChainHolderImpl(
110                 executorService,
111                 singletonServicesProvider,
112                 entityOwnershipService,
113                 manager);
114         contextChainHolder.addManager(statisticsManager);
115         contextChainHolder.addManager(rpcManager);
116         contextChainHolder.addManager(deviceManager);
117         contextChainHolder.addManager(roleManager);
118     }
119
120     @Test
121     public void addManager() {
122         Assert.assertTrue(contextChainHolder.checkAllManagers());
123     }
124
125     @Test
126     public void createContextChain() {
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());
132     }
133
134
135     @Test
136     public void reconciliationFrameworkFailure() {
137         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
138             .thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
139         contextChainHolder.createContextChain(connectionContext);
140         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
141         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
142         Mockito.verify(connectionContext).closeConnection(false);
143     }
144
145     @Test
146     public void reconciliationFrameworkDisconnect() {
147         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
148             .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
149         contextChainHolder.createContextChain(connectionContext);
150         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
151         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
152         Mockito.verify(connectionContext).closeConnection(false);
153     }
154
155     @Test
156     public void reconciliationFrameworkSuccess() {
157         contextChainHolder.createContextChain(connectionContext);
158         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
159             .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
160         contextChainHolder.createContextChain(connectionContext);
161         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
162         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
163         Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
164     }
165
166     @Test
167     public void reconciliationFrameworkSuccessButNotSubmit() {
168         contextChainHolder.createContextChain(connectionContext);
169         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
170                 .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
171         // TODO when if (future != null) check in MastershipChangeServiceManagerImpl's becomeSlaveOrDisconnect() is rm
172         // Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
173         //    .thenReturn(Futures.immediateFuture(null));
174         contextChainHolder.createContextChain(connectionContext);
175         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
176         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
177         contextChainHolder.onNotAbleToStartMastershipMandatory(deviceInfo, "Test reason");
178         Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
179         Mockito.verify(connectionContext).closeConnection(false);
180     }
181
182     @Test
183     public void deviceMastered() throws Exception {
184         registration.close();
185         contextChainHolder.createContextChain(connectionContext);
186         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
187         Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
188         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
189         Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
190         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
191         Assert.assertTrue(contextChainHolder.isAnyDeviceMastered());
192         Assert.assertTrue(contextChainHolder.listOfMasteredDevices().size() == 1);
193     }
194
195     @Test
196     public void deviceConnected() throws Exception {
197         registration.close();
198         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
199                 == ConnectionStatus.MAY_CONTINUE);
200         Short auxiliaryId1 = 1;
201         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(auxiliaryId1);
202         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
203                 == ConnectionStatus.MAY_CONTINUE);
204         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
205         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
206                 == ConnectionStatus.MAY_CONTINUE);
207     }
208
209     @Test
210     public void notToAbleMastership() throws Exception {
211         registration.close();
212         contextChainHolder.deviceConnected(connectionContext);
213         contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true);
214         Mockito.verify(deviceContext).close();
215         Mockito.verify(statisticsContext).close();
216         Mockito.verify(rpcContext).close();
217     }
218
219     @Test
220     public void notAbleToSetSlave() throws Exception {
221         registration.close();
222         contextChainHolder.deviceConnected(connectionContext);
223         contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason");
224         Mockito.verify(deviceContext).close();
225         Mockito.verify(statisticsContext).close();
226         Mockito.verify(rpcContext).close();
227     }
228
229     @Test
230     public void deviceDisconnected() throws Exception {
231         registration.close();
232         contextChainHolder.createContextChain(connectionContext);
233         contextChainHolder.onDeviceDisconnected(connectionContext);
234         Mockito.verify(deviceContext).close();
235         Mockito.verify(statisticsContext).close();
236         Mockito.verify(rpcContext).close();
237     }
238
239     @Test
240     public void onClose() throws Exception {
241         registration.close();
242         contextChainHolder.createContextChain(connectionContext);
243         contextChainHolder.close();
244         Mockito.verify(deviceContext).close();
245         Mockito.verify(statisticsContext).close();
246         Mockito.verify(rpcContext).close();
247     }
248
249     @Test
250     public void ownershipChanged() throws Exception {
251         registration.close();
252         contextChainHolder.createContextChain(connectionContext);
253         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
254         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
255         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
256         EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
257                 new Entity(ENTITY_TEST, OPENFLOW_TEST),
258                 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER
259         );
260         contextChainHolder.ownershipChanged(ownershipChange);
261         Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
262     }
263
264     @Test
265     public void ownershipChangedButHasOwner() throws Exception {
266         registration.close();
267         contextChainHolder.createContextChain(connectionContext);
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                 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER
274         );
275         contextChainHolder.ownershipChanged(ownershipChange);
276         Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());
277     }
278 }