Mastership service provider API
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainImplTest.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 com.google.common.util.concurrent.MoreExecutors;
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.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
26 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
27 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
28 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
30 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
31
32 @RunWith(MockitoJUnitRunner.class)
33 public class ContextChainImplTest {
34
35     private static final String TEST_NODE = "test node";
36     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
37
38     @Mock
39     private StatisticsContext statisticsContext;
40     @Mock
41     private RpcContext rpcContext;
42     @Mock
43     private DeviceContext deviceContext;
44     @Mock
45     private DeviceInfo deviceInfo;
46     @Mock
47     private ConnectionContext connectionContext;
48     @Mock
49     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
50     @Mock
51     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
52     @Mock
53     private ContextChainMastershipWatcher contextChainMastershipWatcher;
54     @Mock
55     private DeviceRemovedHandler deviceRemovedHandler;
56
57     private ContextChain contextChain;
58
59     @Before
60     public void setUp() throws Exception {
61         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
62         Mockito.when(deviceInfo.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
63         Mockito.when(deviceContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
64         Mockito.when(rpcContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
65         Mockito.when(statisticsContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
66         Mockito.when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateFuture(null));
67         Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
68         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
69         Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
70                 .thenReturn(clusterSingletonServiceRegistration);
71
72         contextChain = new ContextChainImpl(contextChainMastershipWatcher, connectionContext,
73                 MoreExecutors.newDirectExecutorService());
74         contextChain.addContext(statisticsContext);
75         contextChain.addContext(rpcContext);
76         contextChain.addContext(deviceContext);
77         contextChain.registerServices(clusterSingletonServiceProvider);
78     }
79
80     @Test
81     public void closeServiceInstance() throws Exception {
82         contextChain.closeServiceInstance();
83         Mockito.verify(deviceContext).closeServiceInstance();
84         Mockito.verify(rpcContext).closeServiceInstance();
85         Mockito.verify(statisticsContext).closeServiceInstance();
86     }
87
88     @Test
89     public void close() throws Exception {
90         contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
91         contextChain.close();
92         Mockito.verify(statisticsContext).close();
93         Mockito.verify(deviceContext).close();
94         Mockito.verify(rpcContext).close();
95         Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
96     }
97
98     @Test
99     public void closeTwoTimes() throws Exception {
100         contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
101         contextChain.close();
102         contextChain.close();
103         Mockito.verify(deviceRemovedHandler, Mockito.times(1))
104                 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
105     }
106
107     @Test
108     public void closeThreeTimes() throws Exception {
109         contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
110         contextChain.close();
111         contextChain.close();
112         Mockito.verify(deviceRemovedHandler, Mockito.times(1))
113                 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
114     }
115
116     @Test
117     public void getIdentifier() throws Exception {
118         Assert.assertEquals(contextChain.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
119     }
120
121     @Test
122     public void makeDeviceSlave() throws Exception {
123         Mockito.when(deviceContext.makeDeviceSlave()).thenReturn(Futures.immediateFuture(null));
124         contextChain.makeDeviceSlave();
125         Mockito.verify(contextChainMastershipWatcher).onSlaveRoleAcquired(Mockito.any(DeviceInfo.class));
126     }
127
128     @Test
129     public void makeDeviceSlaveFailure() throws Exception {
130         Mockito.when(deviceContext.makeDeviceSlave())
131                 .thenReturn(Futures.immediateFailedFuture(new RoleChangeException(TEST_NODE)));
132         contextChain.makeDeviceSlave();
133         Mockito.verify(contextChainMastershipWatcher).onSlaveRoleNotAcquired(Mockito.any(DeviceInfo.class));
134     }
135
136     @Test
137     public void instantiateServiceInstanceFail() throws Exception {
138         Mockito.doThrow(new IllegalStateException()).when(deviceContext).instantiateServiceInstance();
139         contextChain.instantiateServiceInstance();
140         Mockito.verify(contextChainMastershipWatcher).onNotAbleToStartMastershipMandatory(Mockito.any(DeviceInfo.class), Mockito.anyString());
141     }
142 }