Merge "BUG 2799: Migration of Message Bus from deprecated Helium MD-SAL APIs to Lithi...
[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.util.HashSet;
12 import java.util.Set;
13 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
14 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
16 import org.opendaylight.yangtools.concepts.ObjectRegistration;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.binding.RpcService;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21
22 public class BindingDOMRpcProviderServiceAdapter {
23
24     private static final Set<YangInstanceIdentifier> GLOBAL = ImmutableSet.of(YangInstanceIdentifier.builder().build());
25     private final BindingToNormalizedNodeCodec codec;
26     private final DOMRpcProviderService domRpcRegistry;
27
28     public BindingDOMRpcProviderServiceAdapter(final DOMRpcProviderService domRpcRegistry, final BindingToNormalizedNodeCodec codec) {
29         this.codec = codec;
30         this.domRpcRegistry = domRpcRegistry;
31     }
32
33     public <S extends RpcService, T extends S> ObjectRegistration<T> registerRpcImplementation(final Class<S> type,
34             final T implementation) {
35         return register(type,implementation,createDomRpcIdentifiers(type,GLOBAL));
36     }
37
38     public <S extends RpcService, T extends S> ObjectRegistration<T> registerRpcImplementation(final Class<S> type,
39             final T implementation, final Set<InstanceIdentifier<?>> paths) {
40         return register(type,implementation,createDomRpcIdentifiers(type,toYangInstanceIdentifiers(paths)));
41     }
42
43     private <S extends RpcService, T extends S> ObjectRegistration<T> register(final Class<S> type, final T implementation, final Set<DOMRpcIdentifier> domRpcs) {
44         final BindingRpcImplementationAdapter adapter = new BindingRpcImplementationAdapter(codec.getCodecRegistry(), type, implementation);
45
46
47         final DOMRpcImplementationRegistration<?> domReg = domRpcRegistry.registerRpcImplementation(adapter, domRpcs);
48         return new BindingRpcAdapterRegistration<>(implementation, domReg);
49     }
50
51     private Set<DOMRpcIdentifier> createDomRpcIdentifiers(final Class<? extends RpcService> type, final Set<YangInstanceIdentifier> paths) {
52         final Set<SchemaPath> rpcs = getRpcSchemaPaths(type);
53
54         final Set<DOMRpcIdentifier> ret = new HashSet<>();
55         for(final YangInstanceIdentifier path : paths) {
56             for(final SchemaPath rpc : rpcs) {
57                 ret.add(DOMRpcIdentifier.create(rpc, path));
58             }
59         }
60         return ret;
61     }
62
63     private Set<YangInstanceIdentifier> toYangInstanceIdentifiers(final Set<InstanceIdentifier<?>> identifiers) {
64         final Set<YangInstanceIdentifier> ret = new HashSet<>();
65         for(final InstanceIdentifier<?> binding: identifiers) {
66             ret.add(codec.toNormalized(binding));
67         }
68         return ret;
69     }
70
71     private Set<SchemaPath> getRpcSchemaPaths(final Class<? extends RpcService> type) {
72         return codec.getRpcMethodToSchemaPath(type).values();
73     }
74
75 }