0db38fe3aa3df5c1188640391edf05c15507544c
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMDataStoreFactory.java
1 /*
2  * Copyright (c) 2014 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.mdsal.dom.store.inmemory;
9
10 import java.util.concurrent.ExecutorService;
11 import org.eclipse.jdt.annotation.NonNullByDefault;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
14 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
15
16 /**
17  * A factory for creating InMemoryDOMDataStore instances.
18  *
19  * @author Thomas Pantelis
20  */
21 @NonNullByDefault
22 public final class InMemoryDOMDataStoreFactory {
23
24     private InMemoryDOMDataStoreFactory() {
25     }
26
27     /**
28      * Creates an InMemoryDOMDataStore instance with default properties.
29      *
30      * @param name the name of the data store
31      * @param schemaService the SchemaService to which to register the data store.
32      * @return an InMemoryDOMDataStore instance
33      */
34     public static InMemoryDOMDataStore create(final String name, final @Nullable DOMSchemaService schemaService) {
35         return create(name, InMemoryDOMDataStoreConfigProperties.getDefault(), schemaService);
36     }
37
38     /**
39      * Creates an InMemoryDOMDataStore instance.
40      *
41      * @param name the name of the data store
42      * @param properties configuration properties for the InMemoryDOMDataStore instance.
43      * @param schemaService the SchemaService to which to register the data store.
44      * @return an InMemoryDOMDataStore instance
45      */
46     public static InMemoryDOMDataStore create(final String name, final InMemoryDOMDataStoreConfigProperties properties,
47             @Nullable final DOMSchemaService schemaService) {
48         final ExecutorService dataChangeListenerExecutor = createExecutorService(name, properties);
49         final InMemoryDOMDataStore dataStore = new InMemoryDOMDataStore(name, dataChangeListenerExecutor,
50             properties.getMaxDataChangeListenerQueueSize(), properties.getDebugTransactions());
51
52         if (schemaService != null) {
53             schemaService.registerSchemaContextListener(dataStore);
54         }
55
56         return dataStore;
57     }
58
59     private static ExecutorService createExecutorService(final String name,
60             final InMemoryDOMDataStoreConfigProperties props) {
61         // For DataChangeListener notifications we use an executor that provides the fastest
62         // task execution time to get higher throughput as DataChangeListeners typically provide
63         // much of the business logic for a data model. If the executor queue size limit is reached,
64         // subsequent submitted notifications will block the calling thread.
65         return SpecialExecutors.newBlockingBoundedFastThreadPool(
66             props.getMaxDataChangeExecutorPoolSize(), props.getMaxDataChangeExecutorQueueSize(),
67             name + "-DCL", InMemoryDOMDataStoreFactory.class);
68     }
69 }