Merge "Bug-835:Reserve ports should be logical ports"
[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 com.google.common.collect.ImmutableMap;
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
15 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
16 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
18 import org.osgi.framework.BundleContext;
19
20 import java.util.Hashtable;
21 import java.util.concurrent.Executors;
22
23 /**
24 *
25 */
26 public final class DomInmemoryDataBrokerModule extends
27         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
28
29     private BundleContext bundleContext;
30
31     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
32             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
33         super(identifier, dependencyResolver);
34     }
35
36     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
37             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
38             final DomInmemoryDataBrokerModule oldModule, final java.lang.AutoCloseable oldInstance) {
39
40         super(identifier, dependencyResolver, oldModule, oldInstance);
41     }
42
43     @Override
44     protected void customValidation() {
45         // Add custom validation for module attributes here.
46     }
47
48     @Override
49     public java.lang.AutoCloseable createInstance() {
50         ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
51         //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
52         DOMStore operStore =  getOperationalDataStoreDependency();
53         if(operStore == null){
54            //we will default to InMemoryDOMDataStore creation
55           operStore = new InMemoryDOMDataStore("DOM-OPER", storeExecutor);
56           //here we will register the SchemaContext listener
57           getSchemaServiceDependency().registerSchemaServiceListener((InMemoryDOMDataStore)operStore);
58         }
59
60         DOMStore configStore = getConfigDataStoreDependency();
61         if(configStore == null){
62            //we will default to InMemoryDOMDataStore creation
63            configStore = new InMemoryDOMDataStore("DOM-CFG", storeExecutor);
64           //here we will register the SchemaContext listener
65           getSchemaServiceDependency().registerSchemaServiceListener((InMemoryDOMDataStore)configStore);
66         }
67         ImmutableMap<LogicalDatastoreType, DOMStore> datastores = ImmutableMap
68                 .<LogicalDatastoreType, DOMStore> builder().put(LogicalDatastoreType.OPERATIONAL, operStore)
69                 .put(LogicalDatastoreType.CONFIGURATION, configStore).build();
70
71         DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores, MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()));
72
73         getBundleContext().registerService(DOMDataBroker.class, newDataBroker, new Hashtable<String, String>());
74
75
76         return newDataBroker;
77     }
78
79     private BundleContext getBundleContext() {
80         return bundleContext;
81     }
82
83     void setBundleContext(final BundleContext ctx) {
84         bundleContext = ctx;
85     }
86 }