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;
11 import java.util.Iterator;
12 import java.util.List;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
21 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
26 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.api.Status;
31 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.UsesNode;
35 import static com.google.common.base.Preconditions.*;
37 import com.google.common.base.Function;
38 import com.google.common.collect.FluentIterable;
40 public class YangSchemaUtils {
42 private static final Function<PathArgument, QName> QNAME_FROM_PATH_ARGUMENT = new Function<PathArgument, QName>(){
45 public QName apply(PathArgument input) {
49 return input.getNodeType();
53 private YangSchemaUtils() {
54 throw new UnsupportedOperationException("Utility class.");
58 public static DataSchemaNode getSchemaNode(SchemaContext schema,InstanceIdentifier path) {
59 checkArgument(schema != null,"YANG Schema must not be null.");
60 checkArgument(path != null,"Path must not be null.");
61 return getSchemaNode(schema, FluentIterable.from(path.getPath()).transform(QNAME_FROM_PATH_ARGUMENT));
64 public static DataSchemaNode getSchemaNode(SchemaContext schema,Iterable<QName> path) {
65 checkArgument(schema != null,"YANG Schema must not be null.");
66 checkArgument(path != null,"Path must not be null.");
67 if(!path.iterator().hasNext()){
68 return toRootDataNode(schema);
71 QName firstNode = path.iterator().next();
72 DataNodeContainer previous = schema.findModuleByNamespaceAndRevision(firstNode.getNamespace(),
73 firstNode.getRevision());
74 Iterator<QName> iterator = path.iterator();
76 while (iterator.hasNext()) {
77 checkArgument(previous!= null, "Supplied path does not resolve into valid schema node.");
78 QName arg = iterator.next();
79 DataSchemaNode currentNode = previous.getDataChildByName(arg);
80 if (currentNode == null && previous instanceof DataNodeContainer) {
81 currentNode = searchInChoices(previous, arg);
83 if (currentNode instanceof DataNodeContainer) {
84 previous = (DataNodeContainer) currentNode;
85 } else if (currentNode instanceof LeafSchemaNode || currentNode instanceof LeafListSchemaNode) {
86 checkArgument(!iterator.hasNext(), "Path nests inside leaf node, which is not allowed.");
89 checkState(currentNode != null, "Current node should not be null for %s",path);
91 checkState(previous instanceof DataSchemaNode, "Schema node for %s should be instance of DataSchemaNode. Found %s",path,previous);
92 return (DataSchemaNode) previous;
95 private static DataSchemaNode searchInChoices(DataNodeContainer node, QName arg) {
96 Set<DataSchemaNode> children = node.getChildNodes();
97 for (DataSchemaNode child : children) {
98 if (child instanceof ChoiceNode) {
99 ChoiceNode choiceNode = (ChoiceNode) child;
100 DataSchemaNode potential = searchInCases(choiceNode, arg);
101 if (potential != null) {
109 private static DataSchemaNode searchInCases(ChoiceNode choiceNode, QName arg) {
110 Set<ChoiceCaseNode> cases = choiceNode.getCases();
111 for (ChoiceCaseNode caseNode : cases) {
112 DataSchemaNode node = caseNode.getDataChildByName(arg);
120 private static ContainerSchemaNode toRootDataNode(SchemaContext schema) {
121 return new NetconfDataRootNode(schema);
124 private static final class NetconfDataRootNode implements ContainerSchemaNode {
126 public NetconfDataRootNode(SchemaContext schema) {
127 // TODO Auto-generated constructor stub
131 public Set<TypeDefinition<?>> getTypeDefinitions() {
132 // TODO Auto-generated method stub
137 public Set<DataSchemaNode> getChildNodes() {
138 // TODO Auto-generated method stub
143 public Set<GroupingDefinition> getGroupings() {
144 // TODO Auto-generated method stub
149 public DataSchemaNode getDataChildByName(QName name) {
150 // TODO Auto-generated method stub
155 public DataSchemaNode getDataChildByName(String name) {
156 // TODO Auto-generated method stub
161 public Set<UsesNode> getUses() {
162 // TODO Auto-generated method stub
167 public Set<AugmentationSchema> getAvailableAugmentations() {
168 // TODO Auto-generated method stub
173 public boolean isAugmenting() {
174 // TODO Auto-generated method stub
179 public boolean isAddedByUses() {
180 // TODO Auto-generated method stub
185 public boolean isConfiguration() {
186 // TODO Auto-generated method stub
191 public ConstraintDefinition getConstraints() {
192 // TODO Auto-generated method stub
197 public QName getQName() {
198 // TODO Auto-generated method stub
203 public SchemaPath getPath() {
204 // TODO Auto-generated method stub
209 public String getDescription() {
210 // TODO Auto-generated method stub
215 public String getReference() {
216 // TODO Auto-generated method stub
221 public Status getStatus() {
222 // TODO Auto-generated method stub
227 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
228 // TODO Auto-generated method stub
233 public boolean isPresenceContainer() {
234 // TODO Auto-generated method stub