Migrate from YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMRpcProviderServiceAdapter.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.collect.ImmutableSet;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.Map;
16 import java.util.Set;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
20 import org.opendaylight.yangtools.concepts.ObjectRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.RpcService;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25
26 @Deprecated
27 public class BindingDOMRpcProviderServiceAdapter {
28
29     private static final Set<YangInstanceIdentifier> GLOBAL = ImmutableSet.of(YangInstanceIdentifier.empty());
30     private final BindingToNormalizedNodeCodec codec;
31     private final DOMRpcProviderService domRpcRegistry;
32
33     public BindingDOMRpcProviderServiceAdapter(final DOMRpcProviderService domRpcRegistry,
34             final BindingToNormalizedNodeCodec codec) {
35         this.codec = codec;
36         this.domRpcRegistry = domRpcRegistry;
37     }
38
39     public <S extends RpcService, T extends S> ObjectRegistration<T> registerRpcImplementation(final Class<S> type,
40             final T implementation) {
41         return register(type, implementation, GLOBAL);
42     }
43
44     public <S extends RpcService, T extends S> ObjectRegistration<T> registerRpcImplementation(final Class<S> type,
45             final T implementation, final Set<InstanceIdentifier<?>> paths) {
46         return register(type, implementation, toYangInstanceIdentifiers(paths));
47     }
48
49     private <S extends RpcService, T extends S> ObjectRegistration<T> register(final Class<S> type,
50             final T implementation, final Collection<YangInstanceIdentifier> rpcContextPaths) {
51         final Map<SchemaPath, Method> rpcs = codec.getRpcMethodToSchemaPath(type).inverse();
52
53         final BindingDOMRpcImplementationAdapter adapter = new BindingDOMRpcImplementationAdapter(
54                 codec.getCodecRegistry(), type, rpcs, implementation);
55         final Set<DOMRpcIdentifier> domRpcs = createDomRpcIdentifiers(rpcs.keySet(), rpcContextPaths);
56         final DOMRpcImplementationRegistration<?> domReg = domRpcRegistry.registerRpcImplementation(adapter, domRpcs);
57         return new BindingRpcAdapterRegistration<>(implementation, domReg);
58     }
59
60     private static Set<DOMRpcIdentifier> createDomRpcIdentifiers(final Set<SchemaPath> rpcs,
61             final Collection<YangInstanceIdentifier> paths) {
62         final Set<DOMRpcIdentifier> ret = new HashSet<>();
63         for (final YangInstanceIdentifier path : paths) {
64             for (final SchemaPath rpc : rpcs) {
65                 ret.add(DOMRpcIdentifier.create(rpc, path));
66             }
67         }
68         return ret;
69     }
70
71     private Collection<YangInstanceIdentifier> toYangInstanceIdentifiers(final Set<InstanceIdentifier<?>> identifiers) {
72         final Collection<YangInstanceIdentifier> ret = new ArrayList<>(identifiers.size());
73         for (final InstanceIdentifier<?> binding : identifiers) {
74             ret.add(codec.toYangInstanceIdentifierCached(binding));
75         }
76         return ret;
77     }
78 }

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