22dad6af23c1464b63f8a4aa25075551b64ef686
[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.Executors;
11
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.InMemoryDOMDataStore;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
17 import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
18
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.util.concurrent.ListeningExecutorService;
21 import com.google.common.util.concurrent.MoreExecutors;
22
23 /**
24 *
25 */
26 public final class DomInmemoryDataBrokerModule extends
27         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
28
29     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
30             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
31         super(identifier, dependencyResolver);
32     }
33
34     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
35             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
36             final DomInmemoryDataBrokerModule oldModule, final java.lang.AutoCloseable oldInstance) {
37
38         super(identifier, dependencyResolver, oldModule, oldInstance);
39     }
40
41     @Override
42     protected void customValidation() {
43         // Add custom validation for module attributes here.
44     }
45
46     @Override
47     public java.lang.AutoCloseable createInstance() {
48         ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
49         //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
50         DOMStore operStore =  getOperationalDataStoreDependency();
51         if(operStore == null){
52            //we will default to InMemoryDOMDataStore creation
53           operStore = new InMemoryDOMDataStore("DOM-OPER", storeExecutor);
54           //here we will register the SchemaContext listener
55           getSchemaServiceDependency().registerSchemaContextListener((InMemoryDOMDataStore)operStore);
56         }
57
58         DOMStore configStore = getConfigDataStoreDependency();
59         if(configStore == null){
60            //we will default to InMemoryDOMDataStore creation
61            configStore = new InMemoryDOMDataStore("DOM-CFG", storeExecutor);
62           //here we will register the SchemaContext listener
63           getSchemaServiceDependency().registerSchemaContextListener((InMemoryDOMDataStore)configStore);
64         }
65         ImmutableMap<LogicalDatastoreType, DOMStore> datastores = ImmutableMap
66                 .<LogicalDatastoreType, DOMStore> builder().put(LogicalDatastoreType.OPERATIONAL, operStore)
67                 .put(LogicalDatastoreType.CONFIGURATION, configStore).build();
68
69         DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores,
70                 new DeadlockDetectingListeningExecutorService(Executors.newSingleThreadExecutor(),
71                                               TransactionCommitDeadlockException.DEADLOCK_EXECUTOR_FUNCTION));
72
73         return newDataBroker;
74     }
75 }