Cleanup use of Guava library
[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 static java.util.Objects.requireNonNull;
11
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;
20
21 public class SchemaNodeUtils {
22
23     private SchemaNodeUtils() {
24         throw new UnsupportedOperationException("Utility class");
25     }
26
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();
31             return ret;
32         }
33         return Optional.empty();
34     }
35
36     public static SchemaNode getRootOriginalIfPossible(final SchemaNode data) {
37         Optional<SchemaNode> previous = Optional.empty();
38         Optional<SchemaNode> next = getOriginalIfPossible(data);
39         while (next.isPresent()) {
40             previous = next;
41             next = getOriginalIfPossible(next.get());
42         }
43         return previous.orElse(null);
44     }
45
46     /**
47      * Returns RPC input or output schema based on supplied QName.
48      *
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.
52      */
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()) {
58             case "input":
59                 return rpc.getInput();
60             case "output":
61                 return rpc.getOutput();
62             default:
63                 throw new IllegalArgumentException("Supplied qname " + qname
64                         + " does not represent rpc input or output.");
65         }
66     }
67 }