1 package org.opendaylight.yangtools.yang.util;
4 import java.util.Iterator;
8 import org.opendaylight.yangtools.yang.common.QName;
9 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
10 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
11 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
12 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
13 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
27 import org.opendaylight.yangtools.yang.model.api.YangNode;
29 import static com.google.common.base.Preconditions.*;
31 import com.google.common.base.Function;
32 import com.google.common.collect.FluentIterable;
34 public class YangSchemaUtils {
36 private static final Function<PathArgument, QName> QNAME_FROM_PATH_ARGUMENT = new Function<PathArgument, QName>(){
39 public QName apply(PathArgument input) {
43 return input.getNodeType();
47 private YangSchemaUtils() {
48 throw new UnsupportedOperationException("Utility class.");
52 public static DataSchemaNode getSchemaNode(SchemaContext schema,InstanceIdentifier path) {
53 checkArgument(schema != null,"YANG Schema must not be null.");
54 checkArgument(path != null,"Path must not be null.");
55 return getSchemaNode(schema, FluentIterable.from(path.getPath()).transform(QNAME_FROM_PATH_ARGUMENT));
58 public static DataSchemaNode getSchemaNode(SchemaContext schema,Iterable<QName> path) {
59 checkArgument(schema != null,"YANG Schema must not be null.");
60 checkArgument(path != null,"Path must not be null.");
61 if(!path.iterator().hasNext()){
62 return toRootDataNode(schema);
65 QName firstNode = path.iterator().next();
66 DataNodeContainer previous = schema.findModuleByNamespaceAndRevision(firstNode.getNamespace(),
67 firstNode.getRevision());
68 Iterator<QName> iterator = path.iterator();
70 while (iterator.hasNext()) {
71 checkArgument(previous!= null, "Supplied path does not resolve into valid schema node.");
72 QName arg = iterator.next();
73 DataSchemaNode currentNode = previous.getDataChildByName(arg);
74 if (currentNode == null && previous instanceof DataNodeContainer) {
75 currentNode = searchInChoices(previous, arg);
77 if (currentNode instanceof DataNodeContainer) {
78 previous = (DataNodeContainer) currentNode;
79 } else if (currentNode instanceof LeafSchemaNode || currentNode instanceof LeafListSchemaNode) {
80 checkArgument(!iterator.hasNext(), "Path nests inside leaf node, which is not allowed.");
84 return (DataSchemaNode) previous;
88 private static DataSchemaNode searchInChoices(DataNodeContainer node, QName arg) {
89 Set<DataSchemaNode> children = node.getChildNodes();
90 for (DataSchemaNode child : children) {
91 if (child instanceof ChoiceNode) {
92 ChoiceNode choiceNode = (ChoiceNode) child;
93 DataSchemaNode potential = searchInCases(choiceNode, arg);
94 if (potential != null) {
102 private static DataSchemaNode searchInCases(ChoiceNode choiceNode, QName arg) {
103 Set<ChoiceCaseNode> cases = choiceNode.getCases();
104 for (ChoiceCaseNode caseNode : cases) {
105 DataSchemaNode node = caseNode.getDataChildByName(arg);
113 private static ContainerSchemaNode toRootDataNode(SchemaContext schema) {
114 return new NetconfDataRootNode(schema);
117 private static final class NetconfDataRootNode implements ContainerSchemaNode {
119 public NetconfDataRootNode(SchemaContext schema) {
120 // TODO Auto-generated constructor stub
124 public YangNode getParent() {
125 // TODO Auto-generated method stub
130 public Set<TypeDefinition<?>> getTypeDefinitions() {
131 // TODO Auto-generated method stub
136 public Set<DataSchemaNode> getChildNodes() {
137 // TODO Auto-generated method stub
142 public Set<GroupingDefinition> getGroupings() {
143 // TODO Auto-generated method stub
148 public DataSchemaNode getDataChildByName(QName name) {
149 // TODO Auto-generated method stub
154 public DataSchemaNode getDataChildByName(String name) {
155 // TODO Auto-generated method stub
160 public Set<UsesNode> getUses() {
161 // TODO Auto-generated method stub
166 public Set<AugmentationSchema> getAvailableAugmentations() {
167 // TODO Auto-generated method stub
172 public boolean isAugmenting() {
173 // TODO Auto-generated method stub
178 public boolean isAddedByUses() {
179 // TODO Auto-generated method stub
184 public boolean isConfiguration() {
185 // TODO Auto-generated method stub
190 public ConstraintDefinition getConstraints() {
191 // TODO Auto-generated method stub
196 public QName getQName() {
197 // TODO Auto-generated method stub
202 public SchemaPath getPath() {
203 // TODO Auto-generated method stub
208 public String getDescription() {
209 // TODO Auto-generated method stub
214 public String getReference() {
215 // TODO Auto-generated method stub
220 public Status getStatus() {
221 // TODO Auto-generated method stub
226 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
227 // TODO Auto-generated method stub
232 public boolean isPresenceContainer() {
233 // TODO Auto-generated method stub