2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.binding.compat;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.ImmutableClassToInstanceMap;
14 import java.util.concurrent.ExecutorService;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
17 import org.opendaylight.controller.md.sal.common.api.RegistrationListener;
18 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
19 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandlerRegistration;
20 import org.opendaylight.controller.md.sal.common.api.data.DataReader;
21 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
25 import org.opendaylight.controller.sal.binding.api.NotificationListener;
26 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
27 import org.opendaylight.controller.sal.binding.api.NotificationService;
28 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
29 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
31 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
32 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
33 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
34 import org.opendaylight.controller.sal.binding.api.mount.MountProviderInstance;
35 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
36 import org.opendaylight.yangtools.concepts.ListenerRegistration;
37 import org.opendaylight.yangtools.concepts.Registration;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.Notification;
41 import org.opendaylight.yangtools.yang.binding.RpcService;
44 public class HydrogenMountInstanceAdapter implements MountProviderInstance {
46 private final ClassToInstanceMap<BindingAwareService> services;
47 private final InstanceIdentifier<?> identifier;
50 public HydrogenMountInstanceAdapter(final MountPoint key) {
51 this.identifier = key.getIdentifier();
52 final ImmutableClassToInstanceMap.Builder<BindingAwareService> builder = ImmutableClassToInstanceMap.builder();
54 final Optional<DataBroker> dataBroker = key.getService(DataBroker.class);
55 if(dataBroker.isPresent()) {
56 builder.put(DataBrokerService.class, new HydrogenDataBrokerAdapter(dataBroker.get()));
58 final Optional<org.opendaylight.controller.md.sal.binding.api.NotificationService> notificationService = key.getService(org.opendaylight.controller.md.sal.binding.api.NotificationService.class);
59 if(notificationService.isPresent()) {
60 builder.put(NotificationService.class, new HeliumNotificationServiceAdapter(notificationService.get()));
62 final Optional<RpcConsumerRegistry> rpcRegistry = key.getService(RpcConsumerRegistry.class);
63 if(rpcRegistry.isPresent()) {
64 builder.put(RpcConsumerRegistry.class, rpcRegistry.get());
66 services = builder.build();
70 private <T extends BindingAwareService> T service(final Class<T> service) {
71 final T potential = services.getInstance(service);
72 Preconditions.checkState(potential != null, "Service %s is not supported by mount point %s",service,this.getIdentifier());
77 public <T extends RpcService> T getRpcService(final Class<T> serviceInterface) {
78 return service(RpcConsumerRegistry.class).getRpcService(serviceInterface);
82 public InstanceIdentifier<?> getIdentifier() {
87 public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(
88 final Class<T> notificationType, final NotificationListener<T> listener) {
89 return service(NotificationService.class).registerNotificationListener(notificationType, listener);
93 public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(
94 final org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
95 return service(NotificationService.class).registerNotificationListener(listener);
99 public DataModificationTransaction beginTransaction() {
100 return service(DataBrokerService.class).beginTransaction();
104 public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
105 return service(DataBrokerService.class).readConfigurationData(path);
109 public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
110 return service(DataBrokerService.class).readOperationalData(path);
114 public ListenerRegistration<DataChangeListener> registerDataChangeListener(
115 final InstanceIdentifier<? extends DataObject> path, final DataChangeListener listener) {
116 return service(DataBrokerService.class).registerDataChangeListener(path,listener);
120 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> serviceInterface,
121 final T implementation) throws IllegalStateException {
122 return service(RpcProviderRegistry.class).addRoutedRpcImplementation(serviceInterface, implementation);
126 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> serviceInterface, final T implementation)
127 throws IllegalStateException {
128 return service(RpcProviderRegistry.class).addRpcImplementation(serviceInterface, implementation);
132 public void publish(final Notification notification) {
133 service(NotificationProviderService.class).publish(notification);
137 public void publish(final Notification notification, final ExecutorService executor) {
138 service(NotificationProviderService.class).publish(notification);
142 public Registration registerCommitHandler(final InstanceIdentifier<? extends DataObject> arg0,
143 final DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject> arg1) {
144 return service(DataProviderService.class).registerCommitHandler(arg0, arg1);
148 public ListenerRegistration<RegistrationListener<DataCommitHandlerRegistration<InstanceIdentifier<? extends DataObject>, DataObject>>> registerCommitHandlerListener(
149 final RegistrationListener<DataCommitHandlerRegistration<InstanceIdentifier<? extends DataObject>, DataObject>> arg0) {
150 return service(DataProviderService.class).registerCommitHandlerListener(arg0);
154 public Registration registerDataReader(final InstanceIdentifier<? extends DataObject> path,
155 final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> reader) {
156 return service(DataProviderService.class).registerDataReader(path, reader);
160 public ListenerRegistration<NotificationInterestListener> registerInterestListener(
161 final NotificationInterestListener interestListener) {
162 return service(NotificationProviderService.class).registerInterestListener(interestListener);
166 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
168 return service(RpcProviderRegistry.class).registerRouteChangeListener(arg0);