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.NOTIFICATION;
27 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.parameterizedTypeFor;
28 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
29 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
31 import com.google.common.annotations.Beta;
32 import com.google.common.base.Preconditions;
33 import com.google.common.collect.Iterables;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.List;
39 import java.util.stream.Collectors;
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.model.api.type.builder.MethodSignatureBuilder;
63 import org.opendaylight.mdsal.binding.javav2.spec.base.BaseIdentity;
64 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
65 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
66 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
67 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
68 import org.opendaylight.yangtools.yang.common.QName;
69 import org.opendaylight.yangtools.yang.model.api.AnyDataSchemaNode;
70 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
71 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
72 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
73 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
75 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
76 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
77 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
78 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
79 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
80 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
81 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
83 import org.opendaylight.yangtools.yang.model.api.Module;
84 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
85 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
86 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
87 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
88 import org.opendaylight.yangtools.yang.model.api.Status;
89 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
90 import org.opendaylight.yangtools.yang.model.api.UsesNode;
91 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
92 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
93 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
94 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
95 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
98 * Helper util class used for generation of types in Binding spec v2.
101 final class GenHelperUtil {
103 private GenHelperUtil() {
104 throw new UnsupportedOperationException("Util class");
108 * Create GeneratedTypeBuilder object from module argument.
111 * Module object from which builder will be created
112 * @param genCtx generated context
113 * @param verboseClassComments verbosity switch
115 * @return <code>GeneratedTypeBuilder</code> which is internal
116 * representation of the module
117 * @throws IllegalArgumentException
120 static GeneratedTypeBuilder moduleToDataType(final Module module, final Map<Module, ModuleContext> genCtx, final boolean verboseClassComments) {
121 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
123 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data", verboseClassComments);
124 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder, genCtx);
125 moduleDataTypeBuilder.addImplementsType(BindingTypes.TREE_ROOT);
126 moduleDataTypeBuilder.addComment(module.getDescription());
127 moduleDataTypeBuilder.setDescription(createDescription(module, verboseClassComments));
128 moduleDataTypeBuilder.setReference(module.getReference());
129 return moduleDataTypeBuilder;
133 * Generates type builder for <code>module</code>.
136 * Module which is source of package name for generated type
139 * string which is added to the module class name representation
141 * @param verboseClassComments verbosity switch
142 * @return instance of GeneratedTypeBuilder which represents
143 * <code>module</code>.
144 * @throws IllegalArgumentException
145 * if <code>module</code> is null
147 static GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix, final boolean
148 verboseClassComments) {
149 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
150 final String packageName = BindingMapping.getRootPackageName(module);
151 // underscore used as separator for distinction of module name parts
152 final String moduleName = new StringBuilder(module.getName()).append('_').append(postfix).toString();
154 final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
155 moduleBuilder.setDescription(createDescription(module, verboseClassComments));
156 moduleBuilder.setReference(module.getReference());
157 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) {
201 static GeneratedTOBuilder findIdentityByQname(final QName qname, final Map<Module, ModuleContext> genCtx) {
202 for (final ModuleContext ctx : genCtx.values()) {
203 final GeneratedTOBuilder result = ctx.getIdentities().get(qname);
204 if (result != null) {
212 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
213 * node for which <code>typeBuilder</code> was created.
215 * The subnodes aren't mapped to the methods if they are part of grouping or
216 * augment (in this case are already part of them).
220 * @param basePackageName
221 * string contains the module package name
223 * generated type builder which represents any node. The subnodes
224 * of this node are added to the <code>typeBuilder</code> as
225 * methods. The subnode can be of type leaf, leaf-list, list,
230 * set of data schema nodes which are the children of the node
231 * for which <code>typeBuilder</code> was created
232 * @return generated type builder which is the same builder as input
233 * parameter. The getter methods (representing child nodes) could be
236 static GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
237 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf,
238 final Iterable<DataSchemaNode> schemaNodes, final Map<Module, ModuleContext> genCtx,
239 final SchemaContext schemaContext, final boolean verboseClassComments,
240 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
241 final TypeProvider typeProvider, final BindingNamespaceType namespaceType) {
243 if (schemaNodes != null && parent != null) {
244 for (final DataSchemaNode schemaNode : schemaNodes) {
245 if (!schemaNode.isAugmenting()) {
246 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module, genCtx,
247 schemaContext, verboseClassComments, genTypeBuilders, typeProvider, namespaceType);
254 static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String basePackageName, final SchemaNode
255 schemaNode, final Module module, final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext,
256 final boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
257 final TypeProvider typeProvider, final BindingNamespaceType namespaceType) {
258 return addDefaultInterfaceDefinition(basePackageName, schemaNode, null, module, genCtx, schemaContext,
259 verboseClassComments, genTypeBuilders, typeProvider , namespaceType);
262 static Map<Module, ModuleContext> processUsesAugments(final SchemaContext schemaContext, final
263 DataNodeContainer node, final Module module, Map<Module, ModuleContext> genCtx,
264 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
265 final boolean verboseClassComments, final TypeProvider typeProvider,
266 final BindingNamespaceType namespaceType) {
267 final String basePackageName = BindingMapping.getRootPackageName(module);
268 for (final UsesNode usesNode : node.getUses()) {
269 Map<SchemaPath, List<AugmentationSchema>> augmentationsGrouped =
270 usesNode.getAugmentations().stream().collect(Collectors.groupingBy(AugmentationSchema::getTargetPath));
271 for (Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry : augmentationsGrouped.entrySet()) {
272 genCtx = AugmentToGenType.usesAugmentationToGenTypes(schemaContext, basePackageName,
273 schemaPathAugmentListEntry.getValue(), module,
274 usesNode, node, genCtx, genTypeBuilders, verboseClassComments, typeProvider, namespaceType);
275 for (AugmentationSchema augSchema : schemaPathAugmentListEntry.getValue()) {
276 genCtx = processUsesAugments(schemaContext, augSchema, module, genCtx, genTypeBuilders,
277 verboseClassComments, typeProvider, namespaceType);
284 static GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
285 for (final ModuleContext ctx : genCtx.values()) {
286 final GeneratedTypeBuilder result = ctx.getChildNode(path);
287 if (result != null) {
294 static GeneratedTypeBuilder findCaseByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
295 for (final ModuleContext ctx : genCtx.values()) {
296 final GeneratedTypeBuilder result = ctx.getCase(path);
297 if (result != null) {
304 static Map<Module, ModuleContext> addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
305 final Type targetTypeRef, final List<AugmentationSchema> schemaPathAugmentListEntry,
306 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final Map<Module, ModuleContext> genCtx,
307 final SchemaContext schemaContext, final boolean verboseClassComments, final TypeProvider typeProvider,
308 final BindingNamespaceType namespaceType) {
310 //pick augmentation grouped by augmentation target, there is always at least one
311 final AugmentationSchema augSchema = schemaPathAugmentListEntry.get(0);
313 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.computeIfAbsent(
314 augmentPackageName, k -> new HashMap<>());
316 //this requires valid semantics in YANG model
317 String augIdentifier = null;
318 for (AugmentationSchema aug : schemaPathAugmentListEntry) {
319 augIdentifier = getAugmentIdentifier(aug.getUnknownSchemaNodes());
323 boolean isTypeNormalized = false;
324 if (augIdentifier == null) {
325 augIdentifier = augGenTypeName(augmentBuilders, targetTypeRef.getName());
326 isTypeNormalized = true;
329 GeneratedTypeBuilderImpl augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augIdentifier,
330 false, isTypeNormalized);
332 augTypeBuilder.addImplementsType(BindingTypes.TREE_NODE);
333 augTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, augTypeBuilder));
334 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
335 augTypeBuilder.setBasePackageName(BindingMapping.getRootPackageName(module));
336 augTypeBuilder.setWithBuilder(true);
337 annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
339 //produces getters for augTypeBuilder eventually
340 for (AugmentationSchema aug : schemaPathAugmentListEntry) {
342 addImplementedInterfaceFromUses(aug, augTypeBuilder, genCtx);
343 augSchemaNodeToMethods(module, BindingMapping.getRootPackageName(module), augTypeBuilder, augTypeBuilder, aug.getChildNodes(),
344 genCtx, schemaContext, verboseClassComments, typeProvider, genTypeBuilders, namespaceType);
347 augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder);
349 if(!augSchema.getChildNodes().isEmpty()) {
350 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
351 genCtx.get(module).addTargetToAugmentation(augTypeBuilder, augSchema.getTargetPath());
353 genCtx.get(module).addAugmentType(augTypeBuilder);
358 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
359 * node for which <code>typeBuilder</code> was created.
363 * @param basePackageName
364 * string contains the module package name
366 * generated type builder which represents any node. The subnodes
367 * of this node are added to the <code>typeBuilder</code> as
368 * methods. The subnode can be of type leaf, leaf-list, list,
373 * set of data schema nodes which are the children of the node
374 * for which <code>typeBuilder</code> was created
375 * @return generated type builder which is the same object as the input
376 * parameter <code>typeBuilder</code>. The getter method could be
379 private static GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
380 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes,
381 final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
382 verboseClassComments, final TypeProvider typeProvider, final Map<String, Map<String,
383 GeneratedTypeBuilder>> genTypeBuilders, final BindingNamespaceType namespaceType) {
384 if (schemaNodes != null && typeBuilder != null) {
385 for (final DataSchemaNode schemaNode : schemaNodes) {
386 if (!schemaNode.isAugmenting()) {
387 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module, genCtx,
388 schemaContext, verboseClassComments, genTypeBuilders, typeProvider, namespaceType);
396 * Instantiates generated type builder with <code>packageName</code> and
397 * <code>schemaNode</code>.
399 * The new builder always implements
400 * {@link TreeNode TreeNode}.<br>
401 * If <code>schemaNode</code> is instance of GroupingDefinition it also
402 * implements {@link Augmentable
404 * If <code>schemaNode</code> is instance of
405 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
406 * DataNodeContainer} it can also implement nodes which are specified in
409 * @param basePackageName
410 * string contains the module package name
412 * schema node for which is created generated type builder
414 * parent type (can be null)
415 * @param schemaContext schema context
416 * @return generated type builder <code>schemaNode</code>
418 private static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String basePackageName, final SchemaNode
419 schemaNode, final Type parent, final Module module, final Map<Module, ModuleContext> genCtx,
420 final SchemaContext schemaContext, final boolean verboseClassComments, final Map<String, Map<String,
421 GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider, final BindingNamespaceType namespaceType) {
424 if (schemaNode instanceof GroupingDefinition) {
426 } else if (namespaceType.equals(BindingNamespaceType.Grouping)) {
430 GeneratedTypeBuilder it = addRawInterfaceDefinition(basePackageName, schemaNode, schemaContext, "", suffix,
431 verboseClassComments, genTypeBuilders, namespaceType);
432 if (namespaceType.equals(BindingNamespaceType.Data)) {
433 if (parent == null) {
434 it.addImplementsType(BindingTypes.TREE_NODE);
436 if (parent instanceof ListSchemaNode) {
437 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
438 (BindingTypes.IDENTIFIABLE_ITEM, parent)));
440 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
441 (BindingTypes.ITEM, parent)));
442 it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
446 if (!(schemaNode instanceof GroupingDefinition)) {
447 it.addImplementsType(BindingTypes.augmentable(it));
450 it.addImplementsType(BindingTypes.TREE_NODE);
453 if (schemaNode instanceof DataNodeContainer) {
454 groupingsToGenTypes(module, ((DataNodeContainer) schemaNode).getGroupings(), genCtx, schemaContext,
455 verboseClassComments, genTypeBuilders, typeProvider);
456 it = addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it, genCtx);
462 static GeneratedTypeBuilder resolveNotification(final GeneratedTypeBuilder listenerInterface, String
463 parentName, final String basePackageName, final NotificationDefinition notification, final Module module,
464 final SchemaContext schemaContext, final boolean verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>>
465 genTypeBuilders, TypeProvider typeProvider, Map<Module, ModuleContext> genCtx) {
467 processUsesAugments(schemaContext, notification, module, genCtx, genTypeBuilders,
468 verboseClassComments, typeProvider, BindingNamespaceType.Data);
470 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition
471 (basePackageName, notification, null, module, genCtx, schemaContext,
472 verboseClassComments, genTypeBuilders, typeProvider, BindingNamespaceType.Data);
473 annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
474 notificationInterface.addImplementsType(NOTIFICATION);
475 genCtx.get(module).addChildNodeType(notification, notificationInterface);
477 // Notification object
478 resolveDataSchemaNodes(module, basePackageName, notificationInterface,
479 notificationInterface, notification.getChildNodes(), genCtx, schemaContext,
480 verboseClassComments, genTypeBuilders, typeProvider, BindingNamespaceType.Data);
482 //in case of tied notification, incorporate parent's localName
483 final StringBuilder sb = new StringBuilder("on_");
484 if (parentName != null) {
485 sb.append(parentName).append('_');
487 sb.append(notificationInterface.getName());
489 listenerInterface.addMethod(JavaIdentifierNormalizer.normalizeSpecificIdentifier(sb.toString(), JavaIdentifier.METHOD))
490 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
491 .setComment(encodeAngleBrackets(notification.getDescription())).setReturnType(Types.VOID);
492 return listenerInterface;
496 * Returns reference to generated type builder for specified
497 * <code>schemaNode</code> with <code>packageName</code>.
499 * Firstly the generated type builder is searched in
500 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
501 * found it is created and added to <code>genTypeBuilders</code>.
503 * @param basePackageName
504 * string contains the module package name
506 * schema node which provide data about the schema node name
507 * @param schemaContext schema context
509 * return type name prefix
510 * @return generated type builder for <code>schemaNode</code>
511 * @throws IllegalArgumentException
513 * <li>if <code>schemaNode</code> is null</li>
514 * <li>if <code>packageName</code> is null</li>
515 * <li>if QName of schema node is null</li>
516 * <li>if schemaNode name is null</li>
520 static GeneratedTypeBuilder addRawInterfaceDefinition(final String basePackageName, final SchemaNode schemaNode,
521 final SchemaContext schemaContext, final String prefix, final String suffix,
522 final boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
523 final BindingNamespaceType namespaceType) {
525 Preconditions.checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
526 Preconditions.checkArgument(basePackageName != null, "Base package Name for Generated Type cannot be NULL.");
527 String schemaNodeName = schemaNode.getQName().getLocalName();
528 Preconditions.checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
530 if (prefix != null && !prefix.isEmpty()) {
531 // underscore used as separator for distinction of class name parts
532 schemaNodeName = new StringBuilder(prefix).append('_').append(schemaNodeName).toString();
535 if (suffix != null && !suffix.isEmpty()) {
536 // underscore used as separator for distinction of class name parts
537 schemaNodeName = new StringBuilder(schemaNodeName).append('_').append(suffix).toString();
540 final String packageName = packageNameForGeneratedType(basePackageName, schemaNode.getPath(), namespaceType);
541 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, schemaNodeName);
542 final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
543 qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
544 newType.addComment(schemaNode.getDescription());
545 newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName(), schemaContext,
546 verboseClassComments, namespaceType));
547 newType.setReference(schemaNode.getReference());
548 newType.setSchemaPath((List<QName>) schemaNode.getPath().getPathFromRoot());
549 newType.setModuleName(module.getName());
550 newType.setBasePackageName(BindingMapping.getRootPackageName(module));
551 newType.setWithBuilder(AuxiliaryGenUtils.hasBuilderClass(schemaNode, namespaceType));
553 if (!genTypeBuilders.containsKey(packageName)) {
554 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
555 builders.put(newType.getName(), newType);
556 genTypeBuilders.put(packageName, builders);
558 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
559 if (!builders.containsKey(newType.getName())) {
560 builders.put(newType.getName(), newType);
567 private static void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
568 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module,
569 final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
570 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider,
571 final BindingNamespaceType namespaceType) {
573 if (node != null && typeBuilder != null) {
574 if (node instanceof ContainerSchemaNode) {
575 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node,
576 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider, namespaceType);
577 } else if (node instanceof LeafListSchemaNode) {
578 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) node, module,
579 typeProvider, genCtx);
580 } else if (node instanceof LeafSchemaNode) {
581 resolveLeafSchemaNodeAsMethod("", schemaContext, typeBuilder, genCtx, (LeafSchemaNode) node, module,
583 } else if (node instanceof ListSchemaNode) {
584 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node, schemaContext,
585 verboseClassComments, genCtx, genTypeBuilders, typeProvider, namespaceType);
586 } else if (node instanceof ChoiceSchemaNode) {
587 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, childOf,
588 (ChoiceSchemaNode) node, genTypeBuilders, genCtx, typeProvider, namespaceType);
589 } else if (node instanceof AnyXmlSchemaNode || node instanceof AnyDataSchemaNode) {
590 resolveAnyNodeAsMethod(schemaContext, typeBuilder, genCtx, node, module, typeProvider);
596 * Converts <code>choiceNode</code> to the list of generated types for
597 * choice and its cases.
599 * The package names for choice and for its cases are created as
600 * concatenation of the module package (<code>basePackageName</code>) and
601 * names of all parents node.
605 * @param basePackageName
606 * string with the module package name
610 * choice node which is mapped to generated type. Also child
611 * nodes - cases are mapped to generated types.
612 * @throws IllegalArgumentException
614 * <li>if <code>basePackageName</code> is null</li>
615 * <li>if <code>choiceNode</code> is null</li>
618 private static void choiceToGenType(final Module module, final SchemaContext schemaContext, final boolean
619 verboseClasssComments, final String basePackageName, final GeneratedTypeBuilder parent, final
620 ChoiceSchemaNode choiceNode, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
621 final Map<Module, ModuleContext> genCtx, final TypeProvider typeProvider, final BindingNamespaceType namespaceType) {
622 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
623 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
625 if (!choiceNode.isAddedByUses()) {
626 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(basePackageName, choiceNode,
627 schemaContext, "", "", verboseClasssComments, genTypeBuilders, namespaceType);
628 constructGetter(parent, choiceNode.getQName().getLocalName(),
629 choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus());
630 choiceTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, choiceTypeBuilder));
631 annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
632 genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
633 generateTypesFromChoiceCases(module, schemaContext, genCtx, basePackageName, choiceTypeBuilder.toInstance(),
634 choiceNode, verboseClasssComments, typeProvider, genTypeBuilders, namespaceType);
638 private static void containerToGenType(final Module module, final String basePackageName,
639 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node,
640 final SchemaContext schemaContext, final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
641 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider,
642 final BindingNamespaceType namespaceType) {
644 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
645 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider, namespaceType);
646 if (genType != null) {
647 StringBuilder getterName = new StringBuilder(node.getQName().getLocalName());
648 if (!namespaceType.equals(BindingNamespaceType.Data)) {
649 getterName.append('_').append(BindingNamespaceType.Data);
651 final MethodSignatureBuilder getter = constructGetter(parent, getterName.toString(), node.getDescription(), genType, node.getStatus());
652 if (!namespaceType.equals(BindingNamespaceType.Data)) {
653 getter.setAccessModifier(AccessModifier.DEFAULT);
655 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes(), genCtx,
656 schemaContext, verboseClassComments, genTypeBuilders, typeProvider, namespaceType);
660 private static void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder
661 parent, final GeneratedTypeBuilder childOf, final ListSchemaNode node, final SchemaContext schemaContext,
662 final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
663 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider,
664 final BindingNamespaceType namespaceType) {
666 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
667 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider, namespaceType);
668 if (genType != null) {
669 final String nodeName = node.getQName().getLocalName();
670 constructGetter(parent, nodeName, node.getDescription(), Types.listTypeFor(genType), node.getStatus());
671 final List<QName> listKeys = node.getKeyDefinition();
672 final String packageName = new StringBuilder(packageNameForGeneratedType(basePackageName, node.getPath(),
673 BindingNamespaceType.Key)).append('.').append(nodeName).toString();
675 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
677 for (final DataSchemaNode schemaNode : node.getChildNodes()) {
678 if (!schemaNode.isAugmenting()) {
679 addSchemaNodeToListBuilders(nodeName, basePackageName, schemaNode, genType, genTOBuilder, listKeys,
680 module, typeProvider, schemaContext, genCtx, genTypeBuilders, verboseClassComments, namespaceType);
685 if (genTOBuilder != null) {
686 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
687 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
688 genTOBuilder.setSUID(prop);
691 typeBuildersToGenTypes(module, genType, genTOBuilder, genCtx);
695 private static void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder,
696 final GeneratedTOBuilder genTOBuilder, final Map<Module, ModuleContext> genCtx) {
697 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
698 if (genTOBuilder != null) {
699 final GeneratedTransferObject genTO = genTOBuilder.toInstance();
700 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO, Status.CURRENT);
701 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
706 * Converts <code>leaf</code> to the getter method which is added to
707 * <code>typeBuilder</code>.
710 * generated type builder to which is added getter method as
711 * <code>leaf</code> mapping
713 * leaf schema node which is mapped as getter method which is
714 * added to <code>typeBuilder</code>
716 * Module in which type was defined
717 * @return boolean value
719 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
721 * <li>true - in other cases</li>
724 private static Type resolveLeafSchemaNodeAsMethod(final String nodeName, final SchemaContext schemaContext,
725 final GeneratedTypeBuilder typeBuilder, final Map<Module, ModuleContext> genCtx, final LeafSchemaNode leaf,
726 final Module module, final TypeProvider typeProvider) {
727 if (leaf == null || typeBuilder == null) {
731 final String leafName = leaf.getQName().getLocalName();
732 if (leafName == null) {
736 final Module parentModule = findParentModule(schemaContext, leaf);
737 Type returnType = null;
739 final TypeDefinition<?> typeDef = leaf.getType();
740 if (isInnerType(leaf, typeDef)) {
741 if (typeDef instanceof EnumTypeDefinition) {
742 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
743 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
744 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
745 genCtx, typeBuilder, module);
746 if (enumBuilder != null) {
747 returnType = enumBuilder.toInstance(typeBuilder);
749 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
750 } else if (typeDef instanceof UnionTypeDefinition) {
751 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
752 typeProvider, schemaContext);
753 if (genTOBuilder != null) {
754 //TODO: https://bugs.opendaylight.org/show_bug.cgi?id=2289
755 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
757 } else if (typeDef instanceof BitsTypeDefinition) {
758 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
759 typeProvider, schemaContext);
760 if (genTOBuilder != null) {
761 returnType = genTOBuilder.toInstance();
764 // It is constrained version of already declared type (inner declared type exists,
765 // onlyfor special cases (Enum, Union, Bits), which were already checked.
766 // In order to get proper class we need to look up closest derived type
767 // and apply restrictions from leaf type
768 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
769 returnType = typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
773 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
774 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
777 if (returnType == null) {
781 if (typeDef instanceof EnumTypeDefinition) {
782 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
785 String leafDesc = leaf.getDescription();
786 if (leafDesc == null) {
790 final String leafGetterName;
791 if (!"".equals(nodeName)) {
792 StringBuilder sb = new StringBuilder(nodeName)
795 leafGetterName = sb.toString();
797 leafGetterName = leafName;
800 constructGetter(typeBuilder, leafGetterName, leafDesc, returnType, leaf.getStatus());
805 * Converts <code>node</code> leaf list schema node to getter method of
806 * <code>typeBuilder</code>.
809 * generated type builder to which is <code>node</code> added as
812 * leaf list schema node which is added to
813 * <code>typeBuilder</code> as getter method
814 * @param module module
815 * @param typeProvider type provider instance
816 * @param genCtx actual generated context
817 * @return boolean value
819 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
820 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
821 * <li>false - other cases</li>
824 private static boolean resolveLeafListSchemaNode(final SchemaContext schemaContext, final GeneratedTypeBuilder
825 typeBuilder, final LeafListSchemaNode node, final Module module, final TypeProvider typeProvider,
826 final Map<Module, ModuleContext> genCtx) {
827 if (node == null || typeBuilder == null || node.isAddedByUses()) {
831 final QName nodeName = node.getQName();
833 final TypeDefinition<?> typeDef = node.getType();
834 final Module parentModule = findParentModule(schemaContext, node);
836 Type returnType = null;
837 if (typeDef.getBaseType() == null) {
838 if (typeDef instanceof EnumTypeDefinition) {
839 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
840 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
841 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
842 genCtx, typeBuilder, module);
843 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
844 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
845 } else if (typeDef instanceof UnionTypeDefinition) {
846 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
847 typeProvider, schemaContext);
848 if (genTOBuilder != null) {
849 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
851 } else if (typeDef instanceof BitsTypeDefinition) {
852 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
853 typeProvider, schemaContext);
854 returnType = genTOBuilder.toInstance();
856 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
857 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
860 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
861 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
864 final ParameterizedType listType = Types.listTypeFor(returnType);
865 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType, node.getStatus());
870 * Converts <code>caseNodes</code> set to list of corresponding generated
873 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
874 * created generated type builder. The package names for the builder is
875 * created as concatenation of the module package (
876 * <code>basePackageName</code>) and names of all parents nodes of the
877 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
878 * between every case builder and <i>choice</i> type
882 * @param schemaContext
883 * current schema context
885 * actual generated context
886 * @param basePackageName
887 * string with the module package name
888 * @param refChoiceType
889 * type which represents superior <i>case</i>
891 * choice case node which is mapped to generated type
892 * @param verboseClassComments
893 * Javadoc verbosity switch
894 * @throws IllegalArgumentException
896 * <li>if <code>basePackageName</code> equals null</li>
897 * <li>if <code>refChoiceType</code> equals null</li>
898 * <li>if <code>caseNodes</code> equals null</li>
901 private static void generateTypesFromChoiceCases(final Module module, final SchemaContext schemaContext,
902 final Map<Module, ModuleContext> genCtx, final String basePackageName, final Type refChoiceType,
903 final ChoiceSchemaNode choiceNode, final boolean verboseClassComments, final TypeProvider typeProvider,
904 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final BindingNamespaceType namespaceType) {
905 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
906 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
907 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
909 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
910 if (caseNodes == null) {
914 for (final ChoiceCaseNode caseNode : caseNodes) {
915 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
916 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(basePackageName, caseNode,
917 module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider, namespaceType);
918 caseTypeBuilder.addImplementsType(refChoiceType);
919 caseTypeBuilder.setParentTypeForBuilder(refChoiceType);
920 annotateDeprecatedIfNecessary(caseNode.getStatus(), caseTypeBuilder);
921 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
922 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
923 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
924 if (caseChildNodes != null) {
925 final SchemaPath choiceNodeParentPath = choiceNode.getPath().getParent();
927 if (!Iterables.isEmpty(choiceNodeParentPath.getPathFromRoot())) {
928 SchemaNode parent = findDataSchemaNode(schemaContext, choiceNodeParentPath);
930 if (parent instanceof AugmentationSchema) {
931 final AugmentationSchema augSchema = (AugmentationSchema) parent;
932 final SchemaPath targetPath = augSchema.getTargetPath();
933 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
934 if (targetSchemaNode instanceof DataSchemaNode
935 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
936 if (targetSchemaNode instanceof DerivableSchemaNode) {
937 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
939 if (targetSchemaNode == null) {
940 throw new IllegalStateException(
941 "Failed to find target node from grouping for augmentation " + augSchema
942 + " in module " + module.getName());
945 parent = targetSchemaNode;
948 Preconditions.checkState(parent != null, "Could not find Choice node parent %s",
949 choiceNodeParentPath);
950 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath(), genCtx);
951 if (childOfType == null) {
952 childOfType = findGroupingByPath(parent.getPath(), genCtx);
954 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes,
955 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider, namespaceType);
957 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, moduleToDataType(module,
958 genCtx, verboseClassComments), caseChildNodes, genCtx, schemaContext,
959 verboseClassComments, genTypeBuilders, typeProvider, namespaceType);
963 processUsesAugments(schemaContext, caseNode, module, genCtx, genTypeBuilders, verboseClassComments,
964 typeProvider, namespaceType);
968 private static Type resolveAnyNodeAsMethod(final SchemaContext schemaContext, final GeneratedTypeBuilder
969 typeBuilder, final Map<Module, ModuleContext> genCtx, final DataSchemaNode node, final Module module,
970 final TypeProvider typeProvider) {
972 final String anyName = node.getQName().getLocalName();
973 if (anyName == null) {
977 String anyDesc = node.getDescription();
978 if (anyDesc == null) {
982 Type returnType = Types.DOCUMENT;
984 constructGetter(typeBuilder, anyName, anyDesc, returnType, node.getStatus());
989 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
990 * or to <code>genTOBuilder</code> as property.
993 * string contains the name of list
994 * @param basePackageName
995 * string contains the module package name
997 * data schema node which should be added as getter method to
998 * <code>typeBuilder</code> or as a property to
999 * <code>genTOBuilder</code> if is part of the list key
1000 * @param typeBuilder
1001 * generated type builder for the list schema node
1002 * @param genTOBuilder
1003 * generated TO builder for the list keys
1005 * list of string which contains QNames of the list keys
1008 * @param typeProvider
1009 * provider that defines contract for generated types
1010 * @param schemaContext
1013 * map of generated entities in context of YANG modules
1014 * @param genTypeBuilders
1015 * map of generated type builders
1016 * @param verboseClassComments
1017 * generate verbose comments
1018 * @throws IllegalArgumentException
1020 * <li>if <code>schemaNode</code> equals null</li>
1021 * <li>if <code>typeBuilder</code> equals null</li>
1024 private static void addSchemaNodeToListBuilders(final String nodeName, final String basePackageName,
1025 final DataSchemaNode schemaNode, final GeneratedTypeBuilder typeBuilder,
1026 final GeneratedTOBuilder genTOBuilder, final List<QName> listKeys, final Module module,
1027 final TypeProvider typeProvider, final SchemaContext schemaContext, final Map<Module, ModuleContext> genCtx,
1028 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final boolean verboseClassComments,
1029 final BindingNamespaceType namespaceType) {
1030 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
1031 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
1033 if (schemaNode instanceof LeafSchemaNode) {
1034 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
1035 final QName leafQName = leaf.getQName();
1037 final Type type = resolveLeafSchemaNodeAsMethod(nodeName, schemaContext, typeBuilder, genCtx, leaf, module,
1039 if (listKeys.contains(leafQName)) {
1041 resolveLeafSchemaNodeAsProperty(schemaContext, typeProvider, genCtx, genTOBuilder, leaf, true,
1044 AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1047 } else if (!schemaNode.isAddedByUses()) {
1048 if (schemaNode instanceof LeafListSchemaNode) {
1049 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) schemaNode, module,
1050 typeProvider, genCtx);
1051 } else if (schemaNode instanceof ContainerSchemaNode) {
1052 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode,
1053 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider, namespaceType);
1054 } else if (schemaNode instanceof ListSchemaNode) {
1055 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode,
1056 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider, namespaceType);
1057 } else if (schemaNode instanceof ChoiceSchemaNode) {
1058 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, typeBuilder,
1059 (ChoiceSchemaNode) schemaNode, genTypeBuilders, genCtx, typeProvider, namespaceType);
1064 private static boolean resolveLeafSchemaNodeAsProperty(final SchemaContext schemaContext, final TypeProvider
1065 typeProvider, final Map<Module, ModuleContext> genCtx, final GeneratedTOBuilder
1066 toBuilder, final LeafSchemaNode leaf, final boolean isReadOnly, final Module module) {
1068 if (leaf != null && toBuilder != null) {
1070 final TypeDefinition<?> typeDef = leaf.getType();
1071 if (typeDef instanceof UnionTypeDefinition) {
1072 // GeneratedType for this type definition should be already
1074 final QName qname = typeDef.getQName();
1075 final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1076 qname.getRevision());
1077 final ModuleContext mc = genCtx.get(unionModule);
1078 returnType = mc.getTypedefs().get(typeDef.getPath());
1079 } else if (typeDef instanceof EnumTypeDefinition && typeDef.getBaseType() == null) {
1080 // Annonymous enumeration (already generated, since it is inherited via uses).
1081 LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
1082 QName qname = originalLeaf.getQName();
1083 final Module enumModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1084 qname.getRevision());
1085 returnType = genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
1087 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1089 return AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1094 private static TypeDefinition<?> getBaseOrDeclaredType(final TypeDefinition<?> typeDef) {
1095 final TypeDefinition<?> baseType = typeDef.getBaseType();
1096 return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef;
1099 @SuppressWarnings({ "rawtypes", "unchecked" })
1100 private static GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
1101 final GeneratedTypeBuilder childOf, final DataSchemaNode node, final SchemaContext schemaContext,
1102 final boolean verboseClassComments, Map<Module, ModuleContext> genCtx, final Map<String, Map<String,
1103 GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider, final BindingNamespaceType namespaceType) {
1105 if (node.isAugmenting()) {
1109 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(basePackageName, node, childOf, module,
1110 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider, namespaceType);
1111 genType.addComment(node.getDescription());
1112 annotateDeprecatedIfNecessary(node.getStatus(), genType);
1113 genType.setDescription(createDescription(node, genType.getFullyQualifiedName(), schemaContext,
1114 verboseClassComments, namespaceType));
1115 genType.setModuleName(module.getName());
1116 genType.setReference(node.getReference());
1117 genType.setSchemaPath((List) node.getPath().getPathFromRoot());
1118 genType.setParentTypeForBuilder(childOf);
1119 if (node instanceof DataNodeContainer) {
1120 genCtx.get(module).addChildNodeType(node, genType);
1121 genCtx = groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings(), genCtx, schemaContext,
1122 verboseClassComments, genTypeBuilders, typeProvider);
1123 processUsesAugments(schemaContext, (DataNodeContainer) node, module, genCtx, genTypeBuilders,
1124 verboseClassComments, typeProvider, namespaceType);
1130 * Converts all <b>groupings</b> of the module to the list of
1131 * <code>Type</code> objects. Firstly are groupings sorted according mutual
1132 * dependencies. At least dependent (independent) groupings are in the list
1133 * saved at first positions. For every grouping the record is added to map
1134 * {@link ModuleContext#groupings allGroupings}
1139 * collection of groupings from which types will be generated
1140 * @param typeProvider
1141 * provider that defines contract for generated types
1142 * @param schemaContext
1145 * map of generated entities in context of YANG modules
1146 * @param genTypeBuilders
1147 * map of generated type builders
1148 * @param verboseClassComments
1149 * generate verbose comments
1152 static Map<Module, ModuleContext> groupingsToGenTypes(final Module module, final Collection<GroupingDefinition>
1153 groupings, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1154 verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1155 final String basePackageName = BindingMapping.getRootPackageName(module);
1156 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
1158 for (final GroupingDefinition grouping : groupingsSortedByDependencies) {
1159 genCtx = groupingToGenType(basePackageName, grouping, module, genCtx, schemaContext,
1160 verboseClassComments, genTypeBuilders, typeProvider);
1166 * Converts individual grouping to GeneratedType. Firstly generated type
1167 * builder is created and every child node of grouping is resolved to the
1170 * @param basePackageName
1171 * string contains the module package name
1173 * GroupingDefinition which contains data about grouping
1176 * @param typeProvider
1177 * provider that defines contract for generated types
1178 * @param schemaContext
1181 * map of generated entities in context of YANG modules
1182 * @param genTypeBuilders
1183 * map of generated type builders
1184 * @param verboseClassComments
1185 * generate verbose comments
1187 * @return GeneratedType which is generated from grouping (object of type
1188 * <code>GroupingDefinition</code>)
1190 private static Map<Module, ModuleContext> groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module
1191 module, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1192 verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1193 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(basePackageName, grouping, module, genCtx,
1194 schemaContext, verboseClassComments, genTypeBuilders, typeProvider, BindingNamespaceType.Grouping);
1195 annotateDeprecatedIfNecessary(grouping.getStatus(), genType);
1196 genCtx.get(module).addGroupingType(grouping, genType);
1197 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes(), genCtx,
1198 schemaContext, verboseClassComments, genTypeBuilders, typeProvider, BindingNamespaceType.Grouping);
1199 genCtx = groupingsToGenTypes(module, grouping.getGroupings(), genCtx, schemaContext, verboseClassComments,
1200 genTypeBuilders, typeProvider);
1201 genCtx = processUsesAugments(schemaContext, grouping, module, genCtx, genTypeBuilders, verboseClassComments,
1202 typeProvider, BindingNamespaceType.Grouping);
1207 * //TODO: add information about multiple base identities in YANG 1.1
1208 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
1209 * created transport object builder. If identity contains base identity then
1210 * reference to base identity is added to superior identity as its extend.
1211 * If identity doesn't contain base identity then only reference to abstract
1212 * class {@link org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode
1213 * BaseIdentity} is added
1217 * @param basePackageName
1218 * string contains the module package name
1220 * IdentitySchemaNode which contains data about identity
1221 * @param schemaContext
1222 * SchemaContext which is used to get package and name
1223 * information about base of identity
1224 * @param genCtx generated context
1225 * @return returns generated context
1227 static Map<Module, ModuleContext> identityToGenType(final Module module, final String basePackageName,
1228 final IdentitySchemaNode identity, final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx,
1229 boolean verboseClassComments) {
1231 resolveIdentitySchemaNode(basePackageName, schemaContext, identity, module, verboseClassComments, genCtx);
1235 private static GeneratedTOBuilder resolveIdentitySchemaNode(final String basePackageName, final SchemaContext schemaContext,
1236 final IdentitySchemaNode identity, final Module module, final boolean verboseClassComments,
1237 final Map<Module, ModuleContext> genCtx) {
1238 Preconditions.checkNotNull(identity,"Identity can not be null!");
1240 //check first if identity has been resolved as base identity of some other one
1241 GeneratedTOBuilder newType = findIdentityByQname(identity.getQName(), genCtx);
1242 if (newType == null) {
1243 final Module parentModule = SchemaContextUtil.findParentModule(schemaContext, identity);
1244 Preconditions.checkState(module.equals(parentModule),
1245 "If the type is null ,it must be in the same module, otherwise it must has been"
1246 + "resolved by an imported module.");
1248 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath(),
1249 BindingNamespaceType.Identity);
1250 newType = new GeneratedTOBuilderImpl(packageName, identity.getQName().getLocalName(), true, false);
1252 final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
1253 if (baseIdentities.size() == 0) {
1254 //no base - abstract
1255 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
1256 BaseIdentity.class.getSimpleName());
1257 newType.setExtendsType(gto.toInstance());
1259 //one base - inheritance
1260 final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
1261 GeneratedTOBuilder baseType = resolveIdentitySchemaNode(basePackageName, schemaContext,
1262 baseIdentity, module, verboseClassComments, genCtx);
1263 newType.setExtendsType(baseType.toInstance());
1266 newType.setAbstract(true);
1267 newType.addComment(identity.getDescription());
1268 newType.setDescription(createDescription(identity, newType.getFullyQualifiedName(), schemaContext,
1269 verboseClassComments, BindingNamespaceType.Identity));
1270 newType.setReference(identity.getReference());
1271 newType.setModuleName(module.getName());
1272 newType.setSchemaPath((List) identity.getPath().getPathFromRoot());
1274 qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
1276 genCtx.get(module).addIdentityType(identity.getQName(), newType);