Use 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 public class BindingDOMRpcProviderServiceAdapter {
27
28     private static final Set<YangInstanceIdentifier> GLOBAL = ImmutableSet.of(YangInstanceIdentifier.EMPTY);
29     private final BindingToNormalizedNodeCodec codec;
30     private final DOMRpcProviderService domRpcRegistry;
31
32     public BindingDOMRpcProviderServiceAdapter(final DOMRpcProviderService domRpcRegistry, final BindingToNormalizedNodeCodec codec) {
33         this.codec = codec;
34         this.domRpcRegistry = domRpcRegistry;
35     }
36
37     public <S extends RpcService, T extends S> ObjectRegistration<T> registerRpcImplementation(final Class<S> type,
38             final T implementation) {
39         return register(type, implementation, GLOBAL);
40     }
41
42     public <S extends RpcService, T extends S> ObjectRegistration<T> registerRpcImplementation(final Class<S> type,
43             final T implementation, final Set<InstanceIdentifier<?>> paths) {
44         return register(type, implementation, toYangInstanceIdentifiers(paths));
45     }
46
47     private <S extends RpcService, T extends S> ObjectRegistration<T> register(final Class<S> type, final T implementation, final Collection<YangInstanceIdentifier> rpcContextPaths) {
48         final Map<SchemaPath, Method> rpcs = codec.getRpcMethodToSchemaPath(type).inverse();
49
50         final BindingDOMRpcImplementationAdapter adapter = new BindingDOMRpcImplementationAdapter(codec.getCodecRegistry(), type, rpcs, implementation);
51         final Set<DOMRpcIdentifier> domRpcs = createDomRpcIdentifiers(rpcs.keySet(), rpcContextPaths);
52         final DOMRpcImplementationRegistration<?> domReg = domRpcRegistry.registerRpcImplementation(adapter, domRpcs);
53         return new BindingRpcAdapterRegistration<>(implementation, domReg);
54     }
55
56     private static Set<DOMRpcIdentifier> createDomRpcIdentifiers(final Set<SchemaPath> rpcs, final Collection<YangInstanceIdentifier> paths) {
57         final Set<DOMRpcIdentifier> ret = new HashSet<>();
58         for (final YangInstanceIdentifier path : paths) {
59             for (final SchemaPath rpc : rpcs) {
60                 ret.add(DOMRpcIdentifier.create(rpc, path));
61             }
62         }
63         return ret;
64     }
65
66     private Collection<YangInstanceIdentifier> toYangInstanceIdentifiers(final Set<InstanceIdentifier<?>> identifiers) {
67         final Collection<YangInstanceIdentifier> ret = new ArrayList<>(identifiers.size());
68         for (final InstanceIdentifier<?> binding : identifiers) {
69             ret.add(codec.toYangInstanceIdentifierCached(binding));
70         }
71         return ret;
72     }
73 }

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