Bug 488: Removed requirement for DOM providers to supply BundleContext.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / config / yang / md / sal / dom / impl / DomBrokerImplModule.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 org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
11 import org.opendaylight.controller.md.sal.dom.broker.impl.compat.BackwardsCompatibleDataBroker;
12 import org.opendaylight.controller.sal.core.api.BrokerService;
13 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
14 import org.opendaylight.controller.sal.core.api.data.DataBrokerService;
15 import org.opendaylight.controller.sal.core.api.data.DataProviderService;
16 import org.opendaylight.controller.sal.core.api.data.DataStore;
17 import org.opendaylight.controller.sal.core.api.model.SchemaService;
18 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
19 import org.opendaylight.controller.sal.core.api.mount.MountService;
20 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
21 import org.opendaylight.controller.sal.dom.broker.DataBrokerImpl;
22 import org.opendaylight.controller.sal.dom.broker.GlobalBundleScanningSchemaServiceImpl;
23 import org.opendaylight.controller.sal.dom.broker.MountPointManagerImpl;
24 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareDataStoreAdapter;
25 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
26 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProviders;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
28
29 import com.google.common.collect.ClassToInstanceMap;
30 import com.google.common.collect.MutableClassToInstanceMap;
31
32 /**
33 *
34 */
35 public final class DomBrokerImplModule extends org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomBrokerImplModule
36 {
37
38     public DomBrokerImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
39         super(identifier, dependencyResolver);
40     }
41
42     public DomBrokerImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final DomBrokerImplModule oldModule, final java.lang.AutoCloseable oldInstance) {
43         super(identifier, dependencyResolver, oldModule, oldInstance);
44     }
45
46     @Override
47     public void validate(){
48         super.validate();
49     }
50
51     @Override
52     public java.lang.AutoCloseable createInstance() {
53         final DataStore legacyStore = getDataStoreDependency();
54         final DOMDataBroker asyncBroker= getAsyncDataBrokerDependency();
55
56         ClassToInstanceMap<BrokerService> services = MutableClassToInstanceMap.create();
57
58
59         SchemaService schemaService = getSchemaServiceImpl();
60         services.putInstance(SchemaService.class, schemaService);
61         SchemaAwareRpcBroker router = new SchemaAwareRpcBroker("/", SchemaContextProviders
62                 .fromSchemaService(schemaService));
63         services.putInstance(RpcProvisionRegistry.class, router);
64
65         final DataProviderService legacyData;
66         if(asyncBroker != null) {
67             services.putInstance(DOMDataBroker.class, asyncBroker);
68             legacyData = new BackwardsCompatibleDataBroker(asyncBroker,schemaService);
69         } else {
70             legacyData = createLegacyDataService(legacyStore,schemaService);
71         }
72         services.putInstance(DataProviderService.class,legacyData);
73         services.putInstance(DataBrokerService.class, legacyData);
74
75
76         MountPointManagerImpl mountService = new MountPointManagerImpl();
77         services.putInstance(MountService.class, mountService);
78         services.putInstance(MountProvisionService.class, mountService);
79
80         return new BrokerImpl(router, services);
81     }
82
83     private DataProviderService createLegacyDataService(final DataStore legacyStore, final SchemaService schemaService) {
84         InstanceIdentifier rootPath = InstanceIdentifier.builder().toInstance();
85         DataBrokerImpl dataService = new DataBrokerImpl();
86         SchemaAwareDataStoreAdapter wrappedStore = new SchemaAwareDataStoreAdapter();
87         wrappedStore.changeDelegate(legacyStore);
88         wrappedStore.setValidationEnabled(false);
89
90         schemaService.registerSchemaServiceListener(wrappedStore);
91
92         dataService.registerConfigurationReader(rootPath, wrappedStore);
93         dataService.registerCommitHandler(rootPath, wrappedStore);
94         dataService.registerOperationalReader(rootPath, wrappedStore);
95         return dataService;
96     }
97
98     private SchemaService getSchemaServiceImpl() {
99         final SchemaService schemaService;
100         if(getRootSchemaService() != null) {
101             schemaService = getRootSchemaServiceDependency();
102         } else {
103             schemaService = GlobalBundleScanningSchemaServiceImpl.getInstance();
104         }
105         return schemaService;
106     }
107 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.