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