7357926b9e6226a612bd0fc453dc28b737cff5b8
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / config / yang / md / sal / binding / impl / DataBrokerImplModule.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.binding.impl;\r
9 \r
10 import java.util.concurrent.ExecutorService;
11
12 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
13 import org.opendaylight.controller.sal.binding.impl.RootDataBrokerImpl;
14 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingDomConnectorDeployer;
15 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentConnector;
16 import org.opendaylight.controller.sal.binding.impl.forward.DomForwardedDataBrokerImpl;
17 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
18 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceReference;
21 \r
22 /**\r
23 *\r
24 */\r
25 public final class DataBrokerImplModule extends\r
26         org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractDataBrokerImplModule {\r
27 \r
28     private BundleContext bundleContext;\r
29 \r
30     public DataBrokerImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,\r
31             org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {\r
32         super(identifier, dependencyResolver);\r
33     }\r
34 \r
35     public DataBrokerImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,\r
36             org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,\r
37             DataBrokerImplModule oldModule, java.lang.AutoCloseable oldInstance) {\r
38         super(identifier, dependencyResolver, oldModule, oldInstance);\r
39     }\r
40 \r
41     @Override\r
42     public void validate() {\r
43         super.validate();\r
44     }\r
45 \r
46     @Override\r
47     public java.lang.AutoCloseable createInstance() {\r
48         RootDataBrokerImpl dataBindingBroker;\r
49 \r
50 \r
51         ExecutorService listeningExecutor = SingletonHolder.getDefaultCommitExecutor();\r
52         BindingIndependentMappingService potentialMapping = resolveMappingServiceDependency();\r
53         if (getDomBrokerDependency() != null && potentialMapping != null) {\r
54 \r
55             dataBindingBroker = createDomConnectedBroker(listeningExecutor,potentialMapping);\r
56         } else {\r
57             dataBindingBroker = createStandAloneBroker(listeningExecutor);\r
58         }\r
59         dataBindingBroker.registerRuntimeBean(getRootRuntimeBeanRegistratorWrapper());\r
60         dataBindingBroker.setNotificationExecutor(SingletonHolder.getDefaultChangeEventExecutor());\r
61         return dataBindingBroker;\r
62     }\r
63     private BindingIndependentMappingService resolveMappingServiceDependency() {\r
64         if(getMappingService() != null) {\r
65             return getMappingServiceDependency();\r
66         }\r
67 \r
68         ServiceReference<BindingIndependentMappingService> potentialMappingService = bundleContext.getServiceReference(BindingIndependentMappingService.class);\r
69         if(potentialMappingService != null) {\r
70             return bundleContext.getService(potentialMappingService);\r
71         }\r
72         return null;\r
73     }\r
74 \r
75     private RootDataBrokerImpl createStandAloneBroker(ExecutorService listeningExecutor) {\r
76         RootDataBrokerImpl broker = new RootDataBrokerImpl();\r
77         broker.setExecutor(listeningExecutor);\r
78         return broker;\r
79     }\r
80 \r
81     private RootDataBrokerImpl createDomConnectedBroker(ExecutorService listeningExecutor, BindingIndependentMappingService mappingService) {\r
82         DomForwardedDataBrokerImpl forwardedBroker = new DomForwardedDataBrokerImpl();\r
83         forwardedBroker.setExecutor(listeningExecutor);\r
84         BindingIndependentConnector connector = BindingDomConnectorDeployer.createConnector(mappingService);\r
85         getDomBrokerDependency().registerProvider(forwardedBroker, getBundleContext());\r
86         ProviderSession domContext = forwardedBroker.getDomProviderContext();\r
87         forwardedBroker.setConnector(connector);\r
88         forwardedBroker.setDomProviderContext(domContext);\r
89         forwardedBroker.startForwarding();\r
90         return forwardedBroker;\r
91     }\r
92 \r
93     public BundleContext getBundleContext() {\r
94         return bundleContext;\r
95     }\r
96 \r
97     public void setBundleContext(BundleContext bundleContext2) {\r
98         this.bundleContext = bundleContext2;\r
99     }\r
100 }\r

©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.