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

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