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 java.awt.image.SinglePixelPackedSampleModel;
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.BindingAwareProvider;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider.ProviderFunctionality;
19 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
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.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.RpcService;
28 import static com.google.common.base.Preconditions.*;
30 import com.google.common.collect.ClassToInstanceMap;
31 import com.google.common.collect.MutableClassToInstanceMap;
33 public class BindingContextUtils {
35 public static ConsumerContext createConsumerContext(BindingAwareConsumer consumer,
36 ClassToInstanceMap<BindingAwareService> serviceProvider) {
37 checkNotNull(consumer,"Consumer should not be null");
38 checkNotNull(serviceProvider,"Service map should not be null");
39 return new SingleConsumerContextImpl(serviceProvider);
42 public static ProviderContext createProviderContext(BindingAwareProvider provider,
43 ClassToInstanceMap<BindingAwareService> serviceProvider) {
44 checkNotNull(provider,"Provider should not be null");
45 checkNotNull(serviceProvider,"Service map should not be null");
46 return new SingleProviderContextImpl(serviceProvider);
49 public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer,
50 ClassToInstanceMap<BindingAwareService> serviceProvider) {
51 ConsumerContext context = createConsumerContext(consumer, serviceProvider);
52 consumer.onSessionInitialized(context);
56 public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider,
57 ClassToInstanceMap<BindingAwareService> serviceProvider) {
58 ProviderContext context = createProviderContext(provider, serviceProvider);
59 provider.onSessionInitiated(context);
63 public static <T extends BindingAwareService> T createContextProxyOrReturnService(Class<T> service, T instance) {
64 // FIXME: Create Proxy
68 private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
70 private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
71 private ClassToInstanceMap<BindingAwareService> serviceProvider;
73 public SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
74 this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
75 this.serviceProvider = serviceProvider;
79 public final <T extends RpcService> T getRpcService(Class<T> module) {
80 return getSALService(RpcConsumerRegistry.class).getRpcService(module);
84 public final <T extends BindingAwareService> T getSALService(Class<T> service) {
85 checkNotNull(service,"Service class should not be null.");
86 T potential = alreadyRetrievedServices.getInstance(service);
87 if(potential != null) {
90 return tryToRetrieveSalService(service);
93 private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(Class<T> service) {
94 final T potential = alreadyRetrievedServices.getInstance(service);
95 if(potential != null) {
98 final T requested = serviceProvider.getInstance(service);
99 if(requested == null) {
100 throw new IllegalArgumentException("Requested service "+service.getName() +" is not available.");
102 final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
103 alreadyRetrievedServices.put(service, retrieved);
108 public final void close() throws Exception {
109 alreadyRetrievedServices = null;
110 serviceProvider = null;
114 private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
116 public SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
117 super(serviceProvider);
121 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
123 return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
127 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
128 T implementation) throws IllegalStateException {
129 return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
133 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
134 throws IllegalStateException {
135 return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);
140 public void registerFunctionality(ProviderFunctionality functionality) {
146 public void unregisterFunctionality(ProviderFunctionality functionality) {