Moved binding MD-SAL APIs to binding folder
[mdsal.git] / binding / mdsal-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 static com.google.common.base.Preconditions.checkNotNull;
11
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.MutableClassToInstanceMap;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
21 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.yangtools.yang.binding.RpcService;
24
25 public class BindingContextUtils {
26
27     public static ConsumerContext createConsumerContext(final BindingAwareConsumer consumer,
28             final ClassToInstanceMap<BindingAwareService> serviceProvider) {
29         checkNotNull(consumer,"Consumer should not be null");
30         checkNotNull(serviceProvider,"Service map should not be null");
31         return new SingleConsumerContextImpl(serviceProvider);
32     }
33
34     public static ProviderContext createProviderContext(final BindingAwareProvider provider,
35             final ClassToInstanceMap<BindingAwareService> serviceProvider) {
36         checkNotNull(provider,"Provider should not be null");
37         checkNotNull(serviceProvider,"Service map should not be null");
38         return new SingleProviderContextImpl(serviceProvider);
39     }
40
41     public static ConsumerContext createConsumerContextAndInitialize(final BindingAwareConsumer consumer,
42             final ClassToInstanceMap<BindingAwareService> serviceProvider) {
43         final ConsumerContext context = createConsumerContext(consumer, serviceProvider);
44         consumer.onSessionInitialized(context);
45         return context;
46     }
47
48     public static ProviderContext createProviderContextAndInitialize(final BindingAwareProvider provider,
49             final ClassToInstanceMap<BindingAwareService> serviceProvider) {
50         final ProviderContext context = createProviderContext(provider, serviceProvider);
51         provider.onSessionInitiated(context);
52         return context;
53     }
54
55     public static <T extends BindingAwareService> T createContextProxyOrReturnService(final Class<T> service, final T instance) {
56         // FIXME: Create Proxy
57         return instance;
58     }
59
60     private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
61
62         private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
63         private ClassToInstanceMap<BindingAwareService> serviceProvider;
64
65         public SingleConsumerContextImpl(final ClassToInstanceMap<BindingAwareService> serviceProvider) {
66             this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
67             this.serviceProvider = serviceProvider;
68         }
69
70         @Override
71         public final <T extends RpcService> T getRpcService(final Class<T> module) {
72             return getSALService(RpcConsumerRegistry.class).getRpcService(module);
73         }
74
75         @Override
76         public final <T extends BindingAwareService> T getSALService(final Class<T> service) {
77             checkNotNull(service,"Service class should not be null.");
78             final T potential = alreadyRetrievedServices.getInstance(service);
79             if(potential != null) {
80                 return potential;
81             }
82             return tryToRetrieveSalService(service);
83         }
84
85         private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(final Class<T> service) {
86             final T potential = alreadyRetrievedServices.getInstance(service);
87             if(potential != null) {
88                 return potential;
89             }
90             final T requested = serviceProvider.getInstance(service);
91             if(requested == null) {
92                 throw new IllegalArgumentException("Requested service "+service.getName() +" is not available.");
93             }
94             final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
95             alreadyRetrievedServices.put(service, retrieved);
96             return retrieved;
97         }
98
99         @Override
100         public final void close() throws Exception {
101             alreadyRetrievedServices = null;
102             serviceProvider = null;
103         }
104     }
105
106     private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
107
108         public SingleProviderContextImpl(final ClassToInstanceMap<BindingAwareService> serviceProvider) {
109             super(serviceProvider);
110         }
111
112         @Override
113         public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
114                 final T implementation) throws IllegalStateException {
115             return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
116         }
117
118         @Override
119         public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
120                 throws IllegalStateException {
121             return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);
122         }
123     }
124 }