Merge "Revert "Revert "BUG-1425: Integrated new Binding to Normalized Node codec...
[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 com.google.common.collect.ImmutableMap;
20
21 /**
22 *
23 */
24 public final class DomInmemoryDataBrokerModule extends
25         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
26
27     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
28             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
29         super(identifier, dependencyResolver);
30     }
31
32     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
33             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
34             final DomInmemoryDataBrokerModule oldModule, final java.lang.AutoCloseable oldInstance) {
35
36         super(identifier, dependencyResolver, oldModule, oldInstance);
37     }
38
39     @Override
40     protected void customValidation() {
41         // Add custom validation for module attributes here.
42     }
43
44     @Override
45     public java.lang.AutoCloseable createInstance() {
46         //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
47         DOMStore operStore =  getOperationalDataStoreDependency();
48         if(operStore == null){
49            //we will default to InMemoryDOMDataStore creation
50           operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", getSchemaServiceDependency());
51         }
52
53         DOMStore configStore = getConfigDataStoreDependency();
54         if(configStore == null){
55            //we will default to InMemoryDOMDataStore creation
56            configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", getSchemaServiceDependency());
57         }
58         ImmutableMap<LogicalDatastoreType, DOMStore> datastores = ImmutableMap
59                 .<LogicalDatastoreType, DOMStore> builder().put(LogicalDatastoreType.OPERATIONAL, operStore)
60                 .put(LogicalDatastoreType.CONFIGURATION, configStore).build();
61
62         /*
63          * We use a single-threaded executor for commits with a bounded queue capacity. If the
64          * queue capacity is reached, subsequent commit tasks will be rejected and the commits will
65          * fail. This is done to relieve back pressure. This should be an extreme scenario - either
66          * there's deadlock(s) somewhere and the controller is unstable or some rogue component is
67          * continuously hammering commits too fast or the controller is just over-capacity for the
68          * system it's running on.
69          */
70         ExecutorService commitExecutor = SpecialExecutors.newBoundedSingleThreadExecutor(
71                 getMaxDataBrokerCommitQueueSize(), "WriteTxCommit");
72
73         /*
74          * We use an executor for commit ListenableFuture callbacks that favors reusing available
75          * threads over creating new threads at the expense of execution time. The assumption is
76          * that most ListenableFuture callbacks won't execute a lot of business logic where we want
77          * it to run quicker - many callbacks will likely just handle error conditions and do
78          * nothing on success. The executor queue capacity is bounded and, if the capacity is
79          * reached, subsequent submitted tasks will block the caller.
80          */
81         Executor listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
82                 getMaxDataBrokerFutureCallbackPoolSize(), getMaxDataBrokerFutureCallbackQueueSize(),
83                 "CommitFutures");
84
85         DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores,
86                 new DeadlockDetectingListeningExecutorService(commitExecutor,
87                     TransactionCommitDeadlockException.DEADLOCK_EXECUTOR_FUNCTION,
88                     listenableFutureExecutor));
89
90         return newDataBroker;
91     }
92 }