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