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.BindingAwareProvider.ProviderFunctionality;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
21 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.RpcService;
28 import com.google.common.collect.ClassToInstanceMap;
29 import com.google.common.collect.MutableClassToInstanceMap;
31 public class BindingContextUtils {
33 public static ConsumerContext createConsumerContext(BindingAwareConsumer consumer,
34 ClassToInstanceMap<BindingAwareService> serviceProvider) {
35 checkNotNull(consumer,"Consumer should not be null");
36 checkNotNull(serviceProvider,"Service map should not be null");
37 return new SingleConsumerContextImpl(serviceProvider);
40 public static ProviderContext createProviderContext(BindingAwareProvider provider,
41 ClassToInstanceMap<BindingAwareService> serviceProvider) {
42 checkNotNull(provider,"Provider should not be null");
43 checkNotNull(serviceProvider,"Service map should not be null");
44 return new SingleProviderContextImpl(serviceProvider);
47 public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer,
48 ClassToInstanceMap<BindingAwareService> serviceProvider) {
49 ConsumerContext context = createConsumerContext(consumer, serviceProvider);
50 consumer.onSessionInitialized(context);
54 public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider,
55 ClassToInstanceMap<BindingAwareService> serviceProvider) {
56 ProviderContext context = createProviderContext(provider, serviceProvider);
57 provider.onSessionInitiated(context);
61 public static <T extends BindingAwareService> T createContextProxyOrReturnService(Class<T> service, T instance) {
62 // FIXME: Create Proxy
66 private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
68 private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
69 private ClassToInstanceMap<BindingAwareService> serviceProvider;
71 public SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
72 this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
73 this.serviceProvider = serviceProvider;
77 public final <T extends RpcService> T getRpcService(Class<T> module) {
78 return getSALService(RpcConsumerRegistry.class).getRpcService(module);
82 public final <T extends BindingAwareService> T getSALService(Class<T> service) {
83 checkNotNull(service,"Service class should not be null.");
84 T potential = alreadyRetrievedServices.getInstance(service);
85 if(potential != null) {
88 return tryToRetrieveSalService(service);
91 private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(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() throws Exception {
107 alreadyRetrievedServices = null;
108 serviceProvider = null;
112 private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
114 public SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
115 super(serviceProvider);
119 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
121 return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
125 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
126 T implementation) throws IllegalStateException {
127 return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
131 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
132 throws IllegalStateException {
133 return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);
138 public void registerFunctionality(ProviderFunctionality functionality) {
144 public void unregisterFunctionality(ProviderFunctionality functionality) {