Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[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.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;
42
43 @Deprecated
44 public class HydrogenMountInstanceAdapter implements MountProviderInstance {
45
46     private final ClassToInstanceMap<BindingAwareService> services;
47     private final InstanceIdentifier<?> identifier;
48
49
50     public HydrogenMountInstanceAdapter(final MountPoint key) {
51         this.identifier = key.getIdentifier();
52         final ImmutableClassToInstanceMap.Builder<BindingAwareService> builder = ImmutableClassToInstanceMap.builder();
53
54         final Optional<DataBroker> dataBroker = key.getService(DataBroker.class);
55         if(dataBroker.isPresent()) {
56             builder.put(DataBrokerService.class, new HydrogenDataBrokerAdapter(dataBroker.get()));
57         }
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()));
61         }
62         final Optional<RpcConsumerRegistry> rpcRegistry = key.getService(RpcConsumerRegistry.class);
63         if(rpcRegistry.isPresent()) {
64             builder.put(RpcConsumerRegistry.class, rpcRegistry.get());
65         }
66         services = builder.build();
67     }
68
69
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());
73         return potential;
74     }
75
76     @Override
77     public <T extends RpcService> T getRpcService(final Class<T> serviceInterface) {
78         return service(RpcConsumerRegistry.class).getRpcService(serviceInterface);
79     }
80
81     @Override
82     public InstanceIdentifier<?> getIdentifier() {
83         return identifier;
84     }
85
86     @Override
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);
90     }
91
92     @Override
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);
96     }
97
98     @Override
99     public DataModificationTransaction beginTransaction() {
100         return service(DataBrokerService.class).beginTransaction();
101     }
102
103     @Override
104     public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
105         return service(DataBrokerService.class).readConfigurationData(path);
106     }
107
108     @Override
109     public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
110         return service(DataBrokerService.class).readOperationalData(path);
111     }
112
113     @Override
114     public ListenerRegistration<DataChangeListener> registerDataChangeListener(
115             final InstanceIdentifier<? extends DataObject> path, final DataChangeListener listener) {
116         return service(DataBrokerService.class).registerDataChangeListener(path,listener);
117     }
118
119     @Override
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);
123     }
124
125     @Override
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);
129     }
130
131     @Override
132     public void publish(final Notification notification) {
133         service(NotificationProviderService.class).publish(notification);
134     }
135
136     @Override
137     public void publish(final Notification notification, final ExecutorService executor) {
138         service(NotificationProviderService.class).publish(notification);
139     }
140
141     @Override
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);
145     }
146
147     @Override
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);
151     }
152
153     @Override
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);
157     }
158
159     @Override
160     public ListenerRegistration<NotificationInterestListener> registerInterestListener(
161             final NotificationInterestListener interestListener) {
162         return service(NotificationProviderService.class).registerInterestListener(interestListener);
163     }
164
165     @Override
166     public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
167             final L arg0) {
168         return service(RpcProviderRegistry.class).registerRouteChangeListener(arg0);
169     }
170
171 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.