a9685b24fdf5bee3cebc7fdb934d50f8eca6c3d3
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / configuration / ConfigurationServiceFactoryImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.configuration;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.collect.ImmutableMap;
19 import java.util.Hashtable;
20 import java.util.Map;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
27 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
28 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint16Type;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
32
33 @RunWith(MockitoJUnitRunner.class)
34 public class ConfigurationServiceFactoryImplTest {
35     private static final int CONFIG_PROP_COUNT = 23;
36     private static final boolean IS_STATISTICS_POLLING_ON = true;
37     private static final int BARRIER_COUNT_LIMIT = 2000;
38     private static final long BARRIER_INTERVAL_TIMEOUT_LIMIT = 3000;
39     private static final long ECHO_REPLY_TIMEOUT = 4000;
40     private static final boolean ENABLE_FLOW_REMOVED_NOTIFICATION = true;
41     private static final boolean SKIP_TABLE_FEATURES = true;
42     private static final long BASIC_TIMER_DELAY = 2690;
43     private static final long MAXIMUM_TIMER_DELAY = 3679;
44     private static final boolean SWITCH_FEATURES_MANDATORY = false;
45     private static final boolean IS_STATISTICS_RPC_ENABLED = false;
46     private static final boolean USE_SINGLE_LAYER_SERIALIZATION = true;
47     private static final int RPC_REQUESTS_QUOTA = 2500;
48     private static final long GLOBAL_NOTIFICATION_QUOTA = 9000;
49     private static final int THREAD_POOL_MIN_THREADS = 3;
50     private static final int THREAD_POOL_MIN_THREADS_UPDATE = 4;
51     private static final int THREAD_POOL_MAX_THREADS = 1000;
52     private static final long THREAD_POOL_TIMEOUT = 60;
53     private static final int DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = 0;
54
55     @Mock
56     private OpenflowProviderConfig config;
57
58     @Mock
59     private ConfigurationListener configurationListener;
60
61     private ConfigurationService configurationService;
62
63     @Before
64     public void setUp() throws Exception {
65         when(config.isIsStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
66         when(config.isIsFlowStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
67         when(config.isIsTableStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
68         when(config.isIsFlowStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
69         when(config.isIsGroupStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
70         when(config.isIsMeterStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
71         when(config.isIsQueueStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
72         when(config.isIsPortStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
73         when(config.getBarrierCountLimit()).thenReturn(new NonZeroUint16Type(BARRIER_COUNT_LIMIT));
74         when(config.getBarrierIntervalTimeoutLimit()).thenReturn(new NonZeroUint32Type(BARRIER_INTERVAL_TIMEOUT_LIMIT));
75         when(config.getEchoReplyTimeout()).thenReturn(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT));
76         when(config.isEnableFlowRemovedNotification()).thenReturn(ENABLE_FLOW_REMOVED_NOTIFICATION);
77         when(config.isSkipTableFeatures()).thenReturn(SKIP_TABLE_FEATURES);
78         when(config.getBasicTimerDelay()).thenReturn(new NonZeroUint32Type(BASIC_TIMER_DELAY));
79         when(config.getMaximumTimerDelay()).thenReturn(new NonZeroUint32Type(MAXIMUM_TIMER_DELAY));
80         when(config.isSwitchFeaturesMandatory()).thenReturn(SWITCH_FEATURES_MANDATORY);
81         when(config.isIsStatisticsRpcEnabled()).thenReturn(IS_STATISTICS_RPC_ENABLED);
82         when(config.isUseSingleLayerSerialization()).thenReturn(USE_SINGLE_LAYER_SERIALIZATION);
83         when(config.getRpcRequestsQuota()).thenReturn(new NonZeroUint16Type(RPC_REQUESTS_QUOTA));
84         when(config.getGlobalNotificationQuota()).thenReturn(GLOBAL_NOTIFICATION_QUOTA);
85         when(config.getThreadPoolMinThreads()).thenReturn(THREAD_POOL_MIN_THREADS);
86         when(config.getThreadPoolMaxThreads()).thenReturn(new NonZeroUint16Type(THREAD_POOL_MAX_THREADS));
87         when(config.getThreadPoolTimeout()).thenReturn(THREAD_POOL_TIMEOUT);
88         when(config.getDeviceConnectionRateLimitPerMin()).thenReturn(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN);
89
90         final Map<String, String> properties = new Hashtable<>();
91         properties.put(ConfigurationProperty.IS_STATISTICS_POLLING_ON.toString(),
92                 Boolean.toString(IS_STATISTICS_POLLING_ON));
93
94         configurationService = new ConfigurationServiceFactoryImpl().newInstance(config);
95         configurationService.update(properties);
96     }
97
98     @Test
99     public void update() throws Exception {
100         final int tpMinThreads = configurationService
101                 .getProperty(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), Integer::valueOf);
102
103         configurationService.update(ImmutableMap
104                 .<String, String>builder()
105                 .put(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), String
106                         .valueOf(THREAD_POOL_MIN_THREADS_UPDATE))
107                 .build());
108
109         final int tpMinThreadsNew = configurationService
110                 .getProperty(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), Integer::valueOf);
111
112         assertNotEquals(tpMinThreadsNew, tpMinThreads);
113         assertEquals(tpMinThreadsNew, THREAD_POOL_MIN_THREADS_UPDATE);
114     }
115
116     @Test
117     public void getProperty() throws Exception {
118         final int tpMaxThreads = configurationService
119                 .getProperty(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(), Integer::valueOf);
120
121         assertEquals(THREAD_POOL_MAX_THREADS, tpMaxThreads);
122     }
123
124     @Test
125     public void registerListener() throws Exception {
126         configurationService.registerListener(configurationListener);
127         verify(configurationListener, times(CONFIG_PROP_COUNT)).onPropertyChanged(any(), any());
128     }
129
130     @Test(expected = NumberFormatException.class)
131     public void close() throws Exception {
132         configurationService.close();
133         configurationService.getProperty(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(), Integer::valueOf);
134     }
135 }