Renamed controller.md.sal.binding.api to mdsal.binding.api
[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 org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
13
14 import com.google.common.collect.ClassToInstanceMap;
15 import com.google.common.collect.MutableClassToInstanceMap;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.yangtools.yang.binding.RpcService;
25
26 public class BindingContextUtils {
27
28     public static ConsumerContext createConsumerContext(final BindingAwareConsumer consumer,
29             final 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(final BindingAwareProvider provider,
36             final 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(final BindingAwareConsumer consumer,
43             final ClassToInstanceMap<BindingAwareService> serviceProvider) {
44         final ConsumerContext context = createConsumerContext(consumer, serviceProvider);
45         consumer.onSessionInitialized(context);
46         return context;
47     }
48
49     public static ProviderContext createProviderContextAndInitialize(final BindingAwareProvider provider,
50             final ClassToInstanceMap<BindingAwareService> serviceProvider) {
51         final ProviderContext context = createProviderContext(provider, serviceProvider);
52         provider.onSessionInitiated(context);
53         return context;
54     }
55
56     public static <T extends BindingAwareService> T createContextProxyOrReturnService(final Class<T> service, final 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(final ClassToInstanceMap<BindingAwareService> serviceProvider) {
67             this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
68             this.serviceProvider = serviceProvider;
69         }
70
71         @Override
72         public final <T extends RpcService> T getRpcService(final Class<T> module) {
73             return getSALService(RpcConsumerRegistry.class).getRpcService(module);
74         }
75
76         @Override
77         public final <T extends BindingAwareService> T getSALService(final Class<T> service) {
78             checkNotNull(service,"Service class should not be null.");
79             final 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(final 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(final ClassToInstanceMap<BindingAwareService> serviceProvider) {
110             super(serviceProvider);
111         }
112
113         @Override
114         public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
115                 final T implementation) throws IllegalStateException {
116             return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
117         }
118
119         @Override
120         public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
121                 throws IllegalStateException {
122             return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);
123         }
124     }
125 }