2c421e6ff49b352e69d055b3ecf33128a3aded43
[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.util.concurrent.Futures;
17 import java.util.List;
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.EntityOwnershipService;
29 import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
30 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
31 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
32 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
33 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
34 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
35 import org.opendaylight.yangtools.concepts.Registration;
36 import org.opendaylight.yangtools.yang.common.Uint16;
37 import org.opendaylight.yangtools.yang.common.Uint32;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class OpenFlowPluginProviderImplTest {
41     @Mock
42     PingPongDataBroker dataBroker;
43     @Mock
44     RpcProviderService rpcProviderRegistry;
45     @Mock
46     NotificationPublishService notificationPublishService;
47     @Mock
48     DiagStatusProvider ofPluginDiagstatusProvider;
49     @Mock
50     SystemReadyMonitor systemReadyMonitor;
51     @Mock
52     WriteTransaction writeTransaction;
53     @Mock
54     EntityOwnershipService entityOwnershipService;
55     @Mock
56     Registration entityOwnershipListenerRegistration;
57     @Mock
58     SwitchConnectionProvider switchConnectionProvider;
59     @Mock
60     ClusterSingletonServiceProvider clusterSingletonServiceProvider;
61     @Mock
62     ConfigurationService configurationService;
63     @Mock
64     MastershipChangeServiceManager mastershipChangeServiceManager;
65     @Mock
66     MessageIntelligenceAgency messageIntelligenceAgency;
67
68     private static final Uint16 THREAD_POOL_MIN_THREADS = Uint16.ONE;
69     private static final Uint16 THREAD_POOL_MAX_THREADS = Uint16.valueOf(32000);
70     private static final Uint32 THREAD_POOL_TIMEOUT = Uint32.valueOf(60);
71     private static final boolean USE_SINGLE_LAYER_SERIALIZATION = false;
72     private static final Uint16 DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = Uint16.ZERO;
73     private static final Uint16 DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS = Uint16.valueOf(60);
74
75     @Before
76     public void setUp() {
77         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
78         doReturn(CommitInfo.emptyFluentFuture()).when(writeTransaction).commit();
79         when(entityOwnershipService.registerListener(any(), any())).thenReturn(entityOwnershipListenerRegistration);
80         when(switchConnectionProvider.startup()).thenReturn(Futures.immediateFuture(true));
81         when(switchConnectionProvider.shutdown()).thenReturn(Futures.immediateFuture(true));
82         when(configurationService.getProperty(eq(ConfigurationProperty.USE_SINGLE_LAYER_SERIALIZATION.toString()),
83                 any())).thenReturn(USE_SINGLE_LAYER_SERIALIZATION);
84         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString()), any()))
85                 .thenReturn(THREAD_POOL_MIN_THREADS);
86         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString()), any()))
87                 .thenReturn(THREAD_POOL_MAX_THREADS);
88         when(configurationService.getProperty(eq(ConfigurationProperty.THREAD_POOL_TIMEOUT.toString()), any()))
89                 .thenReturn(THREAD_POOL_TIMEOUT);
90         when(configurationService.getProperty(eq(ConfigurationProperty.DEVICE_CONNECTION_RATE_LIMIT_PER_MIN.toString()),
91                 any())).thenReturn(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN);
92         when(configurationService.getProperty(
93                 eq(ConfigurationProperty.DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS.toString()), any()))
94                 .thenReturn(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS);
95     }
96
97     @Test
98     public void testInitializeAndClose() {
99         try (var provider = new OpenFlowPluginProviderImpl(configurationService, List.of(switchConnectionProvider),
100                 dataBroker, rpcProviderRegistry, notificationPublishService, clusterSingletonServiceProvider,
101                 entityOwnershipService, mastershipChangeServiceManager, messageIntelligenceAgency,
102                 ofPluginDiagstatusProvider, systemReadyMonitor)) {
103             // Calling the onSystemBootReady() callback
104             provider.onSystemBootReady();
105             verify(switchConnectionProvider).startup();
106         }
107         verify(switchConnectionProvider).shutdown();
108     }
109 }