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 static com.google.common.base.Preconditions.checkArgument;
12 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.addTOToTypeBuilder;
13 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.annotateDeprecatedIfNecessary;
14 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.augGenTypeName;
15 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.constructGetter;
16 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.createDescription;
17 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.createReturnTypeForUnion;
18 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.getAugmentIdentifier;
19 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.isInnerType;
20 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.qNameConstant;
21 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.resolveInnerEnumFromTypeDefinition;
22 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.resolveListKeyTOBuilder;
23 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.computeDefaultSUID;
24 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
25 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.packageNameForGeneratedType;
26 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.INSTANTIABLE;
27 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.NOTIFICATION;
28 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.parameterizedTypeFor;
29 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
30 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
32 import com.google.common.annotations.Beta;
33 import com.google.common.base.Preconditions;
34 import com.google.common.collect.Iterables;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.List;
40 import java.util.stream.Collectors;
42 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
43 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil;
44 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes;
45 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
46 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer;
47 import org.opendaylight.mdsal.binding.javav2.generator.util.ReferencedTypeImpl;
48 import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
49 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
50 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
51 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
52 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.GroupingDefinitionDependencySort;
53 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
54 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
55 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject;
56 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
57 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
58 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
59 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
60 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
61 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
62 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
63 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
64 import org.opendaylight.mdsal.binding.javav2.spec.base.BaseIdentity;
65 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
66 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
67 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
68 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
69 import org.opendaylight.yangtools.yang.common.QName;
70 import org.opendaylight.yangtools.yang.model.api.AnyDataSchemaNode;
71 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
72 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
73 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
74 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
75 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
76 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
77 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
78 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
79 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
80 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
81 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
83 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
84 import org.opendaylight.yangtools.yang.model.api.Module;
85 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
86 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
87 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
88 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
89 import org.opendaylight.yangtools.yang.model.api.Status;
90 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
91 import org.opendaylight.yangtools.yang.model.api.UsesNode;
92 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
93 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
94 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
95 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
96 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
99 * Helper util class used for generation of types in Binding spec v2.
102 final class GenHelperUtil {
104 private GenHelperUtil() {
105 throw new UnsupportedOperationException("Util class");
109 * Create GeneratedTypeBuilder object from module argument.
112 * Module object from which builder will be created
114 * @param verboseClassComments
116 * @return <code>GeneratedTypeBuilder</code> which is internal
117 * representation of the module
118 * @throws IllegalArgumentException
121 static GeneratedTypeBuilder moduleToDataType(final Module module, final Map<Module, ModuleContext> genCtx, final boolean verboseClassComments) {
122 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
124 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data", verboseClassComments);
125 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder, genCtx);
126 moduleDataTypeBuilder.addImplementsType(BindingTypes.TREE_ROOT);
127 moduleDataTypeBuilder.addComment(module.getDescription());
128 moduleDataTypeBuilder.setDescription(createDescription(module, verboseClassComments));
129 moduleDataTypeBuilder.setReference(module.getReference());
130 return moduleDataTypeBuilder;
134 * Generates type builder for <code>module</code>.
137 * Module which is source of package name for generated type
140 * string which is added to the module class name representation
142 * @param verboseClassComments
143 * @return instance of GeneratedTypeBuilder which represents
144 * <code>module</code>.
145 * @throws IllegalArgumentException
146 * if <code>module</code> is null
148 static GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix, final boolean
149 verboseClassComments) {
150 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
151 final String packageName = BindingMapping.getRootPackageName(module);
152 // underscore used as separator for distinction of module name parts
153 final String moduleName = new StringBuilder(module.getName()).append('_').append(postfix).toString();
155 final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
156 moduleBuilder.setDescription(createDescription(module, verboseClassComments));
157 moduleBuilder.setReference(module.getReference());
158 moduleBuilder.setModuleName(moduleName);
160 return moduleBuilder;
164 * Adds the implemented types to type builder.
166 * The method passes through the list of <i>uses</i> in
167 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
168 * generated type from all groupings
169 * allGroupings} which is added as <i>implements type</i> to
170 * <code>builder</code>
172 * @param dataNodeContainer
173 * element which contains the list of used YANG groupings
175 * builder to which are added implemented types according to
176 * <code>dataNodeContainer</code>
177 * @param genCtx generated context
178 * @return generated type builder with all implemented types
180 static GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
181 final GeneratedTypeBuilder builder, final Map<Module, ModuleContext> genCtx) {
182 for (final UsesNode usesNode : dataNodeContainer.getUses()) {
183 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath(), genCtx).toInstance();
184 if (genType == null) {
185 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
186 + builder.getName());
188 builder.addImplementsType(genType);
193 static GeneratedTypeBuilder findGroupingByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
194 for (final ModuleContext ctx : genCtx.values()) {
195 final GeneratedTypeBuilder result = ctx.getGrouping(path);
196 if (result != null) {
204 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
205 * node for which <code>typeBuilder</code> was created.
207 * The subnodes aren't mapped to the methods if they are part of grouping or
208 * augment (in this case are already part of them).
212 * @param basePackageName
213 * string contains the module package name
215 * generated type builder which represents any node. The subnodes
216 * of this node are added to the <code>typeBuilder</code> as
217 * methods. The subnode can be of type leaf, leaf-list, list,
222 * set of data schema nodes which are the children of the node
223 * for which <code>typeBuilder</code> was created
224 * @return generated type builder which is the same builder as input
225 * parameter. The getter methods (representing child nodes) could be
228 static GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
229 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf,
230 final Iterable<DataSchemaNode> schemaNodes, final Map<Module, ModuleContext> genCtx,
231 final SchemaContext schemaContext, final boolean verboseClassComments,
232 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
233 final TypeProvider typeProvider) {
235 if (schemaNodes != null && parent != null) {
236 for (final DataSchemaNode schemaNode : schemaNodes) {
237 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
238 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module, genCtx,
239 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
246 static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
247 schemaNode, final Module module, final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext,
248 final boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
249 final TypeProvider typeProvider) {
250 return addDefaultInterfaceDefinition(packageName, schemaNode, null, module, genCtx, schemaContext,
251 verboseClassComments, genTypeBuilders, typeProvider);
254 static Map<Module, ModuleContext> processUsesAugments(final SchemaContext schemaContext, final
255 DataNodeContainer node, final Module module, Map<Module, ModuleContext> genCtx,
256 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
257 final boolean verboseClassComments, final TypeProvider typeProvider) {
258 final String basePackageName = BindingMapping.getRootPackageName(module);
259 for (final UsesNode usesNode : node.getUses()) {
260 Map<SchemaPath, List<AugmentationSchema>> augmentationsGrouped =
261 usesNode.getAugmentations().stream().collect(Collectors.groupingBy(AugmentationSchema::getTargetPath));
262 for (Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry : augmentationsGrouped.entrySet()) {
263 genCtx = AugmentToGenType.usesAugmentationToGenTypes(schemaContext, basePackageName,
264 schemaPathAugmentListEntry.getValue(), module,
265 usesNode, node, genCtx, genTypeBuilders, verboseClassComments, typeProvider);
266 for (AugmentationSchema augSchema : schemaPathAugmentListEntry.getValue()) {
267 genCtx = processUsesAugments(schemaContext, augSchema, module, genCtx, genTypeBuilders,
268 verboseClassComments, typeProvider);
275 static GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
276 for (final ModuleContext ctx : genCtx.values()) {
277 final GeneratedTypeBuilder result = ctx.getChildNode(path);
278 if (result != null) {
285 static GeneratedTypeBuilder findCaseByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
286 for (final ModuleContext ctx : genCtx.values()) {
287 final GeneratedTypeBuilder result = ctx.getCase(path);
288 if (result != null) {
295 static Map<Module, ModuleContext> addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
296 final Type targetTypeRef, final List<AugmentationSchema> schemaPathAugmentListEntry,
297 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final Map<Module, ModuleContext> genCtx,
298 final SchemaContext schemaContext, final boolean verboseClassComments, final TypeProvider typeProvider) {
300 //pick augmentation grouped by augmentation target, there is always at least one
301 final AugmentationSchema augSchema = schemaPathAugmentListEntry.get(0);
303 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.computeIfAbsent(
304 augmentPackageName, k -> new HashMap<>());
306 //this requires valid semantics in YANG model
307 String augIdentifier = null;
308 for (AugmentationSchema aug : schemaPathAugmentListEntry) {
309 augIdentifier = getAugmentIdentifier(aug.getUnknownSchemaNodes());
313 if (augIdentifier == null) {
314 augIdentifier = augGenTypeName(augmentBuilders, targetTypeRef.getName());
317 GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augIdentifier,
320 augTypeBuilder.addImplementsType(BindingTypes.TREE_NODE);
321 augTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, augTypeBuilder));
322 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
323 annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
325 //produces getters for augTypeBuilder eventually
326 for (AugmentationSchema aug : schemaPathAugmentListEntry) {
328 addImplementedInterfaceFromUses(aug, augTypeBuilder, genCtx);
329 augSchemaNodeToMethods(module, BindingMapping.getRootPackageName(module), augTypeBuilder, augTypeBuilder, aug.getChildNodes(),
330 genCtx, schemaContext, verboseClassComments, typeProvider, genTypeBuilders);
333 augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder);
335 if(!augSchema.getChildNodes().isEmpty()) {
336 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
337 genCtx.get(module).addTargetToAugmentation(augTypeBuilder, augSchema.getTargetPath());
339 genCtx.get(module).addAugmentType(augTypeBuilder);
343 //TODO: delete this method eventually when uses-augments & augmented choice cases are implemented
345 * Returns a generated type builder for an augmentation.
347 * The name of the type builder is equal to the name of augmented node with
348 * serial number as suffix.
352 * @param augmentPackageName
353 * string with contains the package name to which the augment
355 * @param basePackageName
356 * string with the package name to which the augmented node
358 * @param targetTypeRef
361 * augmentation schema which contains data about the child nodes
362 * and uses of augment
363 * @return generated type builder for augment in genCtx
366 static Map<Module, ModuleContext> addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
367 final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema,
368 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final Map<Module,
369 ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments, final
370 TypeProvider typeProvider) {
372 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.computeIfAbsent(augmentPackageName, k -> new HashMap<>());
373 String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
375 if (augIdentifier == null) {
376 augIdentifier = augGenTypeName(augmentBuilders, targetTypeRef.getName());
379 GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augIdentifier);
381 augTypeBuilder.addImplementsType(BindingTypes.TREE_NODE);
382 augTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, augTypeBuilder));
383 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
384 annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
385 augTypeBuilder = addImplementedInterfaceFromUses(augSchema, augTypeBuilder, genCtx);
387 augTypeBuilder = augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema
388 .getChildNodes(), genCtx, schemaContext, verboseClassComments, typeProvider, genTypeBuilders);
389 augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder);
391 if(!augSchema.getChildNodes().isEmpty()) {
392 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
395 genCtx.get(module).addAugmentType(augTypeBuilder);
400 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
401 * node for which <code>typeBuilder</code> was created.
405 * @param basePackageName
406 * string contains the module package name
408 * generated type builder which represents any node. The subnodes
409 * of this node are added to the <code>typeBuilder</code> as
410 * methods. The subnode can be of type leaf, leaf-list, list,
415 * set of data schema nodes which are the children of the node
416 * for which <code>typeBuilder</code> was created
417 * @return generated type builder which is the same object as the input
418 * parameter <code>typeBuilder</code>. The getter method could be
421 private static GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
422 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes,
423 final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
424 verboseClassComments, final TypeProvider typeProvider, final Map<String, Map<String,
425 GeneratedTypeBuilder>> genTypeBuilders) {
426 if (schemaNodes != null && typeBuilder != null) {
427 for (final DataSchemaNode schemaNode : schemaNodes) {
428 if (!schemaNode.isAugmenting()) {
429 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module, genCtx,
430 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
438 * Instantiates generated type builder with <code>packageName</code> and
439 * <code>schemaNode</code>.
441 * The new builder always implements
442 * {@link TreeNode TreeNode}.<br>
443 * If <code>schemaNode</code> is instance of GroupingDefinition it also
444 * implements {@link Augmentable
446 * If <code>schemaNode</code> is instance of
447 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
448 * DataNodeContainer} it can also implement nodes which are specified in
452 * string with the name of the package to which
453 * <code>schemaNode</code> belongs.
455 * schema node for which is created generated type builder
457 * parent type (can be null)
458 * @param schemaContext schema context
459 * @return generated type builder <code>schemaNode</code>
461 private static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
462 schemaNode, final Type parent, final Module module, final Map<Module, ModuleContext> genCtx,
463 final SchemaContext schemaContext, final boolean verboseClassComments, final Map<String, Map<String,
464 GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
466 GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, schemaContext, "",
467 verboseClassComments, genTypeBuilders);
468 if (parent == null) {
469 it.addImplementsType(BindingTypes.TREE_NODE);
471 if (parent instanceof ListSchemaNode) {
472 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
473 (BindingTypes.IDENTIFIABLE_ITEM, parent)));
475 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
476 (BindingTypes.ITEM, parent)));
477 it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
481 if (!(schemaNode instanceof GroupingDefinition)) {
482 it.addImplementsType(BindingTypes.augmentable(it));
485 if (schemaNode instanceof DataNodeContainer) {
486 groupingsToGenTypes(module, ((DataNodeContainer) schemaNode).getGroupings(), genCtx, schemaContext,
487 verboseClassComments, genTypeBuilders, typeProvider);
488 it = addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it, genCtx);
494 static GeneratedTypeBuilder resolveNotification(final GeneratedTypeBuilder listenerInterface, String
495 parentName, final String basePackageName, final NotificationDefinition notification, final Module module,
496 final SchemaContext schemaContext, final boolean verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>>
497 genTypeBuilders, TypeProvider typeProvider, Map<Module, ModuleContext> genCtx) {
499 processUsesAugments(schemaContext, notification, module, genCtx, genTypeBuilders,
500 verboseClassComments, typeProvider);
502 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition
503 (basePackageName, notification, null, module, genCtx, schemaContext,
504 verboseClassComments, genTypeBuilders, typeProvider);
505 annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
506 notificationInterface.addImplementsType(NOTIFICATION);
507 genCtx.get(module).addChildNodeType(notification, notificationInterface);
509 // Notification object
510 resolveDataSchemaNodes(module, basePackageName, notificationInterface,
511 notificationInterface, notification.getChildNodes(), genCtx, schemaContext,
512 verboseClassComments, genTypeBuilders, typeProvider);
514 //in case of tied notification, incorporate parent's localName
515 final StringBuilder sb = new StringBuilder("on_");
516 if (parentName != null) {
517 sb.append(parentName).append('_');
519 sb.append(notificationInterface.getName());
521 listenerInterface.addMethod(JavaIdentifierNormalizer.normalizeSpecificIdentifier(sb.toString(), JavaIdentifier.METHOD))
522 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
523 .setComment(encodeAngleBrackets(notification.getDescription())).setReturnType(Types.VOID);
524 return listenerInterface;
528 * Returns reference to generated type builder for specified
529 * <code>schemaNode</code> with <code>packageName</code>.
531 * Firstly the generated type builder is searched in
532 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
533 * found it is created and added to <code>genTypeBuilders</code>.
536 * string with the package name to which returning generated type
539 * schema node which provide data about the schema node name
540 * @param schemaContext schema context
542 * return type name prefix
543 * @return generated type builder for <code>schemaNode</code>
544 * @throws IllegalArgumentException
546 * <li>if <code>schemaNode</code> is null</li>
547 * <li>if <code>packageName</code> is null</li>
548 * <li>if QName of schema node is null</li>
549 * <li>if schemaNode name is null</li>
553 static GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
554 final SchemaContext schemaContext, final String prefix, final boolean verboseClassComments,
555 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
557 Preconditions.checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
558 Preconditions.checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
559 String schemaNodeName = schemaNode.getQName().getLocalName();
560 Preconditions.checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
562 if (prefix != null && !prefix.isEmpty()) {
563 // underscore used as separator for distinction of class name parts
564 schemaNodeName = new StringBuilder(prefix).append('_').append(schemaNodeName).toString();
567 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, schemaNodeName);
568 final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
569 qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
570 newType.addComment(schemaNode.getDescription());
571 newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName(), schemaContext, verboseClassComments));
572 newType.setReference(schemaNode.getReference());
573 newType.setSchemaPath((List<QName>) schemaNode.getPath().getPathFromRoot());
574 newType.setModuleName(module.getName());
576 if (!genTypeBuilders.containsKey(packageName)) {
577 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
578 builders.put(newType.getName(), newType);
579 genTypeBuilders.put(packageName, builders);
581 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
582 if (!builders.containsKey(newType.getName())) {
583 builders.put(newType.getName(), newType);
590 private static void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
591 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module,
592 final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
593 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
595 if (node != null && typeBuilder != null) {
596 if (node instanceof ContainerSchemaNode) {
597 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node,
598 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
599 } else if (node instanceof LeafListSchemaNode) {
600 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) node, module,
601 typeProvider, genCtx);
602 } else if (node instanceof LeafSchemaNode) {
603 resolveLeafSchemaNodeAsMethod("", schemaContext, typeBuilder, genCtx, (LeafSchemaNode) node, module,
605 } else if (node instanceof ListSchemaNode) {
606 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node, schemaContext,
607 verboseClassComments, genCtx, genTypeBuilders, typeProvider);
608 } else if (node instanceof ChoiceSchemaNode) {
609 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, childOf,
610 (ChoiceSchemaNode) node, genTypeBuilders, genCtx, typeProvider);
611 } else if (node instanceof AnyXmlSchemaNode || node instanceof AnyDataSchemaNode) {
612 resolveAnyNodeAsMethod(schemaContext, typeBuilder, genCtx, node, module, typeProvider);
618 * Converts <code>choiceNode</code> to the list of generated types for
619 * choice and its cases.
621 * The package names for choice and for its cases are created as
622 * concatenation of the module package (<code>basePackageName</code>) and
623 * names of all parents node.
627 * @param basePackageName
628 * string with the module package name
632 * choice node which is mapped to generated type. Also child
633 * nodes - cases are mapped to generated types.
634 * @throws IllegalArgumentException
636 * <li>if <code>basePackageName</code> is null</li>
637 * <li>if <code>choiceNode</code> is null</li>
640 private static void choiceToGenType(final Module module, final SchemaContext schemaContext, final boolean
641 verboseClasssComments, final String basePackageName, final GeneratedTypeBuilder parent, final
642 ChoiceSchemaNode choiceNode, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
643 final Map<Module, ModuleContext> genCtx, final TypeProvider typeProvider) {
644 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
645 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
647 if (!choiceNode.isAddedByUses()) {
648 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath(),
649 BindingNamespaceType.Data);
650 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode,
651 schemaContext, "", verboseClasssComments, genTypeBuilders);
652 constructGetter(parent, choiceNode.getQName().getLocalName(),
653 choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus());
654 choiceTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, choiceTypeBuilder));
655 annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
656 genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
657 generateTypesFromChoiceCases(module, schemaContext, genCtx, basePackageName, choiceTypeBuilder.toInstance(),
658 choiceNode, verboseClasssComments, typeProvider, genTypeBuilders);
662 private static void containerToGenType(final Module module, final String basePackageName,
663 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node,
664 final SchemaContext schemaContext, final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
665 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
667 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
668 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
669 if (genType != null) {
670 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType, node.getStatus());
671 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes(), genCtx,
672 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
676 private static void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder
677 parent, final GeneratedTypeBuilder childOf, final ListSchemaNode node, final SchemaContext schemaContext,
678 final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
679 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
681 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
682 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
683 if (genType != null) {
684 final String nodeName = node.getQName().getLocalName();
685 constructGetter(parent, nodeName, node.getDescription(), Types.listTypeFor(genType), node.getStatus());
686 final List<QName> listKeys = node.getKeyDefinition();
687 final String packageName = new StringBuilder(packageNameForGeneratedType(basePackageName, node.getPath(),
688 BindingNamespaceType.Key)).append('.').append(nodeName).toString();
690 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
692 for (final DataSchemaNode schemaNode : node.getChildNodes()) {
693 if (!schemaNode.isAugmenting()) {
694 addSchemaNodeToListBuilders(nodeName, basePackageName, schemaNode, genType, genTOBuilder, listKeys,
695 module, typeProvider, schemaContext, genCtx, genTypeBuilders, verboseClassComments);
700 if (genTOBuilder != null) {
701 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
702 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
703 genTOBuilder.setSUID(prop);
706 typeBuildersToGenTypes(module, genType, genTOBuilder, genCtx);
710 private static void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder,
711 final GeneratedTOBuilder genTOBuilder, final Map<Module, ModuleContext> genCtx) {
712 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
713 if (genTOBuilder != null) {
714 final GeneratedTransferObject genTO = genTOBuilder.toInstance();
715 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO, Status.CURRENT);
716 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
721 * Converts <code>leaf</code> to the getter method which is added to
722 * <code>typeBuilder</code>.
725 * generated type builder to which is added getter method as
726 * <code>leaf</code> mapping
728 * leaf schema node which is mapped as getter method which is
729 * added to <code>typeBuilder</code>
731 * Module in which type was defined
732 * @return boolean value
734 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
736 * <li>true - in other cases</li>
739 private static Type resolveLeafSchemaNodeAsMethod(final String nodeName, final SchemaContext schemaContext,
740 final GeneratedTypeBuilder typeBuilder, final Map<Module, ModuleContext> genCtx, final LeafSchemaNode leaf,
741 final Module module, final TypeProvider typeProvider) {
742 if (leaf == null || typeBuilder == null || leaf.isAddedByUses()) {
746 final String leafName = leaf.getQName().getLocalName();
747 if (leafName == null) {
751 final Module parentModule = findParentModule(schemaContext, leaf);
752 Type returnType = null;
754 final TypeDefinition<?> typeDef = leaf.getType();
755 if (isInnerType(leaf, typeDef)) {
756 if (typeDef instanceof EnumTypeDefinition) {
757 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
758 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
759 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
760 genCtx, typeBuilder, module);
761 if (enumBuilder != null) {
762 returnType = enumBuilder.toInstance(typeBuilder);
764 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
765 } else if (typeDef instanceof UnionTypeDefinition) {
766 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
767 typeProvider, schemaContext);
768 if (genTOBuilder != null) {
769 //TODO: https://bugs.opendaylight.org/show_bug.cgi?id=2289
770 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
772 } else if (typeDef instanceof BitsTypeDefinition) {
773 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
774 typeProvider, schemaContext);
775 if (genTOBuilder != null) {
776 returnType = genTOBuilder.toInstance();
779 // It is constrained version of already declared type (inner declared type exists,
780 // onlyfor special cases (Enum, Union, Bits), which were already checked.
781 // In order to get proper class we need to look up closest derived type
782 // and apply restrictions from leaf type
783 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
784 returnType = typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
788 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
789 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
792 if (returnType == null) {
796 if (typeDef instanceof EnumTypeDefinition) {
797 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
800 String leafDesc = leaf.getDescription();
801 if (leafDesc == null) {
805 final String leafGetterName;
806 if (!"".equals(nodeName)) {
807 StringBuilder sb = new StringBuilder(nodeName)
810 leafGetterName = sb.toString();
812 leafGetterName = leafName;
815 constructGetter(typeBuilder, leafGetterName, leafDesc, returnType, leaf.getStatus());
820 * Converts <code>node</code> leaf list schema node to getter method of
821 * <code>typeBuilder</code>.
824 * generated type builder to which is <code>node</code> added as
827 * leaf list schema node which is added to
828 * <code>typeBuilder</code> as getter method
829 * @param module module
830 * @param typeProvider type provider instance
831 * @param genCtx actual generated context
832 * @param genCtx actual generated context
833 * @return boolean value
835 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
836 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
837 * <li>false - other cases</li>
840 private static boolean resolveLeafListSchemaNode(final SchemaContext schemaContext, final GeneratedTypeBuilder
841 typeBuilder, final LeafListSchemaNode node, final Module module, final TypeProvider typeProvider,
842 final Map<Module, ModuleContext> genCtx) {
843 if (node == null || typeBuilder == null || node.isAddedByUses()) {
847 final QName nodeName = node.getQName();
849 final TypeDefinition<?> typeDef = node.getType();
850 final Module parentModule = findParentModule(schemaContext, node);
852 Type returnType = null;
853 if (typeDef.getBaseType() == null) {
854 if (typeDef instanceof EnumTypeDefinition) {
855 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
856 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
857 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
858 genCtx, typeBuilder, module);
859 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
860 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
861 } else if (typeDef instanceof UnionTypeDefinition) {
862 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
863 typeProvider, schemaContext);
864 if (genTOBuilder != null) {
865 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
867 } else if (typeDef instanceof BitsTypeDefinition) {
868 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
869 typeProvider, schemaContext);
870 returnType = genTOBuilder.toInstance();
872 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
873 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
876 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
877 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
880 final ParameterizedType listType = Types.listTypeFor(returnType);
881 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType, node.getStatus());
886 * Converts <code>caseNodes</code> set to list of corresponding generated
889 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
890 * created generated type builder. The package names for the builder is
891 * created as concatenation of the module package (
892 * <code>basePackageName</code>) and names of all parents nodes of the
893 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
894 * between every case builder and <i>choice</i> type
898 * @param schemaContext
899 * current schema context
901 * actual generated context
902 * @param basePackageName
903 * string with the module package name
904 * @param refChoiceType
905 * type which represents superior <i>case</i>
907 * choice case node which is mapped to generated type
908 * @param verboseClassComments
909 * Javadoc verbosity switch
910 * @throws IllegalArgumentException
912 * <li>if <code>basePackageName</code> equals null</li>
913 * <li>if <code>refChoiceType</code> equals null</li>
914 * <li>if <code>caseNodes</code> equals null</li>
917 private static void generateTypesFromChoiceCases(final Module module, final SchemaContext schemaContext,
918 final Map<Module, ModuleContext> genCtx, final String basePackageName, final Type refChoiceType,
919 final ChoiceSchemaNode choiceNode, final boolean verboseClassComments, final TypeProvider typeProvider,
920 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
921 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
922 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
923 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
925 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
926 if (caseNodes == null) {
930 for (final ChoiceCaseNode caseNode : caseNodes) {
931 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
932 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath(),
933 BindingNamespaceType.Data);
934 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode,
935 module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
936 caseTypeBuilder.addImplementsType(refChoiceType);
937 caseTypeBuilder.setParentTypeForBuilder(refChoiceType);
938 annotateDeprecatedIfNecessary(caseNode.getStatus(), caseTypeBuilder);
939 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
940 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
941 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
942 if (caseChildNodes != null) {
943 final SchemaPath choiceNodeParentPath = choiceNode.getPath().getParent();
945 if (!Iterables.isEmpty(choiceNodeParentPath.getPathFromRoot())) {
946 SchemaNode parent = findDataSchemaNode(schemaContext, choiceNodeParentPath);
948 if (parent instanceof AugmentationSchema) {
949 final AugmentationSchema augSchema = (AugmentationSchema) parent;
950 final SchemaPath targetPath = augSchema.getTargetPath();
951 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
952 if (targetSchemaNode instanceof DataSchemaNode
953 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
954 if (targetSchemaNode instanceof DerivableSchemaNode) {
955 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
957 if (targetSchemaNode == null) {
958 throw new IllegalStateException(
959 "Failed to find target node from grouping for augmentation " + augSchema
960 + " in module " + module.getName());
963 parent = targetSchemaNode;
966 Preconditions.checkState(parent != null, "Could not find Choice node parent %s",
967 choiceNodeParentPath);
968 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath(), genCtx);
969 if (childOfType == null) {
970 childOfType = findGroupingByPath(parent.getPath(), genCtx);
972 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes,
973 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
975 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, moduleToDataType(module,
976 genCtx, verboseClassComments), caseChildNodes, genCtx, schemaContext,
977 verboseClassComments, genTypeBuilders, typeProvider);
981 processUsesAugments(schemaContext, caseNode, module, genCtx, genTypeBuilders, verboseClassComments,
986 private static Type resolveAnyNodeAsMethod(final SchemaContext schemaContext, final GeneratedTypeBuilder
987 typeBuilder, final Map<Module, ModuleContext> genCtx, final DataSchemaNode node, final Module module,
988 final TypeProvider typeProvider) {
990 final String anyName = node.getQName().getLocalName();
991 if (anyName == null) {
995 String anyDesc = node.getDescription();
996 if (anyDesc == null) {
1000 Type returnType = Types.DOCUMENT;
1002 constructGetter(typeBuilder, anyName, anyDesc, returnType, node.getStatus());
1007 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1008 * or to <code>genTOBuilder</code> as property.
1011 * string contains the name of list
1012 * @param basePackageName
1013 * string contains the module package name
1015 * data schema node which should be added as getter method to
1016 * <code>typeBuilder</code> or as a property to
1017 * <code>genTOBuilder</code> if is part of the list key
1018 * @param typeBuilder
1019 * generated type builder for the list schema node
1020 * @param genTOBuilder
1021 * generated TO builder for the list keys
1023 * list of string which contains QNames of the list keys
1026 * @param typeProvider
1027 * provider that defines contract for generated types
1028 * @param schemaContext
1031 * map of generated entities in context of YANG modules
1032 * @param genTypeBuilders
1033 * map of generated type builders
1034 * @param verboseClassComments
1035 * generate verbose comments
1036 * @throws IllegalArgumentException
1038 * <li>if <code>schemaNode</code> equals null</li>
1039 * <li>if <code>typeBuilder</code> equals null</li>
1042 private static void addSchemaNodeToListBuilders(final String nodeName, final String basePackageName,
1043 final DataSchemaNode schemaNode, final GeneratedTypeBuilder typeBuilder,
1044 final GeneratedTOBuilder genTOBuilder, final List<QName> listKeys, final Module module,
1045 final TypeProvider typeProvider, final SchemaContext schemaContext, final Map<Module, ModuleContext> genCtx,
1046 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final boolean verboseClassComments) {
1047 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1048 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1050 if (schemaNode instanceof LeafSchemaNode) {
1051 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1052 final QName leafQName = leaf.getQName();
1054 final Type type = resolveLeafSchemaNodeAsMethod(nodeName, schemaContext, typeBuilder, genCtx, leaf, module,
1056 if (listKeys.contains(leafQName)) {
1058 resolveLeafSchemaNodeAsProperty(schemaContext, typeProvider, genCtx, genTOBuilder, leaf, true,
1061 AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1064 } else if (!schemaNode.isAddedByUses()) {
1065 if (schemaNode instanceof LeafListSchemaNode) {
1066 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) schemaNode, module,
1067 typeProvider, genCtx);
1068 } else if (schemaNode instanceof ContainerSchemaNode) {
1069 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode,
1070 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
1071 } else if (schemaNode instanceof ListSchemaNode) {
1072 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode,
1073 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
1074 } else if (schemaNode instanceof ChoiceSchemaNode) {
1075 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, typeBuilder,
1076 (ChoiceSchemaNode) schemaNode, genTypeBuilders, genCtx, typeProvider);
1081 private static boolean resolveLeafSchemaNodeAsProperty(final SchemaContext schemaContext, final TypeProvider
1082 typeProvider, final Map<Module, ModuleContext> genCtx, final GeneratedTOBuilder
1083 toBuilder, final LeafSchemaNode leaf, final boolean isReadOnly, final Module module) {
1085 if (leaf != null && toBuilder != null) {
1087 final TypeDefinition<?> typeDef = leaf.getType();
1088 if (typeDef instanceof UnionTypeDefinition) {
1089 // GeneratedType for this type definition should be already
1091 final QName qname = typeDef.getQName();
1092 final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1093 qname.getRevision());
1094 final ModuleContext mc = genCtx.get(unionModule);
1095 returnType = mc.getTypedefs().get(typeDef.getPath());
1096 } else if (typeDef instanceof EnumTypeDefinition && typeDef.getBaseType() == null) {
1097 // Annonymous enumeration (already generated, since it is inherited via uses).
1098 LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
1099 QName qname = originalLeaf.getQName();
1100 final Module enumModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1101 qname.getRevision());
1102 returnType = genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
1104 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1106 return AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1111 private static TypeDefinition<?> getBaseOrDeclaredType(final TypeDefinition<?> typeDef) {
1112 final TypeDefinition<?> baseType = typeDef.getBaseType();
1113 return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef;
1116 @SuppressWarnings({ "rawtypes", "unchecked" })
1117 private static GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
1118 final GeneratedTypeBuilder childOf, final DataSchemaNode node, final SchemaContext schemaContext,
1119 final boolean verboseClassComments, Map<Module, ModuleContext> genCtx, final Map<String, Map<String,
1120 GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1122 if (node.isAugmenting() || node.isAddedByUses()) {
1125 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath(), BindingNamespaceType.Data);
1126 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf, module,
1127 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1128 genType.addComment(node.getDescription());
1129 annotateDeprecatedIfNecessary(node.getStatus(), genType);
1130 genType.setDescription(createDescription(node, genType.getFullyQualifiedName(), schemaContext, verboseClassComments));
1131 genType.setModuleName(module.getName());
1132 genType.setReference(node.getReference());
1133 genType.setSchemaPath((List) node.getPath().getPathFromRoot());
1134 genType.setParentTypeForBuilder(childOf);
1135 if (node instanceof DataNodeContainer) {
1136 genCtx.get(module).addChildNodeType(node, genType);
1137 genCtx = groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings(), genCtx, schemaContext,
1138 verboseClassComments, genTypeBuilders, typeProvider);
1139 processUsesAugments(schemaContext, (DataNodeContainer) node, module, genCtx, genTypeBuilders,
1140 verboseClassComments, typeProvider);
1146 * Converts all <b>groupings</b> of the module to the list of
1147 * <code>Type</code> objects. Firstly are groupings sorted according mutual
1148 * dependencies. At least dependent (independent) groupings are in the list
1149 * saved at first positions. For every grouping the record is added to map
1150 * {@link ModuleContext#groupings allGroupings}
1155 * collection of groupings from which types will be generated
1156 * @param typeProvider
1157 * provider that defines contract for generated types
1158 * @param schemaContext
1161 * map of generated entities in context of YANG modules
1162 * @param genTypeBuilders
1163 * map of generated type builders
1164 * @param verboseClassComments
1165 * generate verbose comments
1168 static Map<Module, ModuleContext> groupingsToGenTypes(final Module module, final Collection<GroupingDefinition>
1169 groupings, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1170 verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1171 final String basePackageName = BindingMapping.getRootPackageName(module);
1172 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
1174 for (final GroupingDefinition grouping : groupingsSortedByDependencies) {
1175 genCtx = groupingToGenType(basePackageName, grouping, module, genCtx, schemaContext,
1176 verboseClassComments, genTypeBuilders, typeProvider);
1182 * Converts individual grouping to GeneratedType. Firstly generated type
1183 * builder is created and every child node of grouping is resolved to the
1186 * @param basePackageName
1187 * string contains the module package name
1189 * GroupingDefinition which contains data about grouping
1192 * @param typeProvider
1193 * provider that defines contract for generated types
1194 * @param schemaContext
1197 * map of generated entities in context of YANG modules
1198 * @param genTypeBuilders
1199 * map of generated type builders
1200 * @param verboseClassComments
1201 * generate verbose comments
1203 * @return GeneratedType which is generated from grouping (object of type
1204 * <code>GroupingDefinition</code>)
1206 private static Map<Module, ModuleContext> groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module
1207 module, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1208 verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1209 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath(), BindingNamespaceType.Grouping);
1210 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping, module, genCtx,
1211 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1212 annotateDeprecatedIfNecessary(grouping.getStatus(), genType);
1213 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
1214 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes(), genCtx,
1215 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1216 genCtx = groupingsToGenTypes(module, grouping.getGroupings(), genCtx, schemaContext, verboseClassComments,
1217 genTypeBuilders, typeProvider);
1218 genCtx = processUsesAugments(schemaContext, grouping, module, genCtx, genTypeBuilders, verboseClassComments,
1224 * //TODO: add information about multiple base identities in YANG 1.1
1225 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
1226 * created transport object builder. If identity contains base identity then
1227 * reference to base identity is added to superior identity as its extend.
1228 * If identity doesn't contain base identity then only reference to abstract
1229 * class {@link org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode
1230 * BaseIdentity} is added
1234 * @param basePackageName
1235 * string contains the module package name
1237 * IdentitySchemaNode which contains data about identity
1238 * @param schemaContext
1239 * SchemaContext which is used to get package and name
1240 * information about base of identity
1241 * @param genCtx generated context
1242 * @return returns generated context
1244 static Map<Module, ModuleContext> identityToGenType(final Module module, final String basePackageName,
1245 final IdentitySchemaNode identity, final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx,
1246 boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
1247 final TypeProvider typeProvider, Map<QName, GeneratedTOBuilderImpl> generatedIdentities) {
1249 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath(),
1250 BindingNamespaceType.Identity);
1251 final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName,
1252 identity.getQName().getLocalName());
1254 final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
1255 if (baseIdentities.size() == 0) {
1256 //no base - abstract
1257 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
1258 BaseIdentity.class.getSimpleName());
1259 newType.setExtendsType(gto.toInstance());
1260 generatedIdentities.put(identity.getQName(), newType);
1262 //one base - inheritance
1263 final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
1264 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(schemaContext, baseIdentity);
1265 final String returnTypePkgName = new StringBuilder(BindingMapping.getRootPackageName
1266 (baseIdentityParentModule))
1268 .append(BindingNamespaceType.Identity.getPackagePrefix())
1271 final GeneratedTOBuilderImpl existingIdentityGto = generatedIdentities.get(baseIdentity.getQName());
1272 if (existingIdentityGto != null) {
1273 newType.setExtendsType(existingIdentityGto.toInstance());
1275 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(returnTypePkgName,
1276 baseIdentity.getQName().getLocalName());
1277 newType.setExtendsType(gto.toInstance());
1278 generatedIdentities.put(baseIdentity.getQName(), gto);
1281 //FIXME: more bases - possible composition, multiple inheritance not possible
1283 generatedIdentities.put(identity.getQName(), newType);
1285 newType.setAbstract(true);
1286 newType.addComment(identity.getDescription());
1287 newType.setDescription(createDescription(identity, newType.getFullyQualifiedName(), schemaContext,
1288 verboseClassComments));
1289 newType.setReference(identity.getReference());
1290 newType.setModuleName(module.getName());
1291 newType.setSchemaPath((List) identity.getPath().getPathFromRoot());
1293 qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
1295 genCtx.get(module).addIdentityType(identity.getQName(), newType);