Merge "Fix for Bug 3"
[controller.git] / opendaylight / md-sal / sal-binding-util / src / main / java / org / opendaylight / controller / md / sal / binding / util / BindingContextUtils.java
1 package org.opendaylight.controller.md.sal.binding.util;
2
3 import java.awt.image.SinglePixelPackedSampleModel;
4
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;
20
21 import static com.google.common.base.Preconditions.*;
22
23 import com.google.common.collect.ClassToInstanceMap;
24 import com.google.common.collect.MutableClassToInstanceMap;
25
26 public class BindingContextUtils {
27
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);
33     }
34     
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);
40     }
41
42     public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer,
43             ClassToInstanceMap<BindingAwareService> serviceProvider) {
44         ConsumerContext context = createConsumerContext(consumer, serviceProvider);
45         consumer.onSessionInitialized(context);
46         return context;
47     }
48     
49     public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider,
50             ClassToInstanceMap<BindingAwareService> serviceProvider) {
51         ProviderContext context = createProviderContext(provider, serviceProvider);
52         provider.onSessionInitiated(context);
53         return context;
54     }
55
56     public static <T extends BindingAwareService> T createContextProxyOrReturnService(Class<T> service, T instance) {
57         // FIXME: Create Proxy
58         return instance;
59     }
60     
61     private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
62         
63         private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
64         private ClassToInstanceMap<BindingAwareService> serviceProvider;
65
66         public SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
67             this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
68             this.serviceProvider = serviceProvider;
69         }
70
71         @Override
72         public final <T extends RpcService> T getRpcService(Class<T> module) {
73             return getSALService(RpcConsumerRegistry.class).getRpcService(module);
74         }
75         
76         @Override
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) {
81                 return potential;
82             }
83             return tryToRetrieveSalService(service);
84         }
85         
86         private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(Class<T> service) {
87             final T potential = alreadyRetrievedServices.getInstance(service);
88             if(potential != null) {
89                 return potential;
90             }
91             final T requested = serviceProvider.getInstance(service);
92             if(requested == null) {
93                 throw new IllegalArgumentException("Requested service "+service.getName() +" is not available.");
94             }
95             final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
96             alreadyRetrievedServices.put(service, retrieved);
97             return retrieved;
98         }
99         
100         @Override
101         public final void close() throws Exception {
102             alreadyRetrievedServices = null;
103             serviceProvider = null;
104         }
105     }
106     
107     private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
108
109         public SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
110             super(serviceProvider);
111         }
112         
113         @Override
114         public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
115                 L listener) {
116             return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
117         }
118         
119         @Override
120         public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
121                 T implementation) throws IllegalStateException {
122             return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
123         }
124         
125         @Override
126         public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
127                 throws IllegalStateException {
128             return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);
129         }
130         
131         @Deprecated
132         @Override
133         public void registerFunctionality(ProviderFunctionality functionality) {
134             // NOOP
135         }
136         
137         @Deprecated
138         @Override
139         public void unregisterFunctionality(ProviderFunctionality functionality) {
140             // NOOP
141         }
142     }
143 }