X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fgenerator%2Fimpl%2FAbstractTypeGenerator.java;h=c04647d62ee459f382300bac36be88a63133e908;hb=f04f0b4634e4c26f396771bb870e8d7c919b78a3;hp=fd242571c98be016cd93b538cd86cf9ce8be6c7c;hpb=0f351bbc28ddf2cddfe30c8d018646d81953fa17;p=mdsal.git diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java index fd242571c9..c04647d62e 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java @@ -19,34 +19,44 @@ import static org.opendaylight.mdsal.binding.model.util.BindingTypes.DATA_OBJECT import static org.opendaylight.mdsal.binding.model.util.BindingTypes.DATA_ROOT; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.NOTIFICATION; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.NOTIFICATION_LISTENER; +import static org.opendaylight.mdsal.binding.model.util.BindingTypes.QNAME; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.ROUTING_CONTEXT; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.RPC_INPUT; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.RPC_OUTPUT; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.RPC_SERVICE; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.action; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.augmentable; +import static org.opendaylight.mdsal.binding.model.util.BindingTypes.augmentation; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.childOf; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.choiceIn; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.identifiable; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.identifier; +import static org.opendaylight.mdsal.binding.model.util.BindingTypes.instanceNotification; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.keyedListAction; +import static org.opendaylight.mdsal.binding.model.util.BindingTypes.keyedListNotification; +import static org.opendaylight.mdsal.binding.model.util.BindingTypes.opaqueObject; import static org.opendaylight.mdsal.binding.model.util.BindingTypes.rpcResult; import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN; +import static org.opendaylight.mdsal.binding.model.util.Types.STRING; +import static org.opendaylight.mdsal.binding.model.util.Types.classType; import static org.opendaylight.mdsal.binding.model.util.Types.listTypeFor; import static org.opendaylight.mdsal.binding.model.util.Types.listenableFutureTypeFor; -import static org.opendaylight.mdsal.binding.model.util.Types.typeForClass; +import static org.opendaylight.mdsal.binding.model.util.Types.primitiveVoidType; +import static org.opendaylight.mdsal.binding.model.util.Types.wildcardTypeFor; import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode; import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext; import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; -import com.google.common.collect.Sets; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -55,6 +65,7 @@ import java.util.Set; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.binding.model.api.AccessModifier; import org.opendaylight.mdsal.binding.model.api.Constant; +import org.opendaylight.mdsal.binding.model.api.Enumeration; import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject; import org.opendaylight.mdsal.binding.model.api.GeneratedType; import org.opendaylight.mdsal.binding.model.api.JavaTypeName; @@ -73,7 +84,6 @@ import org.opendaylight.mdsal.binding.model.api.type.builder.TypeMemberBuilder; import org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil; import org.opendaylight.mdsal.binding.model.util.ReferencedTypeImpl; import org.opendaylight.mdsal.binding.model.util.TypeConstants; -import org.opendaylight.mdsal.binding.model.util.Types; import org.opendaylight.mdsal.binding.model.util.generated.type.builder.GeneratedPropertyBuilderImpl; import org.opendaylight.mdsal.binding.spec.naming.BindingMapping; import org.opendaylight.mdsal.binding.yang.types.AbstractTypeProvider; @@ -83,6 +93,8 @@ import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.ActionDefinition; import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer; +import org.opendaylight.yangtools.yang.model.api.AnyDataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode; import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; @@ -91,6 +103,7 @@ import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode; import org.opendaylight.yangtools.yang.model.api.DocumentedNode; +import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus; import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; @@ -99,11 +112,11 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.ModuleImport; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; +import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer; import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.UsesNode; @@ -111,7 +124,6 @@ import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.DataNodeIterator; import org.opendaylight.yangtools.yang.model.util.ModuleDependencySort; import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils; import org.opendaylight.yangtools.yang.model.util.type.CompatUtils; @@ -119,7 +131,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; abstract class AbstractTypeGenerator { - private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class); + private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeGenerator.class); private static final Splitter COLON_SPLITTER = Splitter.on(':'); private static final JavaTypeName DEPRECATED_ANNOTATION = JavaTypeName.create(Deprecated.class); private static final JavaTypeName OVERRIDE_ANNOTATION = JavaTypeName.create(Override.class); @@ -159,10 +171,9 @@ abstract class AbstractTypeGenerator { private final Map genCtx = new HashMap<>(); /** - * Outer key represents the package name. Outer value represents map of all - * builders in the same package. Inner key represents the schema node name - * (in JAVA class/interface name format). Inner value represents instance of - * builder for schema node specified in key part. + * Outer key represents the package name. Outer value represents map of all builders in the same package. Inner key + * represents the schema node name (in JAVA class/interface name format). Inner value represents instance of builder + * for schema node specified in key part. */ private final Map> genTypeBuilders = new HashMap<>(); @@ -172,8 +183,7 @@ abstract class AbstractTypeGenerator { private final AbstractTypeProvider typeProvider; /** - * Holds reference to schema context to resolve data of augmented element - * when creating augmentation builder + * Holds reference to schema context to resolve data of augmented element when creating augmentation builder. */ private final SchemaContext schemaContext; @@ -223,15 +233,17 @@ abstract class AbstractTypeGenerator { genCtx.put(module.getQNameModule(), context); allTypeDefinitionsToGenTypes(context); groupingsToGenTypes(context, module.getGroupings()); - rpcMethodsToGenType(context); allIdentitiesToGenTypes(context); - notificationsToGenType(context); if (!module.getChildNodes().isEmpty()) { final GeneratedTypeBuilder moduleType = moduleToDataType(context); context.addModuleNode(moduleType); - resolveDataSchemaNodes(context, moduleType, moduleType, module.getChildNodes()); + resolveDataSchemaNodes(context, moduleType, moduleType, module.getChildNodes(), false); } + + // Resolve RPCs and notifications only after we have created instantiated tree + rpcMethodsToGenType(context); + notificationsToGenType(context); return context; } @@ -252,11 +264,8 @@ abstract class AbstractTypeGenerator { private void allTypeDefinitionsToGenTypes(final ModuleContext context) { final Module module = context.module(); checkArgument(module.getName() != null, "Module name cannot be NULL."); - final DataNodeIterator it = new DataNodeIterator(module); - final List> typeDefinitions = it.allTypedefs(); - checkState(typeDefinitions != null, "Type Definitions for module %s cannot be NULL.", module.getName()); - for (final TypeDefinition typedef : typeDefinitions) { + for (final TypeDefinition typedef : SchemaNodeUtils.getAllTypeDefinitions(module)) { if (typedef != null) { final Type type = typeProvider.generatedTypeForExtendedDefinitionType(typedef, typedef); if (type != null) { @@ -268,12 +277,13 @@ abstract class AbstractTypeGenerator { } private GeneratedTypeBuilder processDataSchemaNode(final ModuleContext context, final Type baseInterface, - final DataSchemaNode node) { + final DataSchemaNode node, final boolean inGrouping) { if (node.isAugmenting() || node.isAddedByUses()) { return null; } final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(context, node, baseInterface); - annotateDeprecatedIfNecessary(node.getStatus(), genType); + defaultImplementedInterace(genType); + annotateDeprecatedIfNecessary(node, genType); final Module module = context.module(); genType.setModuleName(module.getName()); @@ -282,60 +292,68 @@ abstract class AbstractTypeGenerator { if (node instanceof DataNodeContainer) { context.addChildNodeType(node, genType); groupingsToGenTypes(context, ((DataNodeContainer) node).getGroupings()); - processUsesAugments((DataNodeContainer) node, context); + processUsesAugments((DataNodeContainer) node, context, inGrouping); } return genType; } private void containerToGenType(final ModuleContext context, final GeneratedTypeBuilder parent, - final Type baseInterface, final ContainerSchemaNode node) { - final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, node); + final Type baseInterface, final ContainerSchemaNode node, final boolean inGrouping) { + final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, node, inGrouping); if (genType != null) { constructGetter(parent, genType, node); - resolveDataSchemaNodes(context, genType, genType, node.getChildNodes()); - actionsToGenType(context, genType, node, null); + resolveDataSchemaNodes(context, genType, genType, node.getChildNodes(), inGrouping); + actionsToGenType(context, genType, node, null, inGrouping); + notificationsToGenType(context, genType, node, null, inGrouping); } } private void listToGenType(final ModuleContext context, final GeneratedTypeBuilder parent, - final Type baseInterface, final ListSchemaNode node) { - final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, node); + final Type baseInterface, final ListSchemaNode node, final boolean inGrouping) { + final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, node, inGrouping); if (genType != null) { - constructGetter(parent, listTypeFor(genType), node); - final List listKeys = listKeys(node); - final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(context, node); - if (genTOBuilder != null) { - final Type identifierMarker = identifier(genType); - final Type identifiableMarker = identifiable(genTOBuilder); - genTOBuilder.addImplementsType(identifierMarker); - genType.addImplementsType(identifiableMarker); - + final GeneratedTOBuilder keyTypeBuilder; + if (!listKeys.isEmpty()) { + keyTypeBuilder = typeProvider.newGeneratedTOBuilder(JavaTypeName.create( + packageNameForGeneratedType(context.modulePackageName(), node.getPath()), + BindingMapping.getClassName(node.getQName().getLocalName() + "Key"))) + .addImplementsType(identifier(genType)); + genType.addImplementsType(identifiable(keyTypeBuilder)); + } else { + keyTypeBuilder = null; } - actionsToGenType(context, genType, node, genTOBuilder); + + final ParameterizedType listType = listTypeFor(genType); + constructGetter(parent, listType, node); + constructNonnull(parent, listType, node); + + actionsToGenType(context, genType, node, keyTypeBuilder, inGrouping); + notificationsToGenType(context, genType, node, keyTypeBuilder, inGrouping); for (final DataSchemaNode schemaNode : node.getChildNodes()) { if (!schemaNode.isAugmenting()) { - addSchemaNodeToListBuilders(context, schemaNode, genType, genTOBuilder, listKeys); + addSchemaNodeToListBuilders(context, schemaNode, genType, keyTypeBuilder, listKeys, inGrouping); } } // serialVersionUID - if (genTOBuilder != null) { + if (keyTypeBuilder != null) { final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID"); - prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder))); - genTOBuilder.setSUID(prop); + prop.setValue(Long.toString(computeDefaultSUID(keyTypeBuilder))); + keyTypeBuilder.setSUID(prop); } - typeBuildersToGenTypes(context, genType, genTOBuilder); + typeBuildersToGenTypes(context, genType, keyTypeBuilder); } } - private void processUsesAugments(final DataNodeContainer node, final ModuleContext context) { + private void processUsesAugments(final DataNodeContainer node, final ModuleContext context, + final boolean inGrouping) { for (final UsesNode usesNode : node.getUses()) { for (final AugmentationSchemaNode augment : usesNode.getAugmentations()) { - usesAugmentationToGenTypes(context, augment, usesNode, node); - processUsesAugments(augment, context); + usesAugmentationToGenTypes(context, augment, usesNode, node, inGrouping); + processUsesAugments(augment, context, inGrouping); } } } @@ -412,17 +430,24 @@ abstract class AbstractTypeGenerator { } private void actionsToGenType(final ModuleContext context, - final Type parent, final T parentSchema, final Type keyType) { + final Type parent, final T parentSchema, final Type keyType, final boolean inGrouping) { for (final ActionDefinition action : parentSchema.getActions()) { + if (action.isAugmenting()) { + continue; + } + final GeneratedType input; final GeneratedType output; if (action.isAddedByUses()) { final ActionDefinition orig = findOrigAction(parentSchema, action).get(); - input = context.getChildNode(orig.getInput().getPath()).build(); - output = context.getChildNode(orig.getOutput().getPath()).build(); + // Original definition may live in a different module, make sure we account for that + final ModuleContext origContext = moduleContext( + orig.getPath().getPathFromRoot().iterator().next().getModule()); + input = context.addAliasType(origContext, orig.getInput(), action.getInput()); + output = context.addAliasType(origContext, orig.getOutput(), action.getOutput()); } else { - input = actionContainer(context, RPC_INPUT, action.getInput()); - output = actionContainer(context, RPC_OUTPUT, action.getOutput()); + input = actionContainer(context, RPC_INPUT, action.getInput(), inGrouping); + output = actionContainer(context, RPC_OUTPUT, action.getOutput(), inGrouping); } if (!(parentSchema instanceof GroupingDefinition)) { @@ -433,10 +458,9 @@ abstract class AbstractTypeGenerator { final GeneratedTypeBuilder builder = typeProvider.newGeneratedTypeBuilder(JavaTypeName.create( packageNameForGeneratedType(context.modulePackageName(), action.getPath()), BindingMapping.getClassName(qname))); - qnameConstant(builder, JavaTypeName.create(context.modulePackageName(), - BindingMapping.MODULE_INFO_CLASS_NAME), qname.getLocalName()); + qnameConstant(builder, context.moduleInfoType(), qname.getLocalName()); - annotateDeprecatedIfNecessary(action.getStatus(), builder); + annotateDeprecatedIfNecessary(action, builder); builder.addImplementsType(keyType != null ? keyedListAction(parent, keyType, input, output) : action(parent, input, output)); @@ -447,10 +471,12 @@ abstract class AbstractTypeGenerator { } private Optional findOrigAction(final DataNodeContainer parent, final ActionDefinition action) { + final QName qname = action.getQName(); for (UsesNode uses : parent.getUses()) { final GroupingDefinition grp = findUsedGrouping(uses); - final Optional found = grp.getActions().stream() - .filter(act -> action.getQName().equals(act.getQName())).findFirst(); + // Target grouping may reside in a different module, hence we need to rebind the QName to match grouping's + // namespace + final Optional found = grp.findAction(qname.withModule(grp.getQName().getModule())); if (found.isPresent()) { final ActionDefinition result = found.get(); return result.isAddedByUses() ? findOrigAction(grp, result) : found; @@ -461,9 +487,9 @@ abstract class AbstractTypeGenerator { } private GeneratedType actionContainer(final ModuleContext context, final Type baseInterface, - final ContainerSchemaNode schema) { - final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, schema); - resolveDataSchemaNodes(context, genType, genType, schema.getChildNodes()); + final ContainerSchemaNode schema, final boolean inGrouping) { + final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, schema, inGrouping); + resolveDataSchemaNodes(context, genType, genType, schema.getChildNodes(), inGrouping); return genType.build(); } @@ -500,11 +526,11 @@ abstract class AbstractTypeGenerator { for (final RpcDefinition rpc : rpcDefinitions) { if (rpc != null) { final String rpcName = BindingMapping.getClassName(rpc.getQName()); - final String rpcMethodName = BindingMapping.getPropertyName(rpcName); + final String rpcMethodName = BindingMapping.getRpcMethodName(rpc.getQName()); final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName); // Do not refer to annotation class, as it may not be available at runtime - method.addAnnotation("javax.annotation", "CheckReturnValue"); + method.addAnnotation("edu.umd.cs.findbugs.annotations", "CheckReturnValue"); addComment(method, rpc); method.addParameter( createRpcContainer(context, rpcName, rpc, verifyNotNull(rpc.getInput()), RPC_INPUT), "input"); @@ -518,15 +544,16 @@ abstract class AbstractTypeGenerator { private Type createRpcContainer(final ModuleContext context, final String rpcName, final RpcDefinition rpc, final ContainerSchemaNode schema, final Type type) { - processUsesAugments(schema, context); - final GeneratedTypeBuilder outType = addRawInterfaceDefinition( + processUsesAugments(schema, context, false); + final GeneratedTypeBuilder outType = addRawInterfaceDefinition(context, JavaTypeName.create(context.modulePackageName(), rpcName + BindingMapping.getClassName(schema.getQName())), schema); addImplementedInterfaceFromUses(schema, outType); outType.addImplementsType(type); outType.addImplementsType(augmentable(outType)); - annotateDeprecatedIfNecessary(rpc.getStatus(), outType); - resolveDataSchemaNodes(context, outType, outType, schema.getChildNodes()); + defaultImplementedInterace(outType); + annotateDeprecatedIfNecessary(rpc, outType); + resolveDataSchemaNodes(context, outType, outType, schema.getChildNodes(), false); context.addChildNodeType(schema, outType); return outType.build(); } @@ -560,21 +587,22 @@ abstract class AbstractTypeGenerator { for (final NotificationDefinition notification : notifications) { if (notification != null) { - processUsesAugments(notification, context); + processUsesAugments(notification, context, false); final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition( context.modulePackageName(), notification, DATA_OBJECT, context); - annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface); + defaultImplementedInterace(notificationInterface); + annotateDeprecatedIfNecessary(notification, notificationInterface); notificationInterface.addImplementsType(NOTIFICATION); context.addChildNodeType(notification, notificationInterface); // Notification object resolveDataSchemaNodes(context, notificationInterface, notificationInterface, - notification.getChildNodes()); + notification.getChildNodes(), false); addComment(listenerInterface.addMethod("on" + notificationInterface.getName()) .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification") - .setReturnType(Types.primitiveVoidType()), notification); + .setReturnType(primitiveVoidType()), notification); } } @@ -582,6 +610,40 @@ abstract class AbstractTypeGenerator { context.addTopLevelNodeType(listenerInterface); } + private void notificationsToGenType( + final ModuleContext context, final Type parent, final T parentSchema, final Type keyType, + final boolean inGrouping) { + final Set notifications = parentSchema.getNotifications(); + if (notifications.isEmpty()) { + return; + } + + for (NotificationDefinition notif : notifications) { + if (notif.isAugmenting()) { + continue; + } + if (parentSchema instanceof GroupingDefinition) { + // Notifications cannot be really established, as they lack instantiation context, which would be + // completely described by an InstanceIdentifier -- hence we cannot create a binding class + continue; + } + + processUsesAugments(notif, context, false); + + final GeneratedTypeBuilder notifInterface = addDefaultInterfaceDefinition( + packageNameForGeneratedType(context.modulePackageName(), notif.getPath()), notif, DATA_OBJECT, context); + defaultImplementedInterace(notifInterface); + annotateDeprecatedIfNecessary(notif, notifInterface); + + notifInterface.addImplementsType(keyType != null ? keyedListNotification(notifInterface, parent, keyType) + : instanceNotification(notifInterface, parent)); + context.addChildNodeType(notif, notifInterface); + + // Notification object + resolveDataSchemaNodes(context, notifInterface, notifInterface, notif.getChildNodes(), false); + } + } + /** * Converts all identities of the module to the list of * Type objects. @@ -653,15 +715,14 @@ abstract class AbstractTypeGenerator { newType.setModuleName(module.getName()); newType.setSchemaPath(identity.getPath()); - qnameConstant(newType, JavaTypeName.create(context.modulePackageName(), BindingMapping.MODULE_INFO_CLASS_NAME), - identity.getQName().getLocalName()); + qnameConstant(newType, context.moduleInfoType(), identity.getQName().getLocalName()); context.addIdentityType(identity, newType); } private static Constant qnameConstant(final GeneratedTypeBuilderBase toBuilder, final JavaTypeName yangModuleInfo, final String localName) { - return toBuilder.addConstant(typeForClass(QName.class), BindingMapping.QNAME_STATIC_FIELD_NAME, + return toBuilder.addConstant(QNAME, BindingMapping.QNAME_STATIC_FIELD_NAME, new SimpleImmutableEntry<>(yangModuleInfo, localName)); } @@ -683,59 +744,45 @@ abstract class AbstractTypeGenerator { // Converts individual grouping to GeneratedType. Firstly generated type builder is created and every child // node of grouping is resolved to the method. final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(context, grouping); - annotateDeprecatedIfNecessary(grouping.getStatus(), genType); + narrowImplementedInterface(genType); + annotateDeprecatedIfNecessary(grouping, genType); context.addGroupingType(grouping, genType); - resolveDataSchemaNodes(context, genType, genType, grouping.getChildNodes()); + resolveDataSchemaNodes(context, genType, genType, grouping.getChildNodes(), true); groupingsToGenTypes(context, grouping.getGroupings()); - processUsesAugments(grouping, context); - actionsToGenType(context, genType, grouping, null); + processUsesAugments(grouping, context, true); + actionsToGenType(context, genType, grouping, null, true); + notificationsToGenType(context, genType, grouping, null, true); } } /** - * Adds enumeration builder created from enumTypeDef to - * typeBuilder. + * Adds enumeration builder created from enumTypeDef to typeBuilder. Each + * enumTypeDef item is added to builder with its name and value. * - * Each enumTypeDef item is added to builder with its name and - * value. - * - * @param enumTypeDef - * EnumTypeDefinition contains enum data - * @param enumName - * string contains name which will be assigned to enumeration - * builder - * @param typeBuilder - * GeneratedTypeBuilder to which will be enum builder assigned - * @param module - * Module in which type should be generated - * @return enumeration builder which contains data from - * enumTypeDef + * @param enumTypeDef EnumTypeDefinition contains enum data + * @param enumName string contains name which will be assigned to enumeration builder + * @param typeBuilder GeneratedTypeBuilder to which will be enum builder assigned + * @param module Module in which type should be generated + * @return enumeration builder which contains data from enumTypeDef */ - private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName, + private Enumeration resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName, final GeneratedTypeBuilder typeBuilder, final ModuleContext context) { - if (enumTypeDef != null && typeBuilder != null && enumTypeDef.getQName().getLocalName() != null) { - final EnumBuilder enumBuilder = typeBuilder.addEnumeration(BindingMapping.getClassName(enumName)); - typeProvider.addEnumDescription(enumBuilder, enumTypeDef); - enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef); - context.addInnerTypedefType(enumTypeDef.getPath(), enumBuilder); - return enumBuilder; - } - return null; + final EnumBuilder enumBuilder = typeBuilder.addEnumeration(BindingMapping.getClassName(enumName)); + typeProvider.addEnumDescription(enumBuilder, enumTypeDef); + enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef); + final Enumeration ret = enumBuilder.toInstance(typeBuilder); + context.addTypeToSchema(ret, enumTypeDef); + context.addInnerTypedefType(enumTypeDef.getPath(), ret); + return ret; } /** * Generates type builder for module. * - * @param module - * Module which is source of package name for generated type - * builder - * @param postfix - * string which is added to the module class name representation - * as suffix - * @return instance of GeneratedTypeBuilder which represents - * module. - * @throws IllegalArgumentException - * if module is null + * @param module Module which is source of package name for generated type builder + * @param postfix string which is added to the module class name representation as suffix + * @return instance of GeneratedTypeBuilder which represents module. + * @throws IllegalArgumentException if module is null */ private GeneratedTypeBuilder moduleTypeBuilder(final ModuleContext context, final String postfix) { final Module module = context.module(); @@ -749,20 +796,13 @@ abstract class AbstractTypeGenerator { } /** - * Converts augSchema to list of Type which - * contains generated type for augmentation. In addition there are also - * generated types for all containers, list and choices which are child of - * augSchema node or a generated types for cases are added if - * augmented node is choice. + * Converts augSchema to list of Type which contains generated type for augmentation. + * In addition there are also generated types for all containers, list and choices which are child of + * augSchema node or a generated types for cases are added if augmented node is choice. * - * @param augmentPackageName - * string with the name of the package to which the augmentation - * belongs - * @param augSchema - * AugmentationSchema which is contains data about augmentation - * (target path, childs...) - * @param module - * current module + * @param augmentPackageName string with the name of the package to which the augmentation belongs + * @param augSchema AugmentationSchema which is contains data about augmentation (target path, childs...) + * @param module current module * @throws IllegalArgumentException *
    *
  • if augmentPackageName equals null
  • @@ -776,7 +816,7 @@ abstract class AbstractTypeGenerator { checkState(augSchema.getTargetPath() != null, "Augmentation Schema does not contain Target Path (Target Path is NULL)."); - processUsesAugments(augSchema, context); + processUsesAugments(augSchema, context, false); final SchemaPath targetPath = augSchema.getTargetPath(); SchemaNode targetSchemaNode = null; @@ -794,31 +834,39 @@ abstract class AbstractTypeGenerator { throw new IllegalArgumentException("augment target not found: " + targetPath); } - GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath()); - if (targetTypeBuilder == null) { - targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath()); - } - if (targetTypeBuilder == null) { - throw new NullPointerException("Target type not yet generated: " + targetSchemaNode); + if (targetSchemaNode instanceof ChoiceSchemaNode) { + final GeneratedTypeBuilder builder = findChildNodeByPath(targetSchemaNode.getPath()); + checkState(builder != null, "Choice target type not generated for %s", targetSchemaNode); + generateTypesFromAugmentedChoiceCases(context, builder.build(), (ChoiceSchemaNode) targetSchemaNode, + augSchema.getChildNodes(), null, false); + return; } - if (!(targetSchemaNode instanceof ChoiceSchemaNode)) { - final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getIdentifier()); - addRawAugmentGenTypeDefinition(context, targetType, augSchema); - + final JavaTypeName targetName; + if (targetSchemaNode instanceof CaseSchemaNode) { + final GeneratedTypeBuilder builder = findCaseByPath(targetSchemaNode.getPath()); + checkState(builder != null, "Case target type not generated for %s", targetSchemaNode); + targetName = builder.getIdentifier(); } else { - generateTypesFromAugmentedChoiceCases(context, targetTypeBuilder.build(), - (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), null); + final GeneratedTypeBuilder builder = findChildNodeByPath(targetSchemaNode.getPath()); + if (builder == null) { + targetName = findAliasByPath(targetSchemaNode.getPath()); + checkState(targetName != null, "Target type not yet generated: %s", targetSchemaNode); + } else { + targetName = builder.getIdentifier(); + } } + + addRawAugmentGenTypeDefinition(context, new ReferencedTypeImpl(targetName), augSchema, false); } private void usesAugmentationToGenTypes(final ModuleContext context, final AugmentationSchemaNode augSchema, - final UsesNode usesNode, final DataNodeContainer usesNodeParent) { + final UsesNode usesNode, final DataNodeContainer usesNodeParent, final boolean inGrouping) { checkArgument(augSchema != null, "Augmentation Schema cannot be NULL."); checkState(augSchema.getTargetPath() != null, "Augmentation Schema does not contain Target Path (Target Path is NULL)."); - processUsesAugments(augSchema, context); + processUsesAugments(augSchema, context, inGrouping); final SchemaPath targetPath = augSchema.getTargetPath(); final SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(targetPath, usesNode); if (targetSchemaNode == null) { @@ -838,13 +886,13 @@ abstract class AbstractTypeGenerator { addRawAugmentGenTypeDefinition(context, packageNameForAugmentedGeneratedType(context.modulePackageName(), ((SchemaNode) usesNodeParent).getPath()), - targetTypeBuilder.build(), augSchema); + targetTypeBuilder.build(), augSchema, inGrouping); } else { - addRawAugmentGenTypeDefinition(context, targetTypeBuilder.build(), augSchema); + addRawAugmentGenTypeDefinition(context, targetTypeBuilder.build(), augSchema, inGrouping); } } else { generateTypesFromAugmentedChoiceCases(context, targetTypeBuilder.build(), - (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), usesNodeParent); + (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), usesNodeParent, inGrouping); } } @@ -861,20 +909,44 @@ abstract class AbstractTypeGenerator { /** * Convenient method to find node added by uses statement. * - * @param targetPath - * node path - * @param parentUsesNode - * parent of uses node + * @param targetPath node path + * @param parentUsesNode parent of uses node * @return node from its original location in grouping */ private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) { SchemaNode result = findUsedGrouping(parentUsesNode); for (final QName node : targetPath.getPathFromRoot()) { + // FIXME: this dispatch is rather ugly, we probably want to refactor it a bit if (result instanceof DataNodeContainer) { final QName resultNode = node.withModule(result.getQName().getModule()); - result = ((DataNodeContainer) result).getDataChildByName(resultNode); + + SchemaNode found = ((DataNodeContainer) result).getDataChildByName(resultNode); + if (found == null) { + if (result instanceof ActionNodeContainer) { + found = ((ActionNodeContainer) result).findAction(resultNode).orElse(null); + } + if (found == null && result instanceof NotificationNodeContainer) { + found = ((NotificationNodeContainer) result).findNotification(resultNode).orElse(null); + } + } + result = found; } else if (result instanceof ChoiceSchemaNode) { result = findNamedCase((ChoiceSchemaNode) result, node.getLocalName()); + } else if (result instanceof ActionDefinition) { + final ActionDefinition action = (ActionDefinition) result; + final QName resultNode = node.withModule(result.getQName().getModule()); + + final ContainerSchemaNode input = action.getInput(); + final ContainerSchemaNode output = action.getOutput(); + if (resultNode.equals(input.getQName())) { + result = input; + } else if (resultNode.equals(output.getQName())) { + result = output; + } else { + result = null; + } + } else if (result != null) { + throw new IllegalStateException("Cannot handle " + result); } } if (result == null) { @@ -907,29 +979,19 @@ abstract class AbstractTypeGenerator { } /** - * Returns a generated type builder for an augmentation. + * Returns a generated type builder for an augmentation. The name of the type builder is equal to the name + * of augmented node with serial number as suffix. * - * The name of the type builder is equal to the name of augmented node with - * serial number as suffix. - * - * @param context - * current module - * @param augmentPackageName - * string with contains the package name to which the augment - * belongs - * @param basePackageName - * string with the package name to which the augmented node - * belongs - * @param targetTypeRef - * target type - * @param augSchema - * augmentation schema which contains data about the child nodes - * and uses of augment + * @param context current module + * @param augmentPackageName string with contains the package name to which the augment belongs + * @param basePackageName string with the package name to which the augmented node belongs + * @param targetTypeRef target type + * @param augSchema augmentation schema which contains data about the child nodes and uses of augment * @return generated type builder for augment */ private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final ModuleContext context, final String augmentPackageName, final Type targetTypeRef, - final AugmentationSchemaNode augSchema) { + final AugmentationSchemaNode augSchema, final boolean inGrouping) { Map augmentBuilders = genTypeBuilders.computeIfAbsent(augmentPackageName, k -> new HashMap<>()); final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes()); @@ -945,31 +1007,32 @@ abstract class AbstractTypeGenerator { JavaTypeName.create(augmentPackageName, augTypeName)); augTypeBuilder.addImplementsType(DATA_OBJECT); - augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef)); - annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder); + defaultImplementedInterace(augTypeBuilder); + + augTypeBuilder.addImplementsType(augmentation(targetTypeRef)); + annotateDeprecatedIfNecessary(augSchema, augTypeBuilder); addImplementedInterfaceFromUses(augSchema, augTypeBuilder); - augSchemaNodeToMethods(context, augTypeBuilder, augSchema.getChildNodes()); + augSchemaNodeToMethods(context, augTypeBuilder, augSchema.getChildNodes(), inGrouping); + actionsToGenType(context, augTypeBuilder, augSchema, null, inGrouping); + notificationsToGenType(context, augTypeBuilder, augSchema, null, inGrouping); + augmentBuilders.put(augTypeName, augTypeBuilder); if (!augSchema.getChildNodes().isEmpty()) { context.addTypeToAugmentation(augTypeBuilder, augSchema); - } + context.addAugmentType(augTypeBuilder); return augTypeBuilder; } private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final ModuleContext context, final Type targetTypeRef, - final AugmentationSchemaNode augSchema) { - return addRawAugmentGenTypeDefinition(context, context.modulePackageName(), targetTypeRef, augSchema); + final AugmentationSchemaNode augSchema, final boolean inGrouping) { + return addRawAugmentGenTypeDefinition(context, context.modulePackageName(), targetTypeRef, augSchema, + inGrouping); } - /** - * - * @param unknownSchemaNodes - * @return nodeParameter of UnknownSchemaNode - */ private static String getAugmentIdentifier(final List unknownSchemaNodes) { for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) { final QName nodeType = unknownSchemaNode.getNodeType(); @@ -982,15 +1045,11 @@ abstract class AbstractTypeGenerator { } /** - * Returns first unique name for the augment generated type builder. The - * generated type builder name for augment consists from name of augmented - * node and serial number of its augmentation. + * Returns first unique name for the augment generated type builder. The generated type builder name for augment + * consists from name of augmented node and serial number of its augmentation. * - * @param builders - * map of builders which were created in the package to which the - * augmentation belongs - * @param genTypeName - * string with name of augmented node + * @param builders map of builders which were created in the package to which the augmentation belongs + * @param genTypeName string with name of augmented node * @return string with unique name for augmentation builder */ private static String augGenTypeName(final Map builders, final String genTypeName) { @@ -1004,35 +1063,27 @@ abstract class AbstractTypeGenerator { } /** - * Adds the methods to typeBuilder which represent subnodes of - * node for which typeBuilder was created. - * - * The subnodes aren't mapped to the methods if they are part of grouping or - * augment (in this case are already part of them). + * Adds the methods to typeBuilder which represent subnodes of node for which typeBuilder + * was created. The subnodes aren't mapped to the methods if they are part of grouping or augment (in this case are + * already part of them). * - * @param module - * current module - * @param parent - * generated type builder which represents any node. The subnodes - * of this node are added to the typeBuilder as - * methods. The subnode can be of type leaf, leaf-list, list, - * container, choice. - * @param childOf - * parent type - * @param schemaNodes - * set of data schema nodes which are the children of the node - * for which typeBuilder was created - * @return generated type builder which is the same builder as input - * parameter. The getter methods (representing child nodes) could be - * added to it. + * @param module current module + * @param parent generated type builder which represents any node. The subnodes of this node are added + * to the typeBuilder as methods. The subnode can be of type leaf, leaf-list, list, + * container, choice. + * @param childOf parent type + * @param schemaNodes set of data schema nodes which are the children of the node for which + * typeBuilder was created + * @return generated type builder which is the same builder as input parameter. The getter methods (representing + * child nodes) could be added to it. */ private GeneratedTypeBuilder resolveDataSchemaNodes(final ModuleContext context, final GeneratedTypeBuilder parent, - final @Nullable Type childOf, final Iterable schemaNodes) { + final @Nullable Type childOf, final Iterable schemaNodes, final boolean inGrouping) { if (schemaNodes != null && parent != null) { final Type baseInterface = childOf == null ? DATA_OBJECT : childOf(childOf); for (final DataSchemaNode schemaNode : schemaNodes) { if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) { - addSchemaNodeToBuilderAsMethod(context, schemaNode, parent, baseInterface); + addSchemaNodeToBuilderAsMethod(context, schemaNode, parent, baseInterface, inGrouping); } } } @@ -1040,32 +1091,27 @@ abstract class AbstractTypeGenerator { } /** - * Adds the methods to typeBuilder what represents subnodes of - * node for which typeBuilder was created. + * Adds the methods to typeBuilder what represents subnodes of node for which typeBuilder + * was created. * - * @param module - * current module - * @param typeBuilder - * generated type builder which represents any node. The subnodes - * of this node are added to the typeBuilder as - * methods. The subnode can be of type leaf, leaf-list, list, - * container, choice. - * @param childOf - * parent type - * @param schemaNodes - * set of data schema nodes which are the children of the node - * for which typeBuilder was created - * @return generated type builder which is the same object as the input - * parameter typeBuilder. The getter method could be - * added to it. + * @param module current module + * @param typeBuilder generated type builder which represents any node. The subnodes of this node are added + * to the typeBuilder as methods. The subnode can be of type leaf, leaf-list, list, + * container, choice. + * @param childOf parent type + * @param schemaNodes set of data schema nodes which are the children of the node for which typeBuilder + * was created + * @return generated type builder which is the same object as the input parameter typeBuilder. + * The getter method could be added to it. */ private GeneratedTypeBuilder augSchemaNodeToMethods(final ModuleContext context, - final GeneratedTypeBuilder typeBuilder, final Iterable schemaNodes) { + final GeneratedTypeBuilder typeBuilder, final Iterable schemaNodes, + final boolean inGrouping) { if (schemaNodes != null && typeBuilder != null) { final Type baseInterface = childOf(typeBuilder); for (final DataSchemaNode schemaNode : schemaNodes) { if (!schemaNode.isAugmenting()) { - addSchemaNodeToBuilderAsMethod(context, schemaNode, typeBuilder, baseInterface); + addSchemaNodeToBuilderAsMethod(context, schemaNode, typeBuilder, baseInterface, inGrouping); } } } @@ -1073,35 +1119,29 @@ abstract class AbstractTypeGenerator { } /** - * Adds to typeBuilder a method which is derived from - * schemaNode. + * Adds to typeBuilder a method which is derived from schemaNode. * - * @param node - * data schema node which is added to typeBuilder as - * a method - * @param typeBuilder - * generated type builder to which is schemaNode - * added as a method. - * @param childOf - * parent type - * @param module - * current module + * @param node data schema node which is added to typeBuilder as a method + * @param typeBuilder generated type builder to which is schemaNode added as a method. + * @param childOf parent type + * @param module current module */ private void addSchemaNodeToBuilderAsMethod(final ModuleContext context, final DataSchemaNode node, - final GeneratedTypeBuilder typeBuilder, final Type baseInterface) { + final GeneratedTypeBuilder typeBuilder, final Type baseInterface, final boolean inGrouping) { if (node != null && typeBuilder != null) { if (node instanceof LeafSchemaNode) { - resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node, context); + resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node, context, inGrouping); } else if (node instanceof LeafListSchemaNode) { - resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node, context); + resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node, context, inGrouping); } else if (node instanceof ContainerSchemaNode) { - containerToGenType(context, typeBuilder, baseInterface, (ContainerSchemaNode) node); + containerToGenType(context, typeBuilder, baseInterface, (ContainerSchemaNode) node, inGrouping); } else if (node instanceof ListSchemaNode) { - listToGenType(context, typeBuilder, baseInterface, (ListSchemaNode) node); + listToGenType(context, typeBuilder, baseInterface, (ListSchemaNode) node, inGrouping); } else if (node instanceof ChoiceSchemaNode) { - choiceToGeneratedType(context, typeBuilder, (ChoiceSchemaNode) node); + choiceToGeneratedType(context, typeBuilder, (ChoiceSchemaNode) node, inGrouping); + } else if (node instanceof AnyXmlSchemaNode || node instanceof AnyDataSchemaNode) { + opaqueToGeneratedType(context, typeBuilder, node); } else { - // TODO: anyxml not yet supported LOG.debug("Unable to add schema node {} as method in {}: unsupported type of node.", node.getClass(), typeBuilder.getFullyQualifiedName()); } @@ -1109,22 +1149,15 @@ abstract class AbstractTypeGenerator { } /** - * Converts choiceNode to the list of generated types for - * choice and its cases. - * - * The package names for choice and for its cases are created as - * concatenation of the module package (basePackageName) and - * names of all parents node. + * Converts choiceNode to the list of generated types for choice and its cases. The package names + * for choice and for its cases are created as concatenation of the module package (basePackageName) + * and names of all parents node. * - * @param context - * current module - * @param basePackageName - * string with the module package name - * @param parent - * parent type - * @param choiceNode - * choice node which is mapped to generated type. Also child - * nodes - cases are mapped to generated types. + * @param context current module + * @param basePackageName string with the module package name + * @param parent parent type + * @param choiceNode choice node which is mapped to generated type. Also child nodes - cases are mapped to generated + * types. * @throws IllegalArgumentException *
      *
    • if basePackageName is null
    • @@ -1132,38 +1165,46 @@ abstract class AbstractTypeGenerator { *
    */ private void choiceToGeneratedType(final ModuleContext context, final GeneratedTypeBuilder parent, - final ChoiceSchemaNode choiceNode) { - checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL."); - + final ChoiceSchemaNode choiceNode, final boolean inGrouping) { if (!choiceNode.isAddedByUses()) { - final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition( + final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(context, JavaTypeName.create(packageNameForGeneratedType(context.modulePackageName(), choiceNode.getPath()), BindingMapping.getClassName(choiceNode.getQName())), choiceNode); choiceTypeBuilder.addImplementsType(choiceIn(parent)); - annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder); + annotateDeprecatedIfNecessary(choiceNode, choiceTypeBuilder); context.addChildNodeType(choiceNode, choiceTypeBuilder); final GeneratedType choiceType = choiceTypeBuilder.build(); - generateTypesFromChoiceCases(context, choiceType, choiceNode); + generateTypesFromChoiceCases(context, choiceType, choiceNode, inGrouping); constructGetter(parent, choiceType, choiceNode); } } + private void opaqueToGeneratedType(final ModuleContext context, final GeneratedTypeBuilder parent, + final DataSchemaNode anyNode) { + if (!anyNode.isAddedByUses()) { + final GeneratedTypeBuilder anyxmlTypeBuilder = addRawInterfaceDefinition(context, + JavaTypeName.create(packageNameForGeneratedType(context.modulePackageName(), anyNode.getPath()), + BindingMapping.getClassName(anyNode.getQName())), anyNode); + anyxmlTypeBuilder.addImplementsType(opaqueObject(anyxmlTypeBuilder)).addImplementsType(childOf(parent)); + defaultImplementedInterace(anyxmlTypeBuilder); + annotateDeprecatedIfNecessary(anyNode, anyxmlTypeBuilder); + context.addChildNodeType(anyNode, anyxmlTypeBuilder); + + constructGetter(parent, anyxmlTypeBuilder.build(), anyNode); + } + } + /** - * Converts caseNodes set to list of corresponding generated types. + * Converts caseNodes set to list of corresponding generated types. For every case which is not + * added through augment or uses is created generated type builder. The package names for the builder is + * created as concatenation of the module package and names of all parents nodes of the concrete case. There + * is also relation "implements type" between every case builder and choice type * - * For every case which isn't added through augment or uses is created generated type builder. - * The package names for the builder is created as concatenation of the module package and names of all parents - * nodes of the concrete case. There is also relation "implements type" between every case builder - * and choice type - * - * @param context - * current module context - * @param refChoiceType - * type which represents superior case - * @param choiceNode - * choice case node which is mapped to generated type + * @param context current module context + * @param refChoiceType type which represents superior case + * @param choiceNode choice case node which is mapped to generated type * @throws IllegalArgumentException *
      *
    • if refChoiceType equals null
    • @@ -1171,7 +1212,7 @@ abstract class AbstractTypeGenerator { *
    */ private void generateTypesFromChoiceCases(final ModuleContext context, final Type refChoiceType, - final ChoiceSchemaNode choiceNode) { + final ChoiceSchemaNode choiceNode, final boolean inGrouping) { checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL."); checkArgument(choiceNode != null, "ChoiceNode cannot be NULL."); @@ -1179,7 +1220,8 @@ abstract class AbstractTypeGenerator { if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) { final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(context, caseNode); caseTypeBuilder.addImplementsType(refChoiceType); - annotateDeprecatedIfNecessary(caseNode.getStatus(), caseTypeBuilder); + defaultImplementedInterace(caseTypeBuilder); + annotateDeprecatedIfNecessary(caseNode, caseTypeBuilder); context.addCaseType(caseNode.getPath(), caseTypeBuilder); context.addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode); final Iterable caseChildNodes = caseNode.getChildNodes(); @@ -1213,34 +1255,29 @@ abstract class AbstractTypeGenerator { if (childOfType == null) { childOfType = findGroupingByPath(parent.getPath()); } - resolveDataSchemaNodes(context, caseTypeBuilder, childOfType, caseChildNodes); + resolveDataSchemaNodes(context, caseTypeBuilder, childOfType, caseChildNodes, inGrouping); } else { - resolveDataSchemaNodes(context, caseTypeBuilder, moduleToDataType(context), caseChildNodes); + resolveDataSchemaNodes(context, caseTypeBuilder, moduleToDataType(context), caseChildNodes, + inGrouping); } - } + } } - processUsesAugments(caseNode, context); + processUsesAugments(caseNode, context, inGrouping); } } /** - * Generates list of generated types for all the cases of a choice which are - * added to the choice through the augment. + * Generates list of generated types for all the cases of a choice which are added to the choice through + * the augment. * - * @param module - * current module - * @param basePackageName - * string contains name of package to which augment belongs. If - * an augmented choice is from an other package (pcg1) than an - * augmenting choice (pcg2) then case's of the augmenting choice - * will belong to pcg2. - * @param targetType - * Type which represents target choice - * @param targetNode - * node which represents target choice - * @param augmentedNodes - * set of choice case nodes for which is checked if are/aren't - * added to choice through augmentation + * @param module current module + * @param basePackageName string contains name of package to which augment belongs. If an augmented choice is + * from an other package (pcg1) than an augmenting choice (pcg2) then case's + * of the augmenting choice will belong to pcg2. + * @param targetType Type which represents target choice + * @param targetNode node which represents target choice + * @param augmentedNodes set of choice case nodes for which is checked if are/are not added to choice through + * augmentation * @throws IllegalArgumentException *
      *
    • if basePackageName is null
    • @@ -1248,9 +1285,11 @@ abstract class AbstractTypeGenerator { *
    • if augmentedNodes is null
    • *
    */ + // FIXME: nullness rules need to untangled in this method + @SuppressFBWarnings("NP_NULL_ON_SOME_PATH") private void generateTypesFromAugmentedChoiceCases(final ModuleContext context, final Type targetType, final ChoiceSchemaNode targetNode, final Iterable augmentedNodes, - final DataNodeContainer usesNodeParent) { + final DataNodeContainer usesNodeParent, final boolean inGrouping) { checkArgument(targetType != null, "Referenced Choice Type cannot be NULL."); checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL."); @@ -1258,6 +1297,7 @@ abstract class AbstractTypeGenerator { if (caseNode != null) { final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(context, caseNode); caseTypeBuilder.addImplementsType(targetType); + defaultImplementedInterace(caseTypeBuilder); CaseSchemaNode node = null; final String caseLocalName = caseNode.getQName().getLocalName(); @@ -1277,7 +1317,8 @@ abstract class AbstractTypeGenerator { } final Iterable childNodes = node.getChildNodes(); if (childNodes != null) { - resolveDataSchemaNodes(context, caseTypeBuilder, findChildOfType(targetNode), childNodes); + resolveDataSchemaNodes(context, caseTypeBuilder, findChildOfType(targetNode), childNodes, + inGrouping); } context.addCaseType(caseNode.getPath(), caseTypeBuilder); context.addChoiceToCaseMapping(targetType, caseTypeBuilder, node); @@ -1339,17 +1380,11 @@ abstract class AbstractTypeGenerator { } /** - * Converts leaf to the getter method which is added to - * typeBuilder. + * Converts leaf to the getter method which is added to typeBuilder. * - * @param typeBuilder - * generated type builder to which is added getter method as - * leaf mapping - * @param leaf - * leaf schema node which is mapped as getter method which is - * added to typeBuilder - * @param module - * Module in which type was defined + * @param typeBuilder generated type builder to which is added getter method as leaf mapping + * @param leaf leaf schema node which is mapped as getter method which is added to typeBuilder + * @param module Module in which type was defined * @return boolean value *
      *
    • false - if leaf or typeBuilder are @@ -1358,7 +1393,7 @@ abstract class AbstractTypeGenerator { *
    */ private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf, - final ModuleContext context) { + final ModuleContext context, final boolean inGrouping) { if (leaf == null || typeBuilder == null || leaf.isAddedByUses()) { return null; } @@ -1366,16 +1401,11 @@ abstract class AbstractTypeGenerator { final Module parentModule = findParentModule(schemaContext, leaf); Type returnType = null; - final TypeDefinition typeDef = CompatUtils.compatLeafType(leaf); + final TypeDefinition typeDef = CompatUtils.compatType(leaf); if (isInnerType(leaf, typeDef)) { if (typeDef instanceof EnumTypeDefinition) { - returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf); final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef; - final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(), - typeBuilder, context); - if (enumBuilder != null) { - returnType = enumBuilder.toInstance(typeBuilder); - } + returnType = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(), typeBuilder, context); typeProvider.putReferencedType(leaf.getPath(), returnType); } else if (typeDef instanceof UnionTypeDefinition) { final UnionTypeDefinition unionDef = (UnionTypeDefinition)typeDef; @@ -1395,12 +1425,12 @@ abstract class AbstractTypeGenerator { // and apply restrictions from leaf type final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef); returnType = typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf, - restrictions); + restrictions, inGrouping); addPatternConstant(typeBuilder, leaf.getQName().getLocalName(), restrictions.getPatternConstraints()); } } else { final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef); - returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions); + returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions, inGrouping); addPatternConstant(typeBuilder, leaf.getQName().getLocalName(), restrictions.getPatternConstraints()); } @@ -1485,7 +1515,7 @@ abstract class AbstractTypeGenerator { final boolean isReadOnly) { if (leaf != null && toBuilder != null) { Type returnType; - final TypeDefinition typeDef = CompatUtils.compatLeafType(leaf); + final TypeDefinition typeDef = CompatUtils.compatType(leaf); if (typeDef instanceof UnionTypeDefinition) { // GeneratedType for this type definition should have be already created final ModuleContext mc = moduleContext(typeDef.getQName().getModule()); @@ -1508,19 +1538,12 @@ abstract class AbstractTypeGenerator { } /** - * Converts leaf schema node to property of generated TO - * builder. + * Converts leaf schema node to property of generated TO builder. * - * @param toBuilder - * generated TO builder to which is leaf added as - * property - * @param leaf - * leaf schema node which is added to toBuilder as - * property - * @param returnType - * property type - * @param isReadOnly - * boolean value which says if leaf property is|isn't read only + * @param toBuilder generated TO builder to which is leaf added as property + * @param leaf leaf schema node which is added to toBuilder as property + * @param returnType property type + * @param isReadOnly boolean value which says if leaf property is|isn't read only * @return boolean value *
      *
    • false - if leaf, toBuilder or leaf @@ -1546,15 +1569,10 @@ abstract class AbstractTypeGenerator { } /** - * Converts node leaf list schema node to getter method of - * typeBuilder. + * Converts node leaf list schema node to getter method of typeBuilder. * - * @param typeBuilder - * generated type builder to which is node added as - * getter method - * @param node - * leaf list schema node which is added to - * typeBuilder as getter method + * @param typeBuilder generated type builder to which is node added as getter method + * @param node leaf list schema node which is added to typeBuilder as getter method * @param module module * @return boolean value *
        @@ -1564,7 +1582,7 @@ abstract class AbstractTypeGenerator { *
      */ private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node, - final ModuleContext context) { + final ModuleContext context, final boolean inGrouping) { if (node == null || typeBuilder == null || node.isAddedByUses()) { return false; } @@ -1577,11 +1595,8 @@ abstract class AbstractTypeGenerator { Type returnType = null; if (typeDef.getBaseType() == null) { if (typeDef instanceof EnumTypeDefinition) { - returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node); final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef; - final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName, - typeBuilder, context); - returnType = new ReferencedTypeImpl(enumBuilder.getIdentifier()); + returnType = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName, typeBuilder, context); typeProvider.putReferencedType(node.getPath(), returnType); } else if (typeDef instanceof UnionTypeDefinition) { final UnionTypeDefinition unionDef = (UnionTypeDefinition)typeDef; @@ -1592,17 +1607,16 @@ abstract class AbstractTypeGenerator { returnType = genTOBuilder.build(); } else { final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef); - returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions); + returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions, inGrouping); addPatternConstant(typeBuilder, node.getQName().getLocalName(), restrictions.getPatternConstraints()); } } else { final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef); - returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions); + returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions, inGrouping); addPatternConstant(typeBuilder, node.getQName().getLocalName(), restrictions.getPatternConstraints()); } - final ParameterizedType listType = listTypeFor(returnType); - constructGetter(typeBuilder, listType, node); + constructGetter(typeBuilder, listTypeFor(returnType), node); return true; } @@ -1634,17 +1648,12 @@ abstract class AbstractTypeGenerator { final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance"); method.setReturnType(returnType); - method.addParameter(Types.STRING, "defaultValue"); + method.addParameter(STRING, "defaultValue"); method.setAccessModifier(AccessModifier.PUBLIC); method.setStatic(true); final GeneratedTransferObject unionBuilderType = unionBuilder.build(); - final Set types = typeProvider.getAdditionalTypes().get(parentModule); - if (types == null) { - typeProvider.getAdditionalTypes().put(parentModule, Sets.newHashSet(unionBuilderType)); - } else { - types.add(unionBuilderType); - } + typeProvider.getAdditionalTypes().computeIfAbsent(parentModule, key -> new HashSet<>()).add(unionBuilderType); } private GeneratedTypeBuilder addDefaultInterfaceDefinition(final ModuleContext context, @@ -1659,26 +1668,17 @@ abstract class AbstractTypeGenerator { } /** - * Instantiates generated type builder with packageName and - * schemaNode. - * - * The new builder always implements - * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.
      - * If schemaNode is instance of GroupingDefinition it also - * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable - * Augmentable}.
      + * Instantiates generated type builder with packageName and schemaNode. The new builder + * always implements {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.
      + * If schemaNode is instance of GroupingDefinition it also implements + * {@link org.opendaylight.yangtools.yang.binding.Augmentable Augmentable}.
      * If schemaNode is instance of - * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer - * DataNodeContainer} it can also implement nodes which are specified in - * uses. + * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer DataNodeContainer} it can also implement nodes + * which are specified in uses. * - * @param packageName - * string with the name of the package to which - * schemaNode belongs. - * @param schemaNode - * schema node for which is created generated type builder - * @param parent - * parent type (can be null) + * @param packageName string with the name of the package to which schemaNode belongs. + * @param schemaNode schema node for which is created generated type builder + * @param parent parent type (can be null) * @return generated type builder schemaNode */ private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode, @@ -1688,8 +1688,7 @@ abstract class AbstractTypeGenerator { name = JavaTypeName.create(packageName, BindingMapping.getClassName(schemaNode.getQName())); } - final GeneratedTypeBuilder it = addRawInterfaceDefinition(name, schemaNode); - + final GeneratedTypeBuilder it = addRawInterfaceDefinition(context, name, schemaNode); it.addImplementsType(baseInterface); if (!(schemaNode instanceof GroupingDefinition)) { it.addImplementsType(augmentable(it)); @@ -1704,37 +1703,13 @@ abstract class AbstractTypeGenerator { } /** - * Wraps the calling of the same overloaded method. + * Returns reference to generated type builder for specified schemaNode with packageName. + * Firstly the generated type builder is searched in {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. + * If it is not found it is created and added to genTypeBuilders. * - * @param packageName - * string with the package name to which returning generated type - * builder belongs - * @param schemaNode - * schema node which provide data about the schema node name - * @return generated type builder for schemaNode - */ - private GeneratedTypeBuilder addRawInterfaceDefinition(final ModuleContext context, final SchemaNode schemaNode, - final String prefix) { - return addRawInterfaceDefinition( - JavaTypeName.create(packageNameForGeneratedType(context.modulePackageName(), schemaNode.getPath()), - prefix + BindingMapping.getClassName(schemaNode.getQName())), schemaNode); - } - - /** - * Returns reference to generated type builder for specified - * schemaNode with packageName. - * - * Firstly the generated type builder is searched in - * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't - * found it is created and added to genTypeBuilders. - * - * @param packageName - * string with the package name to which returning generated type - * builder belongs - * @param schemaNode - * schema node which provide data about the schema node name - * @param prefix - * return type name prefix + * @param packageName string with the package name to which returning generated type builder belongs + * @param schemaNode schema node which provide data about the schema node name + * @param prefix return type name prefix * @return generated type builder for schemaNode * @throws IllegalArgumentException *
        @@ -1743,9 +1718,9 @@ abstract class AbstractTypeGenerator { *
      • if QName of schema node is null
      • *
      • if schemaNode name is null
      • *
      - * */ - private GeneratedTypeBuilder addRawInterfaceDefinition(final JavaTypeName identifier, final SchemaNode schemaNode) { + private GeneratedTypeBuilder addRawInterfaceDefinition(final ModuleContext context, final JavaTypeName identifier, + final SchemaNode schemaNode) { checkArgument(schemaNode != null, "Data Schema Node cannot be NULL."); checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL."); final String schemaNodeName = schemaNode.getQName().getLocalName(); @@ -1753,10 +1728,9 @@ abstract class AbstractTypeGenerator { // FIXME: Validation of name conflict final GeneratedTypeBuilder newType = typeProvider.newGeneratedTypeBuilder(identifier); - final Module module = findParentModule(schemaContext, schemaNode); - qnameConstant(newType, JavaTypeName.create(BindingMapping.getRootPackageName(module.getQNameModule()), - BindingMapping.MODULE_INFO_CLASS_NAME), schemaNode.getQName().getLocalName()); + qnameConstant(newType, context.moduleInfoType(), schemaNode.getQName().getLocalName()); + final Module module = context.module(); addCodegenInformation(newType, module, schemaNode); newType.setSchemaPath(schemaNode.getPath()); newType.setModuleName(module.getName()); @@ -1779,30 +1753,18 @@ abstract class AbstractTypeGenerator { /** * Creates the name of the getter method name from localName. * - * @param localName - * string with the name of the getter method - * @param returnType - * return type - * @return string with the name of the getter method for - * methodName in JAVA method format + * @param localName string with the name of the getter method + * @param returnType return type + * @return string with the name of the getter method for methodName in JAVA method format */ public static String getterMethodName(final String localName, final Type returnType) { - final StringBuilder method = new StringBuilder(); - if (BOOLEAN.equals(returnType)) { - method.append("is"); - } else { - method.append("get"); - } - final String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName)); - method.append(name); - return method.toString(); + return BindingMapping.getGetterMethodName(localName, BOOLEAN.equals(returnType)); } /** - * Created a method signature builder as part of interfaceBuilder. - * - * The method signature builder is created for the getter method of schemaNodeName. - * Also comment and returnType information are added to the builder. + * Created a method signature builder as part of interfaceBuilder. The method signature builder is + * created for the getter method of schemaNodeName. Also comment + * and returnType information are added to the builder. * * @param interfaceBuilder generated type builder for which the getter method should be created * @param returnType type which represents the return type of the getter method @@ -1817,34 +1779,31 @@ abstract class AbstractTypeGenerator { getterMethodName(node.getQName().getLocalName(), returnType)); getMethod.setReturnType(returnType); - annotateDeprecatedIfNecessary(node.getStatus(), getMethod); - if (!returnType.getPackageName().isEmpty()) { - // The return type has a package, so it's not a primitive type - getMethod.addAnnotation("javax.annotation", "Nullable"); - } + annotateDeprecatedIfNecessary(node, getMethod); addComment(getMethod, node); return getMethod; } + private static void constructNonnull(final GeneratedTypeBuilder interfaceBuilder, final Type returnType, + final ListSchemaNode node) { + final MethodSignatureBuilder getMethod = interfaceBuilder.addMethod( + BindingMapping.getNonnullMethodName(node.getQName().getLocalName())); + getMethod.setReturnType(returnType).setDefault(true); + annotateDeprecatedIfNecessary(node, getMethod); + } + /** - * Adds schemaNode to typeBuilder as getter method - * or to genTOBuilder as property. + * Adds schemaNode to typeBuilder as getter method or to genTOBuilder + * as a property. * - * @param basePackageName - * string contains the module package name - * @param schemaNode - * data schema node which should be added as getter method to - * typeBuilder or as a property to - * genTOBuilder if is part of the list key - * @param typeBuilder - * generated type builder for the list schema node - * @param genTOBuilder - * generated TO builder for the list keys - * @param listKeys - * list of string which contains names of the list keys - * @param module - * current module + * @param basePackageName string contains the module package name + * @param schemaNode data schema node which should be added as getter method to typeBuilder + * or as a property to genTOBuilder if is part of the list key + * @param typeBuilder generated type builder for the list schema node + * @param genTOBuilder generated TO builder for the list keys + * @param listKeys list of string which contains names of the list keys + * @param module current module * @throws IllegalArgumentException *
        *
      • if schemaNode equals null
      • @@ -1853,14 +1812,14 @@ abstract class AbstractTypeGenerator { */ private void addSchemaNodeToListBuilders(final ModuleContext context, final DataSchemaNode schemaNode, final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, - final List listKeys) { + final List listKeys, final boolean inGrouping) { checkArgument(schemaNode != null, "Data Schema Node cannot be NULL."); checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL."); if (schemaNode instanceof LeafSchemaNode) { final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode; final String leafName = leaf.getQName().getLocalName(); - Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf, context); + Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf, context, inGrouping); if (listKeys.contains(leafName)) { if (type == null) { resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true); @@ -1870,14 +1829,16 @@ abstract class AbstractTypeGenerator { } } else if (!schemaNode.isAddedByUses()) { if (schemaNode instanceof LeafListSchemaNode) { - resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode, context); + resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode, context, inGrouping); } else if (schemaNode instanceof ContainerSchemaNode) { containerToGenType(context, typeBuilder, childOf(typeBuilder), - (ContainerSchemaNode) schemaNode); + (ContainerSchemaNode) schemaNode, inGrouping); } else if (schemaNode instanceof ChoiceSchemaNode) { - choiceToGeneratedType(context, typeBuilder, (ChoiceSchemaNode) schemaNode); + choiceToGeneratedType(context, typeBuilder, (ChoiceSchemaNode) schemaNode, inGrouping); } else if (schemaNode instanceof ListSchemaNode) { - listToGenType(context, typeBuilder, childOf(typeBuilder), (ListSchemaNode) schemaNode); + listToGenType(context, typeBuilder, childOf(typeBuilder), (ListSchemaNode) schemaNode, inGrouping); + } else if (schemaNode instanceof AnyXmlSchemaNode || schemaNode instanceof AnyDataSchemaNode) { + opaqueToGeneratedType(context, typeBuilder, schemaNode); } } } @@ -1896,72 +1857,43 @@ abstract class AbstractTypeGenerator { } /** - * Selects the names of the list keys from list and returns - * them as the list of the strings + * Selects the names of the list keys from list and returns them as the list of the strings. * - * @param list - * of string with names of the list keys - * @return list of string which represents names of the list keys. If the - * list contains no keys then the empty list is - * returned. + * @param list of string with names of the list keys + * @return list of string which represents names of the list keys. If the list contains no keys then + * an empty list is returned. */ private static List listKeys(final ListSchemaNode list) { - final List listKeys = new ArrayList<>(); - final List keyDefinition = list.getKeyDefinition(); - if (keyDefinition != null) { - for (final QName keyDef : keyDefinition) { - listKeys.add(keyDef.getLocalName()); - } - } - return listKeys; - } - - /** - * Generates for the list which contains any list keys special - * generated TO builder. - * - * @param packageName - * string with package name to which the list belongs - * @param list - * list schema node which is source of data about the list name - * @return generated TO builder which represents the keys of the - * list or null if list is null or list of - * key definitions is null or empty. - */ - private GeneratedTOBuilder resolveListKeyTOBuilder(final ModuleContext context, final ListSchemaNode list) { - if (list.getKeyDefinition() != null && !list.getKeyDefinition().isEmpty()) { - return typeProvider.newGeneratedTOBuilder(JavaTypeName.create( - packageNameForGeneratedType(context.modulePackageName(), list.getPath()), - BindingMapping.getClassName(list.getQName().getLocalName() + "Key"))); + switch (keyDefinition.size()) { + case 0: + return Collections.emptyList(); + case 1: + return Collections.singletonList(keyDefinition.get(0).getLocalName()); + default: + final List listKeys = new ArrayList<>(keyDefinition.size()); + for (final QName keyDef : keyDefinition) { + listKeys.add(keyDef.getLocalName()); + } + return listKeys; } - return null; } /** - * Builds a GeneratedTOBuilder for a UnionType {@link UnionTypeDefinition}. - * - * If more then one generated TO builder is created for enclosing then all - * of the generated TO builders are added to typeBuilder as + * Builds a GeneratedTOBuilder for a UnionType {@link UnionTypeDefinition}. If more then one generated TO builder + * is created for enclosing then all of the generated TO builders are added to typeBuilder as * enclosing transfer objects. * - * @param typeDef - * type definition which can be of type UnionType or - * BitsTypeDefinition - * @param typeBuilder - * generated type builder to which is added generated TO created - * from typeDef - * @param leaf - * string with name for generated TO builder - * @param parentModule - * parent module + * @param typeDef type definition which can be of type UnionType or BitsTypeDefinition + * @param typeBuilder generated type builder to which is added generated TO created from typeDef + * @param leaf string with name for generated TO builder + * @param parentModule parent module * @return generated TO builder for typeDef */ private Type addTOToTypeBuilder(final UnionTypeDefinition typeDef, final GeneratedTypeBuilder typeBuilder, final DataSchemaNode leaf, final Module parentModule) { final List types = typeProvider.provideGeneratedTOBuildersForUnionTypeDef( - typeBuilder.getIdentifier().createEnclosed(BindingMapping.getClassName(leaf.getQName())), - typeDef, leaf); + allocateNestedType(typeBuilder.getIdentifier(), leaf.getQName()), typeDef, leaf); checkState(!types.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typeDef); final List genTOBuilders = new ArrayList<>(types); @@ -1980,65 +1912,59 @@ abstract class AbstractTypeGenerator { } /** - * Builds generated TO builders for typeDef of type {@link BitsTypeDefinition} which are - * also added to typeBuilder as enclosing transfer object. - * - * If more then one generated TO builder is created for enclosing then all - * of the generated TO builders are added to typeBuilder as - * enclosing transfer objects. + * Builds generated TO builders for typeDef of type {@link BitsTypeDefinition} which are also added + * to typeBuilder as enclosing transfer object. If more then one generated TO builder is created + * for enclosing then all of the generated TO builders are added to typeBuilder as enclosing transfer + * objects. * - * @param typeDef - * type definition which can be of type UnionType or - * BitsTypeDefinition - * @param typeBuilder - * generated type builder to which is added generated TO created - * from typeDef - * @param leaf - * string with name for generated TO builder - * @param parentModule - * parent module + * @param typeDef type definition which can be of type UnionType or BitsTypeDefinition + * @param typeBuilder generated type builder to which is added generated TO created from typeDef + * @param leaf string with name for generated TO builder + * @param parentModule parent module * @return generated TO builder for typeDef */ private GeneratedTOBuilder addTOToTypeBuilder(final BitsTypeDefinition typeDef, final GeneratedTypeBuilder typeBuilder, final DataSchemaNode leaf, final Module parentModule) { final GeneratedTOBuilder genTOBuilder = typeProvider.provideGeneratedTOBuilderForBitsTypeDefinition( - typeBuilder.getIdentifier().createEnclosed(BindingMapping.getClassName(leaf.getQName())), - typeDef, parentModule.getName()); + allocateNestedType(typeBuilder.getIdentifier(), leaf.getQName()), typeDef, parentModule.getName()); typeBuilder.addEnclosingTransferObject(genTOBuilder); return genTOBuilder; - } /** - * Adds the implemented types to type builder. + * Adds the implemented types to type builder. The method passes through the list of uses in + * {@code dataNodeContainer}. For every use is obtained corresponding generated type + * from {@link ModuleContext#groupings allGroupings} which is added as implements type + * to builder * - * The method passes through the list of uses in - * {@code dataNodeContainer}. For every use is obtained corresponding - * generated type from {@link ModuleContext#groupings - * allGroupings} which is added as implements type to - * builder - * - * @param dataNodeContainer - * element which contains the list of used YANG groupings - * @param builder - * builder to which are added implemented types according to - * dataNodeContainer + * @param dataNodeContainer element which contains the list of used YANG groupings + * @param builder builder to which are added implemented types according to dataNodeContainer * @return generated type builder with all implemented types */ private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer, final GeneratedTypeBuilder builder) { for (final UsesNode usesNode : dataNodeContainer.getUses()) { - final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).build(); + final GeneratedTypeBuilder genType = findGroupingByPath(usesNode.getGroupingPath()); if (genType == null) { - throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for " - + builder.getName()); + throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + " is not resolved for " + + builder.getFullyQualifiedName()); } - builder.addImplementsType(genType); + builder.addImplementsType(genType.build()); } return builder; } + private JavaTypeName findAliasByPath(final SchemaPath path) { + for (final ModuleContext ctx : genCtx.values()) { + final JavaTypeName result = ctx.getAlias(path); + if (result != null) { + return result; + } + } + return null; + } + private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) { for (final ModuleContext ctx : genCtx.values()) { final GeneratedTypeBuilder result = ctx.getChildNode(path); @@ -2069,9 +1995,42 @@ abstract class AbstractTypeGenerator { return null; } - private static void annotateDeprecatedIfNecessary(final Status status, final AnnotableTypeBuilder builder) { - if (status == Status.DEPRECATED) { - builder.addAnnotation(DEPRECATED_ANNOTATION); + private static JavaTypeName allocateNestedType(final JavaTypeName parent, final QName child) { + // Single '$' suffix cannot come from user, this mirrors AbstractGeneratedTypeBuilder.addEnumeration() + return parent.createEnclosed(BindingMapping.getClassName(child), "$"); + } + + private static void annotateDeprecatedIfNecessary(final WithStatus node, final AnnotableTypeBuilder builder) { + switch (node.getStatus()) { + case DEPRECATED: + case OBSOLETE: + // FIXME: we really want to use a pre-made annotation + builder.addAnnotation(DEPRECATED_ANNOTATION); + break; + case CURRENT: + // No-op + break; + default: + throw new IllegalStateException("Unhandled status in " + node); } } + + private static void narrowImplementedInterface(final GeneratedTypeBuilder typeBuilder) { + defineImplementedInterfaceMethod(typeBuilder, wildcardTypeFor(typeBuilder.getIdentifier())); + } + + private static void defaultImplementedInterace(final GeneratedTypeBuilder typeBuilder) { + defineImplementedInterfaceMethod(typeBuilder, new ReferencedTypeImpl(typeBuilder.getIdentifier())) + .setDefault(true); + } + + private static MethodSignatureBuilder defineImplementedInterfaceMethod(final GeneratedTypeBuilder typeBuilder, + final Type classType) { + final MethodSignatureBuilder ret = typeBuilder + .addMethod(BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME) + .setAccessModifier(AccessModifier.PUBLIC) + .setReturnType(classType(classType)); + ret.addAnnotation(OVERRIDE_ANNOTATION); + return ret; + } }