Bug 6664 - upgrade draft15 to draft16 - renaming
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / utils / schema / context / 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.utils.schema.context;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Set;
13 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
14 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
15 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
16 import org.opendaylight.restconf.Draft16;
17 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
22
23 /**
24  * Util class for finding {@link DataSchemaNode}.
25  *
26  */
27 public final class RestconfSchemaUtil {
28
29     private RestconfSchemaUtil() {
30         throw new UnsupportedOperationException("Util class");
31     }
32
33     /**
34      * Get {@link DataSchemaNode} from {@link Module} Restconf module by
35      * {@link String} schema node name.
36      *
37      * @param restconfModule
38      *            - restconf module
39      * @param schemaNodeName
40      *            - schema node name
41      * @return {@link DataSchemaNode}
42      */
43     public static DataSchemaNode getRestconfSchemaNode(final Module restconfModule, final String schemaNodeName) {
44
45         final Set<GroupingDefinition> groupings = restconfModule.getGroupings();
46         final GroupingDefinition restGroup = findSchemaNodeInCollection(groupings,
47                 Draft16.RestconfModule.RESTCONF_GROUPING_SCHEMA_NODE);
48         final Collection<DataSchemaNode> childNodes = restGroup.getChildNodes();
49         final DataSchemaNode restCont = childNodes.iterator().next();
50
51         return findSchemaNode(restCont, schemaNodeName);
52     }
53
54     /**
55      * Find specific {@link DataSchemaNode} child in {@link DataNodeContainer}
56      * by {@link String} schema node name.
57      *
58      * @param restCont
59      *            - restconf container
60      * @param schemaNodeName
61      *            - schema node name
62      * @return {@link DataSchemaNode}
63      */
64     private static DataSchemaNode findSchemaNode(final DataSchemaNode restCont, final String schemaNodeName) {
65         switch (schemaNodeName) {
66             //MODULES
67             case Draft16.RestconfModule.MODULE_LIST_SCHEMA_NODE:
68                 final DataSchemaNode moduleListSchNode = findSchemaNodeInCollection(
69                         ((DataNodeContainer) findSchemaNode(restCont,
70                                 Draft16.RestconfModule.MODULES_CONTAINER_SCHEMA_NODE)).getChildNodes(),
71                         Draft16.RestconfModule.MODULE_LIST_SCHEMA_NODE);
72                 Preconditions.checkNotNull(moduleListSchNode);
73                 return moduleListSchNode;
74             case Draft16.RestconfModule.MODULES_CONTAINER_SCHEMA_NODE:
75                 final DataSchemaNode modulesContSchNode = findSchemaNodeInCollection(((DataNodeContainer) restCont).getChildNodes(),
76                         Draft16.RestconfModule.MODULES_CONTAINER_SCHEMA_NODE);
77                 Preconditions.checkNotNull(modulesContSchNode);
78                 return modulesContSchNode;
79
80             //STREAMS
81             case Draft16.MonitoringModule.STREAM_LIST_SCHEMA_NODE:
82                 final DataSchemaNode streamListSchNode = findSchemaNodeInCollection(
83                         ((DataNodeContainer) findSchemaNode(restCont,
84                                 Draft16.MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE)).getChildNodes(),
85                         Draft16.MonitoringModule.STREAM_LIST_SCHEMA_NODE);
86                 Preconditions.checkNotNull(streamListSchNode);
87                 return streamListSchNode;
88             case Draft16.MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE:
89                 final DataSchemaNode streamsContSchNode = findSchemaNodeInCollection(
90                         ((DataNodeContainer) restCont).getChildNodes(),
91                         Draft16.MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE);
92                 Preconditions.checkNotNull(streamsContSchNode);
93                 return streamsContSchNode;
94             default:
95                 throw new RestconfDocumentedException("Schema node " + schemaNodeName + " does not exist in module.",
96                         ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
97         }
98     }
99
100     /**
101      * Find child of {@link SchemaNode} in {@link Collection} by {@link String}
102      * schema node name.
103      *
104      * @param <T>
105      *            - child of SchemaNode
106      * @param collection
107      *            - child of node
108      * @param schemaNodeName
109      *            - schema node name
110      * @return {@link SchemaNode}
111      */
112     public static <T extends SchemaNode> T findSchemaNodeInCollection(final Collection<T> collection,
113             final String schemaNodeName) {
114         for (final T child : collection) {
115             if (child.getQName().getLocalName().equals(schemaNodeName)) {
116                 return child;
117             }
118         }
119         throw new RestconfDocumentedException("Schema node " + schemaNodeName + " does not exist in module.",
120                 ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
121     }
122
123 }