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 java.util.Objects.requireNonNull;
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;
30 public final class BindingContextUtils {
31 private BindingContextUtils() {
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"));
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"));
46 public static ConsumerContext createConsumerContextAndInitialize(final BindingAwareConsumer consumer,
47 final ClassToInstanceMap<BindingAwareService> serviceProvider) {
48 ConsumerContext context = createConsumerContext(consumer, serviceProvider);
49 consumer.onSessionInitialized(context);
53 public static ProviderContext createProviderContextAndInitialize(final BindingAwareProvider provider,
54 final ClassToInstanceMap<BindingAwareService> serviceProvider) {
55 ProviderContext context = createProviderContext(provider, serviceProvider);
56 provider.onSessionInitiated(context);
60 public static <T extends BindingAwareService> T createContextProxyOrReturnService(final Class<T> service,
62 // FIXME: Create Proxy
66 private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
68 private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
69 private ClassToInstanceMap<BindingAwareService> serviceProvider;
71 SingleConsumerContextImpl(final ClassToInstanceMap<BindingAwareService> serviceProvider) {
72 this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
73 this.serviceProvider = serviceProvider;
77 public final <T extends RpcService> T getRpcService(final Class<T> module) {
78 return getSALService(RpcConsumerRegistry.class).getRpcService(module);
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) {
88 return tryToRetrieveSalService(service);
91 private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(final Class<T> service) {
92 final T potential = alreadyRetrievedServices.getInstance(service);
93 if (potential != null) {
96 final T requested = serviceProvider.getInstance(service);
97 if (requested == null) {
98 throw new IllegalArgumentException("Requested service " + service.getName() + " is not available.");
100 final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
101 alreadyRetrievedServices.put(service, retrieved);
106 public final void close() {
107 alreadyRetrievedServices = null;
108 serviceProvider = null;
112 private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
113 SingleProviderContextImpl(final ClassToInstanceMap<BindingAwareService> serviceProvider) {
114 super(serviceProvider);
118 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
119 registerRouteChangeListener(final L listener) {
120 return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
124 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
125 final T implementation) {
126 return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
130 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type,
131 final T implementation) {
132 return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);