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 org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.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 final class SchemaNodeUtils {
22 private SchemaNodeUtils() {
23 throw new UnsupportedOperationException("Utility class");
26 public static Optional<SchemaNode> getOriginalIfPossible(final SchemaNode node) {
27 if (node instanceof DerivableSchemaNode) {
28 @SuppressWarnings("unchecked")
29 final Optional<SchemaNode> ret = (Optional<SchemaNode>) ((DerivableSchemaNode) node).getOriginal();
32 return Optional.empty();
35 public static SchemaNode getRootOriginalIfPossible(final SchemaNode data) {
36 Optional<SchemaNode> previous = Optional.empty();
37 Optional<SchemaNode> next = getOriginalIfPossible(data);
38 while (next.isPresent()) {
40 next = getOriginalIfPossible(next.get());
42 return previous.orElse(null);
46 * Returns RPC input or output schema based on supplied QName.
48 * @param rpc RPC Definition
49 * @param qname input or output QName with namespace same as RPC
50 * @return input or output schema. Returns null if RPC does not have input/output specified.
52 public static @Nullable ContainerSchemaNode getRpcDataSchema(final @NonNull RpcDefinition rpc,
53 final @NonNull QName qname) {
54 requireNonNull(rpc, "Rpc Schema must not be null");
55 switch (requireNonNull(qname, "QName must not be null").getLocalName()) {
57 return rpc.getInput();
59 return rpc.getOutput();
61 throw new IllegalArgumentException("Supplied qname " + qname
62 + " does not represent rpc input or output.");