437550d38d36eb367f0e5dbbc1e31b97d9884e20
[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
9 package org.opendaylight.openflowplugin.impl;
10
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
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.runners.MockitoJUnitRunner;
23 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
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
38 @RunWith(MockitoJUnitRunner.class)
39 public class OpenFlowPluginProviderImplTest {
40
41     @Mock
42     PingPongDataBroker dataBroker;
43
44     @Mock
45     RpcProviderRegistry rpcProviderRegistry;
46
47     @Mock
48     NotificationPublishService notificationPublishService;
49
50     @Mock
51     OpenflowPluginDiagStatusProvider ofPluginDiagstatusProvider;
52
53     @Mock
54     SystemReadyMonitor systemReadyMonitor;
55
56     @Mock
57     WriteTransaction writeTransaction;
58
59     @Mock
60     EntityOwnershipService entityOwnershipService;
61
62     @Mock
63     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
64
65     @Mock
66     BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
67
68     @Mock
69     SwitchConnectionProvider switchConnectionProvider;
70
71     @Mock
72     ClusterSingletonServiceProvider clusterSingletonServiceProvider;
73
74     @Mock
75     ConfigurationService configurationService;
76
77     @Mock
78     MastershipChangeServiceManager mastershipChangeServiceManager;
79
80     private static final int RPC_REQUESTS_QUOTA = 500;
81     private static final long GLOBAL_NOTIFICATION_QUOTA = 131072;
82     private static final int THREAD_POOL_MIN_THREADS = 1;
83     private static final int THREAD_POOL_MAX_THREADS = 32000;
84     private static final long THREAD_POOL_TIMEOUT = 60;
85     private static final long BASIC_TIMER_DELAY = 1L;
86     private static final boolean USE_SINGLE_LAYER_SERIALIZATION = false;
87     private static final int DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = 0;
88
89     @Before
90     public void setUp() throws Exception {
91         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
92         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
93         when(entityOwnershipService.registerListener(any(), any())).thenReturn(entityOwnershipListenerRegistration);
94         when(rpcProviderRegistry.addRpcImplementation(eq(StatisticsManagerControlService.class), any()))
95                 .thenReturn(controlServiceRegistration);
96         when(switchConnectionProvider.startup()).thenReturn(Futures.immediateFuture(true));
97         when(switchConnectionProvider.shutdown()).thenReturn(Futures.immediateFuture(true));
98         when(configurationService.getProperty(eq(ConfigurationProperty.USE_SINGLE_LAYER_SERIALIZATION.toString()),
99                 any())).thenReturn(USE_SINGLE_LAYER_SERIALIZATION);
100         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString()), any()))
101                 .thenReturn(THREAD_POOL_MIN_THREADS);
102         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString()), any()))
103                 .thenReturn(THREAD_POOL_MAX_THREADS);
104         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_TIMEOUT.toString()), any()))
105                 .thenReturn(THREAD_POOL_TIMEOUT);
106         when(configurationService.getProperty(eq(ConfigurationProperty.DEVICE_CONNECTION_RATE_LIMIT_PER_MIN.toString()),
107                 any())).thenReturn(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN);
108     }
109
110     @Test
111     public void testInitializeAndClose() throws Exception {
112         final OpenFlowPluginProviderImpl provider = new OpenFlowPluginProviderImpl(
113                 configurationService,
114                 new SwitchConnectionProviderList(Lists.newArrayList(switchConnectionProvider)),
115                 dataBroker,
116                 rpcProviderRegistry,
117                 notificationPublishService,
118                 clusterSingletonServiceProvider,
119                 entityOwnershipService,
120                 mastershipChangeServiceManager,
121                 ofPluginDiagstatusProvider,
122                 systemReadyMonitor);
123
124         provider.initialize();
125         // Calling the onSystemBootReady() callback
126         provider.onSystemBootReady();
127         verify(switchConnectionProvider).startup();
128         provider.close();
129         verify(switchConnectionProvider).shutdown();
130     }
131 }