Mass-migrate fields to Immutable collections
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / 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.mdsal.binding.dom.adapter;
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.mdsal.binding.api.RpcProviderService;
18 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
19 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
20 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
21 import org.opendaylight.yangtools.concepts.ObjectRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.binding.RpcService;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26
27 public class BindingDOMRpcProviderServiceAdapter extends AbstractBindingAdapter<DOMRpcProviderService>
28         implements RpcProviderService {
29     private static final ImmutableSet<YangInstanceIdentifier> GLOBAL = ImmutableSet.of(YangInstanceIdentifier.EMPTY);
30
31     public BindingDOMRpcProviderServiceAdapter(final DOMRpcProviderService domRpcRegistry,
32             final BindingToNormalizedNodeCodec codec) {
33         super(codec, domRpcRegistry);
34     }
35
36     @Override
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     @Override
43     public <S extends RpcService, T extends S> ObjectRegistration<T> registerRpcImplementation(final Class<S> type,
44             final T implementation, final Set<InstanceIdentifier<?>> paths) {
45         return register(type, implementation, toYangInstanceIdentifiers(paths));
46     }
47
48     private <S extends RpcService, T extends S> ObjectRegistration<T> register(final Class<S> type,
49             final T implementation, final Collection<YangInstanceIdentifier> rpcContextPaths) {
50         final Map<SchemaPath, Method> rpcs = getCodec().getRpcMethodToSchemaPath(type).inverse();
51
52         final BindingDOMRpcImplementationAdapter adapter = new BindingDOMRpcImplementationAdapter(
53             getCodec().getCodecRegistry(), type, rpcs, implementation);
54         final Set<DOMRpcIdentifier> domRpcs = createDomRpcIdentifiers(rpcs.keySet(), rpcContextPaths);
55         final DOMRpcImplementationRegistration<?> domReg = getDelegate().registerRpcImplementation(adapter, domRpcs);
56         return new BindingRpcAdapterRegistration<>(implementation, domReg);
57     }
58
59     private static Set<DOMRpcIdentifier> createDomRpcIdentifiers(final Set<SchemaPath> rpcs,
60             final Collection<YangInstanceIdentifier> paths) {
61         final Set<DOMRpcIdentifier> ret = new HashSet<>();
62         for (final YangInstanceIdentifier path : paths) {
63             for (final SchemaPath rpc : rpcs) {
64                 ret.add(DOMRpcIdentifier.create(rpc, path));
65             }
66         }
67         return ret;
68     }
69
70     private Collection<YangInstanceIdentifier> toYangInstanceIdentifiers(final Set<InstanceIdentifier<?>> identifiers) {
71         final Collection<YangInstanceIdentifier> ret = new ArrayList<>(identifiers.size());
72         for (final InstanceIdentifier<?> binding : identifiers) {
73             ret.add(getCodec().toYangInstanceIdentifierCached(binding));
74         }
75         return ret;
76     }
77 }