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