Merge "Prevent ConfigPusher from killing its thread"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / RootBindingAwareBroker.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.sal.binding.impl;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import org.opendaylight.controller.md.sal.binding.util.AbstractBindingSalProviderInstance;
13 import org.opendaylight.controller.md.sal.binding.util.BindingContextUtils;
14 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
19 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
20 import org.opendaylight.controller.sal.binding.api.NotificationService;
21 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
24 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
25 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
26 import org.opendaylight.controller.sal.binding.api.mount.MountService;
27 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
28 import org.opendaylight.yangtools.concepts.Identifiable;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.concepts.Mutable;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.binding.RpcService;
33 import org.osgi.framework.BundleContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.collect.ImmutableClassToInstanceMap;
38
39 public class RootBindingAwareBroker implements //
40         Mutable, //
41         Identifiable<String>, //
42         BindingAwareBroker, AutoCloseable,
43         RpcProviderRegistry {
44
45     private final static Logger LOG = LoggerFactory.getLogger(RootBindingAwareBroker.class);
46
47     RootSalInstance controllerRoot;
48
49     private final String identifier;
50
51     private RpcProviderRegistry rpcBroker;
52
53     private NotificationProviderService notificationBroker;
54
55     private DataProviderService dataBroker;
56
57     private MountPointManagerImpl mountManager;
58
59     public MountPointManagerImpl getMountManager() {
60         return mountManager;
61     }
62
63     public void setMountManager(MountPointManagerImpl mountManager) {
64         this.mountManager = mountManager;
65     }
66
67     private ImmutableClassToInstanceMap<BindingAwareService> supportedConsumerServices;
68
69     private ImmutableClassToInstanceMap<BindingAwareService> supportedProviderServices;
70
71     public RootBindingAwareBroker(String instanceName) {
72         this.identifier = instanceName;
73         mountManager = new MountPointManagerImpl();
74     }
75
76     public String getIdentifier() {
77         return identifier;
78     }
79
80     public RootSalInstance getRoot() {
81         return controllerRoot;
82     }
83
84     public DataProviderService getDataBroker() {
85         return this.dataBroker;
86     }
87
88     public NotificationProviderService getNotificationBroker() {
89         return this.notificationBroker;
90     }
91
92     public RpcProviderRegistry getRpcProviderRegistry() {
93         return this.rpcBroker;
94     }
95
96     public RpcProviderRegistry getRpcBroker() {
97         return rpcBroker;
98     }
99
100     public void setRpcBroker(RpcProviderRegistry rpcBroker) {
101         this.rpcBroker = rpcBroker;
102     }
103
104     public void setNotificationBroker(NotificationProviderService notificationBroker) {
105         this.notificationBroker = notificationBroker;
106     }
107
108     public void setDataBroker(DataProviderService dataBroker) {
109         this.dataBroker = dataBroker;
110     }
111
112     public void start() {
113         checkState(controllerRoot == null, "Binding Aware Broker was already started.");
114         LOG.info("Starting Binding Aware Broker: {}", identifier);
115
116         controllerRoot = new RootSalInstance(getRpcProviderRegistry(), getNotificationBroker(), getDataBroker());
117         
118
119         supportedConsumerServices = ImmutableClassToInstanceMap.<BindingAwareService> builder()
120                 .put(NotificationService.class, getRoot()) //
121                 .put(DataBrokerService.class, getRoot()) //
122                 .put(RpcConsumerRegistry.class, getRoot()) //
123                 .put(MountService.class, mountManager).build();
124
125         supportedProviderServices = ImmutableClassToInstanceMap.<BindingAwareService> builder()
126                 .putAll(supportedConsumerServices)
127                 .put(NotificationProviderService.class, getRoot()) //
128                 .put(DataProviderService.class, getRoot()) //
129                 .put(RpcProviderRegistry.class, getRoot()) //
130                 .put(MountProviderService.class, mountManager).build();
131     }
132
133     @Override
134     public ConsumerContext registerConsumer(BindingAwareConsumer consumer, BundleContext ctx) {
135         checkState(supportedConsumerServices != null, "Broker is not initialized.");
136         return BindingContextUtils.createConsumerContextAndInitialize(consumer, supportedConsumerServices);
137     }
138
139     @Override
140     public ProviderContext registerProvider(BindingAwareProvider provider, BundleContext ctx) {
141         checkState(supportedProviderServices != null, "Broker is not initialized.");
142         return BindingContextUtils.createProviderContextAndInitialize(provider, supportedProviderServices);
143     }
144
145     @Override
146     public void close() throws Exception {
147         // FIXME: Close all sessions
148     }
149     
150     @Override
151     public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type, T implementation)
152             throws IllegalStateException {
153         return getRoot().addRoutedRpcImplementation(type, implementation);
154     }
155     
156     @Override
157     public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
158             throws IllegalStateException {
159         return getRoot().addRpcImplementation(type, implementation);
160     }
161     
162     @Override
163     public <T extends RpcService> T getRpcService(Class<T> module) {
164         return getRoot().getRpcService(module);
165     }
166     @Override
167     public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
168             L arg0) {
169         return getRoot().registerRouteChangeListener(arg0);
170     }
171     
172
173     public class RootSalInstance extends
174             AbstractBindingSalProviderInstance<DataProviderService, NotificationProviderService, RpcProviderRegistry> {
175
176         public RootSalInstance(RpcProviderRegistry rpcRegistry, NotificationProviderService notificationBroker,
177                 DataProviderService dataBroker) {
178             super(rpcRegistry, notificationBroker, dataBroker);
179         }
180     }
181 }