2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.lifecycle;
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;
32 @RunWith(MockitoJUnitRunner.class)
33 public class ContextChainImplTest {
35 private static final String TEST_NODE = "test node";
36 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
39 private StatisticsContext statisticsContext;
41 private RpcContext rpcContext;
43 private DeviceContext deviceContext;
45 private DeviceInfo deviceInfo;
47 private ConnectionContext connectionContext;
49 private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
51 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
53 private ContextChainMastershipWatcher contextChainMastershipWatcher;
55 private DeviceRemovedHandler deviceRemovedHandler;
57 private ContextChain contextChain;
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);
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);
81 public void closeServiceInstance() throws Exception {
82 contextChain.closeServiceInstance();
83 Mockito.verify(deviceContext).closeServiceInstance();
84 Mockito.verify(rpcContext).closeServiceInstance();
85 Mockito.verify(statisticsContext).closeServiceInstance();
89 public void close() throws Exception {
90 contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
92 Mockito.verify(statisticsContext).close();
93 Mockito.verify(deviceContext).close();
94 Mockito.verify(rpcContext).close();
95 Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
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));
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));
117 public void getIdentifier() throws Exception {
118 Assert.assertEquals(contextChain.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
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));
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));
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());