Merge "OPNFLWPLUG-972: Point to openflowplugin liblldp"
[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.Matchers.any;
12 import static org.mockito.Matchers.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.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
27 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
28 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
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.spi.connection.SwitchConnectionProvider;
33 import org.opendaylight.openflowplugin.api.diagstatus.OpenflowPluginDiagStatusProvider;
34 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
35 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
36 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class OpenFlowPluginProviderImplTest {
41
42     @Mock
43     DataBroker dataBroker;
44
45     @Mock
46     RpcProviderRegistry rpcProviderRegistry;
47
48     @Mock
49     NotificationPublishService notificationPublishService;
50
51     @Mock
52     OpenflowPluginDiagStatusProvider ofPluginDiagstatusProvider;
53
54     @Mock
55     SystemReadyMonitor systemReadyMonitor;
56
57     @Mock
58     WriteTransaction writeTransaction;
59
60     @Mock
61     EntityOwnershipService entityOwnershipService;
62
63     @Mock
64     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
65
66     @Mock
67     BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
68
69     @Mock
70     SwitchConnectionProvider switchConnectionProvider;
71
72     @Mock
73     ClusterSingletonServiceProvider clusterSingletonServiceProvider;
74
75     @Mock
76     ConfigurationService configurationService;
77
78     @Mock
79     MastershipChangeServiceManager mastershipChangeServiceManager;
80
81     private static final int RPC_REQUESTS_QUOTA = 500;
82     private static final long GLOBAL_NOTIFICATION_QUOTA = 131072;
83     private static final int THREAD_POOL_MIN_THREADS = 1;
84     private static final int THREAD_POOL_MAX_THREADS = 32000;
85     private static final long THREAD_POOL_TIMEOUT = 60;
86     private static final long BASIC_TIMER_DELAY = 1L;
87     private static final boolean USE_SINGLE_LAYER_SERIALIZATION = false;
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.RPC_REQUESTS_QUOTA.toString()), any()))
107                 .thenReturn(RPC_REQUESTS_QUOTA);
108         when(configurationService.getProperty(eq(ConfigurationProperty.GLOBAL_NOTIFICATION_QUOTA.toString()), any()))
109                 .thenReturn(GLOBAL_NOTIFICATION_QUOTA);
110         when(configurationService.getProperty(eq(ConfigurationProperty.BASIC_TIMER_DELAY.toString()), any()))
111                 .thenReturn(BASIC_TIMER_DELAY);
112     }
113
114     @Test
115     public void testInitializeAndClose() throws Exception {
116         final OpenFlowPluginProviderImpl provider = new OpenFlowPluginProviderImpl(
117                 configurationService,
118                 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 }