Fix checkstyle violations in sal-binding-util and sal-schema-service
[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 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;
28
29 public final class BindingContextUtils {
30     private BindingContextUtils() {
31     }
32
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);
38     }
39
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);
45     }
46
47     public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer,
48             ClassToInstanceMap<BindingAwareService> serviceProvider) {
49         ConsumerContext context = createConsumerContext(consumer, serviceProvider);
50         consumer.onSessionInitialized(context);
51         return context;
52     }
53
54     public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider,
55             ClassToInstanceMap<BindingAwareService> serviceProvider) {
56         ProviderContext context = createProviderContext(provider, serviceProvider);
57         provider.onSessionInitiated(context);
58         return context;
59     }
60
61     public static <T extends BindingAwareService> T createContextProxyOrReturnService(Class<T> service, T instance) {
62         // FIXME: Create Proxy
63         return instance;
64     }
65
66     private static class SingleConsumerContextImpl implements ConsumerContext, AutoCloseable {
67
68         private ClassToInstanceMap<BindingAwareService> alreadyRetrievedServices;
69         private ClassToInstanceMap<BindingAwareService> serviceProvider;
70
71         SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
72             this.alreadyRetrievedServices = MutableClassToInstanceMap.create();
73             this.serviceProvider = serviceProvider;
74         }
75
76         @Override
77         public final <T extends RpcService> T getRpcService(Class<T> module) {
78             return getSALService(RpcConsumerRegistry.class).getRpcService(module);
79         }
80
81         @Override
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) {
86                 return potential;
87             }
88             return tryToRetrieveSalService(service);
89         }
90
91         private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(Class<T> service) {
92             final T potential = alreadyRetrievedServices.getInstance(service);
93             if (potential != null) {
94                 return potential;
95             }
96             final T requested = serviceProvider.getInstance(service);
97             if (requested == null) {
98                 throw new IllegalArgumentException("Requested service " + service.getName() + " is not available.");
99             }
100             final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested);
101             alreadyRetrievedServices.put(service, retrieved);
102             return retrieved;
103         }
104
105         @Override
106         public final void close() throws Exception {
107             alreadyRetrievedServices = null;
108             serviceProvider = null;
109         }
110     }
111
112     private static class SingleProviderContextImpl extends SingleConsumerContextImpl implements ProviderContext {
113
114         SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) {
115             super(serviceProvider);
116         }
117
118         @Override
119         public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
120                 registerRouteChangeListener(L listener) {
121             return getSALService(RpcProviderRegistry.class).registerRouteChangeListener(listener);
122         }
123
124         @Override
125         public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> type,
126                 T implementation) throws IllegalStateException {
127             return getSALService(RpcProviderRegistry.class).addRoutedRpcImplementation(type, implementation);
128         }
129
130         @Override
131         public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> type, T implementation)
132                 throws IllegalStateException {
133             return getSALService(RpcProviderRegistry.class).addRpcImplementation(type, implementation);
134         }
135     }
136 }

©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.