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