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 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(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);
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);
48 public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer,
49 ClassToInstanceMap<BindingAwareService> serviceProvider) {
50 ConsumerContext context = createConsumerContext(consumer, serviceProvider);
51 consumer.onSessionInitialized(context);
55 public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider,
56 ClassToInstanceMap<BindingAwareService> serviceProvider) {
57 ProviderContext context = createProviderContext(provider, serviceProvider);
58 provider.onSessionInitiated(context);
62 public static <T extends BindingAwareService> T createContextProxyOrReturnService(Class<T> service, T instance) {
63 // FIXME: Create Proxy
67 private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
69 private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
70 private ClassToInstanceMap<BindingAwareService> serviceProvider;
72 SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
73 this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
74 this.serviceProvider = serviceProvider;
78 public final <T extends RpcService> T getRpcService(Class<T> module) {
79 return getSALService(RpcConsumerRegistry.class).getRpcService(module);
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) {
89 return tryToRetrieveSalService(service);
92 private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(Class<T> service) {
93 final T potential = alreadyRetrievedServices.getInstance(service);
94 if (potential != null) {
97 final T requested = serviceProvider.getInstance(service);
98 if (requested == null) {
99 throw new IllegalArgumentException("Requested service " + service.getName() + " is not available.");
101 final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
102 alreadyRetrievedServices.put(service, retrieved);
107 public final void close() {
108 alreadyRetrievedServices = null;
109 serviceProvider = null;
113 private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
115 SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
116 super(serviceProvider);
120 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
121 registerRouteChangeListener(L listener) {
122 return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
126 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
127 T implementation) throws IllegalStateException {
128 return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
132 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
133 throws IllegalStateException {
134 return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);