7e266c7d49048b87640e33817b98609b9d14df4c
[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.yangtools.yang.common.ErrorTag;
13 import org.opendaylight.yangtools.yang.common.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     private RestconfSchemaUtil() {
23         // Hidden on purpose
24     }
25
26     /**
27      * Find child of {@link SchemaNode} in {@link Collection} by {@link String}
28      * schema node name.
29      *
30      * @param <T>
31      *             child of SchemaNode
32      * @param collection
33      *             child of node
34      * @param schemaNodeName
35      *             schema node name
36      * @return {@link SchemaNode}
37      */
38     public static <T extends SchemaNode> T findSchemaNodeInCollection(final Collection<T> collection,
39             final String schemaNodeName) {
40         for (final T child : collection) {
41             if (child.getQName().getLocalName().equals(schemaNodeName)) {
42                 return child;
43             }
44         }
45         throw new RestconfDocumentedException("Schema node " + schemaNodeName + " does not exist in module.",
46                 ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
47     }
48 }