3a4d785b923b980b27ed9d23c5e972b3e0bd7e0f
[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.mount.MountProviderInstance;
26 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.opendaylight.yangtools.yang.binding.RpcService;
31
32 @Deprecated
33 public class HydrogenMountInstanceAdapter implements MountProviderInstance {
34
35     private final ClassToInstanceMap<BindingAwareService> services;
36     private final InstanceIdentifier<?> identifier;
37
38
39     public HydrogenMountInstanceAdapter(final MountPoint key) {
40         this.identifier = key.getIdentifier();
41         final ImmutableClassToInstanceMap.Builder<BindingAwareService> builder = ImmutableClassToInstanceMap.builder();
42
43         final Optional<org.opendaylight.controller.md.sal.binding.api.NotificationService> notificationService = key.getService(org.opendaylight.controller.md.sal.binding.api.NotificationService.class);
44         if(notificationService.isPresent()) {
45             builder.put(NotificationService.class, new HeliumNotificationServiceAdapter(notificationService.get()));
46         }
47         final Optional<RpcConsumerRegistry> rpcRegistry = key.getService(RpcConsumerRegistry.class);
48         if(rpcRegistry.isPresent()) {
49             builder.put(RpcConsumerRegistry.class, rpcRegistry.get());
50         }
51         services = builder.build();
52     }
53
54
55     private <T extends BindingAwareService> T service(final Class<T> service) {
56         final T potential = services.getInstance(service);
57         Preconditions.checkState(potential != null, "Service %s is not supported by mount point %s",service,this.getIdentifier());
58         return potential;
59     }
60
61     @Override
62     public <T extends RpcService> T getRpcService(final Class<T> serviceInterface) {
63         return service(RpcConsumerRegistry.class).getRpcService(serviceInterface);
64     }
65
66     @Override
67     public InstanceIdentifier<?> getIdentifier() {
68         return identifier;
69     }
70
71     @Override
72     public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(
73             final Class<T> notificationType, final NotificationListener<T> listener) {
74         return service(NotificationService.class).registerNotificationListener(notificationType, listener);
75     }
76
77     @Override
78     public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(
79             final org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
80         return service(NotificationService.class).registerNotificationListener(listener);
81     }
82
83     @Override
84     public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> serviceInterface,
85             final T implementation) throws IllegalStateException {
86         return service(RpcProviderRegistry.class).addRoutedRpcImplementation(serviceInterface, implementation);
87     }
88
89     @Override
90     public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> serviceInterface, final T implementation)
91             throws IllegalStateException {
92         return service(RpcProviderRegistry.class).addRpcImplementation(serviceInterface, implementation);
93     }
94
95     @Override
96     public void publish(final Notification notification) {
97         service(NotificationProviderService.class).publish(notification);
98     }
99
100     @Override
101     public void publish(final Notification notification, final ExecutorService executor) {
102         service(NotificationProviderService.class).publish(notification);
103     }
104
105     @Override
106     public ListenerRegistration<NotificationInterestListener> registerInterestListener(
107             final NotificationInterestListener interestListener) {
108         return service(NotificationProviderService.class).registerInterestListener(interestListener);
109     }
110
111     @Override
112     public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
113             final L arg0) {
114         return service(RpcProviderRegistry.class).registerRouteChangeListener(arg0);
115     }
116
117 }