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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
20 public class SchemaNodeUtils {
22 private SchemaNodeUtils() {
23 throw new UnsupportedOperationException("Utility class");
26 public static final 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.absent();
35 public static final SchemaNode getRootOriginalIfPossible(final SchemaNode data) {
36 Optional<SchemaNode> previous = Optional.absent();
37 Optional<SchemaNode> next = getOriginalIfPossible(data);
38 while(next.isPresent()) {
40 next = getOriginalIfPossible(next.get());
42 return previous.orNull();
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 @Nullable public static ContainerSchemaNode getRpcDataSchema(@Nonnull final RpcDefinition rpc, @Nonnull final QName qname) {
53 Preconditions.checkNotNull(rpc, "Rpc Schema must not be null");
54 Preconditions.checkNotNull(qname,"QName must not be null");
55 switch (qname.getLocalName()) {
57 return rpc.getInput();
59 return rpc.getOutput();
61 throw new IllegalArgumentException("Supplied qname " + qname + " does not represent rpc input or output.");