BUG 932 - Swagger HTTP POST contains incorrect object
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / config / yang / md / sal / dom / impl / DomInmemoryDataBrokerModule.java
1 /*
2  * Copyright (c) 2014 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.controller.config.yang.md.sal.dom.impl;
9
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitDeadlockException;
14 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
15 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
17 import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
18 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
19 import org.opendaylight.yangtools.util.PropertyUtils;
20
21 import com.google.common.collect.ImmutableMap;
22
23 /**
24 *
25 */
26 public final class DomInmemoryDataBrokerModule extends
27         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
28
29     private static final String FUTURE_CALLBACK_EXECUTOR_MAX_QUEUE_SIZE_PROP =
30             "mdsal.datastore-future-callback-queue.size";
31     private static final int DEFAULT_FUTURE_CALLBACK_EXECUTOR_MAX_QUEUE_SIZE = 1000;
32
33     private static final String FUTURE_CALLBACK_EXECUTOR_MAX_POOL_SIZE_PROP =
34             "mdsal.datastore-future-callback-pool.size";
35     private static final int DEFAULT_FUTURE_CALLBACK_EXECUTOR_MAX_POOL_SIZE = 20;
36     private static final String COMMIT_EXECUTOR_MAX_QUEUE_SIZE_PROP =
37             "mdsal.datastore-commit-queue.size";
38     private static final int DEFAULT_COMMIT_EXECUTOR_MAX_QUEUE_SIZE = 5000;
39
40     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
41             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
42         super(identifier, dependencyResolver);
43     }
44
45     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
46             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
47             final DomInmemoryDataBrokerModule oldModule, final java.lang.AutoCloseable oldInstance) {
48
49         super(identifier, dependencyResolver, oldModule, oldInstance);
50     }
51
52     @Override
53     protected void customValidation() {
54         // Add custom validation for module attributes here.
55     }
56
57     @Override
58     public java.lang.AutoCloseable createInstance() {
59         //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
60         DOMStore operStore =  getOperationalDataStoreDependency();
61         if(operStore == null){
62            //we will default to InMemoryDOMDataStore creation
63           operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", getSchemaServiceDependency());
64         }
65
66         DOMStore configStore = getConfigDataStoreDependency();
67         if(configStore == null){
68            //we will default to InMemoryDOMDataStore creation
69            configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", getSchemaServiceDependency());
70         }
71         ImmutableMap<LogicalDatastoreType, DOMStore> datastores = ImmutableMap
72                 .<LogicalDatastoreType, DOMStore> builder().put(LogicalDatastoreType.OPERATIONAL, operStore)
73                 .put(LogicalDatastoreType.CONFIGURATION, configStore).build();
74
75         /*
76          * We use a single-threaded executor for commits with a bounded queue capacity. If the
77          * queue capacity is reached, subsequent commit tasks will be rejected and the commits will
78          * fail. This is done to relieve back pressure. This should be an extreme scenario - either
79          * there's deadlock(s) somewhere and the controller is unstable or some rogue component is
80          * continuously hammering commits too fast or the controller is just over-capacity for the
81          * system it's running on.
82          */
83         ExecutorService commitExecutor = SpecialExecutors.newBoundedSingleThreadExecutor(
84                 PropertyUtils.getIntSystemProperty(
85                         COMMIT_EXECUTOR_MAX_QUEUE_SIZE_PROP,
86                         DEFAULT_COMMIT_EXECUTOR_MAX_QUEUE_SIZE), "WriteTxCommit");
87
88         /*
89          * We use an executor for commit ListenableFuture callbacks that favors reusing available
90          * threads over creating new threads at the expense of execution time. The assumption is
91          * that most ListenableFuture callbacks won't execute a lot of business logic where we want
92          * it to run quicker - many callbacks will likely just handle error conditions and do
93          * nothing on success. The executor queue capacity is bounded and, if the capacity is
94          * reached, subsequent submitted tasks will block the caller.
95          */
96         Executor listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
97                 PropertyUtils.getIntSystemProperty(
98                         FUTURE_CALLBACK_EXECUTOR_MAX_POOL_SIZE_PROP,
99                         DEFAULT_FUTURE_CALLBACK_EXECUTOR_MAX_POOL_SIZE),
100                 PropertyUtils.getIntSystemProperty(
101                         FUTURE_CALLBACK_EXECUTOR_MAX_QUEUE_SIZE_PROP,
102                         DEFAULT_FUTURE_CALLBACK_EXECUTOR_MAX_QUEUE_SIZE), "CommitFutures");
103
104         DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores,
105                 new DeadlockDetectingListeningExecutorService(commitExecutor,
106                     TransactionCommitDeadlockException.DEADLOCK_EXECUTOR_FUNCTION,
107                     listenableFutureExecutor));
108
109         return newDataBroker;
110     }
111 }