2 * Copyright (c) 2014 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.controller.sal.dom.broker.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
13 import com.google.common.collect.Lists;
14 import java.util.Iterator;
15 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.model.api.Status;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.UsesNode;
37 public final class YangSchemaUtils {
38 private YangSchemaUtils() {
39 throw new UnsupportedOperationException("Utility class.");
42 public static DataSchemaNode getSchemaNode(final SchemaContext schema, final YangInstanceIdentifier path) {
43 checkArgument(schema != null, "YANG Schema must not be null.");
44 checkArgument(path != null, "Path must not be null.");
45 return getSchemaNode(schema, Lists.transform(path.getPathArguments(), PathArgument::getNodeType));
48 public static DataSchemaNode getSchemaNode(final SchemaContext schema, final Iterable<QName> path) {
49 checkArgument(schema != null, "YANG Schema must not be null.");
50 checkArgument(path != null, "Path must not be null.");
51 if (!path.iterator().hasNext()) {
52 return toRootDataNode(schema);
55 QName firstNode = path.iterator().next();
56 DataNodeContainer previous = schema.findModuleByNamespaceAndRevision(firstNode.getNamespace(),
57 firstNode.getRevision());
58 Iterator<QName> iterator = path.iterator();
60 while (iterator.hasNext()) {
61 checkArgument(previous != null, "Supplied path does not resolve into valid schema node.");
62 QName arg = iterator.next();
63 DataSchemaNode currentNode = previous.getDataChildByName(arg);
64 if (currentNode == null) {
65 currentNode = searchInChoices(previous, arg);
67 if (currentNode instanceof DataNodeContainer) {
68 previous = (DataNodeContainer) currentNode;
69 } else if (currentNode instanceof LeafSchemaNode || currentNode instanceof LeafListSchemaNode) {
70 checkArgument(!iterator.hasNext(), "Path nests inside leaf node, which is not allowed.");
73 checkState(currentNode != null, "Current node should not be null for %s", path);
75 checkState(previous instanceof DataSchemaNode,
76 "Schema node for %s should be instance of DataSchemaNode. Found %s", path, previous);
77 return (DataSchemaNode) previous;
80 private static DataSchemaNode searchInChoices(final DataNodeContainer node, final QName arg) {
81 for (DataSchemaNode child : node.getChildNodes()) {
82 if (child instanceof ChoiceSchemaNode) {
83 ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) child;
84 DataSchemaNode potential = searchInCases(choiceNode, arg);
85 if (potential != null) {
93 private static DataSchemaNode searchInCases(final ChoiceSchemaNode choiceNode, final QName arg) {
94 Set<ChoiceCaseNode> cases = choiceNode.getCases();
95 for (ChoiceCaseNode caseNode : cases) {
96 DataSchemaNode node = caseNode.getDataChildByName(arg);
104 private static ContainerSchemaNode toRootDataNode(final SchemaContext schema) {
105 return new NetconfDataRootNode(schema);
108 private static final class NetconfDataRootNode implements ContainerSchemaNode {
110 NetconfDataRootNode(final SchemaContext schema) {
111 // TODO Auto-generated constructor stub
115 public Set<TypeDefinition<?>> getTypeDefinitions() {
116 // TODO Auto-generated method stub
121 public Set<DataSchemaNode> getChildNodes() {
122 // TODO Auto-generated method stub
127 public Set<GroupingDefinition> getGroupings() {
128 // TODO Auto-generated method stub
133 public DataSchemaNode getDataChildByName(final QName name) {
134 // TODO Auto-generated method stub
139 public Set<UsesNode> getUses() {
140 // TODO Auto-generated method stub
145 public Set<AugmentationSchema> getAvailableAugmentations() {
146 // TODO Auto-generated method stub
151 public boolean isAugmenting() {
152 // TODO Auto-generated method stub
157 public boolean isAddedByUses() {
158 // TODO Auto-generated method stub
163 public boolean isConfiguration() {
164 // TODO Auto-generated method stub
169 public ConstraintDefinition getConstraints() {
170 // TODO Auto-generated method stub
175 public QName getQName() {
176 // TODO Auto-generated method stub
181 public SchemaPath getPath() {
182 // TODO Auto-generated method stub
187 public String getDescription() {
188 // TODO Auto-generated method stub
193 public String getReference() {
194 // TODO Auto-generated method stub
199 public Status getStatus() {
200 // TODO Auto-generated method stub
205 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
206 // TODO Auto-generated method stub
211 public boolean isPresenceContainer() {
212 // TODO Auto-generated method stub