2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Optional;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
19 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 public class SchemaNodeUtils {
23 private SchemaNodeUtils() {
24 throw new UnsupportedOperationException("Utility class");
27 public static Optional<SchemaNode> getOriginalIfPossible(final SchemaNode node) {
28 if (node instanceof DerivableSchemaNode) {
29 @SuppressWarnings("unchecked")
30 final Optional<SchemaNode> ret = (Optional<SchemaNode>) ((DerivableSchemaNode) node).getOriginal();
33 return Optional.empty();
36 public static SchemaNode getRootOriginalIfPossible(final SchemaNode data) {
37 Optional<SchemaNode> previous = Optional.empty();
38 Optional<SchemaNode> next = getOriginalIfPossible(data);
39 while (next.isPresent()) {
41 next = getOriginalIfPossible(next.get());
43 return previous.orElse(null);
47 * Returns RPC input or output schema based on supplied QName.
49 * @param rpc RPC Definition
50 * @param qname input or output QName with namespace same as RPC
51 * @return input or output schema. Returns null if RPC does not have input/output specified.
53 @Nullable public static ContainerSchemaNode getRpcDataSchema(@Nonnull final RpcDefinition rpc,
54 @Nonnull final QName qname) {
55 requireNonNull(rpc, "Rpc Schema must not be null");
56 requireNonNull(qname, "QName must not be null");
57 switch (qname.getLocalName()) {
59 return rpc.getInput();
61 return rpc.getOutput();
63 throw new IllegalArgumentException("Supplied qname " + qname
64 + " does not represent rpc input or output.");