BUG-868: remove InstanceIdentifier.getPath() users
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / util / YangSchemaUtils.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.dom.broker.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.base.Function;
14 import com.google.common.collect.FluentIterable;
15
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
26 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
31 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.api.Status;
36 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.UsesNode;
39
40 public final class YangSchemaUtils {
41
42     private static final Function<PathArgument, QName> QNAME_FROM_PATH_ARGUMENT = new Function<PathArgument, QName>(){
43
44         @Override
45         public QName apply(final PathArgument input) {
46             if(input == null) {
47                 return null;
48             }
49             return input.getNodeType();
50         }
51     };
52
53     private YangSchemaUtils() {
54         throw new UnsupportedOperationException("Utility class.");
55     }
56
57     public static DataSchemaNode getSchemaNode(final SchemaContext schema,final InstanceIdentifier path) {
58         checkArgument(schema != null,"YANG Schema must not be null.");
59         checkArgument(path != null,"Path must not be null.");
60         return getSchemaNode(schema, FluentIterable.from(path.getPathArguments()).transform(QNAME_FROM_PATH_ARGUMENT));
61     }
62
63     public static DataSchemaNode getSchemaNode(final SchemaContext schema,final Iterable<QName> path) {
64         checkArgument(schema != null,"YANG Schema must not be null.");
65         checkArgument(path != null,"Path must not be null.");
66         if(!path.iterator().hasNext()){
67             return toRootDataNode(schema);
68         }
69
70         QName firstNode = path.iterator().next();
71         DataNodeContainer previous = schema.findModuleByNamespaceAndRevision(firstNode.getNamespace(),
72                 firstNode.getRevision());
73         Iterator<QName> iterator = path.iterator();
74
75         while (iterator.hasNext()) {
76             checkArgument(previous!= null, "Supplied path does not resolve into valid schema node.");
77             QName arg = iterator.next();
78             DataSchemaNode currentNode = previous.getDataChildByName(arg);
79             if (currentNode == null && previous instanceof DataNodeContainer) {
80                 currentNode = searchInChoices(previous, arg);
81             }
82             if (currentNode instanceof DataNodeContainer) {
83                 previous = (DataNodeContainer) currentNode;
84             } else if (currentNode instanceof LeafSchemaNode || currentNode instanceof LeafListSchemaNode) {
85                 checkArgument(!iterator.hasNext(), "Path nests inside leaf node, which is not allowed.");
86                 return currentNode;
87             }
88             checkState(currentNode != null, "Current node should not be null for %s",path);
89         }
90         checkState(previous instanceof DataSchemaNode, "Schema node for %s should be instance of DataSchemaNode. Found %s",path,previous);
91         return (DataSchemaNode) previous;
92     }
93
94     private static DataSchemaNode searchInChoices(final DataNodeContainer node, final QName arg) {
95         Set<DataSchemaNode> children = node.getChildNodes();
96         for (DataSchemaNode child : children) {
97             if (child instanceof ChoiceNode) {
98                 ChoiceNode choiceNode = (ChoiceNode) child;
99                 DataSchemaNode potential = searchInCases(choiceNode, arg);
100                 if (potential != null) {
101                     return potential;
102                 }
103             }
104         }
105         return null;
106     }
107
108     private static DataSchemaNode searchInCases(final ChoiceNode choiceNode, final QName arg) {
109         Set<ChoiceCaseNode> cases = choiceNode.getCases();
110         for (ChoiceCaseNode caseNode : cases) {
111             DataSchemaNode node = caseNode.getDataChildByName(arg);
112             if (node != null) {
113                 return node;
114             }
115         }
116         return null;
117     }
118
119     private static ContainerSchemaNode toRootDataNode(final SchemaContext schema) {
120         return new NetconfDataRootNode(schema);
121     }
122
123     private static final class NetconfDataRootNode implements ContainerSchemaNode {
124
125         public NetconfDataRootNode(final SchemaContext schema) {
126             // TODO Auto-generated constructor stub
127         }
128
129         @Override
130         public Set<TypeDefinition<?>> getTypeDefinitions() {
131             // TODO Auto-generated method stub
132             return null;
133         }
134
135         @Override
136         public Set<DataSchemaNode> getChildNodes() {
137             // TODO Auto-generated method stub
138             return null;
139         }
140
141         @Override
142         public Set<GroupingDefinition> getGroupings() {
143             // TODO Auto-generated method stub
144             return null;
145         }
146
147         @Override
148         public DataSchemaNode getDataChildByName(final QName name) {
149             // TODO Auto-generated method stub
150             return null;
151         }
152
153         @Override
154         public DataSchemaNode getDataChildByName(final String name) {
155             // TODO Auto-generated method stub
156             return null;
157         }
158
159         @Override
160         public Set<UsesNode> getUses() {
161             // TODO Auto-generated method stub
162             return null;
163         }
164
165         @Override
166         public Set<AugmentationSchema> getAvailableAugmentations() {
167             // TODO Auto-generated method stub
168             return null;
169         }
170
171         @Override
172         public boolean isAugmenting() {
173             // TODO Auto-generated method stub
174             return false;
175         }
176
177         @Override
178         public boolean isAddedByUses() {
179             // TODO Auto-generated method stub
180             return false;
181         }
182
183         @Override
184         public boolean isConfiguration() {
185             // TODO Auto-generated method stub
186             return false;
187         }
188
189         @Override
190         public ConstraintDefinition getConstraints() {
191             // TODO Auto-generated method stub
192             return null;
193         }
194
195         @Override
196         public QName getQName() {
197             // TODO Auto-generated method stub
198             return null;
199         }
200
201         @Override
202         public SchemaPath getPath() {
203             // TODO Auto-generated method stub
204             return null;
205         }
206
207         @Override
208         public String getDescription() {
209             // TODO Auto-generated method stub
210             return null;
211         }
212
213         @Override
214         public String getReference() {
215             // TODO Auto-generated method stub
216             return null;
217         }
218
219         @Override
220         public Status getStatus() {
221             // TODO Auto-generated method stub
222             return null;
223         }
224
225         @Override
226         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
227             // TODO Auto-generated method stub
228             return null;
229         }
230
231         @Override
232         public boolean isPresenceContainer() {
233             // TODO Auto-generated method stub
234             return false;
235         }
236
237     }
238
239 }