Move createProxy()
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMRpcServiceAdapter.java
1 /*
2  * Copyright (c) 2015 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.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.collect.ClassToInstanceMap;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.Set;
18 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMService;
21 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
22 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
23 import org.opendaylight.yangtools.yang.binding.RpcService;
24
25 @Deprecated
26 public class BindingDOMRpcServiceAdapter implements RpcConsumerRegistry {
27
28     protected static final Factory<RpcConsumerRegistry> BUILDER_FACTORY = Builder::new;
29
30     private final LoadingCache<Class<? extends RpcService>, RpcServiceAdapter> proxies = CacheBuilder.newBuilder()
31             .weakKeys()
32             .build(new CacheLoader<Class<? extends RpcService>, RpcServiceAdapter>() {
33
34                 @Override
35                 public RpcServiceAdapter load(final Class<? extends RpcService> key) {
36                     checkArgument(BindingReflections.isBindingClass(key));
37                     checkArgument(key.isInterface(), "Supplied RPC service type must be interface.");
38                     return new RpcServiceAdapter(key, codec, domService);
39                 }
40
41             });
42
43     private final DOMRpcService domService;
44     private final BindingToNormalizedNodeCodec codec;
45
46     public BindingDOMRpcServiceAdapter(final DOMRpcService domService, final BindingToNormalizedNodeCodec codec) {
47         this.domService = domService;
48         this.codec = codec;
49     }
50
51     @SuppressWarnings("unchecked")
52     @Override
53     public <T extends RpcService> T getRpcService(final Class<T> rpcService) {
54         checkArgument(rpcService != null, "Rpc Service needs to be specied.");
55         return (T) proxies.getUnchecked(rpcService).getProxy();
56     }
57
58     private static final class Builder extends BindingDOMAdapterBuilder<RpcConsumerRegistry> {
59
60         @Override
61         protected RpcConsumerRegistry createInstance(final BindingToNormalizedNodeCodec codec,
62                 final ClassToInstanceMap<DOMService> delegates) {
63             final DOMRpcService domRpc  = delegates.getInstance(DOMRpcService.class);
64             return new BindingDOMRpcServiceAdapter(domRpc  , codec);
65         }
66
67         @Override
68         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
69             return ImmutableSet.of(DOMRpcService.class);
70         }
71     }
72 }

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