OPNFLWPLUG-1080: Nodes not populated in operational/opendaylight-inventory:nodes...
[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.junit.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.openflow.provider.config.rev160510.NonZeroUint32Type;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
46 import org.opendaylight.yangtools.yang.common.Uint8;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class ContextChainHolderImplTest {
50
51     private static final String ENTITY_TEST = "EntityTest";
52     private static final String OPENFLOW_TEST = "openflow:test";
53     private static final Uint8 AUXILIARY_ID = Uint8.ZERO;
54     private static final Long DEVICE_DATASTORE_REMOVAL_DELAY = 500L;
55     @Mock
56     private StatisticsManager statisticsManager;
57     @Mock
58     private RpcManager rpcManager;
59     @Mock
60     private DeviceManager deviceManager;
61     @Mock
62     private RoleManager roleManager;
63     @Mock
64     private StatisticsContext statisticsContext;
65     @Mock
66     private RpcContext rpcContext;
67     @Mock
68     private DeviceContext deviceContext;
69     @Mock
70     private RoleContext roleContext;
71     @Mock
72     private ConnectionContext connectionContext;
73     @Mock
74     private DeviceInfo deviceInfo;
75     @Mock
76     private ClusterSingletonServiceProvider singletonServicesProvider;
77     @Mock
78     private ExecutorService executorService;
79     @Mock
80     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
81     @Mock
82     private EntityOwnershipService entityOwnershipService;
83     @Mock
84     private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
85     @Mock
86     private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
87     @Mock
88     private FeaturesReply featuresReply;
89     @Mock
90     private OpenflowProviderConfig config;
91
92     private ContextChainHolderImpl contextChainHolder;
93     private ReconciliationFrameworkRegistration registration;
94     private final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
95
96     @Before
97     public void setUp() throws Exception {
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         Mockito.when(config.getDeviceDatastoreRemovalDelay())
113                 .thenReturn(new NonZeroUint32Type(DEVICE_DATASTORE_REMOVAL_DELAY));
114
115         registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
116
117         contextChainHolder = new ContextChainHolderImpl(
118                 executorService,
119                 singletonServicesProvider,
120                 entityOwnershipService,
121                 manager,
122                 config);
123         contextChainHolder.addManager(statisticsManager);
124         contextChainHolder.addManager(rpcManager);
125         contextChainHolder.addManager(deviceManager);
126         contextChainHolder.addManager(roleManager);
127     }
128
129     @Test
130     public void addManager() {
131         Assert.assertTrue(contextChainHolder.checkAllManagers());
132     }
133
134     @Test
135     public void createContextChain() {
136         contextChainHolder.createContextChain(connectionContext);
137         Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class));
138         Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class));
139         Mockito.verify(roleManager).createContext(Mockito.any(DeviceContext.class));
140         Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class), Mockito.anyBoolean());
141     }
142
143
144     @Test
145     public void reconciliationFrameworkFailure() {
146         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
147             .thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
148         contextChainHolder.createContextChain(connectionContext);
149         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
150         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
151         Mockito.verify(connectionContext).closeConnection(false);
152     }
153
154     @Test
155     public void reconciliationFrameworkDisconnect() {
156         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
157             .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
158         contextChainHolder.createContextChain(connectionContext);
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() {
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.RPC_REGISTRATION);
171         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
172         Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo);
173     }
174
175     @Test
176     public void reconciliationFrameworkSuccessButNotSubmit() {
177         contextChainHolder.createContextChain(connectionContext);
178         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
179                 .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
180         // TODO when if (future != null) check in MastershipChangeServiceManagerImpl's becomeSlaveOrDisconnect() is rm
181         // Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
182         //    .thenReturn(Futures.immediateFuture(null));
183         contextChainHolder.createContextChain(connectionContext);
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.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         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(Uint8.ONE);
210         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
211                 == ConnectionStatus.MAY_CONTINUE);
212         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
213         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
214                 == ConnectionStatus.MAY_CONTINUE);
215     }
216
217     @Test
218     public void notToAbleMastership() throws Exception {
219         registration.close();
220         contextChainHolder.deviceConnected(connectionContext);
221         contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true);
222         Mockito.verify(deviceContext).close();
223         Mockito.verify(statisticsContext).close();
224         Mockito.verify(rpcContext).close();
225     }
226
227     @Test
228     public void notAbleToSetSlave() throws Exception {
229         registration.close();
230         contextChainHolder.deviceConnected(connectionContext);
231         contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason");
232         Mockito.verify(deviceContext).close();
233         Mockito.verify(statisticsContext).close();
234         Mockito.verify(rpcContext).close();
235     }
236
237     @Test
238     public void deviceDisconnected() throws Exception {
239         registration.close();
240         contextChainHolder.createContextChain(connectionContext);
241         contextChainHolder.onDeviceDisconnected(connectionContext);
242         Mockito.verify(deviceContext).close();
243         Mockito.verify(statisticsContext).close();
244         Mockito.verify(rpcContext).close();
245     }
246
247     @Test
248     public void onClose() throws Exception {
249         registration.close();
250         contextChainHolder.createContextChain(connectionContext);
251         contextChainHolder.close();
252         Mockito.verify(deviceContext).close();
253         Mockito.verify(statisticsContext).close();
254         Mockito.verify(rpcContext).close();
255     }
256
257     @Test
258     public void ownershipChanged() throws Exception {
259         registration.close();
260         contextChainHolder.createContextChain(connectionContext);
261         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
262         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
263         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
264         EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
265                 new Entity(ENTITY_TEST, OPENFLOW_TEST),
266                 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER
267         );
268         contextChainHolder.ownershipChanged(ownershipChange);
269         Mockito.verify(deviceManager, Mockito.timeout(1000)).removeDeviceFromOperationalDS(Mockito.any());
270     }
271
272     @Test
273     public void ownershipChangedButHasOwner() throws Exception {
274         registration.close();
275         contextChainHolder.createContextChain(connectionContext);
276         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
277         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
278         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
279         EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
280                 new Entity(ENTITY_TEST, OPENFLOW_TEST),
281                 EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER
282         );
283         contextChainHolder.ownershipChanged(ownershipChange);
284         Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());
285     }
286 }