Split Restconf implementations (draft02 and RFC) - move
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / util / RestconfSchemaUtil.java
1 /*
2  * Copyright (c) 2016 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.restconf.common.util;
9
10 import java.util.Collection;
11 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
12 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
13 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
16
17 /**
18  * Util class for finding {@link DataSchemaNode}.
19  *
20  */
21 public final class RestconfSchemaUtil {
22
23     private RestconfSchemaUtil() {
24         throw new UnsupportedOperationException("Util class");
25     }
26
27     /**
28      * Find child of {@link SchemaNode} in {@link Collection} by {@link String}
29      * schema node name.
30      *
31      * @param <T>
32      *             child of SchemaNode
33      * @param collection
34      *             child of node
35      * @param schemaNodeName
36      *             schema node name
37      * @return {@link SchemaNode}
38      */
39     public static <T extends SchemaNode> T findSchemaNodeInCollection(final Collection<T> collection,
40             final String schemaNodeName) {
41         for (final T child : collection) {
42             if (child.getQName().getLocalName().equals(schemaNodeName)) {
43                 return child;
44             }
45         }
46         throw new RestconfDocumentedException("Schema node " + schemaNodeName + " does not exist in module.",
47                 ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
48     }
49
50 }