Bump upstreams for Silicon
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / RpcUtil.java
1 /*
2  * Copyright (c) 2017 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.blueprint.ext;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.function.Predicate;
13 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
14 import org.opendaylight.mdsal.dom.spi.RpcRoutingStrategy;
15 import org.opendaylight.yangtools.yang.binding.RpcService;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Utility methods for dealing with various aspects of RPCs and actions.
26  *
27  * @author Robert Varga
28  */
29 final class RpcUtil {
30     private static final Logger LOG = LoggerFactory.getLogger(RpcUtil.class);
31
32     private RpcUtil() {
33         throw new UnsupportedOperationException();
34     }
35
36     static Collection<QName> decomposeRpcService(final Class<RpcService> service,
37             final SchemaContext schemaContext, final Predicate<RpcRoutingStrategy> filter) {
38         final QNameModule moduleName = BindingReflections.getQNameModule(service);
39         final Module module = schemaContext.findModule(moduleName).orElseThrow(() -> new IllegalArgumentException(
40                 "Module not found in SchemaContext: " + moduleName + "; service: " + service));
41         LOG.debug("Resolved service {} to module {}", service, module);
42
43         final Collection<? extends RpcDefinition> rpcs = module.getRpcs();
44         final Collection<QName> ret = new ArrayList<>(rpcs.size());
45         for (RpcDefinition rpc : rpcs) {
46             final RpcRoutingStrategy strategy = RpcRoutingStrategy.from(rpc);
47             if (filter.test(strategy)) {
48                 ret.add(rpc.getQName());
49             }
50         }
51
52         return ret;
53     }
54 }