26aa27eb15e193d51b766a5ef0369da7ae96ae30
[controller.git] / opendaylight / md-sal / sal-binding-util / src / main / java / org / opendaylight / controller / md / sal / binding / util / BindingContextUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.binding.util;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
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;
26
27 import com.google.common.collect.ClassToInstanceMap;
28 import com.google.common.collect.MutableClassToInstanceMap;
29
30 public class BindingContextUtils {
31
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);
37     }
38
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);
44     }
45
46     public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer,
47             ClassToInstanceMap<BindingAwareService> serviceProvider) {
48         ConsumerContext context = createConsumerContext(consumer, serviceProvider);
49         consumer.onSessionInitialized(context);
50         return context;
51     }
52
53     public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider,
54             ClassToInstanceMap<BindingAwareService> serviceProvider) {
55         ProviderContext context = createProviderContext(provider, serviceProvider);
56         provider.onSessionInitiated(context);
57         return context;
58     }
59
60     public static <T extends BindingAwareService> T createContextProxyOrReturnService(Class<T> service, T instance) {
61         // FIXME: Create Proxy
62         return instance;
63     }
64
65     private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
66
67         private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
68         private ClassToInstanceMap<BindingAwareService> serviceProvider;
69
70         public SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
71             this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
72             this.serviceProvider = serviceProvider;
73         }
74
75         @Override
76         public final <T extends RpcService> T getRpcService(Class<T> module) {
77             return getSALService(RpcConsumerRegistry.class).getRpcService(module);
78         }
79
80         @Override
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) {
85                 return potential;
86             }
87             return tryToRetrieveSalService(service);
88         }
89
90         private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(Class<T> service) {
91             final T potential = alreadyRetrievedServices.getInstance(service);
92             if(potential != null) {
93                 return potential;
94             }
95             final T requested = serviceProvider.getInstance(service);
96             if(requested == null) {
97                 throw new IllegalArgumentException("Requested service "+service.getName() +" is not available.");
98             }
99             final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
100             alreadyRetrievedServices.put(service, retrieved);
101             return retrieved;
102         }
103
104         @Override
105         public final void close() throws Exception {
106             alreadyRetrievedServices = null;
107             serviceProvider = null;
108         }
109     }
110
111     private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
112
113         public SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
114             super(serviceProvider);
115         }
116
117         @Override
118         public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
119                 L listener) {
120             return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
121         }
122
123         @Override
124         public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
125                 T implementation) throws IllegalStateException {
126             return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
127         }
128
129         @Override
130         public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
131                 throws IllegalStateException {
132             return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);
133         }
134     }
135 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.