Scripted update of if statements
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / SchemaNodeUtils.java
1 /*
2  * Copyright (c) 2014 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.model.util;
9
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;
19
20 public class SchemaNodeUtils {
21
22     private SchemaNodeUtils() {
23         throw new UnsupportedOperationException("Utility class");
24     }
25
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());
30             return ret;
31         }
32         return Optional.absent();
33     }
34
35     public static SchemaNode getRootOriginalIfPossible(final SchemaNode data) {
36         Optional<SchemaNode> previous = Optional.absent();
37         Optional<SchemaNode> next = getOriginalIfPossible(data);
38         while (next.isPresent()) {
39             previous = next;
40             next = getOriginalIfPossible(next.get());
41         }
42         return previous.orNull();
43     }
44
45     /**
46      * Returns RPC input or output schema based on supplied QName
47      *
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.
51      */
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()) {
56            case "input":
57                return rpc.getInput();
58            case "output":
59                return rpc.getOutput();
60            default:
61                throw new IllegalArgumentException("Supplied qname " + qname + " does not represent rpc input or output.");
62            }
63        }
64 }