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