2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.binding.util;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
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;
26 public class BindingContextUtils {
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);
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);
42 public static ConsumerContext createConsumerContextAndInitialize(final BindingAwareConsumer consumer,
43 final ClassToInstanceMap<BindingAwareService> serviceProvider) {
44 final ConsumerContext context = createConsumerContext(consumer, serviceProvider);
45 consumer.onSessionInitialized(context);
49 public static ProviderContext createProviderContextAndInitialize(final BindingAwareProvider provider,
50 final ClassToInstanceMap<BindingAwareService> serviceProvider) {
51 final ProviderContext context = createProviderContext(provider, serviceProvider);
52 provider.onSessionInitiated(context);
56 public static <T extends BindingAwareService> T createContextProxyOrReturnService(final Class<T> service, final T instance) {
57 // FIXME: Create Proxy
61 private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
63 private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
64 private ClassToInstanceMap<BindingAwareService> serviceProvider;
66 public SingleConsumerContextImpl(final ClassToInstanceMap<BindingAwareService> serviceProvider) {
67 this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
68 this.serviceProvider = serviceProvider;
72 public final <T extends RpcService> T getRpcService(final Class<T> module) {
73 return getSALService(RpcConsumerRegistry.class).getRpcService(module);
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) {
83 return tryToRetrieveSalService(service);
86 private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(final Class<T> service) {
87 final T potential = alreadyRetrievedServices.getInstance(service);
88 if(potential != null) {
91 final T requested = serviceProvider.getInstance(service);
92 if(requested == null) {
93 throw new IllegalArgumentException("Requested service "+service.getName() +" is not available.");
95 final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
96 alreadyRetrievedServices.put(service, retrieved);
101 public final void close() throws Exception {
102 alreadyRetrievedServices = null;
103 serviceProvider = null;
107 private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
109 public SingleProviderContextImpl(final ClassToInstanceMap<BindingAwareService> serviceProvider) {
110 super(serviceProvider);
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);
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);