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.controller.md.sal.common.api.routing.RouteChangeListener;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
20 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
21 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
22 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.RpcService;
27 import com.google.common.collect.ClassToInstanceMap;
28 import com.google.common.collect.MutableClassToInstanceMap;
30 public class BindingContextUtils {
32 public static ConsumerContext createConsumerContext(BindingAwareConsumer consumer,
33 ClassToInstanceMap<BindingAwareService> serviceProvider) {
34 checkNotNull(consumer,"Consumer should not be null");
35 checkNotNull(serviceProvider,"Service map should not be null");
36 return new SingleConsumerContextImpl(serviceProvider);
39 public static ProviderContext createProviderContext(BindingAwareProvider provider,
40 ClassToInstanceMap<BindingAwareService> serviceProvider) {
41 checkNotNull(provider,"Provider should not be null");
42 checkNotNull(serviceProvider,"Service map should not be null");
43 return new SingleProviderContextImpl(serviceProvider);
46 public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer,
47 ClassToInstanceMap<BindingAwareService> serviceProvider) {
48 ConsumerContext context = createConsumerContext(consumer, serviceProvider);
49 consumer.onSessionInitialized(context);
53 public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider,
54 ClassToInstanceMap<BindingAwareService> serviceProvider) {
55 ProviderContext context = createProviderContext(provider, serviceProvider);
56 provider.onSessionInitiated(context);
60 public static <T extends BindingAwareService> T createContextProxyOrReturnService(Class<T> service, T instance) {
61 // FIXME: Create Proxy
65 private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
67 private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
68 private ClassToInstanceMap<BindingAwareService> serviceProvider;
70 public SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
71 this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
72 this.serviceProvider = serviceProvider;
76 public final <T extends RpcService> T getRpcService(Class<T> module) {
77 return getSALService(RpcConsumerRegistry.class).getRpcService(module);
81 public final <T extends BindingAwareService> T getSALService(Class<T> service) {
82 checkNotNull(service,"Service class should not be null.");
83 T potential = alreadyRetrievedServices.getInstance(service);
84 if(potential != null) {
87 return tryToRetrieveSalService(service);
90 private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(Class<T> service) {
91 final T potential = alreadyRetrievedServices.getInstance(service);
92 if(potential != null) {
95 final T requested = serviceProvider.getInstance(service);
96 if(requested == null) {
97 throw new IllegalArgumentException("Requested service "+service.getName() +" is not available.");
99 final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
100 alreadyRetrievedServices.put(service, retrieved);
105 public final void close() throws Exception {
106 alreadyRetrievedServices = null;
107 serviceProvider = null;
111 private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
113 public SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
114 super(serviceProvider);
118 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
120 return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
124 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
125 T implementation) throws IllegalStateException {
126 return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
130 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
131 throws IllegalStateException {
132 return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);