2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.mdsal.binding.javav2.generator.impl;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.Iterator;
18 import java.util.List;
21 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
22 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil;
23 import org.opendaylight.mdsal.binding.javav2.generator.util.ReferencedTypeImpl;
24 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
25 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
26 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.opendaylight.yangtools.yang.model.api.UsesNode;
41 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
44 final class AugmentToGenType {
47 * Comparator based on augment target path.
49 private static final Comparator<AugmentationSchema> AUGMENT_COMP = (o1, o2) -> {
50 final Iterator<QName> thisIt = o1.getTargetPath().getPathFromRoot().iterator();
51 final Iterator<QName> otherIt = o2.getTargetPath().getPathFromRoot().iterator();
53 while (thisIt.hasNext()) {
54 if (!otherIt.hasNext()) {
58 final int comp = thisIt.next().compareTo(otherIt.next());
64 return otherIt.hasNext() ? -1 : 0;
67 private AugmentToGenType() {
68 throw new UnsupportedOperationException("Utility class");
72 * Converts all <b>augmentation</b> of the module to the list
73 * <code>Type</code> objects.
76 * module from which is obtained list of all augmentation objects
77 * to iterate over them
78 * @param schemaContext
80 * @param genTypeBuilders
82 * @throws IllegalArgumentException
84 * <li>if the module is null</li>
85 * <li>if the name of module is null</li>
87 * @throws IllegalStateException
88 * if set of augmentations from module is null
90 static Map<Module, ModuleContext> generate(final Module module, final SchemaContext schemaContext,
91 final TypeProvider typeProvider, final Map<Module, ModuleContext> genCtx,
92 Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final boolean verboseClassComments) {
94 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
95 Preconditions.checkArgument(module.getName() != null, "Module name cannot be NULL.");
96 Preconditions.checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
98 final String basePackageName = BindingMapping.getRootPackageName(module);
99 final List<AugmentationSchema> augmentations = resolveAugmentations(module);
100 Map<Module, ModuleContext> resultCtx = genCtx;
101 for (final AugmentationSchema augment : augmentations) {
102 resultCtx = augmentationToGenTypes(basePackageName, augment, module, schemaContext, verboseClassComments,
103 resultCtx, genTypeBuilders, typeProvider);
109 * Returns list of <code>AugmentationSchema</code> objects. The objects are
110 * sorted according to the length of their target path from the shortest to
114 * module from which is obtained list of all augmentation objects
115 * @return list of sorted <code>AugmentationSchema</code> objects obtained
116 * from <code>module</code>
117 * @throws IllegalArgumentException
119 * @throws IllegalStateException
120 * if set of module augmentations is null
122 private static List<AugmentationSchema> resolveAugmentations(final Module module) {
123 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
124 Preconditions.checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL.");
126 final Set<AugmentationSchema> augmentations = module.getAugmentations();
127 final List<AugmentationSchema> sortedAugmentations = new ArrayList<>(augmentations);
128 Collections.sort(sortedAugmentations, AUGMENT_COMP);
130 return sortedAugmentations;
134 * Converts <code>augSchema</code> to list of <code>Type</code> which
135 * contains generated type for augmentation. In addition there are also
136 * generated types for all containers, list and choices which are child of
137 * <code>augSchema</code> node or a generated types for cases are added if
138 * augmented node is choice.
140 * @param augmentPackageName
141 * string with the name of the package to which the augmentation
144 * AugmentationSchema which is contains data about augmentation
145 * (target path, childs...)
148 * @param schemaContext
150 * @param genTypeBuilders
151 * @throws IllegalArgumentException
153 * <li>if <code>augmentPackageName</code> equals null</li>
154 * <li>if <code>augSchema</code> equals null</li>
156 * @throws IllegalStateException
157 * if augment target path is null
160 private static Map<Module, ModuleContext> augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema,
161 final Module module, final SchemaContext schemaContext, final boolean verboseClassComments,
162 Map<Module, ModuleContext> genCtx, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
163 final TypeProvider typeProvider) {
165 Map<Module, ModuleContext> generatedCtx;
166 Preconditions.checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
167 Preconditions.checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
168 Preconditions.checkState(augSchema.getTargetPath() != null,
169 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
171 generatedCtx = GenHelperUtil.processUsesAugments(schemaContext, augSchema, module, genCtx, genTypeBuilders,
172 verboseClassComments, typeProvider);
173 final SchemaPath targetPath = augSchema.getTargetPath();
174 SchemaNode targetSchemaNode;
176 targetSchemaNode = SchemaContextUtil.findDataSchemaNode(schemaContext, targetPath);
177 if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
178 if (targetSchemaNode instanceof DerivableSchemaNode) {
179 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
181 if (targetSchemaNode == null) {
182 throw new IllegalStateException("Failed to find target node from grouping in augmentation " + augSchema
183 + " in module " + module.getName());
186 if (targetSchemaNode == null) {
187 throw new IllegalArgumentException("augment target not found: " + targetPath);
190 GeneratedTypeBuilder targetTypeBuilder = GenHelperUtil.findChildNodeByPath(targetSchemaNode.getPath(),
192 if (targetTypeBuilder == null) {
193 targetTypeBuilder = GenHelperUtil.findCaseByPath(targetSchemaNode.getPath(), generatedCtx);
195 if (targetTypeBuilder == null) {
196 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
199 if (!(targetSchemaNode instanceof ChoiceSchemaNode)) {
200 final String packageName = augmentPackageName;
201 generatedCtx = GenHelperUtil.addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName,
202 targetTypeBuilder.toInstance(),augSchema, genTypeBuilders, generatedCtx, schemaContext,
203 verboseClassComments, typeProvider);
207 generatedCtx = generateTypesFromAugmentedChoiceCases(schemaContext, module, augmentPackageName,
208 targetTypeBuilder.toInstance(), (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(),
209 null, generatedCtx, verboseClassComments, genTypeBuilders, typeProvider);
214 public static Map<Module, ModuleContext> usesAugmentationToGenTypes(final SchemaContext schemaContext, final String
215 augmentPackageName, final AugmentationSchema augSchema, final Module module, final UsesNode usesNode, final DataNodeContainer
216 usesNodeParent, Map<Module, ModuleContext> genCtx,
217 Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
218 final boolean verboseClassComments, final TypeProvider typeProvider) {
220 Map<Module, ModuleContext> generatedCtx;
221 Preconditions.checkArgument(augmentPackageName != null, "Package Name cannot be NULL.");
222 Preconditions.checkArgument(augSchema != null, "Augmentation Schema cannot be NULL.");
223 Preconditions.checkState(augSchema.getTargetPath() != null,
224 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
226 generatedCtx = GenHelperUtil.processUsesAugments(schemaContext, augSchema, module, genCtx, genTypeBuilders,
227 verboseClassComments, typeProvider);
228 final SchemaPath targetPath = augSchema.getTargetPath();
229 final SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(schemaContext, targetPath, usesNode);
230 if (targetSchemaNode == null) {
231 throw new IllegalArgumentException("augment target not found: " + targetPath);
234 GeneratedTypeBuilder targetTypeBuilder = GenHelperUtil.findChildNodeByPath(targetSchemaNode.getPath(),
236 if (targetTypeBuilder == null) {
237 targetTypeBuilder = GenHelperUtil.findCaseByPath(targetSchemaNode.getPath(), generatedCtx);
239 if (targetTypeBuilder == null) {
240 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
243 if (!(targetSchemaNode instanceof ChoiceSchemaNode)) {
244 String packageName = augmentPackageName;
245 if (usesNodeParent instanceof SchemaNode) {
246 packageName = BindingGeneratorUtil.packageNameForAugmentedGeneratedType(augmentPackageName,
247 ((SchemaNode) usesNodeParent).getPath());
249 generatedCtx = GenHelperUtil.addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName,
250 targetTypeBuilder.toInstance(), augSchema, genTypeBuilders, generatedCtx, schemaContext,
251 verboseClassComments, typeProvider);
254 generatedCtx = generateTypesFromAugmentedChoiceCases(schemaContext, module, augmentPackageName,
255 targetTypeBuilder.toInstance(), (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(),
256 usesNodeParent, generatedCtx, verboseClassComments, genTypeBuilders, typeProvider);
262 * Convenient method to find node added by uses statement.
263 * @param schemaContext
266 * @param parentUsesNode
267 * parent of uses node
268 * @return node from its original location in grouping
270 private static DataSchemaNode findOriginalTargetFromGrouping(final SchemaContext schemaContext, final SchemaPath targetPath,
271 final UsesNode parentUsesNode) {
272 SchemaNode targetGrouping = null;
273 QName current = parentUsesNode.getGroupingPath().getPathFromRoot().iterator().next();
274 Module module = schemaContext.findModuleByNamespaceAndRevision(current.getNamespace(), current.getRevision());
275 if (module == null) {
276 throw new IllegalArgumentException("Fialed to find module for grouping in: " + parentUsesNode);
278 for (GroupingDefinition group : module.getGroupings()) {
279 if (group.getQName().equals(current)) {
280 targetGrouping = group;
286 if (targetGrouping == null) {
287 throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
290 final GroupingDefinition grouping = (GroupingDefinition) targetGrouping;
291 SchemaNode result = grouping;
292 for (final QName node : targetPath.getPathFromRoot()) {
293 if (result instanceof DataNodeContainer) {
294 final QName resultNode = QName.create(result.getQName().getModule(), node.getLocalName());
295 result = ((DataNodeContainer) result).getDataChildByName(resultNode);
296 } else if (result instanceof ChoiceSchemaNode) {
297 result = ((ChoiceSchemaNode) result).getCaseNodeByName(node.getLocalName());
300 if (result == null) {
304 if (result instanceof DerivableSchemaNode) {
305 DerivableSchemaNode castedResult = (DerivableSchemaNode) result;
306 Optional<? extends SchemaNode> originalNode = castedResult
308 if (castedResult.isAddedByUses() && originalNode.isPresent()) {
309 result = originalNode.get();
313 if (result instanceof DataSchemaNode) {
314 DataSchemaNode resultDataSchemaNode = (DataSchemaNode) result;
315 if (resultDataSchemaNode.isAddedByUses()) {
316 // The original node is required, but we have only the copy of
317 // the original node.
318 // Maybe this indicates a bug in Yang parser.
319 throw new IllegalStateException(
320 "Failed to generate code for augment in "
323 return resultDataSchemaNode;
326 throw new IllegalStateException(
327 "Target node of uses-augment statement must be DataSchemaNode. Failed to generate code for augment in "
333 * Generates list of generated types for all the cases of a choice which are
334 * added to the choice through the augment.
336 * @param schemaContext
339 * @param basePackageName
340 * string contains name of package to which augment belongs. If
341 * an augmented choice is from an other package (pcg1) than an
342 * augmenting choice (pcg2) then case's of the augmenting choice
343 * will belong to pcg2.
345 * Type which represents target choice
347 * node which represents target choice
348 * @param augmentedNodes
349 * set of choice case nodes for which is checked if are/aren't
350 * added to choice through augmentation
351 * @return list of generated types which represents augmented cases of
352 * choice <code>refChoiceType</code>
353 * @throws IllegalArgumentException
355 * <li>if <code>basePackageName</code> is null</li>
356 * <li>if <code>targetType</code> is null</li>
357 * <li>if <code>augmentedNodes</code> is null</li>
360 private static Map<Module, ModuleContext> generateTypesFromAugmentedChoiceCases(final SchemaContext schemaContext, final Module module,
361 final String basePackageName, final Type targetType, final ChoiceSchemaNode targetNode,
362 final Iterable<DataSchemaNode> augmentedNodes, final DataNodeContainer usesNodeParent,
363 Map<Module, ModuleContext> genCtx, final boolean verboseClassComments,
364 Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
365 Preconditions.checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
366 Preconditions.checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
367 Preconditions.checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
369 for (final DataSchemaNode caseNode : augmentedNodes) {
370 if (caseNode != null) {
371 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
372 caseNode.getPath(), null);
373 final GeneratedTypeBuilder caseTypeBuilder = GenHelperUtil.addDefaultInterfaceDefinition(packageName,
374 caseNode, module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
375 caseTypeBuilder.addImplementsType(targetType);
378 final SchemaPath nodeSp = targetNode.getPath();
379 parent = SchemaContextUtil.findDataSchemaNode(schemaContext, nodeSp.getParent());
381 GeneratedTypeBuilder childOfType = null;
382 if (parent instanceof Module) {
383 childOfType = genCtx.get(parent).getModuleNode();
384 } else if (parent instanceof ChoiceCaseNode) {
385 childOfType = GenHelperUtil.findCaseByPath(parent.getPath(), genCtx);
386 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
387 childOfType = GenHelperUtil.findChildNodeByPath(parent.getPath(), genCtx);
388 } else if (parent instanceof GroupingDefinition) {
389 childOfType = GenHelperUtil.findGroupingByPath(parent.getPath(), genCtx);
392 if (childOfType == null) {
393 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
396 ChoiceCaseNode node = null;
397 final String caseLocalName = caseNode.getQName().getLocalName();
398 if (caseNode instanceof ChoiceCaseNode) {
399 node = (ChoiceCaseNode) caseNode;
400 } else if (targetNode.getCaseNodeByName(caseLocalName) == null) {
401 final String targetNodeLocalName = targetNode.getQName().getLocalName();
402 for (DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) {
403 if (dataSchemaNode instanceof ChoiceSchemaNode && targetNodeLocalName.equals(dataSchemaNode.getQName
404 ().getLocalName())) {
405 node = ((ChoiceSchemaNode) dataSchemaNode).getCaseNodeByName(caseLocalName);
410 node = targetNode.getCaseNodeByName(caseLocalName);
412 final Iterable<DataSchemaNode> childNodes = node.getChildNodes();
413 if (childNodes != null) {
414 GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType,
415 childNodes, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
417 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
418 genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);