1 package org.opendaylight.controller.md.sal.binding.util;
3 import java.awt.image.SinglePixelPackedSampleModel;
5 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
6 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
7 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
8 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
9 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
10 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
11 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider.ProviderFunctionality;
12 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
15 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
16 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.binding.RpcService;
21 import static com.google.common.base.Preconditions.*;
23 import com.google.common.collect.ClassToInstanceMap;
24 import com.google.common.collect.MutableClassToInstanceMap;
26 public class BindingContextUtils {
28 public static ConsumerContext createConsumerContext(BindingAwareConsumer consumer,
29 ClassToInstanceMap<BindingAwareService> serviceProvider) {
30 checkNotNull(consumer,"Consumer should not be null");
31 checkNotNull(serviceProvider,"Service map should not be null");
32 return new SingleConsumerContextImpl(serviceProvider);
35 public static ProviderContext createProviderContext(BindingAwareProvider provider,
36 ClassToInstanceMap<BindingAwareService> serviceProvider) {
37 checkNotNull(provider,"Provider should not be null");
38 checkNotNull(serviceProvider,"Service map should not be null");
39 return new SingleProviderContextImpl(serviceProvider);
42 public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer,
43 ClassToInstanceMap<BindingAwareService> serviceProvider) {
44 ConsumerContext context = createConsumerContext(consumer, serviceProvider);
45 consumer.onSessionInitialized(context);
49 public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider,
50 ClassToInstanceMap<BindingAwareService> serviceProvider) {
51 ProviderContext context = createProviderContext(provider, serviceProvider);
52 provider.onSessionInitiated(context);
56 public static <T extends BindingAwareService> T createContextProxyOrReturnService(Class<T> service, T instance) {
57 // FIXME: Create Proxy
61 private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
63 private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
64 private ClassToInstanceMap<BindingAwareService> serviceProvider;
66 public SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
67 this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
68 this.serviceProvider = serviceProvider;
72 public final <T extends RpcService> T getRpcService(Class<T> module) {
73 return getSALService(RpcConsumerRegistry.class).getRpcService(module);
77 public final <T extends BindingAwareService> T getSALService(Class<T> service) {
78 checkNotNull(service,"Service class should not be null.");
79 T potential = alreadyRetrievedServices.getInstance(service);
80 if(potential != null) {
83 return tryToRetrieveSalService(service);
86 private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(Class<T> service) {
87 final T potential = alreadyRetrievedServices.getInstance(service);
88 if(potential != null) {
91 final T requested = serviceProvider.getInstance(service);
92 if(requested == null) {
93 throw new IllegalArgumentException("Requested service "+service.getName() +" is not available.");
95 final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
96 alreadyRetrievedServices.put(service, retrieved);
101 public final void close() throws Exception {
102 alreadyRetrievedServices = null;
103 serviceProvider = null;
107 private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
109 public SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
110 super(serviceProvider);
114 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
116 return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
120 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
121 T implementation) throws IllegalStateException {
122 return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
126 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
127 throws IllegalStateException {
128 return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);
133 public void registerFunctionality(ProviderFunctionality functionality) {
139 public void unregisterFunctionality(ProviderFunctionality functionality) {