Bug 8568: Remove deprecated HydrogenDataBrokerAdapter
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / compat / HydrogenMountInstanceAdapter.java
1 /*
2  * Copyright (c) 2015 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.md.sal.binding.compat;
9
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.MountPoint;
16 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
20 import org.opendaylight.controller.sal.binding.api.NotificationListener;
21 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
22 import org.opendaylight.controller.sal.binding.api.NotificationService;
23 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
24 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
25 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
26 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
27 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
28 import org.opendaylight.controller.sal.binding.api.mount.MountProviderInstance;
29 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34 import org.opendaylight.yangtools.yang.binding.RpcService;
35
36 @Deprecated
37 public class HydrogenMountInstanceAdapter implements MountProviderInstance {
38
39     private final ClassToInstanceMap<BindingAwareService> services;
40     private final InstanceIdentifier<?> identifier;
41
42
43     public HydrogenMountInstanceAdapter(final MountPoint key) {
44         this.identifier = key.getIdentifier();
45         final ImmutableClassToInstanceMap.Builder<BindingAwareService> builder = ImmutableClassToInstanceMap.builder();
46
47         final Optional<org.opendaylight.controller.md.sal.binding.api.NotificationService> notificationService = key.getService(org.opendaylight.controller.md.sal.binding.api.NotificationService.class);
48         if(notificationService.isPresent()) {
49             builder.put(NotificationService.class, new HeliumNotificationServiceAdapter(notificationService.get()));
50         }
51         final Optional<RpcConsumerRegistry> rpcRegistry = key.getService(RpcConsumerRegistry.class);
52         if(rpcRegistry.isPresent()) {
53             builder.put(RpcConsumerRegistry.class, rpcRegistry.get());
54         }
55         services = builder.build();
56     }
57
58
59     private <T extends BindingAwareService> T service(final Class<T> service) {
60         final T potential = services.getInstance(service);
61         Preconditions.checkState(potential != null, "Service %s is not supported by mount point %s",service,this.getIdentifier());
62         return potential;
63     }
64
65     @Override
66     public <T extends RpcService> T getRpcService(final Class<T> serviceInterface) {
67         return service(RpcConsumerRegistry.class).getRpcService(serviceInterface);
68     }
69
70     @Override
71     public InstanceIdentifier<?> getIdentifier() {
72         return identifier;
73     }
74
75     @Override
76     public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(
77             final Class<T> notificationType, final NotificationListener<T> listener) {
78         return service(NotificationService.class).registerNotificationListener(notificationType, listener);
79     }
80
81     @Override
82     public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(
83             final org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
84         return service(NotificationService.class).registerNotificationListener(listener);
85     }
86
87     @Override
88     public DataModificationTransaction beginTransaction() {
89         return service(DataBrokerService.class).beginTransaction();
90     }
91
92     @Override
93     public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
94         return service(DataBrokerService.class).readConfigurationData(path);
95     }
96
97     @Override
98     public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
99         return service(DataBrokerService.class).readOperationalData(path);
100     }
101
102     @Override
103     public ListenerRegistration<DataChangeListener> registerDataChangeListener(
104             final InstanceIdentifier<? extends DataObject> path, final DataChangeListener listener) {
105         return service(DataBrokerService.class).registerDataChangeListener(path,listener);
106     }
107
108     @Override
109     public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> serviceInterface,
110             final T implementation) throws IllegalStateException {
111         return service(RpcProviderRegistry.class).addRoutedRpcImplementation(serviceInterface, implementation);
112     }
113
114     @Override
115     public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> serviceInterface, final T implementation)
116             throws IllegalStateException {
117         return service(RpcProviderRegistry.class).addRpcImplementation(serviceInterface, implementation);
118     }
119
120     @Override
121     public void publish(final Notification notification) {
122         service(NotificationProviderService.class).publish(notification);
123     }
124
125     @Override
126     public void publish(final Notification notification, final ExecutorService executor) {
127         service(NotificationProviderService.class).publish(notification);
128     }
129
130     @Override
131     public ListenerRegistration<NotificationInterestListener> registerInterestListener(
132             final NotificationInterestListener interestListener) {
133         return service(NotificationProviderService.class).registerInterestListener(interestListener);
134     }
135
136     @Override
137     public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
138             final L arg0) {
139         return service(RpcProviderRegistry.class).registerRouteChangeListener(arg0);
140     }
141
142 }