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