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 org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
41 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil;
42 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes;
43 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
44 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer;
45 import org.opendaylight.mdsal.binding.javav2.generator.util.ReferencedTypeImpl;
46 import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
47 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
48 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
49 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
50 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.GroupingDefinitionDependencySort;
51 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
52 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
53 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject;
54 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
55 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
56 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
57 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
58 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
59 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
60 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
61 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
62 import org.opendaylight.mdsal.binding.javav2.spec.base.BaseIdentity;
63 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
64 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
65 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
66 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
67 import org.opendaylight.yangtools.yang.common.QName;
68 import org.opendaylight.yangtools.yang.model.api.AnyDataSchemaNode;
69 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
70 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
71 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
72 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
75 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
76 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
77 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
78 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
79 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
80 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
81 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.Module;
83 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
84 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
85 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
86 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
87 import org.opendaylight.yangtools.yang.model.api.Status;
88 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
89 import org.opendaylight.yangtools.yang.model.api.UsesNode;
90 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
91 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
92 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
93 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
94 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
97 * Helper util class used for generation of types in Binding spec v2.
100 final class GenHelperUtil {
102 private GenHelperUtil() {
103 throw new UnsupportedOperationException("Util class");
107 * Create GeneratedTypeBuilder object from module argument.
110 * Module object from which builder will be created
112 * @param verboseClassComments
114 * @return <code>GeneratedTypeBuilder</code> which is internal
115 * representation of the module
116 * @throws IllegalArgumentException
119 static GeneratedTypeBuilder moduleToDataType(final Module module, final Map<Module, ModuleContext> genCtx, final boolean verboseClassComments) {
120 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
122 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data", verboseClassComments);
123 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder, genCtx);
124 moduleDataTypeBuilder.addImplementsType(BindingTypes.TREE_ROOT);
125 moduleDataTypeBuilder.addComment(module.getDescription());
126 moduleDataTypeBuilder.setDescription(createDescription(module, verboseClassComments));
127 moduleDataTypeBuilder.setReference(module.getReference());
128 return moduleDataTypeBuilder;
132 * Generates type builder for <code>module</code>.
135 * Module which is source of package name for generated type
138 * string which is added to the module class name representation
140 * @param verboseClassComments
141 * @return instance of GeneratedTypeBuilder which represents
142 * <code>module</code>.
143 * @throws IllegalArgumentException
144 * if <code>module</code> is null
146 static GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix, final boolean
147 verboseClassComments) {
148 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
149 final String packageName = BindingMapping.getRootPackageName(module);
150 // underscore used as separator for distinction of module name parts
151 final String moduleName = new StringBuilder(module.getName()).append('_').append(postfix).toString();
153 final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
154 moduleBuilder.setDescription(createDescription(module, verboseClassComments));
155 moduleBuilder.setReference(module.getReference());
156 moduleBuilder.setModuleName(moduleName);
158 return moduleBuilder;
162 * Adds the implemented types to type builder.
164 * The method passes through the list of <i>uses</i> in
165 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
166 * generated type from all groupings
167 * allGroupings} which is added as <i>implements type</i> to
168 * <code>builder</code>
170 * @param dataNodeContainer
171 * element which contains the list of used YANG groupings
173 * builder to which are added implemented types according to
174 * <code>dataNodeContainer</code>
175 * @param genCtx generated context
176 * @return generated type builder with all implemented types
178 static GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
179 final GeneratedTypeBuilder builder, final Map<Module, ModuleContext> genCtx) {
180 for (final UsesNode usesNode : dataNodeContainer.getUses()) {
181 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath(), genCtx).toInstance();
182 if (genType == null) {
183 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
184 + builder.getName());
186 builder.addImplementsType(genType);
191 static GeneratedTypeBuilder findGroupingByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
192 for (final ModuleContext ctx : genCtx.values()) {
193 final GeneratedTypeBuilder result = ctx.getGrouping(path);
194 if (result != null) {
202 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
203 * node for which <code>typeBuilder</code> was created.
205 * The subnodes aren't mapped to the methods if they are part of grouping or
206 * augment (in this case are already part of them).
210 * @param basePackageName
211 * string contains the module package name
213 * generated type builder which represents any node. The subnodes
214 * of this node are added to the <code>typeBuilder</code> as
215 * methods. The subnode can be of type leaf, leaf-list, list,
220 * set of data schema nodes which are the children of the node
221 * for which <code>typeBuilder</code> was created
222 * @return generated type builder which is the same builder as input
223 * parameter. The getter methods (representing child nodes) could be
226 static GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
227 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf,
228 final Iterable<DataSchemaNode> schemaNodes, final Map<Module, ModuleContext> genCtx,
229 final SchemaContext schemaContext, final boolean verboseClassComments,
230 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
231 final TypeProvider typeProvider) {
233 if (schemaNodes != null && parent != null) {
234 for (final DataSchemaNode schemaNode : schemaNodes) {
235 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
236 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module, genCtx,
237 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
244 static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
245 schemaNode, final Module module, final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext,
246 final boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
247 final TypeProvider typeProvider) {
248 return addDefaultInterfaceDefinition(packageName, schemaNode, null, module, genCtx, schemaContext,
249 verboseClassComments, genTypeBuilders, typeProvider);
252 static Map<Module, ModuleContext> processUsesAugments(final SchemaContext schemaContext, final
253 DataNodeContainer node, final Module module, Map<Module, ModuleContext> genCtx,
254 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
255 final boolean verboseClassComments, final TypeProvider typeProvider) {
256 final String basePackageName = BindingMapping.getRootPackageName(module);
257 for (final UsesNode usesNode : node.getUses()) {
258 for (final AugmentationSchema augment : usesNode.getAugmentations()) {
259 genCtx = AugmentToGenType.usesAugmentationToGenTypes(schemaContext, basePackageName, augment, module,
260 usesNode, node, genCtx, genTypeBuilders, verboseClassComments, typeProvider);
261 genCtx = processUsesAugments(schemaContext, augment, module, genCtx, genTypeBuilders,
262 verboseClassComments, typeProvider);
268 static GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
269 for (final ModuleContext ctx : genCtx.values()) {
270 final GeneratedTypeBuilder result = ctx.getChildNode(path);
271 if (result != null) {
278 static GeneratedTypeBuilder findCaseByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
279 for (final ModuleContext ctx : genCtx.values()) {
280 final GeneratedTypeBuilder result = ctx.getCase(path);
281 if (result != null) {
288 static Map<Module, ModuleContext> addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
289 final Type targetTypeRef, final List<AugmentationSchema> schemaPathAugmentListEntry,
290 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final Map<Module, ModuleContext> genCtx,
291 final SchemaContext schemaContext, final boolean verboseClassComments, final TypeProvider typeProvider) {
293 //pick augmentation grouped by augmentation target, there is always at least one
294 final AugmentationSchema augSchema = schemaPathAugmentListEntry.get(0);
296 final String augmentNamespacePackageName = BindingGeneratorUtil.packageNameForGeneratedType(augmentPackageName,
297 augSchema.getTargetPath(), BindingNamespaceType.Data);
299 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.computeIfAbsent(
300 augmentNamespacePackageName, k -> new HashMap<>());
302 //this requires valid semantics in YANG model
303 String augIdentifier = null;
304 for (AugmentationSchema aug : schemaPathAugmentListEntry) {
305 augIdentifier = getAugmentIdentifier(aug.getUnknownSchemaNodes());
309 if (augIdentifier == null) {
310 augIdentifier = augGenTypeName(augmentBuilders, targetTypeRef.getName());
313 GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentNamespacePackageName, augIdentifier);
315 augTypeBuilder.addImplementsType(BindingTypes.TREE_NODE);
316 augTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, augTypeBuilder));
317 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
318 annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
320 //produces getters for augTypeBuilder eventually
321 for (AugmentationSchema aug : schemaPathAugmentListEntry) {
323 addImplementedInterfaceFromUses(aug, augTypeBuilder, genCtx);
324 augSchemaNodeToMethods(module, augmentNamespacePackageName, augTypeBuilder, augTypeBuilder, aug.getChildNodes(),
325 genCtx, schemaContext, verboseClassComments, typeProvider, genTypeBuilders);
328 augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder);
330 if(!augSchema.getChildNodes().isEmpty()) {
331 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
333 genCtx.get(module).addAugmentType(augTypeBuilder);
337 //TODO: delete this method eventually when uses-augments & augmented choice cases are implemented
339 * Returns a generated type builder for an augmentation.
341 * The name of the type builder is equal to the name of augmented node with
342 * serial number as suffix.
346 * @param augmentPackageName
347 * string with contains the package name to which the augment
349 * @param basePackageName
350 * string with the package name to which the augmented node
352 * @param targetTypeRef
355 * augmentation schema which contains data about the child nodes
356 * and uses of augment
357 * @return generated type builder for augment in genCtx
360 static Map<Module, ModuleContext> addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
361 final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema,
362 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final Map<Module,
363 ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments, final
364 TypeProvider typeProvider) {
366 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.computeIfAbsent(augmentPackageName, k -> new HashMap<>());
367 String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
369 if (augIdentifier == null) {
370 augIdentifier = augGenTypeName(augmentBuilders, targetTypeRef.getName());
373 GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augIdentifier);
375 augTypeBuilder.addImplementsType(BindingTypes.TREE_NODE);
376 augTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, augTypeBuilder));
377 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
378 annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
379 augTypeBuilder = addImplementedInterfaceFromUses(augSchema, augTypeBuilder, genCtx);
381 augTypeBuilder = augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema
382 .getChildNodes(), genCtx, schemaContext, verboseClassComments, typeProvider, genTypeBuilders);
383 augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder);
385 if(!augSchema.getChildNodes().isEmpty()) {
386 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
389 genCtx.get(module).addAugmentType(augTypeBuilder);
394 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
395 * node for which <code>typeBuilder</code> was created.
399 * @param basePackageName
400 * string contains the module package name
402 * generated type builder which represents any node. The subnodes
403 * of this node are added to the <code>typeBuilder</code> as
404 * methods. The subnode can be of type leaf, leaf-list, list,
409 * set of data schema nodes which are the children of the node
410 * for which <code>typeBuilder</code> was created
411 * @return generated type builder which is the same object as the input
412 * parameter <code>typeBuilder</code>. The getter method could be
415 private static GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
416 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes,
417 final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
418 verboseClassComments, final TypeProvider typeProvider, final Map<String, Map<String,
419 GeneratedTypeBuilder>> genTypeBuilders) {
420 if (schemaNodes != null && typeBuilder != null) {
421 for (final DataSchemaNode schemaNode : schemaNodes) {
422 if (!schemaNode.isAugmenting()) {
423 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module, genCtx,
424 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
432 * Instantiates generated type builder with <code>packageName</code> and
433 * <code>schemaNode</code>.
435 * The new builder always implements
436 * {@link TreeNode TreeNode}.<br>
437 * If <code>schemaNode</code> is instance of GroupingDefinition it also
438 * implements {@link Augmentable
440 * If <code>schemaNode</code> is instance of
441 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
442 * DataNodeContainer} it can also implement nodes which are specified in
446 * string with the name of the package to which
447 * <code>schemaNode</code> belongs.
449 * schema node for which is created generated type builder
451 * parent type (can be null)
452 * @param schemaContext schema context
453 * @return generated type builder <code>schemaNode</code>
455 private static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
456 schemaNode, final Type parent, final Module module, final Map<Module, ModuleContext> genCtx,
457 final SchemaContext schemaContext, final boolean verboseClassComments, final Map<String, Map<String,
458 GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
460 GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, schemaContext, "",
461 verboseClassComments, genTypeBuilders);
462 if (parent == null) {
463 it.addImplementsType(BindingTypes.TREE_NODE);
465 if (parent instanceof ListSchemaNode) {
466 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
467 (BindingTypes.IDENTIFIABLE_ITEM, parent)));
469 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
470 (BindingTypes.ITEM, parent)));
471 it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
475 if (!(schemaNode instanceof GroupingDefinition)) {
476 it.addImplementsType(BindingTypes.augmentable(it));
479 if (schemaNode instanceof DataNodeContainer) {
480 groupingsToGenTypes(module, ((DataNodeContainer) schemaNode).getGroupings(), genCtx, schemaContext,
481 verboseClassComments, genTypeBuilders, typeProvider);
482 it = addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it, genCtx);
488 static GeneratedTypeBuilder resolveNotification(final GeneratedTypeBuilder listenerInterface, String
489 parentName, final String basePackageName, final NotificationDefinition notification, final Module module,
490 final SchemaContext schemaContext, final boolean verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>>
491 genTypeBuilders, TypeProvider typeProvider, Map<Module, ModuleContext> genCtx) {
493 processUsesAugments(schemaContext, notification, module, genCtx, genTypeBuilders,
494 verboseClassComments, typeProvider);
496 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition
497 (basePackageName, notification, null, module, genCtx, schemaContext,
498 verboseClassComments, genTypeBuilders, typeProvider);
499 annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
500 notificationInterface.addImplementsType(NOTIFICATION);
501 genCtx.get(module).addChildNodeType(notification, notificationInterface);
503 // Notification object
504 resolveDataSchemaNodes(module, basePackageName, notificationInterface,
505 notificationInterface, notification.getChildNodes(), genCtx, schemaContext,
506 verboseClassComments, genTypeBuilders, typeProvider);
508 //in case of tied notification, incorporate parent's localName
509 final StringBuilder sb = new StringBuilder("on_");
510 if (parentName != null) {
511 sb.append(parentName).append('_');
513 sb.append(notificationInterface.getName());
515 listenerInterface.addMethod(JavaIdentifierNormalizer.normalizeSpecificIdentifier(sb.toString(), JavaIdentifier.METHOD))
516 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
517 .setComment(encodeAngleBrackets(notification.getDescription())).setReturnType(Types.VOID);
518 return listenerInterface;
522 * Returns reference to generated type builder for specified
523 * <code>schemaNode</code> with <code>packageName</code>.
525 * Firstly the generated type builder is searched in
526 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
527 * found it is created and added to <code>genTypeBuilders</code>.
530 * string with the package name to which returning generated type
533 * schema node which provide data about the schema node name
534 * @param schemaContext schema context
536 * return type name prefix
537 * @return generated type builder for <code>schemaNode</code>
538 * @throws IllegalArgumentException
540 * <li>if <code>schemaNode</code> is null</li>
541 * <li>if <code>packageName</code> is null</li>
542 * <li>if QName of schema node is null</li>
543 * <li>if schemaNode name is null</li>
547 static GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
548 final SchemaContext schemaContext, final String prefix, final boolean verboseClassComments,
549 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
551 Preconditions.checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
552 Preconditions.checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
553 String schemaNodeName = schemaNode.getQName().getLocalName();
554 Preconditions.checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
556 if (prefix != null && !prefix.isEmpty()) {
557 // underscore used as separator for distinction of class name parts
558 schemaNodeName = new StringBuilder(prefix).append('_').append(schemaNodeName).toString();
561 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, schemaNodeName);
562 final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
563 qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
564 newType.addComment(schemaNode.getDescription());
565 newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName(), schemaContext, verboseClassComments));
566 newType.setReference(schemaNode.getReference());
567 newType.setSchemaPath((List<QName>) schemaNode.getPath().getPathFromRoot());
568 newType.setModuleName(module.getName());
570 if (!genTypeBuilders.containsKey(packageName)) {
571 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
572 builders.put(newType.getName(), newType);
573 genTypeBuilders.put(packageName, builders);
575 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
576 if (!builders.containsKey(newType.getName())) {
577 builders.put(newType.getName(), newType);
584 private static void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
585 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module,
586 final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
587 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
589 if (node != null && typeBuilder != null) {
590 if (node instanceof ContainerSchemaNode) {
591 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node,
592 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
593 } else if (node instanceof LeafListSchemaNode) {
594 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) node, module,
595 typeProvider, genCtx);
596 } else if (node instanceof LeafSchemaNode) {
597 resolveLeafSchemaNodeAsMethod("", schemaContext, typeBuilder, genCtx, (LeafSchemaNode) node, module,
599 } else if (node instanceof ListSchemaNode) {
600 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node, schemaContext,
601 verboseClassComments, genCtx, genTypeBuilders, typeProvider);
602 } else if (node instanceof ChoiceSchemaNode) {
603 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, childOf,
604 (ChoiceSchemaNode) node, genTypeBuilders, genCtx, typeProvider);
605 } else if (node instanceof AnyXmlSchemaNode || node instanceof AnyDataSchemaNode) {
606 resolveAnyNodeAsMethod(schemaContext, typeBuilder, genCtx, node, module, typeProvider);
612 * Converts <code>choiceNode</code> to the list of generated types for
613 * choice and its cases.
615 * The package names for choice and for its cases are created as
616 * concatenation of the module package (<code>basePackageName</code>) and
617 * names of all parents node.
621 * @param basePackageName
622 * string with the module package name
626 * choice node which is mapped to generated type. Also child
627 * nodes - cases are mapped to generated types.
628 * @throws IllegalArgumentException
630 * <li>if <code>basePackageName</code> is null</li>
631 * <li>if <code>choiceNode</code> is null</li>
634 private static void choiceToGenType(final Module module, final SchemaContext schemaContext, final boolean
635 verboseClasssComments, final String basePackageName, final GeneratedTypeBuilder parent, final
636 ChoiceSchemaNode choiceNode, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
637 final Map<Module, ModuleContext> genCtx, final TypeProvider typeProvider) {
638 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
639 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
641 if (!choiceNode.isAddedByUses()) {
642 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath(),
643 BindingNamespaceType.Data);
644 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode,
645 schemaContext, "", verboseClasssComments, genTypeBuilders);
646 constructGetter(parent, choiceNode.getQName().getLocalName(),
647 choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus());
648 choiceTypeBuilder.addImplementsType(INSTANTIABLE);
649 annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
650 genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
651 generateTypesFromChoiceCases(module, schemaContext, genCtx, basePackageName, choiceTypeBuilder.toInstance(),
652 choiceNode, verboseClasssComments, typeProvider, genTypeBuilders);
656 private static void containerToGenType(final Module module, final String basePackageName,
657 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node,
658 final SchemaContext schemaContext, final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
659 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
661 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
662 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
663 if (genType != null) {
664 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType, node.getStatus());
665 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes(), genCtx,
666 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
670 private static void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder
671 parent, final GeneratedTypeBuilder childOf, final ListSchemaNode node, final SchemaContext schemaContext,
672 final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
673 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
675 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
676 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
677 if (genType != null) {
678 final String nodeName = node.getQName().getLocalName();
679 constructGetter(parent, nodeName, node.getDescription(), Types.listTypeFor(genType), node.getStatus());
680 final List<QName> listKeys = node.getKeyDefinition();
681 final String packageName = new StringBuilder(packageNameForGeneratedType(basePackageName, node.getPath(),
682 BindingNamespaceType.Key)).append('.').append(nodeName).toString();
684 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
686 for (final DataSchemaNode schemaNode : node.getChildNodes()) {
687 if (!schemaNode.isAugmenting()) {
688 addSchemaNodeToListBuilders(nodeName, basePackageName, schemaNode, genType, genTOBuilder, listKeys,
689 module, typeProvider, schemaContext, genCtx, genTypeBuilders, verboseClassComments);
694 if (genTOBuilder != null) {
695 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
696 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
697 genTOBuilder.setSUID(prop);
700 typeBuildersToGenTypes(module, genType, genTOBuilder, genCtx);
704 private static void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder,
705 final GeneratedTOBuilder genTOBuilder, final Map<Module, ModuleContext> genCtx) {
706 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
707 if (genTOBuilder != null) {
708 final GeneratedTransferObject genTO = genTOBuilder.toInstance();
709 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO, Status.CURRENT);
710 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
715 * Converts <code>leaf</code> to the getter method which is added to
716 * <code>typeBuilder</code>.
719 * generated type builder to which is added getter method as
720 * <code>leaf</code> mapping
722 * leaf schema node which is mapped as getter method which is
723 * added to <code>typeBuilder</code>
725 * Module in which type was defined
726 * @return boolean value
728 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
730 * <li>true - in other cases</li>
733 private static Type resolveLeafSchemaNodeAsMethod(final String nodeName, final SchemaContext schemaContext,
734 final GeneratedTypeBuilder typeBuilder, final Map<Module, ModuleContext> genCtx, final LeafSchemaNode leaf,
735 final Module module, final TypeProvider typeProvider) {
736 if (leaf == null || typeBuilder == null || leaf.isAddedByUses()) {
740 final String leafName = leaf.getQName().getLocalName();
741 if (leafName == null) {
745 final Module parentModule = findParentModule(schemaContext, leaf);
746 Type returnType = null;
748 final TypeDefinition<?> typeDef = leaf.getType();
749 if (isInnerType(leaf, typeDef)) {
750 if (typeDef instanceof EnumTypeDefinition) {
751 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
752 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
753 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
754 genCtx, typeBuilder, module);
755 if (enumBuilder != null) {
756 returnType = enumBuilder.toInstance(typeBuilder);
758 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
759 } else if (typeDef instanceof UnionTypeDefinition) {
760 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
761 typeProvider, schemaContext);
762 if (genTOBuilder != null) {
763 //TODO: https://bugs.opendaylight.org/show_bug.cgi?id=2289
764 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
766 } else if (typeDef instanceof BitsTypeDefinition) {
767 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
768 typeProvider, schemaContext);
769 if (genTOBuilder != null) {
770 returnType = genTOBuilder.toInstance();
773 // It is constrained version of already declared type (inner declared type exists,
774 // onlyfor special cases (Enum, Union, Bits), which were already checked.
775 // In order to get proper class we need to look up closest derived type
776 // and apply restrictions from leaf type
777 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
778 returnType = typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
782 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
783 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
786 if (returnType == null) {
790 if (typeDef instanceof EnumTypeDefinition) {
791 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
794 String leafDesc = leaf.getDescription();
795 if (leafDesc == null) {
799 final String leafGetterName;
800 if (!"".equals(nodeName)) {
801 StringBuilder sb = new StringBuilder(nodeName)
804 leafGetterName = sb.toString();
806 leafGetterName = leafName;
809 constructGetter(typeBuilder, leafGetterName, leafDesc, returnType, leaf.getStatus());
814 * Converts <code>node</code> leaf list schema node to getter method of
815 * <code>typeBuilder</code>.
818 * generated type builder to which is <code>node</code> added as
821 * leaf list schema node which is added to
822 * <code>typeBuilder</code> as getter method
823 * @param module module
824 * @param typeProvider type provider instance
825 * @param genCtx actual generated context
826 * @param genCtx actual generated context
827 * @return boolean value
829 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
830 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
831 * <li>false - other cases</li>
834 private static boolean resolveLeafListSchemaNode(final SchemaContext schemaContext, final GeneratedTypeBuilder
835 typeBuilder, final LeafListSchemaNode node, final Module module, final TypeProvider typeProvider,
836 final Map<Module, ModuleContext> genCtx) {
837 if (node == null || typeBuilder == null || node.isAddedByUses()) {
841 final QName nodeName = node.getQName();
843 final TypeDefinition<?> typeDef = node.getType();
844 final Module parentModule = findParentModule(schemaContext, node);
846 Type returnType = null;
847 if (typeDef.getBaseType() == null) {
848 if (typeDef instanceof EnumTypeDefinition) {
849 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
850 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
851 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
852 genCtx, typeBuilder, module);
853 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
854 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
855 } else if (typeDef instanceof UnionTypeDefinition) {
856 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
857 typeProvider, schemaContext);
858 if (genTOBuilder != null) {
859 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
861 } else if (typeDef instanceof BitsTypeDefinition) {
862 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
863 typeProvider, schemaContext);
864 returnType = genTOBuilder.toInstance();
866 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
867 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
870 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
871 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
874 final ParameterizedType listType = Types.listTypeFor(returnType);
875 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType, node.getStatus());
880 * Converts <code>caseNodes</code> set to list of corresponding generated
883 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
884 * created generated type builder. The package names for the builder is
885 * created as concatenation of the module package (
886 * <code>basePackageName</code>) and names of all parents nodes of the
887 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
888 * between every case builder and <i>choice</i> type
892 * @param schemaContext
893 * current schema context
895 * actual generated context
896 * @param basePackageName
897 * string with the module package name
898 * @param refChoiceType
899 * type which represents superior <i>case</i>
901 * choice case node which is mapped to generated type
902 * @param verboseClassComments
903 * Javadoc verbosity switch
904 * @throws IllegalArgumentException
906 * <li>if <code>basePackageName</code> equals null</li>
907 * <li>if <code>refChoiceType</code> equals null</li>
908 * <li>if <code>caseNodes</code> equals null</li>
911 private static void generateTypesFromChoiceCases(final Module module, final SchemaContext schemaContext,
912 final Map<Module, ModuleContext> genCtx, final String basePackageName, final Type refChoiceType,
913 final ChoiceSchemaNode choiceNode, final boolean verboseClassComments, final TypeProvider typeProvider,
914 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
915 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
916 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
917 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
919 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
920 if (caseNodes == null) {
924 for (final ChoiceCaseNode caseNode : caseNodes) {
925 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
926 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath(),
927 BindingNamespaceType.Data);
928 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode,
929 module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
930 caseTypeBuilder.addImplementsType(refChoiceType);
931 caseTypeBuilder.setParentTypeForBuilder(refChoiceType);
932 annotateDeprecatedIfNecessary(caseNode.getStatus(), caseTypeBuilder);
933 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
934 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
935 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
936 if (caseChildNodes != null) {
937 final SchemaPath choiceNodeParentPath = choiceNode.getPath().getParent();
939 if (!Iterables.isEmpty(choiceNodeParentPath.getPathFromRoot())) {
940 SchemaNode parent = findDataSchemaNode(schemaContext, choiceNodeParentPath);
942 if (parent instanceof AugmentationSchema) {
943 final AugmentationSchema augSchema = (AugmentationSchema) parent;
944 final SchemaPath targetPath = augSchema.getTargetPath();
945 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
946 if (targetSchemaNode instanceof DataSchemaNode
947 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
948 if (targetSchemaNode instanceof DerivableSchemaNode) {
949 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
951 if (targetSchemaNode == null) {
952 throw new IllegalStateException(
953 "Failed to find target node from grouping for augmentation " + augSchema
954 + " in module " + module.getName());
957 parent = targetSchemaNode;
960 Preconditions.checkState(parent != null, "Could not find Choice node parent %s",
961 choiceNodeParentPath);
962 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath(), genCtx);
963 if (childOfType == null) {
964 childOfType = findGroupingByPath(parent.getPath(), genCtx);
966 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes,
967 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
969 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, moduleToDataType(module,
970 genCtx, verboseClassComments), caseChildNodes, genCtx, schemaContext,
971 verboseClassComments, genTypeBuilders, typeProvider);
975 processUsesAugments(schemaContext, caseNode, module, genCtx, genTypeBuilders, verboseClassComments,
980 private static Type resolveAnyNodeAsMethod(final SchemaContext schemaContext, final GeneratedTypeBuilder
981 typeBuilder, final Map<Module, ModuleContext> genCtx, final DataSchemaNode node, final Module module,
982 final TypeProvider typeProvider) {
984 final String anyName = node.getQName().getLocalName();
985 if (anyName == null) {
989 String anyDesc = node.getDescription();
990 if (anyDesc == null) {
994 Type returnType = Types.DOCUMENT;
996 constructGetter(typeBuilder, anyName, anyDesc, returnType, node.getStatus());
1001 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1002 * or to <code>genTOBuilder</code> as property.
1005 * string contains the name of list
1006 * @param basePackageName
1007 * string contains the module package name
1009 * data schema node which should be added as getter method to
1010 * <code>typeBuilder</code> or as a property to
1011 * <code>genTOBuilder</code> if is part of the list key
1012 * @param typeBuilder
1013 * generated type builder for the list schema node
1014 * @param genTOBuilder
1015 * generated TO builder for the list keys
1017 * list of string which contains QNames of the list keys
1020 * @param typeProvider
1021 * provider that defines contract for generated types
1022 * @param schemaContext
1025 * map of generated entities in context of YANG modules
1026 * @param genTypeBuilders
1027 * map of generated type builders
1028 * @param verboseClassComments
1029 * generate verbose comments
1030 * @throws IllegalArgumentException
1032 * <li>if <code>schemaNode</code> equals null</li>
1033 * <li>if <code>typeBuilder</code> equals null</li>
1036 private static void addSchemaNodeToListBuilders(final String nodeName, final String basePackageName,
1037 final DataSchemaNode schemaNode, final GeneratedTypeBuilder typeBuilder,
1038 final GeneratedTOBuilder genTOBuilder, final List<QName> listKeys, final Module module,
1039 final TypeProvider typeProvider, final SchemaContext schemaContext, final Map<Module, ModuleContext> genCtx,
1040 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final boolean verboseClassComments) {
1041 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1042 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1044 if (schemaNode instanceof LeafSchemaNode) {
1045 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1046 final QName leafQName = leaf.getQName();
1048 final Type type = resolveLeafSchemaNodeAsMethod(nodeName, schemaContext, typeBuilder, genCtx, leaf, module,
1050 if (listKeys.contains(leafQName)) {
1052 resolveLeafSchemaNodeAsProperty(schemaContext, typeProvider, genCtx, genTOBuilder, leaf, true,
1055 AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1058 } else if (!schemaNode.isAddedByUses()) {
1059 if (schemaNode instanceof LeafListSchemaNode) {
1060 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) schemaNode, module,
1061 typeProvider, genCtx);
1062 } else if (schemaNode instanceof ContainerSchemaNode) {
1063 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode,
1064 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
1065 } else if (schemaNode instanceof ListSchemaNode) {
1066 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode,
1067 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
1068 } else if (schemaNode instanceof ChoiceSchemaNode) {
1069 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, typeBuilder,
1070 (ChoiceSchemaNode) schemaNode, genTypeBuilders, genCtx, typeProvider);
1075 private static boolean resolveLeafSchemaNodeAsProperty(final SchemaContext schemaContext, final TypeProvider
1076 typeProvider, final Map<Module, ModuleContext> genCtx, final GeneratedTOBuilder
1077 toBuilder, final LeafSchemaNode leaf, final boolean isReadOnly, final Module module) {
1079 if (leaf != null && toBuilder != null) {
1081 final TypeDefinition<?> typeDef = leaf.getType();
1082 if (typeDef instanceof UnionTypeDefinition) {
1083 // GeneratedType for this type definition should be already
1085 final QName qname = typeDef.getQName();
1086 final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1087 qname.getRevision());
1088 final ModuleContext mc = genCtx.get(unionModule);
1089 returnType = mc.getTypedefs().get(typeDef.getPath());
1090 } else if (typeDef instanceof EnumTypeDefinition && typeDef.getBaseType() == null) {
1091 // Annonymous enumeration (already generated, since it is inherited via uses).
1092 LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
1093 QName qname = originalLeaf.getQName();
1094 final Module enumModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1095 qname.getRevision());
1096 returnType = genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
1098 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1100 return AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1105 private static TypeDefinition<?> getBaseOrDeclaredType(final TypeDefinition<?> typeDef) {
1106 final TypeDefinition<?> baseType = typeDef.getBaseType();
1107 return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef;
1110 @SuppressWarnings({ "rawtypes", "unchecked" })
1111 private static GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
1112 final GeneratedTypeBuilder childOf, final DataSchemaNode node, final SchemaContext schemaContext,
1113 final boolean verboseClassComments, Map<Module, ModuleContext> genCtx, final Map<String, Map<String,
1114 GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1116 if (node.isAugmenting() || node.isAddedByUses()) {
1119 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath(), BindingNamespaceType.Data);
1120 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf, module,
1121 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1122 genType.addComment(node.getDescription());
1123 annotateDeprecatedIfNecessary(node.getStatus(), genType);
1124 genType.setDescription(createDescription(node, genType.getFullyQualifiedName(), schemaContext, verboseClassComments));
1125 genType.setModuleName(module.getName());
1126 genType.setReference(node.getReference());
1127 genType.setSchemaPath((List) node.getPath().getPathFromRoot());
1128 genType.setParentTypeForBuilder(childOf);
1129 if (node instanceof DataNodeContainer) {
1130 genCtx.get(module).addChildNodeType(node, genType);
1131 genCtx = groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings(), genCtx, schemaContext,
1132 verboseClassComments, genTypeBuilders, typeProvider);
1133 processUsesAugments(schemaContext, (DataNodeContainer) node, module, genCtx, genTypeBuilders,
1134 verboseClassComments, typeProvider);
1140 * Converts all <b>groupings</b> of the module to the list of
1141 * <code>Type</code> objects. Firstly are groupings sorted according mutual
1142 * dependencies. At least dependent (independent) groupings are in the list
1143 * saved at first positions. For every grouping the record is added to map
1144 * {@link ModuleContext#groupings allGroupings}
1149 * collection of groupings from which types will be generated
1150 * @param typeProvider
1151 * provider that defines contract for generated types
1152 * @param schemaContext
1155 * map of generated entities in context of YANG modules
1156 * @param genTypeBuilders
1157 * map of generated type builders
1158 * @param verboseClassComments
1159 * generate verbose comments
1162 static Map<Module, ModuleContext> groupingsToGenTypes(final Module module, final Collection<GroupingDefinition>
1163 groupings, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1164 verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1165 final String basePackageName = BindingMapping.getRootPackageName(module);
1166 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
1168 for (final GroupingDefinition grouping : groupingsSortedByDependencies) {
1169 genCtx = groupingToGenType(basePackageName, grouping, module, genCtx, schemaContext,
1170 verboseClassComments, genTypeBuilders, typeProvider);
1176 * Converts individual grouping to GeneratedType. Firstly generated type
1177 * builder is created and every child node of grouping is resolved to the
1180 * @param basePackageName
1181 * string contains the module package name
1183 * GroupingDefinition which contains data about grouping
1186 * @param typeProvider
1187 * provider that defines contract for generated types
1188 * @param schemaContext
1191 * map of generated entities in context of YANG modules
1192 * @param genTypeBuilders
1193 * map of generated type builders
1194 * @param verboseClassComments
1195 * generate verbose comments
1197 * @return GeneratedType which is generated from grouping (object of type
1198 * <code>GroupingDefinition</code>)
1200 private static Map<Module, ModuleContext> groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module
1201 module, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1202 verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1203 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath(), BindingNamespaceType.Grouping);
1204 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping, module, genCtx,
1205 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1206 annotateDeprecatedIfNecessary(grouping.getStatus(), genType);
1207 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
1208 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes(), genCtx,
1209 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1210 genCtx = groupingsToGenTypes(module, grouping.getGroupings(), genCtx, schemaContext, verboseClassComments,
1211 genTypeBuilders, typeProvider);
1212 genCtx = processUsesAugments(schemaContext, grouping, module, genCtx, genTypeBuilders, verboseClassComments,
1218 * //TODO: add information about multiple base identities in YANG 1.1
1219 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
1220 * created transport object builder. If identity contains base identity then
1221 * reference to base identity is added to superior identity as its extend.
1222 * If identity doesn't contain base identity then only reference to abstract
1223 * class {@link org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode
1224 * BaseIdentity} is added
1228 * @param basePackageName
1229 * string contains the module package name
1231 * IdentitySchemaNode which contains data about identity
1232 * @param schemaContext
1233 * SchemaContext which is used to get package and name
1234 * information about base of identity
1235 * @param genCtx generated context
1236 * @return returns generated context
1238 static Map<Module, ModuleContext> identityToGenType(final Module module, final String basePackageName,
1239 final IdentitySchemaNode identity, final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx,
1240 boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
1241 final TypeProvider typeProvider, Map<QName, GeneratedTOBuilderImpl> generatedIdentities) {
1243 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath(),
1244 BindingNamespaceType.Identity);
1245 final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName,
1246 identity.getQName().getLocalName());
1248 final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
1249 if (baseIdentities.size() == 0) {
1250 //no base - abstract
1251 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
1252 BaseIdentity.class.getSimpleName());
1253 newType.setExtendsType(gto.toInstance());
1254 generatedIdentities.put(identity.getQName(), newType);
1256 //one base - inheritance
1257 final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
1258 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(schemaContext, baseIdentity);
1259 final String returnTypePkgName = new StringBuilder(BindingMapping.getRootPackageName
1260 (baseIdentityParentModule))
1262 .append(BindingNamespaceType.Identity.getPackagePrefix())
1265 final GeneratedTOBuilderImpl existingIdentityGto = generatedIdentities.get(baseIdentity.getQName());
1266 if (existingIdentityGto != null) {
1267 newType.setExtendsType(existingIdentityGto.toInstance());
1269 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(returnTypePkgName,
1270 baseIdentity.getQName().getLocalName());
1271 newType.setExtendsType(gto.toInstance());
1272 generatedIdentities.put(baseIdentity.getQName(), gto);
1275 //FIXME: more bases - possible composition, multiple inheritance not possible
1277 generatedIdentities.put(identity.getQName(), newType);
1279 newType.setAbstract(true);
1280 newType.addComment(identity.getDescription());
1281 newType.setDescription(createDescription(identity, newType.getFullyQualifiedName(), schemaContext,
1282 verboseClassComments));
1283 newType.setReference(identity.getReference());
1284 newType.setModuleName(module.getName());
1285 newType.setSchemaPath((List) identity.getPath().getPathFromRoot());
1287 qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
1289 genCtx.get(module).addIdentityType(identity.getQName(), newType);