Use moved BindingReflections
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DatastoreContextIntrospectorTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS;
14 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_OPERATION_TIMEOUT_IN_MS;
15 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_INITIALIZATION_TIMEOUT;
16 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE;
17 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT;
18 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS;
19 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
20 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
21
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.Hashtable;
25 import java.util.Map;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
28 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
29 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStoreConfigProperties;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.distributed.datastore.provider.rev140612.DataStorePropertiesContainer;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 /**
36  * Unit tests for DatastoreContextIntrospector.
37  *
38  * @author Thomas Pantelis
39  */
40 @SuppressWarnings("checkstyle:IllegalCatch")
41 public class DatastoreContextIntrospectorTest {
42
43     static SchemaContext SCHEMA_CONTEXT;
44     static DatastoreContextIntrospectorFactory INTROSPECTOR_FACTORY;
45
46     static {
47         final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
48         try {
49             moduleContext.addModuleInfos(Arrays.asList(
50                     BindingReflections.getModuleInfo(DataStorePropertiesContainer.class)));
51         } catch (Exception e) {
52             throw new RuntimeException(e);
53         }
54         SCHEMA_CONTEXT = moduleContext.tryToCreateSchemaContext().get();
55
56         DOMSchemaService mockSchemaService = mock(DOMSchemaService.class);
57         doReturn(SCHEMA_CONTEXT).when(mockSchemaService).getGlobalContext();
58         INTROSPECTOR_FACTORY = new DatastoreContextIntrospectorFactory(mockSchemaService,
59                 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
60     }
61
62     @Test
63     public void testYangDefaults() {
64         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(
65                 DatastoreContext.newBuilder().shardBatchedModificationCount(2)
66                 .transactionDebugContextEnabled(true).build());
67         DatastoreContext context = introspector.getContext();
68
69         assertEquals(1000, context.getShardBatchedModificationCount());
70         assertEquals(false, context.isTransactionDebugContextEnabled());
71     }
72
73     @Test
74     public void testUpdate() {
75         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
76
77         final Map<String, Object> properties = new HashMap<>();
78         properties.put("shard-transaction-idle-timeout-in-minutes", "31");
79         properties.put("operation-timeout-in-seconds", "26");
80         properties.put("shard-transaction-commit-timeout-in-seconds", "100");
81         properties.put("shard-journal-recovery-log-batch-size", "199");
82         properties.put("shard-snapshot-batch-count", "212");
83         properties.put("shard-heartbeat-interval-in-millis", "101");
84         properties.put("shard-transaction-commit-queue-capacity", "567");
85         properties.put("shard-initialization-timeout-in-seconds", "82");
86         properties.put("shard-leader-election-timeout-in-seconds", "66");
87         properties.put("shard-isolated-leader-check-interval-in-millis", "123");
88         properties.put("shard-snapshot-data-threshold-percentage", "100");
89         properties.put("shard-election-timeout-factor", "21");
90         properties.put("shard-batched-modification-count", "901");
91         properties.put("transactionCreationInitialRateLimit", "200");
92         properties.put("MaxShardDataChangeExecutorPoolSize", "41");
93         properties.put("Max-Shard-Data-Change Executor-Queue Size", "1111");
94         properties.put(" max shard data change listener queue size", "2222");
95         properties.put("mAx-shaRd-data-STORE-executor-quEUe-size", "3333");
96         properties.put("persistent", "false");
97
98         boolean updated = introspector.update(properties);
99         assertEquals("updated", true, updated);
100         DatastoreContext context = introspector.getContext();
101
102         assertEquals(31, context.getShardTransactionIdleTimeout().toMinutes());
103         assertEquals(26000, context.getOperationTimeoutInMillis());
104         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
105         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
106         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
107         assertEquals(101, context.getShardRaftConfig().getHeartBeatInterval().length());
108         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
109         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
110         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
111         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
112         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
113         assertEquals(21, context.getShardRaftConfig().getElectionTimeoutFactor());
114         assertEquals(901, context.getShardBatchedModificationCount());
115         assertEquals(200, context.getTransactionCreationInitialRateLimit());
116         assertEquals(41, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
117         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
118         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
119         assertEquals(3333, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
120         assertEquals(false, context.isPersistent());
121
122         properties.put("shard-transaction-idle-timeout-in-minutes", "32");
123         properties.put("operation-timeout-in-seconds", "27");
124         properties.put("shard-heartbeat-interval-in-millis", "102");
125         properties.put("shard-election-timeout-factor", "22");
126         properties.put("max-shard-data-change-executor-pool-size", "42");
127         properties.put("max-shard-data-store-executor-queue-size", "4444");
128         properties.put("persistent", "true");
129
130         updated = introspector.update(properties);
131         assertEquals("updated", true, updated);
132         context = introspector.getContext();
133
134         assertEquals(32, context.getShardTransactionIdleTimeout().toMinutes());
135         assertEquals(27000, context.getOperationTimeoutInMillis());
136         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
137         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
138         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
139         assertEquals(102, context.getShardRaftConfig().getHeartBeatInterval().length());
140         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
141         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
142         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
143         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
144         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
145         assertEquals(22, context.getShardRaftConfig().getElectionTimeoutFactor());
146         assertEquals(200, context.getTransactionCreationInitialRateLimit());
147         assertEquals(42, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
148         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
149         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
150         assertEquals(4444, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
151         assertEquals(true, context.isPersistent());
152
153         updated = introspector.update(null);
154         assertEquals("updated", false, updated);
155
156         updated = introspector.update(new Hashtable<>());
157         assertEquals("updated", false, updated);
158     }
159
160
161     @Test
162     public void testUpdateWithInvalidValues() {
163         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
164
165         final Map<String, Object> properties = new HashMap<>();
166         properties.put("shard-transaction-idle-timeout-in-minutes", "0"); // bad - must be > 0
167         properties.put("shard-journal-recovery-log-batch-size", "199");
168         properties.put("shard-transaction-commit-timeout-in-seconds", "bogus"); // bad - NaN
169         properties.put("shard-snapshot-batch-count", "212"); // good
170         properties.put("operation-timeout-in-seconds", "4"); // bad - must be >= 5
171         properties.put("shard-heartbeat-interval-in-millis", "99"); // bad - must be >= 100
172         properties.put("shard-transaction-commit-queue-capacity", "567"); // good
173         properties.put("shard-snapshot-data-threshold-percentage", "101"); // bad - must be 0-100
174         properties.put("shard-initialization-timeout-in-seconds", "-1"); // bad - must be > 0
175         properties.put("max-shard-data-change-executor-pool-size", "bogus"); // bad - NaN
176         properties.put("unknownProperty", "1"); // bad - invalid property name
177
178         final boolean updated = introspector.update(properties);
179         assertEquals("updated", true, updated);
180         DatastoreContext context = introspector.getContext();
181
182         assertEquals(DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT, context.getShardTransactionIdleTimeout());
183         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
184         assertEquals(DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS, context.getShardTransactionCommitTimeoutInSeconds());
185         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
186         assertEquals(DEFAULT_OPERATION_TIMEOUT_IN_MS, context.getOperationTimeoutInMillis());
187         assertEquals(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS,
188                 context.getShardRaftConfig().getHeartBeatInterval().length());
189         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
190         assertEquals(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE,
191                 context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
192         assertEquals(DEFAULT_SHARD_INITIALIZATION_TIMEOUT, context.getShardInitializationTimeout());
193         assertEquals(InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE,
194                 context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
195     }
196
197     @Test
198     public void testUpdateWithDatastoreTypeSpecificProperties() {
199         final Map<String, Object> properties = new HashMap<>();
200         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
201         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
202         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
203
204         properties.put("max-shard-data-change-executor-pool-size", "222"); // global setting
205         properties.put("operational.max-shard-data-change-executor-pool-size", "333"); // operational override
206         properties.put("config.max-shard-data-change-executor-pool-size", "444"); // config override
207
208         properties.put("persistent", "false"); // global setting
209         properties.put("operational.Persistent", "true"); // operational override
210
211         final DatastoreContextIntrospector operIntrospector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
212         boolean updated = operIntrospector.update(properties);
213         assertEquals("updated", true, updated);
214         DatastoreContext operContext = operIntrospector.getContext();
215
216         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
217         assertEquals(true, operContext.isPersistent());
218         assertEquals(333, operContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
219
220         final DatastoreContextIntrospector configIntrospector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
221         updated = configIntrospector.update(properties);
222         assertEquals("updated", true, updated);
223         DatastoreContext configContext = configIntrospector.getContext();
224
225         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
226         assertEquals(false, configContext.isPersistent());
227         assertEquals(444, configContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
228     }
229
230     @Test
231     public void testGetDatastoreContextForShard() {
232         final Map<String, Object> properties = new HashMap<>();
233         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
234         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
235         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
236         properties.put("topology.shard-transaction-idle-timeout-in-minutes", "55"); // global shard override
237
238         final DatastoreContextIntrospector operIntrospector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
239
240         DatastoreContext shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
241         assertEquals(10, shardContext.getShardTransactionIdleTimeout().toMinutes());
242
243         operIntrospector.update(properties);
244         DatastoreContext operContext = operIntrospector.getContext();
245         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
246
247         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
248         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
249
250         final DatastoreContextIntrospector configIntrospector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
251         configIntrospector.update(properties);
252         DatastoreContext configContext = configIntrospector.getContext();
253         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
254
255         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
256         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
257
258         // operational shard override
259         properties.put("operational.topology.shard-transaction-idle-timeout-in-minutes", "66");
260         // config shard override
261         properties.put("config.topology.shard-transaction-idle-timeout-in-minutes", "77");
262
263         operIntrospector.update(properties);
264         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
265         assertEquals(66, shardContext.getShardTransactionIdleTimeout().toMinutes());
266
267         configIntrospector.update(properties);
268         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
269         assertEquals(77, shardContext.getShardTransactionIdleTimeout().toMinutes());
270
271         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("default");
272         assertEquals(44, shardContext.getShardTransactionIdleTimeout().toMinutes());
273     }
274 }