2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.utils.schema.context;
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
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;
24 * Util class for finding {@link DataSchemaNode}.
27 public final class RestconfSchemaUtil {
29 private RestconfSchemaUtil() {
30 throw new UnsupportedOperationException("Util class");
34 * Get {@link DataSchemaNode} from {@link Module} Restconf module by
35 * {@link String} schema node name.
37 * @param restconfModule
39 * @param schemaNodeName
41 * @return {@link DataSchemaNode}
43 public static DataSchemaNode getRestconfSchemaNode(final Module restconfModule, final String schemaNodeName) {
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();
51 return findSchemaNode(restCont, schemaNodeName);
55 * Find specific {@link DataSchemaNode} child in {@link DataNodeContainer}
56 * by {@link String} schema node name.
59 * - restconf container
60 * @param schemaNodeName
62 * @return {@link DataSchemaNode}
64 private static DataSchemaNode findSchemaNode(final DataSchemaNode restCont, final String schemaNodeName) {
65 switch (schemaNodeName) {
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;
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;
95 throw new RestconfDocumentedException("Schema node " + schemaNodeName + " does not exist in module.",
96 ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
101 * Find child of {@link SchemaNode} in {@link Collection} by {@link String}
105 * - child of SchemaNode
108 * @param schemaNodeName
110 * @return {@link SchemaNode}
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)) {
119 throw new RestconfDocumentedException("Schema node " + schemaNodeName + " does not exist in module.",
120 ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);