3dbce2883d0663af48ca27e08df32fc842023423
[mdsal.git] / yang / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / util / LocalNameRpcServiceInvoker.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.yangtools.yang.binding.util;
9
10 import com.google.common.base.Preconditions;
11 import java.lang.reflect.Method;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17
18 final class LocalNameRpcServiceInvoker extends AbstractMappedRpcInvoker<String> {
19     private final QNameModule module;
20
21     private LocalNameRpcServiceInvoker(final QNameModule module, final Map<String, Method> map) {
22         super(map);
23         this.module = Preconditions.checkNotNull(module);
24     }
25
26     static RpcServiceInvoker instanceFor(final QNameModule module, final Map<QName, Method> qnameToMethod) {
27         final Map<String, Method> map = new HashMap<>();
28         for (Entry<QName, Method> e : qnameToMethod.entrySet()) {
29             map.put(e.getKey().getLocalName(), e.getValue());
30         }
31
32         return new LocalNameRpcServiceInvoker(module, map);
33     }
34
35     @Override
36     protected String qnameToKey(final QName qname) {
37         if (module.equals(qname.getModule())) {
38             return qname.getLocalName();
39         } else {
40             return null;
41         }
42     }
43 }