2 * Copyright (c) 2016 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 org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.runners.MockitoJUnitRunner;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
20 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
21 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
24 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
25 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
26 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
29 @RunWith(MockitoJUnitRunner.class)
30 public class LifecycleServiceImplTest {
32 private static final String TEST_NODE = "test node";
33 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
36 private DeviceInfo deviceInfo;
38 private DeviceContext deviceContext;
40 private RpcContext rpcContext;
42 private StatisticsContext statContext;
44 private ConnectionContext connectionContext;
46 private DeviceFlowRegistry deviceFlowRegistry;
48 private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
50 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
52 private LifecycleService lifecycleService;
56 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
57 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
58 Mockito.when(deviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
59 Mockito.when(deviceContext.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
60 Mockito.when(deviceFlowRegistry.fill()).thenReturn(Futures.immediateFuture(null));
61 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
62 Mockito.when(deviceInfo.getLOGValue()).thenReturn(TEST_NODE);
63 Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
64 .thenReturn(clusterSingletonServiceRegistration);
66 Mockito.when(deviceContext.stopClusterServices(Mockito.anyBoolean())).thenReturn(Futures.immediateFuture(null));
67 Mockito.when(statContext.stopClusterServices(Mockito.anyBoolean())).thenReturn(Futures.immediateFuture(null));
68 Mockito.when(rpcContext.stopClusterServices(Mockito.anyBoolean())).thenReturn(Futures.immediateFuture(null));
70 lifecycleService = new LifecycleServiceImpl();
71 lifecycleService.setDeviceContext(deviceContext);
72 lifecycleService.setRpcContext(rpcContext);
73 lifecycleService.setStatContext(statContext);
74 lifecycleService.registerService(clusterSingletonServiceProvider);
78 public void instantiateServiceInstance() throws Exception {
79 lifecycleService.instantiateServiceInstance();
80 Mockito.verify(deviceContext).setLifecycleInitializationPhaseHandler(Mockito.any());
81 Mockito.verify(statContext).setLifecycleInitializationPhaseHandler(Mockito.any());
82 Mockito.verify(statContext).setInitialSubmitHandler(Mockito.any());
83 Mockito.verify(rpcContext).setLifecycleInitializationPhaseHandler(Mockito.any());
87 public void closeServiceInstance() throws Exception {
88 lifecycleService.closeServiceInstance().get();
89 Mockito.verify(statContext).stopClusterServices(false);
90 Mockito.verify(deviceContext).stopClusterServices(false);
91 Mockito.verify(rpcContext).stopClusterServices(false);
95 public void getIdentifier() throws Exception {
96 Assert.assertEquals(lifecycleService.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
100 public void closeConnection() throws Exception {
101 lifecycleService.closeConnection();
102 Mockito.verify(deviceContext).shutdownConnection();