BUG-614: migrate RuntimeGeneratedInvoker
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / BrokerConfigActivator.java
1 /**
2  * Copyright (c) 2013 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.sal.dom.broker;
9
10 import java.util.Hashtable;
11
12 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
13 import org.opendaylight.controller.md.sal.dom.broker.impl.compat.BackwardsCompatibleDataBroker;
14 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
15 import org.opendaylight.controller.sal.core.api.data.DataBrokerService;
16 import org.opendaylight.controller.sal.core.api.data.DataProviderService;
17 import org.opendaylight.controller.sal.core.api.data.DataStore;
18 import org.opendaylight.controller.sal.core.api.model.SchemaService;
19 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
20 import org.opendaylight.controller.sal.core.api.mount.MountService;
21 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareDataStoreAdapter;
22 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
23 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProviders;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.ServiceReference;
28 import org.osgi.framework.ServiceRegistration;
29
30 public class BrokerConfigActivator implements AutoCloseable {
31
32     private static InstanceIdentifier ROOT = InstanceIdentifier.builder()
33             .toInstance();
34
35     private DataProviderService dataService;
36
37     private ServiceRegistration<DataBrokerService> dataReg = null;
38     private ServiceRegistration<DataProviderService> dataProviderReg = null;
39     private ServiceRegistration<MountService> mountReg = null;
40     private ServiceRegistration<MountProvisionService> mountProviderReg = null;
41     private SchemaService schemaService = null;
42     private ServiceRegistration<RpcProvisionRegistry> rpcProvisionRegistryReg = null;
43     private MountPointManagerImpl mountService = null;
44
45     private SchemaAwareDataStoreAdapter wrappedStore = null;
46
47     public void start(final BrokerImpl broker, final DataStore store,
48             final DOMDataBroker asyncBroker, final BundleContext context) {
49
50         final Hashtable<String, String> emptyProperties = new Hashtable<String, String>();
51         broker.setBundleContext(context);
52
53         final ServiceReference<SchemaService> serviceRef = context
54                 .getServiceReference(SchemaService.class);
55         schemaService = context.<SchemaService> getService(serviceRef);
56
57         broker.setRouter(new SchemaAwareRpcBroker("/", SchemaContextProviders
58                 .fromSchemaService(schemaService)));
59
60         if (asyncBroker == null) {
61             dataService = new DataBrokerImpl();
62             dataProviderReg = context.registerService(
63                     DataProviderService.class, dataService, emptyProperties);
64
65             wrappedStore = new SchemaAwareDataStoreAdapter();
66             wrappedStore.changeDelegate(store);
67             wrappedStore.setValidationEnabled(false);
68             context.registerService(SchemaServiceListener.class, wrappedStore,
69                     emptyProperties);
70
71             dataService.registerConfigurationReader(ROOT, wrappedStore);
72             dataService.registerCommitHandler(ROOT, wrappedStore);
73             dataService.registerOperationalReader(ROOT, wrappedStore);
74         } else {
75             BackwardsCompatibleDataBroker compatibleDataBroker = new BackwardsCompatibleDataBroker(
76                     asyncBroker);
77             context.registerService(SchemaServiceListener.class,
78                     compatibleDataBroker, emptyProperties);
79             dataService = compatibleDataBroker;
80         }
81
82         mountService = new MountPointManagerImpl();
83         dataReg = context.registerService(DataBrokerService.class, dataService,
84                 emptyProperties);
85         mountReg = context.registerService(MountService.class, mountService,
86                 emptyProperties);
87         mountProviderReg = context.registerService(MountProvisionService.class,
88                 mountService, emptyProperties);
89
90         rpcProvisionRegistryReg = context
91                 .registerService(RpcProvisionRegistry.class,
92                         broker.getRouter(), emptyProperties);
93     }
94
95     @Override
96     public void close() {
97
98         if (dataReg != null) {
99             dataReg.unregister();
100             dataReg = null;
101         }
102         if (dataProviderReg != null) {
103             dataProviderReg.unregister();
104             dataProviderReg = null;
105         }
106         if (mountReg != null) {
107             mountReg.unregister();
108             mountReg = null;
109         }
110         if (mountProviderReg != null) {
111             mountProviderReg.unregister();
112             mountProviderReg = null;
113         }
114         if (rpcProvisionRegistryReg != null) {
115             rpcProvisionRegistryReg.unregister();
116             rpcProvisionRegistryReg = null;
117         }
118     }
119
120     /**
121      * @return the dataService
122      */
123     public DataProviderService getDataService() {
124         return dataService;
125     }
126
127     /**
128      * @param dataService
129      *            the dataService to set
130      */
131     public void setDataService(final DataProviderService dataService) {
132         this.dataService = dataService;
133     }
134 }