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.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
26 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
27 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
28 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
31 @RunWith(MockitoJUnitRunner.class)
32 public class LifecycleServiceImplTest {
34 private static final String TEST_NODE = "test node";
35 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
38 private DeviceInfo deviceInfo;
40 private DeviceContext deviceContext;
42 private RpcContext rpcContext;
44 private StatisticsContext statContext;
46 private ConnectionContext connectionContext;
48 private DeviceFlowRegistry deviceFlowRegistry;
50 private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
52 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
54 EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
56 private LifecycleService lifecycleService;
60 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
61 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
62 Mockito.when(deviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
63 Mockito.when(deviceContext.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
64 Mockito.when(deviceFlowRegistry.fill()).thenReturn(Futures.immediateFuture(null));
65 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
66 Mockito.when(deviceInfo.getLOGValue()).thenReturn(TEST_NODE);
67 Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
68 .thenReturn(clusterSingletonServiceRegistration);
70 Mockito.when(deviceContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
71 Mockito.when(statContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
72 Mockito.when(rpcContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
74 lifecycleService = new LifecycleServiceImpl();
75 lifecycleService.setDeviceContext(deviceContext);
76 lifecycleService.setRpcContext(rpcContext);
77 lifecycleService.setStatContext(statContext);
78 lifecycleService.registerService(clusterSingletonServiceProvider);
82 public void instantiateServiceInstance() throws Exception {
83 lifecycleService.instantiateServiceInstance();
84 Mockito.verify(deviceContext).setLifecycleInitializationPhaseHandler(Mockito.any());
85 Mockito.verify(statContext).setLifecycleInitializationPhaseHandler(Mockito.any());
86 Mockito.verify(statContext).setInitialSubmitHandler(Mockito.any());
87 Mockito.verify(rpcContext).setLifecycleInitializationPhaseHandler(Mockito.any());
91 public void closeServiceInstance() throws Exception {
92 lifecycleService.closeServiceInstance().get();
93 Mockito.verify(statContext).stopClusterServices();
94 Mockito.verify(deviceContext).stopClusterServices();
95 Mockito.verify(rpcContext).stopClusterServices();
99 public void getIdentifier() throws Exception {
100 Assert.assertEquals(lifecycleService.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
104 public void closeConnection() throws Exception {
105 lifecycleService.closeConnection();
106 Mockito.verify(deviceContext).shutdownConnection();