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