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