1b875c86063659b1b84fd7bf45c04547ee81b5ae
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import com.google.common.collect.Lists;
17 import com.google.common.util.concurrent.Futures;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
24 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
25 import org.opendaylight.mdsal.binding.api.RpcProviderService;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.CommitInfo;
28 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
29 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
30 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
31 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
32 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProviderList;
33 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
34 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
35 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
37 import org.opendaylight.yangtools.concepts.ObjectRegistration;
38 import org.opendaylight.yangtools.yang.common.Uint16;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class OpenFlowPluginProviderImplTest {
43
44     @Mock
45     PingPongDataBroker dataBroker;
46
47     @Mock
48     RpcProviderService rpcProviderRegistry;
49
50     @Mock
51     NotificationPublishService notificationPublishService;
52
53     @Mock
54     DiagStatusProvider ofPluginDiagstatusProvider;
55
56     @Mock
57     SystemReadyMonitor systemReadyMonitor;
58
59     @Mock
60     WriteTransaction writeTransaction;
61
62     @Mock
63     EntityOwnershipService entityOwnershipService;
64
65     @Mock
66     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
67
68     @Mock
69     ObjectRegistration<StatisticsManagerControlService> controlServiceRegistration;
70
71     @Mock
72     SwitchConnectionProvider switchConnectionProvider;
73
74     @Mock
75     ClusterSingletonServiceProvider clusterSingletonServiceProvider;
76
77     @Mock
78     ConfigurationService configurationService;
79
80     @Mock
81     MastershipChangeServiceManager mastershipChangeServiceManager;
82
83     private static final Uint16 THREAD_POOL_MIN_THREADS = Uint16.ONE;
84     private static final Uint16 THREAD_POOL_MAX_THREADS = Uint16.valueOf(32000);
85     private static final Uint32 THREAD_POOL_TIMEOUT = Uint32.valueOf(60);
86     private static final boolean USE_SINGLE_LAYER_SERIALIZATION = false;
87     private static final Uint16 DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = Uint16.ZERO;
88     private static final Uint16 DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS = Uint16.valueOf(60);
89
90     @Before
91     public void setUp() {
92         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
93         doReturn(CommitInfo.emptyFluentFuture()).when(writeTransaction).commit();
94         when(entityOwnershipService.registerListener(any(), any())).thenReturn(entityOwnershipListenerRegistration);
95         when(rpcProviderRegistry.registerRpcImplementation(eq(StatisticsManagerControlService.class), any()))
96                 .thenReturn(controlServiceRegistration);
97         when(switchConnectionProvider.startup()).thenReturn(Futures.immediateFuture(true));
98         when(switchConnectionProvider.shutdown()).thenReturn(Futures.immediateFuture(true));
99         when(configurationService.getProperty(eq(ConfigurationProperty.USE_SINGLE_LAYER_SERIALIZATION.toString()),
100                 any())).thenReturn(USE_SINGLE_LAYER_SERIALIZATION);
101         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString()), any()))
102                 .thenReturn(THREAD_POOL_MIN_THREADS);
103         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString()), any()))
104                 .thenReturn(THREAD_POOL_MAX_THREADS);
105         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_TIMEOUT.toString()), any()))
106                 .thenReturn(THREAD_POOL_TIMEOUT);
107         when(configurationService.getProperty(eq(ConfigurationProperty.DEVICE_CONNECTION_RATE_LIMIT_PER_MIN.toString()),
108                 any())).thenReturn(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN);
109         when(configurationService.getProperty(
110                 eq(ConfigurationProperty.DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS.toString()), any()))
111                 .thenReturn(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS);
112     }
113
114     @Test
115     public void testInitializeAndClose() {
116         final OpenFlowPluginProviderImpl provider = new OpenFlowPluginProviderImpl(
117                 configurationService,
118                 new SwitchConnectionProviderList(Lists.newArrayList(switchConnectionProvider)),
119                 dataBroker,
120                 rpcProviderRegistry,
121                 notificationPublishService,
122                 clusterSingletonServiceProvider,
123                 entityOwnershipService,
124                 mastershipChangeServiceManager,
125                 ofPluginDiagstatusProvider,
126                 systemReadyMonitor);
127
128         provider.initialize();
129         // Calling the onSystemBootReady() callback
130         provider.onSystemBootReady();
131         verify(switchConnectionProvider).startup();
132         provider.close();
133         verify(switchConnectionProvider).shutdown();
134     }
135 }