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