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