From: Tony Tkacik Date: Mon, 19 Aug 2013 10:50:11 +0000 (+0200) Subject: Added instance identifier support. X-Git-Tag: yangtools-0.1.0~70 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=ecc1a92f3b9b1533f186a7fb321aafb81ba2b7f1;p=yangtools.git Added instance identifier support. - Added Identifiable and Identifier concepts to YANG binding - Updated binding generator to use Identifiable and Identifier for list statements. Signed-off-by: Tony Tkacik --- diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java index 48df06b137..f20e01dc98 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java @@ -1,1708 +1,1719 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.yangtools.sal.binding.generator.impl; - -import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.moduleNamespaceToPackageName; -import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.packageNameForGeneratedType; -import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToClassName; -import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToValidParamName; -import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.schemaNodeToTransferObjectBuilder; -import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode; -import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.Future; - -import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl; -import org.opendaylight.yangtools.binding.generator.util.Types; -import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl; -import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl; -import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator; -import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider; -import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject; -import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType; -import org.opendaylight.yangtools.sal.binding.model.api.Type; -import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder; -import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder; -import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder; -import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder; -import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder; -import org.opendaylight.yangtools.sal.binding.yang.types.GroupingDefinitionDependencySort; -import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl; -import org.opendaylight.yangtools.yang.binding.DataRoot; -import org.opendaylight.yangtools.yang.binding.RpcService; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; -import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceNode; -import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; -import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; -import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; -import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; -import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; -import org.opendaylight.yangtools.yang.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; -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.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.UsesNode; -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.EnumTypeDefinition.EnumPair; -import org.opendaylight.yangtools.yang.model.util.DataNodeIterator; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; -import org.opendaylight.yangtools.yang.model.util.UnionType; - -public final class BindingGeneratorImpl implements BindingGenerator { - - /** - * Outter key represents the package name. Outter 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 Map> genTypeBuilders; - - /** - * Provide methods for converting YANG types to JAVA types. - */ - private TypeProvider typeProvider; - - /** - * Holds reference to schema context to resolve data of augmented elemnt - * when creating augmentation builder - */ - private SchemaContext schemaContext; - - /** - * Each grouping which is converted from schema node to generated type is - * added to this map with its Schema path as key to make it easier to get - * reference to it. In schema nodes in uses attribute there is - * only Schema Path but when building list of implemented interfaces for - * Schema node the object of type Type is required. So in this - * case is used this map. - */ - private final Map allGroupings = new HashMap(); - - /** - * Only parent constructor is invoked. - */ - public BindingGeneratorImpl() { - super(); - } - - /** - * Resolves generated types from context schema nodes of all - * modules. - * - * Generated types are created for modules, groupings, types, containers, - * lists, choices, augments, rpcs, notification, identities. - * - * @param context - * schema context which contains data about all schema nodes - * saved in modules - * @return list of types (usually GeneratedType - * GeneratedTransferObjectwhich are generated from - * context data. - * @throws IllegalArgumentException - * if param context is null - * @throws IllegalStateException - * if context contain no modules - */ - @Override - public List generateTypes(final SchemaContext context) { - if (context == null) { - throw new IllegalArgumentException("Schema Context reference cannot be NULL!"); - } - if (context.getModules() == null) { - throw new IllegalStateException("Schema Context does not contain defined modules!"); - } - - final List generatedTypes = new ArrayList<>(); - schemaContext = context; - typeProvider = new TypeProviderImpl(context); - final Set modules = context.getModules(); - genTypeBuilders = new HashMap<>(); - for (final Module module : modules) { - - generatedTypes.addAll(allGroupingsToGenTypes(module)); - - if (false == module.getChildNodes().isEmpty()) { - generatedTypes.add(moduleToDataType(module)); - } - generatedTypes.addAll(allTypeDefinitionsToGenTypes(module)); - generatedTypes.addAll(allContainersToGenTypes(module)); - generatedTypes.addAll(allListsToGenTypes(module)); - generatedTypes.addAll(allChoicesToGenTypes(module)); - generatedTypes.addAll(allAugmentsToGenTypes(module)); - generatedTypes.addAll(allRPCMethodsToGenType(module)); - generatedTypes.addAll(allNotificationsToGenType(module)); - generatedTypes.addAll(allIdentitiesToGenTypes(module, context)); - - } - return generatedTypes; - } - - /** - * Resolves generated types from context schema nodes only for - * modules specified in modules - * - * Generated types are created for modules, groupings, types, containers, - * lists, choices, augments, rpcs, notification, identities. - * - * @param context - * schema context which contains data about all schema nodes - * saved in modules - * @param modules - * set of modules for which schema nodes should be generated - * types - * @return list of types (usually GeneratedType or - * GeneratedTransferObject) which: - *
    - *
  • are generated from context schema nodes and
  • - *
  • are also part of some of the module in modules - * set
  • . - *
- * @throws IllegalArgumentException - *
    - *
  • if param context is null or
  • - *
  • if param modules is null
  • - *
- * @throws IllegalStateException - * if context contain no modules - */ - @Override - public List generateTypes(final SchemaContext context, final Set modules) { - if (context == null) { - throw new IllegalArgumentException("Schema Context reference cannot be NULL!"); - } - if (context.getModules() == null) { - throw new IllegalStateException("Schema Context does not contain defined modules!"); - } - if (modules == null) { - throw new IllegalArgumentException("Sef of Modules cannot be NULL!"); - } - - final List filteredGenTypes = new ArrayList<>(); - schemaContext = context; - typeProvider = new TypeProviderImpl(context); - final Set contextModules = context.getModules(); - genTypeBuilders = new HashMap<>(); - for (final Module contextModule : contextModules) { - final List generatedTypes = new ArrayList<>(); - - generatedTypes.addAll(allGroupingsToGenTypes(contextModule)); - if (false == contextModule.getChildNodes().isEmpty()) { - generatedTypes.add(moduleToDataType(contextModule)); - } - generatedTypes.addAll(allTypeDefinitionsToGenTypes(contextModule)); - generatedTypes.addAll(allContainersToGenTypes(contextModule)); - generatedTypes.addAll(allListsToGenTypes(contextModule)); - generatedTypes.addAll(allChoicesToGenTypes(contextModule)); - generatedTypes.addAll(allAugmentsToGenTypes(contextModule)); - generatedTypes.addAll(allRPCMethodsToGenType(contextModule)); - generatedTypes.addAll(allNotificationsToGenType(contextModule)); - generatedTypes.addAll(allIdentitiesToGenTypes(contextModule, context)); - - if (modules.contains(contextModule)) { - filteredGenTypes.addAll(generatedTypes); - } - } - return filteredGenTypes; - } - - /** - * Converts all extended type definitions of module to the list of - * Type objects. - * - * @param module - * module from which is obtained set of type definitions - * @return list of Type which are generated from extended - * definition types (object of type ExtendedType) - * @throws IllegalArgumentException - *
    - *
  • if module equals null
  • - *
  • if name of module equals null
  • - *
  • if type definitions of module equal null
  • - *
- * - */ - private List allTypeDefinitionsToGenTypes(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - if (module.getName() == null) { - throw new IllegalArgumentException("Module name cannot be NULL!"); - } - if (module.getTypeDefinitions() == null) { - throw new IllegalArgumentException("Type Definitions for module " + module.getName() + " cannot be NULL!"); - } - - final Set> typeDefinitions = module.getTypeDefinitions(); - final List generatedTypes = new ArrayList<>(); - for (final TypeDefinition typedef : typeDefinitions) { - if (typedef != null) { - final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef); - if ((type != null) && !generatedTypes.contains(type)) { - generatedTypes.add(type); - } - } - } - return generatedTypes; - } - - /** - * Converts all containers of the module to the list of - * Type objects. - * - * @param module - * module from which is obtained DataNodeIterator to iterate over - * all containers - * @return list of Type which are generated from containers - * (objects of type ContainerSchemaNode) - * @throws IllegalArgumentException - *
    - *
  • if the module equals null
  • - *
  • if the name of module equals null
  • - *
  • if the set of child nodes equals null
  • - *
- * - */ - private List allContainersToGenTypes(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - - if (module.getName() == null) { - throw new IllegalArgumentException("Module name cannot be NULL!"); - } - - if (module.getChildNodes() == null) { - throw new IllegalArgumentException("Reference to Set of Child Nodes in module " + module.getName() - + " cannot be NULL!"); - } - - final List generatedTypes = new ArrayList<>(); - final DataNodeIterator it = new DataNodeIterator(module); - final List schemaContainers = it.allContainers(); - final String basePackageName = moduleNamespaceToPackageName(module); - for (final ContainerSchemaNode container : schemaContainers) { - if (!container.isAddedByUses()) { - generatedTypes.add(containerToGenType(basePackageName, container)); - } - } - return generatedTypes; - } - - /** - * Converts all lists of the module to the list of Type - * objects. - * - * @param module - * module from which is obtained DataNodeIterator to iterate over - * all lists - * @return list of Type which are generated from lists (objects - * of type ListSchemaNode) - * @throws IllegalArgumentException - *
    - *
  • if the module equals null
  • - *
  • if the name of module equals null
  • - *
  • if the set of child nodes equals null
  • - *
- * - */ - private List allListsToGenTypes(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - - if (module.getName() == null) { - throw new IllegalArgumentException("Module name cannot be NULL!"); - } - - if (module.getChildNodes() == null) { - throw new IllegalArgumentException("Reference to Set of Child Nodes in module " + module.getName() - + " cannot be NULL!"); - } - - final List generatedTypes = new ArrayList<>(); - final DataNodeIterator it = new DataNodeIterator(module); - final List schemaLists = it.allLists(); - final String basePackageName = moduleNamespaceToPackageName(module); - if (schemaLists != null) { - for (final ListSchemaNode list : schemaLists) { - if (!list.isAddedByUses()) { - generatedTypes.addAll(listToGenType(basePackageName, list)); - } - } - } - return generatedTypes; - } - - /** - * Converts all choices of the module to the list of - * Type objects. - * - * @param module - * module from which is obtained DataNodeIterator to iterate over - * all choices - * @return list of Type which are generated from choices - * (objects of type ChoiceNode) - * @throws IllegalArgumentException - *
    - *
  • if the module equals null
  • - *
  • if the name of module equals null
  • * - *
- * - */ - private List allChoicesToGenTypes(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - if (module.getName() == null) { - throw new IllegalArgumentException("Module name cannot be NULL!"); - } - - final DataNodeIterator it = new DataNodeIterator(module); - final List choiceNodes = it.allChoices(); - final String basePackageName = moduleNamespaceToPackageName(module); - - final List generatedTypes = new ArrayList<>(); - for (final ChoiceNode choice : choiceNodes) { - if ((choice != null) && !choice.isAddedByUses()) { - generatedTypes.addAll(choiceToGeneratedType(basePackageName, choice)); - } - } - return generatedTypes; - } - - /** - * Converts all augmentation of the module to the list - * Type objects. - * - * @param module - * module from which is obtained list of all augmentation objects - * to iterate over them - * @return list of Type which are generated from augments - * (objects of type AugmentationSchema) - * @throws IllegalArgumentException - *
    - *
  • if the module equals null
  • - *
  • if the name of module equals null
  • - *
  • if the set of child nodes equals null
  • - *
- * - */ - private List allAugmentsToGenTypes(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - if (module.getName() == null) { - throw new IllegalArgumentException("Module name cannot be NULL!"); - } - if (module.getChildNodes() == null) { - throw new IllegalArgumentException("Reference to Set of Augmentation Definitions in module " - + module.getName() + " cannot be NULL!"); - } - - final List generatedTypes = new ArrayList<>(); - final String basePackageName = moduleNamespaceToPackageName(module); - final List augmentations = resolveAugmentations(module); - for (final AugmentationSchema augment : augmentations) { - generatedTypes.addAll(augmentationToGenTypes(basePackageName, augment)); - } - return generatedTypes; - } - - /** - * Returns list of AugmentationSchema objects. The objects are - * sorted according to the length of their target path from the shortest to - * the longest. - * - * @param module - * module from which is obtained list of all augmentation objects - * @return list of sorted AugmentationSchema objects obtained - * from module - * @throws IllegalArgumentException - *
    - *
  • if the module equals null
  • - *
  • if the set of augmentation equals null
  • - *
- * - */ - private List resolveAugmentations(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - if (module.getAugmentations() == null) { - throw new IllegalStateException("Augmentations Set cannot be NULL!"); - } - - final Set augmentations = module.getAugmentations(); - final List sortedAugmentations = new ArrayList<>(augmentations); - Collections.sort(sortedAugmentations, new Comparator() { - - @Override - public int compare(AugmentationSchema augSchema1, AugmentationSchema augSchema2) { - - if (augSchema1.getTargetPath().getPath().size() > augSchema2.getTargetPath().getPath().size()) { - return 1; - } else if (augSchema1.getTargetPath().getPath().size() < augSchema2.getTargetPath().getPath().size()) { - return -1; - } - return 0; - - } - }); - - return sortedAugmentations; - } - - /** - * Converts whole module to GeneratedType object. - * Firstly is created the module builder object from which is finally - * obtained reference to GeneratedType object. - * - * @param module - * module from which are obtained the module name, child nodes, - * uses and is derived package name - * @return GeneratedType which is internal representation of - * the module - * @throws IllegalArgumentException - * if the module equals null - * - */ - private GeneratedType moduleToDataType(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - - final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data"); - addImplementedInterfaceFromUses(module, moduleDataTypeBuilder); - moduleDataTypeBuilder.addImplementsType(Types.typeForClass(DataRoot.class)); - - final String basePackageName = moduleNamespaceToPackageName(module); - if (moduleDataTypeBuilder != null) { - final Set dataNodes = module.getChildNodes(); - resolveDataSchemaNodes(basePackageName, moduleDataTypeBuilder, dataNodes); - } - return moduleDataTypeBuilder.toInstance(); - } - - /** - * Converts all rpcs inputs and outputs substatements of the module - * to the list of Type objects. In addition are to containers - * and lists which belong to input or output also part of returning list. - * - * @param module - * module from which is obtained set of all rpc objects to - * iterate over them - * @return list of Type which are generated from rpcs inputs, - * outputs + container and lists which are part of inputs or outputs - * @throws IllegalArgumentException - *
    - *
  • if the module equals null
  • - *
  • if the name of module equals null
  • - *
  • if the set of child nodes equals null
  • - *
- * - */ - private List allRPCMethodsToGenType(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - - if (module.getName() == null) { - throw new IllegalArgumentException("Module name cannot be NULL!"); - } - - if (module.getChildNodes() == null) { - throw new IllegalArgumentException("Reference to Set of RPC Method Definitions in module " - + module.getName() + " cannot be NULL!"); - } - - final String basePackageName = moduleNamespaceToPackageName(module); - final Set rpcDefinitions = module.getRpcs(); - - if (rpcDefinitions.isEmpty()) { - return Collections.emptyList(); - } - - final List genRPCTypes = new ArrayList<>(); - final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, "Service"); - interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class)); - final Type future = Types.typeForClass(Future.class); - for (final RpcDefinition rpc : rpcDefinitions) { - if (rpc != null) { - - String rpcName = parseToClassName(rpc.getQName().getLocalName()); - String rpcMethodName = parseToValidParamName(rpcName); - MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName); - - final List rpcInOut = new ArrayList<>(); - - ContainerSchemaNode input = rpc.getInput(); - ContainerSchemaNode output = rpc.getOutput(); - - if (input != null) { - rpcInOut.add(new DataNodeIterator(input)); - GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName); - addImplementedInterfaceFromUses(input, inType); - inType.addImplementsType(Types.DATA_OBJECT); - resolveDataSchemaNodes(basePackageName, inType, input.getChildNodes()); - Type inTypeInstance = inType.toInstance(); - genRPCTypes.add(inTypeInstance); - method.addParameter(inTypeInstance, "input"); - } - - Type outTypeInstance = Types.typeForClass(Void.class); - if (output != null) { - rpcInOut.add(new DataNodeIterator(output)); - GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName); - addImplementedInterfaceFromUses(output, outType); - outType.addImplementsType(Types.DATA_OBJECT); - resolveDataSchemaNodes(basePackageName, outType, output.getChildNodes()); - outTypeInstance = outType.toInstance(); - genRPCTypes.add(outTypeInstance); - - } - - final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance); - method.setReturnType(Types.parameterizedTypeFor(future, rpcRes)); - for (DataNodeIterator it : rpcInOut) { - List nContainers = it.allContainers(); - if ((nContainers != null) && !nContainers.isEmpty()) { - for (final ContainerSchemaNode container : nContainers) { - if (!container.isAddedByUses()) { - genRPCTypes.add(containerToGenType(basePackageName, container)); - } - } - } - List nLists = it.allLists(); - if ((nLists != null) && !nLists.isEmpty()) { - for (final ListSchemaNode list : nLists) { - if (!list.isAddedByUses()) { - genRPCTypes.addAll(listToGenType(basePackageName, list)); - } - } - } - } - } - } - genRPCTypes.add(interfaceBuilder.toInstance()); - return genRPCTypes; - } - - /** - * Converts all notifications of the module to the list of - * Type objects. In addition are to this list added containers - * and lists which are part of this notification. - * - * @param module - * module from which is obtained set of all notification objects - * to iterate over them - * @return list of Type which are generated from notification - * (object of type NotificationDefinition - * @throws IllegalArgumentException - *
    - *
  • if the module equals null
  • - *
  • if the name of module equals null
  • - *
  • if the set of child nodes equals null
  • - *
- * - */ - private List allNotificationsToGenType(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - - if (module.getName() == null) { - throw new IllegalArgumentException("Module name cannot be NULL!"); - } - - if (module.getChildNodes() == null) { - throw new IllegalArgumentException("Reference to Set of Notification Definitions in module " - + module.getName() + " cannot be NULL!"); - } - - final String basePackageName = moduleNamespaceToPackageName(module); - final List genNotifyTypes = new ArrayList<>(); - final Set notifications = module.getNotifications(); - - for (final NotificationDefinition notification : notifications) { - if (notification != null) { - DataNodeIterator it = new DataNodeIterator(notification); - - // Containers - for (ContainerSchemaNode node : it.allContainers()) { - if (!node.isAddedByUses()) { - genNotifyTypes.add(containerToGenType(basePackageName, node)); - } - } - // Lists - for (ListSchemaNode node : it.allLists()) { - if (!node.isAddedByUses()) { - genNotifyTypes.addAll(listToGenType(basePackageName, node)); - } - } - final GeneratedTypeBuilder notificationTypeBuilder = addDefaultInterfaceDefinition(basePackageName, - notification); - notificationTypeBuilder.addImplementsType(Types - .typeForClass(org.opendaylight.yangtools.yang.binding.Notification.class)); - // Notification object - resolveDataSchemaNodes(basePackageName, notificationTypeBuilder, notification.getChildNodes()); - genNotifyTypes.add(notificationTypeBuilder.toInstance()); - } - } - return genNotifyTypes; - } - - /** - * Converts all identities of the module to the list of - * Type objects. - * - * @param module - * module from which is obtained set of all identity objects to - * iterate over them - * @param context - * schema context only used as input parameter for method - * {@link identityToGenType} - * @return list of Type which are generated from identities - * (object of type IdentitySchemaNode - * - */ - private List allIdentitiesToGenTypes(final Module module, final SchemaContext context) { - List genTypes = new ArrayList<>(); - - final Set schemaIdentities = module.getIdentities(); - - final String basePackageName = moduleNamespaceToPackageName(module); - - if (schemaIdentities != null && !schemaIdentities.isEmpty()) { - for (final IdentitySchemaNode identity : schemaIdentities) { - genTypes.add(identityToGenType(basePackageName, identity, context)); - } - } - return genTypes; - } - - /** - * Converts the identity object to GeneratedType. Firstly it is - * created transport object builder. If identity contains base identity then - * reference to base identity is added to superior identity as its extend. - * If identity doesn't contain base identity then only reference to abstract - * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity - * BaseIdentity} is added - * - * @param basePackageName - * string containing package name to which identity belongs - * @param identity - * IdentitySchemaNode which contains data about identity - * @param context - * SchemaContext which is used to get package and name - * information about base of identity - * - * @return GeneratedType which is generated from identity (object of type - * IdentitySchemaNode - * - */ - private GeneratedType identityToGenType(final String basePackageName, final IdentitySchemaNode identity, - final SchemaContext context) { - if (identity == null) { - return null; - } - - final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath()); - final String genTypeName = parseToClassName(identity.getQName().getLocalName()); - final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName, genTypeName); - - IdentitySchemaNode baseIdentity = identity.getBaseIdentity(); - if (baseIdentity != null) { - Module baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity); - - final String returnTypePkgName = moduleNamespaceToPackageName(baseIdentityParentModule); - final String returnTypeName = parseToClassName(baseIdentity.getQName().getLocalName()); - - GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName).toInstance(); - newType.setExtendsType(gto); - } else { - newType.setExtendsType(Types.getBaseIdentityTO()); - } - newType.setAbstract(true); - return newType.toInstance(); - } - - /** - * Converts all groupings of the module to the list of - * Type objects. Firstly are groupings sorted according mutual - * dependencies. At least dependend (indepedent) groupings are in the list - * saved at first positions. For every grouping the record is added to map - * {@link BindingGeneratorImpl#allGroupings allGroupings} - * - * @param module - * module from which is obtained set of all grouping objects to - * iterate over them - * @return list of Type which are generated from groupings - * (object of type GroupingDefinition) - * - */ - private List allGroupingsToGenTypes(final Module module) { - if (module == null) { - throw new IllegalArgumentException("Module parameter can not be null"); - } - final List genTypes = new ArrayList<>(); - final String basePackageName = moduleNamespaceToPackageName(module); - final Set groupings = module.getGroupings(); - List groupingsSortedByDependencies; - - groupingsSortedByDependencies = GroupingDefinitionDependencySort.sort(groupings); - - for (final GroupingDefinition grouping : groupingsSortedByDependencies) { - GeneratedType genType = groupingToGenType(basePackageName, grouping); - genTypes.add(genType); - SchemaPath schemaPath = grouping.getPath(); - allGroupings.put(schemaPath, genType); - } - return genTypes; - } - - /** - * Converts individual grouping to GeneratedType. Firstly generated type - * builder is created and every child node of grouping is resolved to the - * method. - * - * @param basePackageName - * string containing name of package to which grouping belongs. - * @param grouping - * GroupingDefinition which contains data about grouping - * @return GeneratedType which is generated from grouping (object of type - * GroupingDefinition) - */ - private GeneratedType groupingToGenType(final String basePackageName, GroupingDefinition grouping) { - if (grouping == null) { - return null; - } - - final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath()); - final Set schemaNodes = grouping.getChildNodes(); - final GeneratedTypeBuilder typeBuilder = addDefaultInterfaceDefinition(packageName, grouping); - - resolveDataSchemaNodes(basePackageName, typeBuilder, schemaNodes); - return typeBuilder.toInstance(); - } - - /** - * Tries to find EnumTypeDefinition in typeDefinition. If base - * type of typeDefinition is of the type ExtendedType then this - * method is recursivelly called with this base type. - * - * @param typeDefinition - * TypeDefinition in which should be EnumTypeDefinition found as - * base type - * @return EnumTypeDefinition if it is found inside - * typeDefinition or null in other case - */ - private EnumTypeDefinition enumTypeDefFromExtendedType(final TypeDefinition typeDefinition) { - if (typeDefinition != null) { - if (typeDefinition.getBaseType() instanceof EnumTypeDefinition) { - return (EnumTypeDefinition) typeDefinition.getBaseType(); - } else if (typeDefinition.getBaseType() instanceof ExtendedType) { - return enumTypeDefFromExtendedType(typeDefinition.getBaseType()); - } - } - return null; - } - - /** - * Adds enumeration builder created from enumTypeDef to - * typeBuilder. - * - * 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 - * @return enumeration builder which contais data from - * enumTypeDef - */ - private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final String enumName, - final GeneratedTypeBuilder typeBuilder) { - if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null) - && (enumTypeDef.getQName().getLocalName() != null)) { - - final String enumerationName = parseToClassName(enumName); - final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName); - - if (enumBuilder != null) { - final List enums = enumTypeDef.getValues(); - if (enums != null) { - int listIndex = 0; - for (final EnumPair enumPair : enums) { - if (enumPair != null) { - final String enumPairName = parseToClassName(enumPair.getName()); - Integer enumPairValue = enumPair.getValue(); - - if (enumPairValue == null) { - enumPairValue = listIndex; - } - enumBuilder.addValue(enumPairName, enumPairValue); - listIndex++; - } - } - } - return enumBuilder; - } - } - return null; - } - - /** - * 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 equals null - */ - private GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix) { - if (module == null) { - throw new IllegalArgumentException("Module reference cannot be NULL!"); - } - String packageName = moduleNamespaceToPackageName(module); - final String moduleName = parseToClassName(module.getName()) + postfix; - - return new GeneratedTypeBuilderImpl(packageName, moduleName); - - } - - /** - * 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 agumentation - * (target path, childs...) - * @return list of Type objects which contains generated type - * for augmentation and for container, list and choice child nodes - * @throws IllegalArgumentException - *
    - *
  • if augmentPackageName equals null
  • - *
  • if augSchema equals null
  • - *
  • if target path of augSchema equals null
  • - *
- */ - private List augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema) { - if (augmentPackageName == null) { - throw new IllegalArgumentException("Package Name cannot be NULL!"); - } - if (augSchema == null) { - throw new IllegalArgumentException("Augmentation Schema cannot be NULL!"); - } - if (augSchema.getTargetPath() == null) { - throw new IllegalStateException("Augmentation Schema does not contain Target Path (Target Path is NULL)."); - } - - final List genTypes = new ArrayList<>(); - - // EVERY augmented interface will extends Augmentation interface - // and DataObject interface!!! - final SchemaPath targetPath = augSchema.getTargetPath(); - final DataSchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath); - if ((targetSchemaNode != null) && (targetSchemaNode.getQName() != null) - && (targetSchemaNode.getQName().getLocalName() != null)) { - final Module targetModule = findParentModule(schemaContext, targetSchemaNode); - final String targetBasePackage = moduleNamespaceToPackageName(targetModule); - final String targetPackageName = packageNameForGeneratedType(targetBasePackage, targetSchemaNode.getPath()); - final String targetSchemaNodeName = targetSchemaNode.getQName().getLocalName(); - final Set augChildNodes = augSchema.getChildNodes(); - - if (!(targetSchemaNode instanceof ChoiceNode)) { - final GeneratedTypeBuilder augTypeBuilder = addRawAugmentGenTypeDefinition(augmentPackageName, - targetPackageName, targetSchemaNodeName, augSchema); - final GeneratedType augType = augTypeBuilder.toInstance(); - genTypes.add(augType); - } else { - final Type refChoiceType = new ReferencedTypeImpl(targetPackageName, - parseToClassName(targetSchemaNodeName)); - final ChoiceNode choiceTarget = (ChoiceNode) targetSchemaNode; - final Set choiceCaseNodes = choiceTarget.getCases(); - genTypes.addAll(generateTypesFromAugmentedChoiceCases(augmentPackageName, refChoiceType, - choiceCaseNodes)); - } - genTypes.addAll(augmentationBodyToGenTypes(augmentPackageName, augChildNodes)); - } - return genTypes; - } - - /** - * 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. - * - * @param augmentPackageName - * string with contains the package name to which the augment - * belongs - * @param targetPackageName - * string with the package name to which the augmented node - * belongs - * @param targetSchemaNodeName - * string with the name of the augmented node - * @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 String augmentPackageName, - final String targetPackageName, final String targetSchemaNodeName, final AugmentationSchema augSchema) { - final String targetTypeName = parseToClassName(targetSchemaNodeName); - Map augmentBuilders = genTypeBuilders.get(augmentPackageName); - if (augmentBuilders == null) { - augmentBuilders = new HashMap<>(); - genTypeBuilders.put(augmentPackageName, augmentBuilders); - } - - final String augTypeName = augGenTypeName(augmentBuilders, targetTypeName); - final Type targetTypeRef = new ReferencedTypeImpl(targetPackageName, targetTypeName); - final Set augChildNodes = augSchema.getChildNodes(); - - final GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName); - - augTypeBuilder.addImplementsType(Types.DATA_OBJECT); - augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef)); - addImplementedInterfaceFromUses(augSchema, augTypeBuilder); - - augSchemaNodeToMethods(augmentPackageName, augTypeBuilder, augChildNodes); - augmentBuilders.put(augTypeName, augTypeBuilder); - return augTypeBuilder; - } - - /** - * Convert a container, list and choice subnodes (and recursivelly their - * subnodes) of augment to generated types - * - * @param augBasePackageName - * string with the augment package name - * @param augChildNodes - * set of data schema nodes which represents child nodes of the - * augment - * - * @return list of Type which represents container, list and - * choice subnodes of augment - */ - private List augmentationBodyToGenTypes(final String augBasePackageName, - final Set augChildNodes) { - final List genTypes = new ArrayList<>(); - final List augSchemaIts = new ArrayList<>(); - for (final DataSchemaNode childNode : augChildNodes) { - if (childNode instanceof DataNodeContainer) { - augSchemaIts.add(new DataNodeIterator((DataNodeContainer) childNode)); - - if (childNode instanceof ContainerSchemaNode) { - genTypes.add(containerToGenType(augBasePackageName, (ContainerSchemaNode) childNode)); - } else if (childNode instanceof ListSchemaNode) { - genTypes.addAll(listToGenType(augBasePackageName, (ListSchemaNode) childNode)); - } - } else if (childNode instanceof ChoiceNode) { - final ChoiceNode choice = (ChoiceNode) childNode; - for (final ChoiceCaseNode caseNode : choice.getCases()) { - augSchemaIts.add(new DataNodeIterator(caseNode)); - } - genTypes.addAll(choiceToGeneratedType(augBasePackageName, (ChoiceNode) childNode)); - } - } - - for (final DataNodeIterator it : augSchemaIts) { - final List augContainers = it.allContainers(); - final List augLists = it.allLists(); - final List augChoices = it.allChoices(); - - if (augContainers != null) { - for (final ContainerSchemaNode container : augContainers) { - genTypes.add(containerToGenType(augBasePackageName, container)); - } - } - if (augLists != null) { - for (final ListSchemaNode list : augLists) { - genTypes.addAll(listToGenType(augBasePackageName, list)); - } - } - if (augChoices != null) { - for (final ChoiceNode choice : augChoices) { - genTypes.addAll(choiceToGeneratedType(augBasePackageName, choice)); - } - } - } - return genTypes; - } - - /** - * 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 - * @return string with unique name for augmentation builder - */ - private String augGenTypeName(final Map builders, final String genTypeName) { - String augTypeName = genTypeName; - - int index = 1; - while ((builders != null) && builders.containsKey(genTypeName + index)) { - index++; - } - augTypeName += index; - return augTypeName; - } - - /** - * Converts containerNode to generated type. Firstly the - * generated type builder is created. The subnodes of - * containerNode are added as methods and the instance of - * GeneratedType is returned. - * - * @param basePackageName - * string with name of the package to which the superior node - * belongs - * @param containerNode - * container schema node with the data about childs nodes and - * schema paths - * @return generated type for containerNode - */ - private GeneratedType containerToGenType(final String basePackageName, ContainerSchemaNode containerNode) { - if (containerNode == null) { - return null; - } - - final String packageName = packageNameForGeneratedType(basePackageName, containerNode.getPath()); - final Set schemaNodes = containerNode.getChildNodes(); - final GeneratedTypeBuilder typeBuilder = addDefaultInterfaceDefinition(packageName, containerNode); - - resolveDataSchemaNodes(basePackageName, typeBuilder, schemaNodes); - return typeBuilder.toInstance(); - } - - /** - * - * @param basePackageName - * @param typeBuilder - * @param schemaNodes - * @return - */ - private GeneratedTypeBuilder resolveDataSchemaNodes(final String basePackageName, - final GeneratedTypeBuilder typeBuilder, final Set schemaNodes) { - if ((schemaNodes != null) && (typeBuilder != null)) { - for (final DataSchemaNode schemaNode : schemaNodes) { - if (schemaNode.isAugmenting() || schemaNode.isAddedByUses()) { - continue; - } - addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder); - } - } - return typeBuilder; - } - - private GeneratedTypeBuilder augSchemaNodeToMethods(final String basePackageName, - final GeneratedTypeBuilder typeBuilder, final Set schemaNodes) { - if ((schemaNodes != null) && (typeBuilder != null)) { - for (final DataSchemaNode schemaNode : schemaNodes) { - if (schemaNode.isAugmenting()) { - addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder); - } - } - } - return typeBuilder; - } - - private void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode schemaNode, - final GeneratedTypeBuilder typeBuilder) { - if (schemaNode != null && typeBuilder != null) { - if (schemaNode instanceof LeafSchemaNode) { - resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) schemaNode); - } else if (schemaNode instanceof LeafListSchemaNode) { - resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode); - } else if (schemaNode instanceof ContainerSchemaNode) { - resolveContainerSchemaNode(basePackageName, typeBuilder, (ContainerSchemaNode) schemaNode); - } else if (schemaNode instanceof ListSchemaNode) { - resolveListSchemaNode(basePackageName, typeBuilder, (ListSchemaNode) schemaNode); - } else if (schemaNode instanceof ChoiceNode) { - resolveChoiceSchemaNode(basePackageName, typeBuilder, (ChoiceNode) schemaNode); - } - } - } - - private void resolveChoiceSchemaNode(final String basePackageName, final GeneratedTypeBuilder typeBuilder, - final ChoiceNode choiceNode) { - if (basePackageName == null) { - throw new IllegalArgumentException("Base Package Name cannot be NULL!"); - } - if (typeBuilder == null) { - throw new IllegalArgumentException("Generated Type Builder cannot be NULL!"); - } - if (choiceNode == null) { - throw new IllegalArgumentException("Choice Schema Node cannot be NULL!"); - } - - final String choiceName = choiceNode.getQName().getLocalName(); - if (choiceName != null && !choiceNode.isAddedByUses()) { - final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath()); - final GeneratedTypeBuilder choiceType = addDefaultInterfaceDefinition(packageName, choiceNode); - constructGetter(typeBuilder, choiceName, choiceNode.getDescription(), choiceType); - } - } - - private List choiceToGeneratedType(final String basePackageName, final ChoiceNode choiceNode) { - if (basePackageName == null) { - throw new IllegalArgumentException("Base Package Name cannot be NULL!"); - } - if (choiceNode == null) { - throw new IllegalArgumentException("Choice Schema Node cannot be NULL!"); - } - - final List generatedTypes = new ArrayList<>(); - final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath()); - final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode); - choiceTypeBuilder.addImplementsType(Types.DATA_OBJECT); - final GeneratedType choiceType = choiceTypeBuilder.toInstance(); - - generatedTypes.add(choiceType); - final Set caseNodes = choiceNode.getCases(); - if ((caseNodes != null) && !caseNodes.isEmpty()) { - generatedTypes.addAll(generateTypesFromChoiceCases(basePackageName, choiceType, caseNodes)); - } - return generatedTypes; - } - - private List generateTypesFromChoiceCases(final String basePackageName, final Type refChoiceType, - final Set caseNodes) { - if (basePackageName == null) { - throw new IllegalArgumentException("Base Package Name cannot be NULL!"); - } - if (refChoiceType == null) { - throw new IllegalArgumentException("Referenced Choice Type cannot be NULL!"); - } - if (caseNodes == null) { - throw new IllegalArgumentException("Set of Choice Case Nodes cannot be NULL!"); - } - - final List generatedTypes = new ArrayList<>(); - for (final ChoiceCaseNode caseNode : caseNodes) { - if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) { - final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath()); - final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode); - caseTypeBuilder.addImplementsType(refChoiceType); - - final Set childNodes = caseNode.getChildNodes(); - if (childNodes != null) { - resolveDataSchemaNodes(basePackageName, caseTypeBuilder, childNodes); - } - generatedTypes.add(caseTypeBuilder.toInstance()); - } - } - - return generatedTypes; - } - - /** - * Generates list of generated types for all the cases of a choice which are - * added to the choice through the augment. - * - * - * @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 refChoiceType - * Type which represents the choice to which case belongs. Every - * case has to contain its choice in extend part. - * @param caseNodes - * set of choice case nodes for which is checked if are/aren't - * added to choice through augmentation - * @return list of generated types which represents augmented cases of - * choice refChoiceType - * @throws IllegalArgumentException - *
    - *
  • if basePackageName equals null
  • - *
  • if refChoiceType equals null
  • - *
  • if caseNodes equals null
  • - *
- */ - private List generateTypesFromAugmentedChoiceCases(final String basePackageName, - final Type refChoiceType, final Set caseNodes) { - if (basePackageName == null) { - throw new IllegalArgumentException("Base Package Name cannot be NULL!"); - } - if (refChoiceType == null) { - throw new IllegalArgumentException("Referenced Choice Type cannot be NULL!"); - } - if (caseNodes == null) { - throw new IllegalArgumentException("Set of Choice Case Nodes cannot be NULL!"); - } - - final List generatedTypes = new ArrayList<>(); - for (final ChoiceCaseNode caseNode : caseNodes) { - if (caseNode != null && caseNode.isAugmenting()) { - final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath()); - final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode); - caseTypeBuilder.addImplementsType(refChoiceType); - - final Set childNodes = caseNode.getChildNodes(); - if (childNodes != null) { - resolveDataSchemaNodes(basePackageName, caseTypeBuilder, childNodes); - } - generatedTypes.add(caseTypeBuilder.toInstance()); - } - } - - return generatedTypes; - } - - private boolean resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) { - if ((leaf != null) && (typeBuilder != null)) { - final String leafName = leaf.getQName().getLocalName(); - String leafDesc = leaf.getDescription(); - if (leafDesc == null) { - leafDesc = ""; - } - - if (leafName != null && !leaf.isAddedByUses()) { - final TypeDefinition typeDef = leaf.getType(); - - Type returnType = null; - if (typeDef instanceof EnumTypeDefinition) { - returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef); - final EnumTypeDefinition enumTypeDef = enumTypeDefFromExtendedType(typeDef); - final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leafName, - typeBuilder); - - if (enumBuilder != null) { - returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName()); - } - ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType); - } else if (typeDef instanceof UnionType) { - GeneratedTOBuilder genTOBuilder = addEnclosedTOToTypeBuilder(typeDef, typeBuilder, leafName); - if (genTOBuilder != null) { - returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName()); - } - } else if (typeDef instanceof BitsTypeDefinition) { - GeneratedTOBuilder genTOBuilder = addEnclosedTOToTypeBuilder(typeDef, typeBuilder, leafName); - if (genTOBuilder != null) { - returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName()); - } - } else { - returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef); - } - if (returnType != null) { - constructGetter(typeBuilder, leafName, leafDesc, returnType); - return true; - } - } - } - return false; - } - - private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf, - boolean isReadOnly) { - if ((leaf != null) && (toBuilder != null)) { - final String leafName = leaf.getQName().getLocalName(); - String leafDesc = leaf.getDescription(); - if (leafDesc == null) { - leafDesc = ""; - } - - if (leafName != null && !leaf.isAddedByUses()) { - final TypeDefinition typeDef = leaf.getType(); - - // TODO: properly resolve enum types - final Type returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef); - - if (returnType != null) { - final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToClassName(leafName)); - - propBuilder.setReadOnly(isReadOnly); - propBuilder.setReturnType(returnType); - propBuilder.setComment(leafDesc); - - toBuilder.addEqualsIdentity(propBuilder); - toBuilder.addHashIdentity(propBuilder); - toBuilder.addToStringProperty(propBuilder); - - return true; - } - } - } - return false; - } - - private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node) { - if ((node != null) && (typeBuilder != null)) { - final String nodeName = node.getQName().getLocalName(); - String nodeDesc = node.getDescription(); - if (nodeDesc == null) { - nodeDesc = ""; - } - - if (nodeName != null && !node.isAddedByUses()) { - final TypeDefinition type = node.getType(); - final Type listType = Types.listTypeFor(typeProvider.javaTypeForSchemaDefinitionType(type)); - - constructGetter(typeBuilder, nodeName, nodeDesc, listType); - return true; - } - } - return false; - } - - private boolean resolveContainerSchemaNode(final String basePackageName, final GeneratedTypeBuilder typeBuilder, - final ContainerSchemaNode containerNode) { - if ((containerNode != null) && (typeBuilder != null)) { - final String nodeName = containerNode.getQName().getLocalName(); - - if (nodeName != null && !containerNode.isAddedByUses()) { - final String packageName = packageNameForGeneratedType(basePackageName, containerNode.getPath()); - - final GeneratedTypeBuilder rawGenType = addDefaultInterfaceDefinition(packageName, containerNode); - constructGetter(typeBuilder, nodeName, containerNode.getDescription(), rawGenType); - - return true; - } - } - return false; - } - - private boolean resolveListSchemaNode(final String basePackageName, final GeneratedTypeBuilder typeBuilder, - final ListSchemaNode schemaNode) { - if ((schemaNode != null) && (typeBuilder != null)) { - final String listName = schemaNode.getQName().getLocalName(); - - if (listName != null && !schemaNode.isAddedByUses()) { - final String packageName = packageNameForGeneratedType(basePackageName, schemaNode.getPath()); - final GeneratedTypeBuilder rawGenType = addDefaultInterfaceDefinition(packageName, schemaNode); - constructGetter(typeBuilder, listName, schemaNode.getDescription(), Types.listTypeFor(rawGenType)); - return true; - } - } - return false; - } - - /** - * Method instantiates new Generated Type Builder and sets the implements - * definitions of Data Object and Augmentable. - * - * @param packageName - * Generated Type Package Name - * @param schemaNode - * Schema Node definition - * @return Generated Type Builder instance for Schema Node definition - */ - private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode) { - final GeneratedTypeBuilder builder = addRawInterfaceDefinition(packageName, schemaNode, ""); - builder.addImplementsType(Types.DATA_OBJECT); - if (!(schemaNode instanceof GroupingDefinition)) { - builder.addImplementsType(Types.augmentableTypeFor(builder)); - } - - if (schemaNode instanceof DataNodeContainer) { - addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, builder); - } - - return builder; - } - - /** - * - * @param packageName - * @param schemaNode - * @return - */ - private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode) { - return addRawInterfaceDefinition(packageName, schemaNode, ""); - } - - private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode, - final String prefix) { - if (schemaNode == null) { - throw new IllegalArgumentException("Data Schema Node cannot be NULL!"); - } - if (packageName == null) { - throw new IllegalArgumentException("Package Name for Generated Type cannot be NULL!"); - } - if (schemaNode.getQName() == null) { - throw new IllegalArgumentException("QName for Data Schema Node cannot be NULL!"); - } - final String schemaNodeName = schemaNode.getQName().getLocalName(); - if (schemaNodeName == null) { - throw new IllegalArgumentException("Local Name of QName for Data Schema Node cannot be NULL!"); - } - - final String genTypeName; - if (prefix == null) { - genTypeName = parseToClassName(schemaNodeName); - } else { - genTypeName = prefix + parseToClassName(schemaNodeName); - } - - final GeneratedTypeBuilder newType = new GeneratedTypeBuilderImpl(packageName, genTypeName); - if (!genTypeBuilders.containsKey(packageName)) { - final Map builders = new HashMap<>(); - builders.put(genTypeName, newType); - genTypeBuilders.put(packageName, builders); - } else { - final Map builders = genTypeBuilders.get(packageName); - if (!builders.containsKey(genTypeName)) { - builders.put(genTypeName, newType); - } - } - return newType; - } - - private String getterMethodName(final String methodName) { - final StringBuilder method = new StringBuilder(); - method.append("get"); - method.append(parseToClassName(methodName)); - return method.toString(); - } - - private String setterMethodName(final String methodName) { - final StringBuilder method = new StringBuilder(); - method.append("set"); - method.append(parseToClassName(methodName)); - return method.toString(); - } - - private MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder, - final String schemaNodeName, final String comment, final Type returnType) { - final MethodSignatureBuilder getMethod = interfaceBuilder.addMethod(getterMethodName(schemaNodeName)); - - getMethod.setComment(comment); - getMethod.setReturnType(returnType); - - return getMethod; - } - - private MethodSignatureBuilder constructSetter(final GeneratedTypeBuilder interfaceBuilder, - final String schemaNodeName, final String comment, final Type parameterType) { - final MethodSignatureBuilder setMethod = interfaceBuilder.addMethod(setterMethodName(schemaNodeName)); - - setMethod.setComment(comment); - setMethod.addParameter(parameterType, parseToValidParamName(schemaNodeName)); - setMethod.setReturnType(Types.voidType()); - - return setMethod; - } - - private List listToGenType(final String basePackageName, final ListSchemaNode list) { - if (basePackageName == null) { - throw new IllegalArgumentException("Package Name for Generated Type cannot be NULL!"); - } - if (list == null) { - throw new IllegalArgumentException("List Schema Node cannot be NULL!"); - } - - final String packageName = packageNameForGeneratedType(basePackageName, list.getPath()); - final GeneratedTypeBuilder typeBuilder = resolveListTypeBuilder(packageName, list); - final List listKeys = listKeys(list); - GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, list, listKeys); - - final Set schemaNodes = list.getChildNodes(); - - for (final DataSchemaNode schemaNode : schemaNodes) { - if (schemaNode.isAugmenting()) { - continue; - } - addSchemaNodeToListBuilders(basePackageName, schemaNode, typeBuilder, genTOBuilder, listKeys); - } - return typeBuildersToGenTypes(typeBuilder, genTOBuilder); - } - - private void addSchemaNodeToListBuilders(final String basePackageName, final DataSchemaNode schemaNode, - final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, final List listKeys) { - if (schemaNode == null) { - throw new IllegalArgumentException("Data Schema Node cannot be NULL!"); - } - - if (typeBuilder == null) { - throw new IllegalArgumentException("Generated Type Builder cannot be NULL!"); - } - - if (schemaNode instanceof LeafSchemaNode) { - final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode; - if (!isPartOfListKey(leaf, listKeys)) { - resolveLeafSchemaNodeAsMethod(typeBuilder, leaf); - } else { - resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true); - } - } else if (schemaNode instanceof LeafListSchemaNode) { - resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode); - } else if (schemaNode instanceof ContainerSchemaNode) { - resolveContainerSchemaNode(basePackageName, typeBuilder, (ContainerSchemaNode) schemaNode); - } else if (schemaNode instanceof ListSchemaNode) { - resolveListSchemaNode(basePackageName, typeBuilder, (ListSchemaNode) schemaNode); - } - } - - private List typeBuildersToGenTypes(final GeneratedTypeBuilder typeBuilder, GeneratedTOBuilder genTOBuilder) { - final List genTypes = new ArrayList<>(); - if (typeBuilder == null) { - throw new IllegalArgumentException("Generated Type Builder cannot be NULL!"); - } - - if (genTOBuilder != null) { - final GeneratedTransferObject genTO = genTOBuilder.toInstance(); - constructGetter(typeBuilder, genTO.getName(), "Returns Primary Key of Yang List Type", genTO); - genTypes.add(genTO); - } - genTypes.add(typeBuilder.toInstance()); - return genTypes; - } - - /** - * @param list - * @return - */ - private GeneratedTOBuilder resolveListKey(final String packageName, final ListSchemaNode list) { - final String listName = list.getQName().getLocalName() + "Key"; - return schemaNodeToTransferObjectBuilder(packageName, list, listName); - } - - private boolean isPartOfListKey(final LeafSchemaNode leaf, final List keys) { - if ((leaf != null) && (keys != null) && (leaf.getQName() != null)) { - final String leafName = leaf.getQName().getLocalName(); - if (keys.contains(leafName)) { - return true; - } - } - return false; - } - - private List listKeys(final ListSchemaNode list) { - final List listKeys = new ArrayList<>(); - - if (list.getKeyDefinition() != null) { - final List keyDefinitions = list.getKeyDefinition(); - - for (final QName keyDefinition : keyDefinitions) { - listKeys.add(keyDefinition.getLocalName()); - } - } - return listKeys; - } - - private GeneratedTypeBuilder resolveListTypeBuilder(final String packageName, final ListSchemaNode list) { - if (packageName == null) { - throw new IllegalArgumentException("Package Name for Generated Type cannot be NULL!"); - } - if (list == null) { - throw new IllegalArgumentException("List Schema Node cannot be NULL!"); - } - - final String schemaNodeName = list.getQName().getLocalName(); - final String genTypeName = parseToClassName(schemaNodeName); - - GeneratedTypeBuilder typeBuilder = null; - final Map builders = genTypeBuilders.get(packageName); - if (builders != null) { - typeBuilder = builders.get(genTypeName); - } - if (typeBuilder == null) { - typeBuilder = addDefaultInterfaceDefinition(packageName, list); - } - return typeBuilder; - } - - private GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list, - final List listKeys) { - GeneratedTOBuilder genTOBuilder = null; - if (listKeys.size() > 0) { - genTOBuilder = resolveListKey(packageName, list); - } - return genTOBuilder; - } - - private GeneratedTOBuilder addEnclosedTOToTypeBuilder(TypeDefinition typeDef, GeneratedTypeBuilder typeBuilder, - String leafName) { - String className = parseToClassName(leafName); - GeneratedTOBuilder genTOBuilder = null; - if (typeDef instanceof UnionType) { - genTOBuilder = ((TypeProviderImpl) typeProvider).addUnionGeneratedTypeDefinition( - typeBuilder.getFullyQualifiedName(), typeDef, className); - } else if (typeDef instanceof BitsTypeDefinition) { - genTOBuilder = ((TypeProviderImpl) typeProvider).bitsTypedefToTransferObject( - typeBuilder.getFullyQualifiedName(), typeDef, className); - } - if (genTOBuilder != null) { - typeBuilder.addEnclosingTransferObject(genTOBuilder); - return genTOBuilder; - } - return null; - - } - - /** - * Adds the implemented types to type builder. The method passes through the - * list of elements which contains {@code dataNodeContainer} and adds them - * 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 - * @return generated type builder which contains implemented types - */ - private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer, - final GeneratedTypeBuilder builder) { - for (UsesNode usesNode : dataNodeContainer.getUses()) { - if (usesNode.getGroupingPath() != null) { - GeneratedType genType = allGroupings.get(usesNode.getGroupingPath()); - if (genType == null) { - throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for " - + builder.getName()); - } - builder.addImplementsType(genType); - } - } - return builder; - } - -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.sal.binding.generator.impl; + +import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.moduleNamespaceToPackageName; +import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.packageNameForGeneratedType; +import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToClassName; +import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToValidParamName; +import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.schemaNodeToTransferObjectBuilder; +import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode; +import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Future; + +import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl; +import org.opendaylight.yangtools.binding.generator.util.Types; +import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl; +import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl; +import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator; +import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider; +import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType; +import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject; +import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType; +import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType; +import org.opendaylight.yangtools.sal.binding.model.api.Type; +import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder; +import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder; +import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder; +import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder; +import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder; +import org.opendaylight.yangtools.sal.binding.yang.types.GroupingDefinitionDependencySort; +import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl; +import org.opendaylight.yangtools.yang.binding.DataRoot; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.RpcService; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; +import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; +import org.opendaylight.yangtools.yang.model.api.ChoiceNode; +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; +import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; +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.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.UsesNode; +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.EnumTypeDefinition.EnumPair; +import org.opendaylight.yangtools.yang.model.util.DataNodeIterator; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; +import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; +import org.opendaylight.yangtools.yang.model.util.UnionType; + +public final class BindingGeneratorImpl implements BindingGenerator { + + /** + * Outter key represents the package name. Outter 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 Map> genTypeBuilders; + + /** + * Provide methods for converting YANG types to JAVA types. + */ + private TypeProvider typeProvider; + + /** + * Holds reference to schema context to resolve data of augmented elemnt + * when creating augmentation builder + */ + private SchemaContext schemaContext; + + /** + * Each grouping which is converted from schema node to generated type is + * added to this map with its Schema path as key to make it easier to get + * reference to it. In schema nodes in uses attribute there is + * only Schema Path but when building list of implemented interfaces for + * Schema node the object of type Type is required. So in this + * case is used this map. + */ + private final Map allGroupings = new HashMap(); + + /** + * Only parent constructor is invoked. + */ + public BindingGeneratorImpl() { + super(); + } + + /** + * Resolves generated types from context schema nodes of all + * modules. + * + * Generated types are created for modules, groupings, types, containers, + * lists, choices, augments, rpcs, notification, identities. + * + * @param context + * schema context which contains data about all schema nodes + * saved in modules + * @return list of types (usually GeneratedType + * GeneratedTransferObjectwhich are generated from + * context data. + * @throws IllegalArgumentException + * if param context is null + * @throws IllegalStateException + * if context contain no modules + */ + @Override + public List generateTypes(final SchemaContext context) { + if (context == null) { + throw new IllegalArgumentException("Schema Context reference cannot be NULL!"); + } + if (context.getModules() == null) { + throw new IllegalStateException("Schema Context does not contain defined modules!"); + } + + final List generatedTypes = new ArrayList<>(); + schemaContext = context; + typeProvider = new TypeProviderImpl(context); + final Set modules = context.getModules(); + genTypeBuilders = new HashMap<>(); + for (final Module module : modules) { + + generatedTypes.addAll(allGroupingsToGenTypes(module)); + + if (false == module.getChildNodes().isEmpty()) { + generatedTypes.add(moduleToDataType(module)); + } + generatedTypes.addAll(allTypeDefinitionsToGenTypes(module)); + generatedTypes.addAll(allContainersToGenTypes(module)); + generatedTypes.addAll(allListsToGenTypes(module)); + generatedTypes.addAll(allChoicesToGenTypes(module)); + generatedTypes.addAll(allAugmentsToGenTypes(module)); + generatedTypes.addAll(allRPCMethodsToGenType(module)); + generatedTypes.addAll(allNotificationsToGenType(module)); + generatedTypes.addAll(allIdentitiesToGenTypes(module, context)); + + } + return generatedTypes; + } + + /** + * Resolves generated types from context schema nodes only for + * modules specified in modules + * + * Generated types are created for modules, groupings, types, containers, + * lists, choices, augments, rpcs, notification, identities. + * + * @param context + * schema context which contains data about all schema nodes + * saved in modules + * @param modules + * set of modules for which schema nodes should be generated + * types + * @return list of types (usually GeneratedType or + * GeneratedTransferObject) which: + *
    + *
  • are generated from context schema nodes and
  • + *
  • are also part of some of the module in modules + * set
  • . + *
+ * @throws IllegalArgumentException + *
    + *
  • if param context is null or
  • + *
  • if param modules is null
  • + *
+ * @throws IllegalStateException + * if context contain no modules + */ + @Override + public List generateTypes(final SchemaContext context, final Set modules) { + if (context == null) { + throw new IllegalArgumentException("Schema Context reference cannot be NULL!"); + } + if (context.getModules() == null) { + throw new IllegalStateException("Schema Context does not contain defined modules!"); + } + if (modules == null) { + throw new IllegalArgumentException("Sef of Modules cannot be NULL!"); + } + + final List filteredGenTypes = new ArrayList<>(); + schemaContext = context; + typeProvider = new TypeProviderImpl(context); + final Set contextModules = context.getModules(); + genTypeBuilders = new HashMap<>(); + for (final Module contextModule : contextModules) { + final List generatedTypes = new ArrayList<>(); + + generatedTypes.addAll(allGroupingsToGenTypes(contextModule)); + if (false == contextModule.getChildNodes().isEmpty()) { + generatedTypes.add(moduleToDataType(contextModule)); + } + generatedTypes.addAll(allTypeDefinitionsToGenTypes(contextModule)); + generatedTypes.addAll(allContainersToGenTypes(contextModule)); + generatedTypes.addAll(allListsToGenTypes(contextModule)); + generatedTypes.addAll(allChoicesToGenTypes(contextModule)); + generatedTypes.addAll(allAugmentsToGenTypes(contextModule)); + generatedTypes.addAll(allRPCMethodsToGenType(contextModule)); + generatedTypes.addAll(allNotificationsToGenType(contextModule)); + generatedTypes.addAll(allIdentitiesToGenTypes(contextModule, context)); + + if (modules.contains(contextModule)) { + filteredGenTypes.addAll(generatedTypes); + } + } + return filteredGenTypes; + } + + /** + * Converts all extended type definitions of module to the list of + * Type objects. + * + * @param module + * module from which is obtained set of type definitions + * @return list of Type which are generated from extended + * definition types (object of type ExtendedType) + * @throws IllegalArgumentException + *
    + *
  • if module equals null
  • + *
  • if name of module equals null
  • + *
  • if type definitions of module equal null
  • + *
+ * + */ + private List allTypeDefinitionsToGenTypes(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + if (module.getName() == null) { + throw new IllegalArgumentException("Module name cannot be NULL!"); + } + if (module.getTypeDefinitions() == null) { + throw new IllegalArgumentException("Type Definitions for module " + module.getName() + " cannot be NULL!"); + } + + final Set> typeDefinitions = module.getTypeDefinitions(); + final List generatedTypes = new ArrayList<>(); + for (final TypeDefinition typedef : typeDefinitions) { + if (typedef != null) { + final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef); + if ((type != null) && !generatedTypes.contains(type)) { + generatedTypes.add(type); + } + } + } + return generatedTypes; + } + + /** + * Converts all containers of the module to the list of + * Type objects. + * + * @param module + * module from which is obtained DataNodeIterator to iterate over + * all containers + * @return list of Type which are generated from containers + * (objects of type ContainerSchemaNode) + * @throws IllegalArgumentException + *
    + *
  • if the module equals null
  • + *
  • if the name of module equals null
  • + *
  • if the set of child nodes equals null
  • + *
+ * + */ + private List allContainersToGenTypes(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + + if (module.getName() == null) { + throw new IllegalArgumentException("Module name cannot be NULL!"); + } + + if (module.getChildNodes() == null) { + throw new IllegalArgumentException("Reference to Set of Child Nodes in module " + module.getName() + + " cannot be NULL!"); + } + + final List generatedTypes = new ArrayList<>(); + final DataNodeIterator it = new DataNodeIterator(module); + final List schemaContainers = it.allContainers(); + final String basePackageName = moduleNamespaceToPackageName(module); + for (final ContainerSchemaNode container : schemaContainers) { + if (!container.isAddedByUses()) { + generatedTypes.add(containerToGenType(basePackageName, container)); + } + } + return generatedTypes; + } + + /** + * Converts all lists of the module to the list of Type + * objects. + * + * @param module + * module from which is obtained DataNodeIterator to iterate over + * all lists + * @return list of Type which are generated from lists (objects + * of type ListSchemaNode) + * @throws IllegalArgumentException + *
    + *
  • if the module equals null
  • + *
  • if the name of module equals null
  • + *
  • if the set of child nodes equals null
  • + *
+ * + */ + private List allListsToGenTypes(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + + if (module.getName() == null) { + throw new IllegalArgumentException("Module name cannot be NULL!"); + } + + if (module.getChildNodes() == null) { + throw new IllegalArgumentException("Reference to Set of Child Nodes in module " + module.getName() + + " cannot be NULL!"); + } + + final List generatedTypes = new ArrayList<>(); + final DataNodeIterator it = new DataNodeIterator(module); + final List schemaLists = it.allLists(); + final String basePackageName = moduleNamespaceToPackageName(module); + if (schemaLists != null) { + for (final ListSchemaNode list : schemaLists) { + if (!list.isAddedByUses()) { + generatedTypes.addAll(listToGenType(basePackageName, list)); + } + } + } + return generatedTypes; + } + + /** + * Converts all choices of the module to the list of + * Type objects. + * + * @param module + * module from which is obtained DataNodeIterator to iterate over + * all choices + * @return list of Type which are generated from choices + * (objects of type ChoiceNode) + * @throws IllegalArgumentException + *
    + *
  • if the module equals null
  • + *
  • if the name of module equals null
  • * + *
+ * + */ + private List allChoicesToGenTypes(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + if (module.getName() == null) { + throw new IllegalArgumentException("Module name cannot be NULL!"); + } + + final DataNodeIterator it = new DataNodeIterator(module); + final List choiceNodes = it.allChoices(); + final String basePackageName = moduleNamespaceToPackageName(module); + + final List generatedTypes = new ArrayList<>(); + for (final ChoiceNode choice : choiceNodes) { + if ((choice != null) && !choice.isAddedByUses()) { + generatedTypes.addAll(choiceToGeneratedType(basePackageName, choice)); + } + } + return generatedTypes; + } + + /** + * Converts all augmentation of the module to the list + * Type objects. + * + * @param module + * module from which is obtained list of all augmentation objects + * to iterate over them + * @return list of Type which are generated from augments + * (objects of type AugmentationSchema) + * @throws IllegalArgumentException + *
    + *
  • if the module equals null
  • + *
  • if the name of module equals null
  • + *
  • if the set of child nodes equals null
  • + *
+ * + */ + private List allAugmentsToGenTypes(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + if (module.getName() == null) { + throw new IllegalArgumentException("Module name cannot be NULL!"); + } + if (module.getChildNodes() == null) { + throw new IllegalArgumentException("Reference to Set of Augmentation Definitions in module " + + module.getName() + " cannot be NULL!"); + } + + final List generatedTypes = new ArrayList<>(); + final String basePackageName = moduleNamespaceToPackageName(module); + final List augmentations = resolveAugmentations(module); + for (final AugmentationSchema augment : augmentations) { + generatedTypes.addAll(augmentationToGenTypes(basePackageName, augment)); + } + return generatedTypes; + } + + /** + * Returns list of AugmentationSchema objects. The objects are + * sorted according to the length of their target path from the shortest to + * the longest. + * + * @param module + * module from which is obtained list of all augmentation objects + * @return list of sorted AugmentationSchema objects obtained + * from module + * @throws IllegalArgumentException + *
    + *
  • if the module equals null
  • + *
  • if the set of augmentation equals null
  • + *
+ * + */ + private List resolveAugmentations(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + if (module.getAugmentations() == null) { + throw new IllegalStateException("Augmentations Set cannot be NULL!"); + } + + final Set augmentations = module.getAugmentations(); + final List sortedAugmentations = new ArrayList<>(augmentations); + Collections.sort(sortedAugmentations, new Comparator() { + + @Override + public int compare(AugmentationSchema augSchema1, AugmentationSchema augSchema2) { + + if (augSchema1.getTargetPath().getPath().size() > augSchema2.getTargetPath().getPath().size()) { + return 1; + } else if (augSchema1.getTargetPath().getPath().size() < augSchema2.getTargetPath().getPath().size()) { + return -1; + } + return 0; + + } + }); + + return sortedAugmentations; + } + + /** + * Converts whole module to GeneratedType object. + * Firstly is created the module builder object from which is finally + * obtained reference to GeneratedType object. + * + * @param module + * module from which are obtained the module name, child nodes, + * uses and is derived package name + * @return GeneratedType which is internal representation of + * the module + * @throws IllegalArgumentException + * if the module equals null + * + */ + private GeneratedType moduleToDataType(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + + final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data"); + addImplementedInterfaceFromUses(module, moduleDataTypeBuilder); + moduleDataTypeBuilder.addImplementsType(Types.typeForClass(DataRoot.class)); + + final String basePackageName = moduleNamespaceToPackageName(module); + if (moduleDataTypeBuilder != null) { + final Set dataNodes = module.getChildNodes(); + resolveDataSchemaNodes(basePackageName, moduleDataTypeBuilder, dataNodes); + } + return moduleDataTypeBuilder.toInstance(); + } + + /** + * Converts all rpcs inputs and outputs substatements of the module + * to the list of Type objects. In addition are to containers + * and lists which belong to input or output also part of returning list. + * + * @param module + * module from which is obtained set of all rpc objects to + * iterate over them + * @return list of Type which are generated from rpcs inputs, + * outputs + container and lists which are part of inputs or outputs + * @throws IllegalArgumentException + *
    + *
  • if the module equals null
  • + *
  • if the name of module equals null
  • + *
  • if the set of child nodes equals null
  • + *
+ * + */ + private List allRPCMethodsToGenType(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + + if (module.getName() == null) { + throw new IllegalArgumentException("Module name cannot be NULL!"); + } + + if (module.getChildNodes() == null) { + throw new IllegalArgumentException("Reference to Set of RPC Method Definitions in module " + + module.getName() + " cannot be NULL!"); + } + + final String basePackageName = moduleNamespaceToPackageName(module); + final Set rpcDefinitions = module.getRpcs(); + + if (rpcDefinitions.isEmpty()) { + return Collections.emptyList(); + } + + final List genRPCTypes = new ArrayList<>(); + final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(module, "Service"); + interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class)); + final Type future = Types.typeForClass(Future.class); + for (final RpcDefinition rpc : rpcDefinitions) { + if (rpc != null) { + + String rpcName = parseToClassName(rpc.getQName().getLocalName()); + String rpcMethodName = parseToValidParamName(rpcName); + MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName); + + final List rpcInOut = new ArrayList<>(); + + ContainerSchemaNode input = rpc.getInput(); + ContainerSchemaNode output = rpc.getOutput(); + + if (input != null) { + rpcInOut.add(new DataNodeIterator(input)); + GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName); + addImplementedInterfaceFromUses(input, inType); + inType.addImplementsType(Types.DATA_OBJECT); + resolveDataSchemaNodes(basePackageName, inType, input.getChildNodes()); + Type inTypeInstance = inType.toInstance(); + genRPCTypes.add(inTypeInstance); + method.addParameter(inTypeInstance, "input"); + } + + Type outTypeInstance = Types.typeForClass(Void.class); + if (output != null) { + rpcInOut.add(new DataNodeIterator(output)); + GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName); + addImplementedInterfaceFromUses(output, outType); + outType.addImplementsType(Types.DATA_OBJECT); + resolveDataSchemaNodes(basePackageName, outType, output.getChildNodes()); + outTypeInstance = outType.toInstance(); + genRPCTypes.add(outTypeInstance); + + } + + final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance); + method.setReturnType(Types.parameterizedTypeFor(future, rpcRes)); + for (DataNodeIterator it : rpcInOut) { + List nContainers = it.allContainers(); + if ((nContainers != null) && !nContainers.isEmpty()) { + for (final ContainerSchemaNode container : nContainers) { + if (!container.isAddedByUses()) { + genRPCTypes.add(containerToGenType(basePackageName, container)); + } + } + } + List nLists = it.allLists(); + if ((nLists != null) && !nLists.isEmpty()) { + for (final ListSchemaNode list : nLists) { + if (!list.isAddedByUses()) { + genRPCTypes.addAll(listToGenType(basePackageName, list)); + } + } + } + } + } + } + genRPCTypes.add(interfaceBuilder.toInstance()); + return genRPCTypes; + } + + /** + * Converts all notifications of the module to the list of + * Type objects. In addition are to this list added containers + * and lists which are part of this notification. + * + * @param module + * module from which is obtained set of all notification objects + * to iterate over them + * @return list of Type which are generated from notification + * (object of type NotificationDefinition + * @throws IllegalArgumentException + *
    + *
  • if the module equals null
  • + *
  • if the name of module equals null
  • + *
  • if the set of child nodes equals null
  • + *
+ * + */ + private List allNotificationsToGenType(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + + if (module.getName() == null) { + throw new IllegalArgumentException("Module name cannot be NULL!"); + } + + if (module.getChildNodes() == null) { + throw new IllegalArgumentException("Reference to Set of Notification Definitions in module " + + module.getName() + " cannot be NULL!"); + } + + final String basePackageName = moduleNamespaceToPackageName(module); + final List genNotifyTypes = new ArrayList<>(); + final Set notifications = module.getNotifications(); + + for (final NotificationDefinition notification : notifications) { + if (notification != null) { + DataNodeIterator it = new DataNodeIterator(notification); + + // Containers + for (ContainerSchemaNode node : it.allContainers()) { + if (!node.isAddedByUses()) { + genNotifyTypes.add(containerToGenType(basePackageName, node)); + } + } + // Lists + for (ListSchemaNode node : it.allLists()) { + if (!node.isAddedByUses()) { + genNotifyTypes.addAll(listToGenType(basePackageName, node)); + } + } + final GeneratedTypeBuilder notificationTypeBuilder = addDefaultInterfaceDefinition(basePackageName, + notification); + notificationTypeBuilder.addImplementsType(Types + .typeForClass(org.opendaylight.yangtools.yang.binding.Notification.class)); + // Notification object + resolveDataSchemaNodes(basePackageName, notificationTypeBuilder, notification.getChildNodes()); + genNotifyTypes.add(notificationTypeBuilder.toInstance()); + } + } + return genNotifyTypes; + } + + /** + * Converts all identities of the module to the list of + * Type objects. + * + * @param module + * module from which is obtained set of all identity objects to + * iterate over them + * @param context + * schema context only used as input parameter for method + * {@link identityToGenType} + * @return list of Type which are generated from identities + * (object of type IdentitySchemaNode + * + */ + private List allIdentitiesToGenTypes(final Module module, final SchemaContext context) { + List genTypes = new ArrayList<>(); + + final Set schemaIdentities = module.getIdentities(); + + final String basePackageName = moduleNamespaceToPackageName(module); + + if (schemaIdentities != null && !schemaIdentities.isEmpty()) { + for (final IdentitySchemaNode identity : schemaIdentities) { + genTypes.add(identityToGenType(basePackageName, identity, context)); + } + } + return genTypes; + } + + /** + * Converts the identity object to GeneratedType. Firstly it is + * created transport object builder. If identity contains base identity then + * reference to base identity is added to superior identity as its extend. + * If identity doesn't contain base identity then only reference to abstract + * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity + * BaseIdentity} is added + * + * @param basePackageName + * string containing package name to which identity belongs + * @param identity + * IdentitySchemaNode which contains data about identity + * @param context + * SchemaContext which is used to get package and name + * information about base of identity + * + * @return GeneratedType which is generated from identity (object of type + * IdentitySchemaNode + * + */ + private GeneratedType identityToGenType(final String basePackageName, final IdentitySchemaNode identity, + final SchemaContext context) { + if (identity == null) { + return null; + } + + final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath()); + final String genTypeName = parseToClassName(identity.getQName().getLocalName()); + final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName, genTypeName); + + IdentitySchemaNode baseIdentity = identity.getBaseIdentity(); + if (baseIdentity != null) { + Module baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity); + + final String returnTypePkgName = moduleNamespaceToPackageName(baseIdentityParentModule); + final String returnTypeName = parseToClassName(baseIdentity.getQName().getLocalName()); + + GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName).toInstance(); + newType.setExtendsType(gto); + } else { + newType.setExtendsType(Types.getBaseIdentityTO()); + } + newType.setAbstract(true); + return newType.toInstance(); + } + + /** + * Converts all groupings of the module to the list of + * Type objects. Firstly are groupings sorted according mutual + * dependencies. At least dependend (indepedent) groupings are in the list + * saved at first positions. For every grouping the record is added to map + * {@link BindingGeneratorImpl#allGroupings allGroupings} + * + * @param module + * module from which is obtained set of all grouping objects to + * iterate over them + * @return list of Type which are generated from groupings + * (object of type GroupingDefinition) + * + */ + private List allGroupingsToGenTypes(final Module module) { + if (module == null) { + throw new IllegalArgumentException("Module parameter can not be null"); + } + final List genTypes = new ArrayList<>(); + final String basePackageName = moduleNamespaceToPackageName(module); + final Set groupings = module.getGroupings(); + List groupingsSortedByDependencies; + + groupingsSortedByDependencies = GroupingDefinitionDependencySort.sort(groupings); + + for (final GroupingDefinition grouping : groupingsSortedByDependencies) { + GeneratedType genType = groupingToGenType(basePackageName, grouping); + genTypes.add(genType); + SchemaPath schemaPath = grouping.getPath(); + allGroupings.put(schemaPath, genType); + } + return genTypes; + } + + /** + * Converts individual grouping to GeneratedType. Firstly generated type + * builder is created and every child node of grouping is resolved to the + * method. + * + * @param basePackageName + * string containing name of package to which grouping belongs. + * @param grouping + * GroupingDefinition which contains data about grouping + * @return GeneratedType which is generated from grouping (object of type + * GroupingDefinition) + */ + private GeneratedType groupingToGenType(final String basePackageName, GroupingDefinition grouping) { + if (grouping == null) { + return null; + } + + final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath()); + final Set schemaNodes = grouping.getChildNodes(); + final GeneratedTypeBuilder typeBuilder = addDefaultInterfaceDefinition(packageName, grouping); + + resolveDataSchemaNodes(basePackageName, typeBuilder, schemaNodes); + return typeBuilder.toInstance(); + } + + /** + * Tries to find EnumTypeDefinition in typeDefinition. If base + * type of typeDefinition is of the type ExtendedType then this + * method is recursivelly called with this base type. + * + * @param typeDefinition + * TypeDefinition in which should be EnumTypeDefinition found as + * base type + * @return EnumTypeDefinition if it is found inside + * typeDefinition or null in other case + */ + private EnumTypeDefinition enumTypeDefFromExtendedType(final TypeDefinition typeDefinition) { + if (typeDefinition != null) { + if (typeDefinition.getBaseType() instanceof EnumTypeDefinition) { + return (EnumTypeDefinition) typeDefinition.getBaseType(); + } else if (typeDefinition.getBaseType() instanceof ExtendedType) { + return enumTypeDefFromExtendedType(typeDefinition.getBaseType()); + } + } + return null; + } + + /** + * Adds enumeration builder created from enumTypeDef to + * typeBuilder. + * + * 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 + * @return enumeration builder which contais data from + * enumTypeDef + */ + private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final String enumName, + final GeneratedTypeBuilder typeBuilder) { + if ((enumTypeDef != null) && (typeBuilder != null) && (enumTypeDef.getQName() != null) + && (enumTypeDef.getQName().getLocalName() != null)) { + + final String enumerationName = parseToClassName(enumName); + final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName); + + if (enumBuilder != null) { + final List enums = enumTypeDef.getValues(); + if (enums != null) { + int listIndex = 0; + for (final EnumPair enumPair : enums) { + if (enumPair != null) { + final String enumPairName = parseToClassName(enumPair.getName()); + Integer enumPairValue = enumPair.getValue(); + + if (enumPairValue == null) { + enumPairValue = listIndex; + } + enumBuilder.addValue(enumPairName, enumPairValue); + listIndex++; + } + } + } + return enumBuilder; + } + } + return null; + } + + /** + * 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 equals null + */ + private GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix) { + if (module == null) { + throw new IllegalArgumentException("Module reference cannot be NULL!"); + } + String packageName = moduleNamespaceToPackageName(module); + final String moduleName = parseToClassName(module.getName()) + postfix; + + return new GeneratedTypeBuilderImpl(packageName, moduleName); + + } + + /** + * 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 agumentation + * (target path, childs...) + * @return list of Type objects which contains generated type + * for augmentation and for container, list and choice child nodes + * @throws IllegalArgumentException + *
    + *
  • if augmentPackageName equals null
  • + *
  • if augSchema equals null
  • + *
  • if target path of augSchema equals null
  • + *
+ */ + private List augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema) { + if (augmentPackageName == null) { + throw new IllegalArgumentException("Package Name cannot be NULL!"); + } + if (augSchema == null) { + throw new IllegalArgumentException("Augmentation Schema cannot be NULL!"); + } + if (augSchema.getTargetPath() == null) { + throw new IllegalStateException("Augmentation Schema does not contain Target Path (Target Path is NULL)."); + } + + final List genTypes = new ArrayList<>(); + + // EVERY augmented interface will extends Augmentation interface + // and DataObject interface!!! + final SchemaPath targetPath = augSchema.getTargetPath(); + final DataSchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath); + if ((targetSchemaNode != null) && (targetSchemaNode.getQName() != null) + && (targetSchemaNode.getQName().getLocalName() != null)) { + final Module targetModule = findParentModule(schemaContext, targetSchemaNode); + final String targetBasePackage = moduleNamespaceToPackageName(targetModule); + final String targetPackageName = packageNameForGeneratedType(targetBasePackage, targetSchemaNode.getPath()); + final String targetSchemaNodeName = targetSchemaNode.getQName().getLocalName(); + final Set augChildNodes = augSchema.getChildNodes(); + + if (!(targetSchemaNode instanceof ChoiceNode)) { + final GeneratedTypeBuilder augTypeBuilder = addRawAugmentGenTypeDefinition(augmentPackageName, + targetPackageName, targetSchemaNodeName, augSchema); + final GeneratedType augType = augTypeBuilder.toInstance(); + genTypes.add(augType); + } else { + final Type refChoiceType = new ReferencedTypeImpl(targetPackageName, + parseToClassName(targetSchemaNodeName)); + final ChoiceNode choiceTarget = (ChoiceNode) targetSchemaNode; + final Set choiceCaseNodes = choiceTarget.getCases(); + genTypes.addAll(generateTypesFromAugmentedChoiceCases(augmentPackageName, refChoiceType, + choiceCaseNodes)); + } + genTypes.addAll(augmentationBodyToGenTypes(augmentPackageName, augChildNodes)); + } + return genTypes; + } + + /** + * 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. + * + * @param augmentPackageName + * string with contains the package name to which the augment + * belongs + * @param targetPackageName + * string with the package name to which the augmented node + * belongs + * @param targetSchemaNodeName + * string with the name of the augmented node + * @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 String augmentPackageName, + final String targetPackageName, final String targetSchemaNodeName, final AugmentationSchema augSchema) { + final String targetTypeName = parseToClassName(targetSchemaNodeName); + Map augmentBuilders = genTypeBuilders.get(augmentPackageName); + if (augmentBuilders == null) { + augmentBuilders = new HashMap<>(); + genTypeBuilders.put(augmentPackageName, augmentBuilders); + } + + final String augTypeName = augGenTypeName(augmentBuilders, targetTypeName); + final Type targetTypeRef = new ReferencedTypeImpl(targetPackageName, targetTypeName); + final Set augChildNodes = augSchema.getChildNodes(); + + final GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName); + + augTypeBuilder.addImplementsType(Types.DATA_OBJECT); + augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef)); + addImplementedInterfaceFromUses(augSchema, augTypeBuilder); + + augSchemaNodeToMethods(augmentPackageName, augTypeBuilder, augChildNodes); + augmentBuilders.put(augTypeName, augTypeBuilder); + return augTypeBuilder; + } + + /** + * Convert a container, list and choice subnodes (and recursivelly their + * subnodes) of augment to generated types + * + * @param augBasePackageName + * string with the augment package name + * @param augChildNodes + * set of data schema nodes which represents child nodes of the + * augment + * + * @return list of Type which represents container, list and + * choice subnodes of augment + */ + private List augmentationBodyToGenTypes(final String augBasePackageName, + final Set augChildNodes) { + final List genTypes = new ArrayList<>(); + final List augSchemaIts = new ArrayList<>(); + for (final DataSchemaNode childNode : augChildNodes) { + if (childNode instanceof DataNodeContainer) { + augSchemaIts.add(new DataNodeIterator((DataNodeContainer) childNode)); + + if (childNode instanceof ContainerSchemaNode) { + genTypes.add(containerToGenType(augBasePackageName, (ContainerSchemaNode) childNode)); + } else if (childNode instanceof ListSchemaNode) { + genTypes.addAll(listToGenType(augBasePackageName, (ListSchemaNode) childNode)); + } + } else if (childNode instanceof ChoiceNode) { + final ChoiceNode choice = (ChoiceNode) childNode; + for (final ChoiceCaseNode caseNode : choice.getCases()) { + augSchemaIts.add(new DataNodeIterator(caseNode)); + } + genTypes.addAll(choiceToGeneratedType(augBasePackageName, (ChoiceNode) childNode)); + } + } + + for (final DataNodeIterator it : augSchemaIts) { + final List augContainers = it.allContainers(); + final List augLists = it.allLists(); + final List augChoices = it.allChoices(); + + if (augContainers != null) { + for (final ContainerSchemaNode container : augContainers) { + genTypes.add(containerToGenType(augBasePackageName, container)); + } + } + if (augLists != null) { + for (final ListSchemaNode list : augLists) { + genTypes.addAll(listToGenType(augBasePackageName, list)); + } + } + if (augChoices != null) { + for (final ChoiceNode choice : augChoices) { + genTypes.addAll(choiceToGeneratedType(augBasePackageName, choice)); + } + } + } + return genTypes; + } + + /** + * 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 + * @return string with unique name for augmentation builder + */ + private String augGenTypeName(final Map builders, final String genTypeName) { + String augTypeName = genTypeName; + + int index = 1; + while ((builders != null) && builders.containsKey(genTypeName + index)) { + index++; + } + augTypeName += index; + return augTypeName; + } + + /** + * Converts containerNode to generated type. Firstly the + * generated type builder is created. The subnodes of + * containerNode are added as methods and the instance of + * GeneratedType is returned. + * + * @param basePackageName + * string with name of the package to which the superior node + * belongs + * @param containerNode + * container schema node with the data about childs nodes and + * schema paths + * @return generated type for containerNode + */ + private GeneratedType containerToGenType(final String basePackageName, ContainerSchemaNode containerNode) { + if (containerNode == null) { + return null; + } + + final String packageName = packageNameForGeneratedType(basePackageName, containerNode.getPath()); + final Set schemaNodes = containerNode.getChildNodes(); + final GeneratedTypeBuilder typeBuilder = addDefaultInterfaceDefinition(packageName, containerNode); + + resolveDataSchemaNodes(basePackageName, typeBuilder, schemaNodes); + return typeBuilder.toInstance(); + } + + /** + * + * @param basePackageName + * @param typeBuilder + * @param schemaNodes + * @return + */ + private GeneratedTypeBuilder resolveDataSchemaNodes(final String basePackageName, + final GeneratedTypeBuilder typeBuilder, final Set schemaNodes) { + if ((schemaNodes != null) && (typeBuilder != null)) { + for (final DataSchemaNode schemaNode : schemaNodes) { + if (schemaNode.isAugmenting() || schemaNode.isAddedByUses()) { + continue; + } + addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder); + } + } + return typeBuilder; + } + + private GeneratedTypeBuilder augSchemaNodeToMethods(final String basePackageName, + final GeneratedTypeBuilder typeBuilder, final Set schemaNodes) { + if ((schemaNodes != null) && (typeBuilder != null)) { + for (final DataSchemaNode schemaNode : schemaNodes) { + if (schemaNode.isAugmenting()) { + addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder); + } + } + } + return typeBuilder; + } + + private void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode schemaNode, + final GeneratedTypeBuilder typeBuilder) { + if (schemaNode != null && typeBuilder != null) { + if (schemaNode instanceof LeafSchemaNode) { + resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) schemaNode); + } else if (schemaNode instanceof LeafListSchemaNode) { + resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode); + } else if (schemaNode instanceof ContainerSchemaNode) { + resolveContainerSchemaNode(basePackageName, typeBuilder, (ContainerSchemaNode) schemaNode); + } else if (schemaNode instanceof ListSchemaNode) { + resolveListSchemaNode(basePackageName, typeBuilder, (ListSchemaNode) schemaNode); + } else if (schemaNode instanceof ChoiceNode) { + resolveChoiceSchemaNode(basePackageName, typeBuilder, (ChoiceNode) schemaNode); + } + } + } + + private void resolveChoiceSchemaNode(final String basePackageName, final GeneratedTypeBuilder typeBuilder, + final ChoiceNode choiceNode) { + if (basePackageName == null) { + throw new IllegalArgumentException("Base Package Name cannot be NULL!"); + } + if (typeBuilder == null) { + throw new IllegalArgumentException("Generated Type Builder cannot be NULL!"); + } + if (choiceNode == null) { + throw new IllegalArgumentException("Choice Schema Node cannot be NULL!"); + } + + final String choiceName = choiceNode.getQName().getLocalName(); + if (choiceName != null && !choiceNode.isAddedByUses()) { + final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath()); + final GeneratedTypeBuilder choiceType = addDefaultInterfaceDefinition(packageName, choiceNode); + constructGetter(typeBuilder, choiceName, choiceNode.getDescription(), choiceType); + } + } + + private List choiceToGeneratedType(final String basePackageName, final ChoiceNode choiceNode) { + if (basePackageName == null) { + throw new IllegalArgumentException("Base Package Name cannot be NULL!"); + } + if (choiceNode == null) { + throw new IllegalArgumentException("Choice Schema Node cannot be NULL!"); + } + + final List generatedTypes = new ArrayList<>(); + final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath()); + final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode); + choiceTypeBuilder.addImplementsType(Types.DATA_OBJECT); + final GeneratedType choiceType = choiceTypeBuilder.toInstance(); + + generatedTypes.add(choiceType); + final Set caseNodes = choiceNode.getCases(); + if ((caseNodes != null) && !caseNodes.isEmpty()) { + generatedTypes.addAll(generateTypesFromChoiceCases(basePackageName, choiceType, caseNodes)); + } + return generatedTypes; + } + + private List generateTypesFromChoiceCases(final String basePackageName, final Type refChoiceType, + final Set caseNodes) { + if (basePackageName == null) { + throw new IllegalArgumentException("Base Package Name cannot be NULL!"); + } + if (refChoiceType == null) { + throw new IllegalArgumentException("Referenced Choice Type cannot be NULL!"); + } + if (caseNodes == null) { + throw new IllegalArgumentException("Set of Choice Case Nodes cannot be NULL!"); + } + + final List generatedTypes = new ArrayList<>(); + for (final ChoiceCaseNode caseNode : caseNodes) { + if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) { + final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath()); + final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode); + caseTypeBuilder.addImplementsType(refChoiceType); + + final Set childNodes = caseNode.getChildNodes(); + if (childNodes != null) { + resolveDataSchemaNodes(basePackageName, caseTypeBuilder, childNodes); + } + generatedTypes.add(caseTypeBuilder.toInstance()); + } + } + + return generatedTypes; + } + + /** + * Generates list of generated types for all the cases of a choice which are + * added to the choice through the augment. + * + * + * @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 refChoiceType + * Type which represents the choice to which case belongs. Every + * case has to contain its choice in extend part. + * @param caseNodes + * set of choice case nodes for which is checked if are/aren't + * added to choice through augmentation + * @return list of generated types which represents augmented cases of + * choice refChoiceType + * @throws IllegalArgumentException + *
    + *
  • if basePackageName equals null
  • + *
  • if refChoiceType equals null
  • + *
  • if caseNodes equals null
  • + *
+ */ + private List generateTypesFromAugmentedChoiceCases(final String basePackageName, + final Type refChoiceType, final Set caseNodes) { + if (basePackageName == null) { + throw new IllegalArgumentException("Base Package Name cannot be NULL!"); + } + if (refChoiceType == null) { + throw new IllegalArgumentException("Referenced Choice Type cannot be NULL!"); + } + if (caseNodes == null) { + throw new IllegalArgumentException("Set of Choice Case Nodes cannot be NULL!"); + } + + final List generatedTypes = new ArrayList<>(); + for (final ChoiceCaseNode caseNode : caseNodes) { + if (caseNode != null && caseNode.isAugmenting()) { + final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath()); + final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode); + caseTypeBuilder.addImplementsType(refChoiceType); + + final Set childNodes = caseNode.getChildNodes(); + if (childNodes != null) { + resolveDataSchemaNodes(basePackageName, caseTypeBuilder, childNodes); + } + generatedTypes.add(caseTypeBuilder.toInstance()); + } + } + + return generatedTypes; + } + + private boolean resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf) { + if ((leaf != null) && (typeBuilder != null)) { + final String leafName = leaf.getQName().getLocalName(); + String leafDesc = leaf.getDescription(); + if (leafDesc == null) { + leafDesc = ""; + } + + if (leafName != null && !leaf.isAddedByUses()) { + final TypeDefinition typeDef = leaf.getType(); + + Type returnType = null; + if (typeDef instanceof EnumTypeDefinition) { + returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef); + final EnumTypeDefinition enumTypeDef = enumTypeDefFromExtendedType(typeDef); + final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leafName, + typeBuilder); + + if (enumBuilder != null) { + returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName()); + } + ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType); + } else if (typeDef instanceof UnionType) { + GeneratedTOBuilder genTOBuilder = addEnclosedTOToTypeBuilder(typeDef, typeBuilder, leafName); + if (genTOBuilder != null) { + returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName()); + } + } else if (typeDef instanceof BitsTypeDefinition) { + GeneratedTOBuilder genTOBuilder = addEnclosedTOToTypeBuilder(typeDef, typeBuilder, leafName); + if (genTOBuilder != null) { + returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName()); + } + } else { + returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef); + } + if (returnType != null) { + constructGetter(typeBuilder, leafName, leafDesc, returnType); + return true; + } + } + } + return false; + } + + private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf, + boolean isReadOnly) { + if ((leaf != null) && (toBuilder != null)) { + final String leafName = leaf.getQName().getLocalName(); + String leafDesc = leaf.getDescription(); + if (leafDesc == null) { + leafDesc = ""; + } + + if (leafName != null && !leaf.isAddedByUses()) { + final TypeDefinition typeDef = leaf.getType(); + + // TODO: properly resolve enum types + final Type returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef); + + if (returnType != null) { + final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToClassName(leafName)); + + propBuilder.setReadOnly(isReadOnly); + propBuilder.setReturnType(returnType); + propBuilder.setComment(leafDesc); + + toBuilder.addEqualsIdentity(propBuilder); + toBuilder.addHashIdentity(propBuilder); + toBuilder.addToStringProperty(propBuilder); + + return true; + } + } + } + return false; + } + + private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node) { + if ((node != null) && (typeBuilder != null)) { + final String nodeName = node.getQName().getLocalName(); + String nodeDesc = node.getDescription(); + if (nodeDesc == null) { + nodeDesc = ""; + } + + if (nodeName != null && !node.isAddedByUses()) { + final TypeDefinition type = node.getType(); + final Type listType = Types.listTypeFor(typeProvider.javaTypeForSchemaDefinitionType(type)); + + constructGetter(typeBuilder, nodeName, nodeDesc, listType); + return true; + } + } + return false; + } + + private boolean resolveContainerSchemaNode(final String basePackageName, final GeneratedTypeBuilder typeBuilder, + final ContainerSchemaNode containerNode) { + if ((containerNode != null) && (typeBuilder != null)) { + final String nodeName = containerNode.getQName().getLocalName(); + + if (nodeName != null && !containerNode.isAddedByUses()) { + final String packageName = packageNameForGeneratedType(basePackageName, containerNode.getPath()); + + final GeneratedTypeBuilder rawGenType = addDefaultInterfaceDefinition(packageName, containerNode); + constructGetter(typeBuilder, nodeName, containerNode.getDescription(), rawGenType); + + return true; + } + } + return false; + } + + private boolean resolveListSchemaNode(final String basePackageName, final GeneratedTypeBuilder typeBuilder, + final ListSchemaNode schemaNode) { + if ((schemaNode != null) && (typeBuilder != null)) { + final String listName = schemaNode.getQName().getLocalName(); + + if (listName != null && !schemaNode.isAddedByUses()) { + final String packageName = packageNameForGeneratedType(basePackageName, schemaNode.getPath()); + final GeneratedTypeBuilder rawGenType = addDefaultInterfaceDefinition(packageName, schemaNode); + constructGetter(typeBuilder, listName, schemaNode.getDescription(), Types.listTypeFor(rawGenType)); + return true; + } + } + return false; + } + + /** + * Method instantiates new Generated Type Builder and sets the implements + * definitions of Data Object and Augmentable. + * + * @param packageName + * Generated Type Package Name + * @param schemaNode + * Schema Node definition + * @return Generated Type Builder instance for Schema Node definition + */ + private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode) { + final GeneratedTypeBuilder builder = addRawInterfaceDefinition(packageName, schemaNode, ""); + builder.addImplementsType(Types.DATA_OBJECT); + if (!(schemaNode instanceof GroupingDefinition)) { + builder.addImplementsType(Types.augmentableTypeFor(builder)); + } + + if (schemaNode instanceof DataNodeContainer) { + addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, builder); + } + + return builder; + } + + /** + * + * @param packageName + * @param schemaNode + * @return + */ + private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode) { + return addRawInterfaceDefinition(packageName, schemaNode, ""); + } + + private GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode, + final String prefix) { + if (schemaNode == null) { + throw new IllegalArgumentException("Data Schema Node cannot be NULL!"); + } + if (packageName == null) { + throw new IllegalArgumentException("Package Name for Generated Type cannot be NULL!"); + } + if (schemaNode.getQName() == null) { + throw new IllegalArgumentException("QName for Data Schema Node cannot be NULL!"); + } + final String schemaNodeName = schemaNode.getQName().getLocalName(); + if (schemaNodeName == null) { + throw new IllegalArgumentException("Local Name of QName for Data Schema Node cannot be NULL!"); + } + + final String genTypeName; + if (prefix == null) { + genTypeName = parseToClassName(schemaNodeName); + } else { + genTypeName = prefix + parseToClassName(schemaNodeName); + } + + final GeneratedTypeBuilder newType = new GeneratedTypeBuilderImpl(packageName, genTypeName); + if (!genTypeBuilders.containsKey(packageName)) { + final Map builders = new HashMap<>(); + builders.put(genTypeName, newType); + genTypeBuilders.put(packageName, builders); + } else { + final Map builders = genTypeBuilders.get(packageName); + if (!builders.containsKey(genTypeName)) { + builders.put(genTypeName, newType); + } + } + return newType; + } + + private String getterMethodName(final String methodName) { + final StringBuilder method = new StringBuilder(); + method.append("get"); + method.append(parseToClassName(methodName)); + return method.toString(); + } + + private String setterMethodName(final String methodName) { + final StringBuilder method = new StringBuilder(); + method.append("set"); + method.append(parseToClassName(methodName)); + return method.toString(); + } + + private MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder, + final String schemaNodeName, final String comment, final Type returnType) { + final MethodSignatureBuilder getMethod = interfaceBuilder.addMethod(getterMethodName(schemaNodeName)); + + getMethod.setComment(comment); + getMethod.setReturnType(returnType); + + return getMethod; + } + + private MethodSignatureBuilder constructSetter(final GeneratedTypeBuilder interfaceBuilder, + final String schemaNodeName, final String comment, final Type parameterType) { + final MethodSignatureBuilder setMethod = interfaceBuilder.addMethod(setterMethodName(schemaNodeName)); + + setMethod.setComment(comment); + setMethod.addParameter(parameterType, parseToValidParamName(schemaNodeName)); + setMethod.setReturnType(Types.voidType()); + + return setMethod; + } + + private List listToGenType(final String basePackageName, final ListSchemaNode list) { + if (basePackageName == null) { + throw new IllegalArgumentException("Package Name for Generated Type cannot be NULL!"); + } + if (list == null) { + throw new IllegalArgumentException("List Schema Node cannot be NULL!"); + } + + final String packageName = packageNameForGeneratedType(basePackageName, list.getPath()); + final GeneratedTypeBuilder typeBuilder = resolveListTypeBuilder(packageName, list); + final List listKeys = listKeys(list); + GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, list, listKeys); + + + if(genTOBuilder != null) { + ParameterizedType identifierMarker = Types.parameterizedTypeFor( Types.typeForClass(Identifier.class), typeBuilder); + ParameterizedType identifiableMarker = Types.parameterizedTypeFor(Types.typeForClass(Identifiable.class), genTOBuilder); + genTOBuilder.addImplementsType(identifierMarker); + typeBuilder.addImplementsType(identifiableMarker); + } + final Set schemaNodes = list.getChildNodes(); + + for (final DataSchemaNode schemaNode : schemaNodes) { + if (schemaNode.isAugmenting()) { + continue; + } + addSchemaNodeToListBuilders(basePackageName, schemaNode, typeBuilder, genTOBuilder, listKeys); + } + return typeBuildersToGenTypes(typeBuilder, genTOBuilder); + } + + private void addSchemaNodeToListBuilders(final String basePackageName, final DataSchemaNode schemaNode, + final GeneratedTypeBuilder typeBuilder, final GeneratedTOBuilder genTOBuilder, final List listKeys) { + if (schemaNode == null) { + throw new IllegalArgumentException("Data Schema Node cannot be NULL!"); + } + + if (typeBuilder == null) { + throw new IllegalArgumentException("Generated Type Builder cannot be NULL!"); + } + + if (schemaNode instanceof LeafSchemaNode) { + final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode; + if (!isPartOfListKey(leaf, listKeys)) { + resolveLeafSchemaNodeAsMethod(typeBuilder, leaf); + } else { + resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true); + } + } else if (schemaNode instanceof LeafListSchemaNode) { + resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode); + } else if (schemaNode instanceof ContainerSchemaNode) { + resolveContainerSchemaNode(basePackageName, typeBuilder, (ContainerSchemaNode) schemaNode); + } else if (schemaNode instanceof ListSchemaNode) { + resolveListSchemaNode(basePackageName, typeBuilder, (ListSchemaNode) schemaNode); + } + } + + private List typeBuildersToGenTypes(final GeneratedTypeBuilder typeBuilder, GeneratedTOBuilder genTOBuilder) { + final List genTypes = new ArrayList<>(); + if (typeBuilder == null) { + throw new IllegalArgumentException("Generated Type Builder cannot be NULL!"); + } + + if (genTOBuilder != null) { + final GeneratedTransferObject genTO = genTOBuilder.toInstance(); + constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO); + genTypes.add(genTO); + } + genTypes.add(typeBuilder.toInstance()); + return genTypes; + } + + /** + * @param list + * @return + */ + private GeneratedTOBuilder resolveListKey(final String packageName, final ListSchemaNode list) { + final String listName = list.getQName().getLocalName() + "Key"; + return schemaNodeToTransferObjectBuilder(packageName, list, listName); + } + + private boolean isPartOfListKey(final LeafSchemaNode leaf, final List keys) { + if ((leaf != null) && (keys != null) && (leaf.getQName() != null)) { + final String leafName = leaf.getQName().getLocalName(); + if (keys.contains(leafName)) { + return true; + } + } + return false; + } + + private List listKeys(final ListSchemaNode list) { + final List listKeys = new ArrayList<>(); + + if (list.getKeyDefinition() != null) { + final List keyDefinitions = list.getKeyDefinition(); + + for (final QName keyDefinition : keyDefinitions) { + listKeys.add(keyDefinition.getLocalName()); + } + } + return listKeys; + } + + private GeneratedTypeBuilder resolveListTypeBuilder(final String packageName, final ListSchemaNode list) { + if (packageName == null) { + throw new IllegalArgumentException("Package Name for Generated Type cannot be NULL!"); + } + if (list == null) { + throw new IllegalArgumentException("List Schema Node cannot be NULL!"); + } + + final String schemaNodeName = list.getQName().getLocalName(); + final String genTypeName = parseToClassName(schemaNodeName); + + GeneratedTypeBuilder typeBuilder = null; + final Map builders = genTypeBuilders.get(packageName); + if (builders != null) { + typeBuilder = builders.get(genTypeName); + } + if (typeBuilder == null) { + typeBuilder = addDefaultInterfaceDefinition(packageName, list); + } + return typeBuilder; + } + + private GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list, + final List listKeys) { + GeneratedTOBuilder genTOBuilder = null; + if (listKeys.size() > 0) { + genTOBuilder = resolveListKey(packageName, list); + } + return genTOBuilder; + } + + private GeneratedTOBuilder addEnclosedTOToTypeBuilder(TypeDefinition typeDef, GeneratedTypeBuilder typeBuilder, + String leafName) { + String className = parseToClassName(leafName); + GeneratedTOBuilder genTOBuilder = null; + if (typeDef instanceof UnionType) { + genTOBuilder = ((TypeProviderImpl) typeProvider).addUnionGeneratedTypeDefinition( + typeBuilder.getFullyQualifiedName(), typeDef, className); + } else if (typeDef instanceof BitsTypeDefinition) { + genTOBuilder = ((TypeProviderImpl) typeProvider).bitsTypedefToTransferObject( + typeBuilder.getFullyQualifiedName(), typeDef, className); + } + if (genTOBuilder != null) { + typeBuilder.addEnclosingTransferObject(genTOBuilder); + return genTOBuilder; + } + return null; + + } + + /** + * Adds the implemented types to type builder. The method passes through the + * list of elements which contains {@code dataNodeContainer} and adds them + * 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 + * @return generated type builder which contains implemented types + */ + private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer, + final GeneratedTypeBuilder builder) { + for (UsesNode usesNode : dataNodeContainer.getUses()) { + if (usesNode.getGroupingPath() != null) { + GeneratedType genType = allGroupings.get(usesNode.getGroupingPath()); + if (genType == null) { + throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for " + + builder.getName()); + } + builder.addImplementsType(genType); + } + } + return builder; + } + +} diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/BaseYangTypes.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/BaseYangTypes.java index 6f9503ae4f..964731d782 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/BaseYangTypes.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/BaseYangTypes.java @@ -1,69 +1,70 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.yangtools.sal.binding.yang.types; - -import java.math.BigInteger; -import java.util.HashMap; -import java.util.Map; - -import org.opendaylight.yangtools.binding.generator.util.Types; -import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider; -import org.opendaylight.yangtools.sal.binding.model.api.Type; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; - -public final class BaseYangTypes { - - private static Map typeMap = new HashMap(); - - public static final Type BOOLEAN_TYPE = Types.typeForClass(Boolean.class); - public static final Type EMPTY_TYPE = Types.typeForClass(Boolean.class); - public static final Type INT8_TYPE = Types.typeForClass(Byte.class); - public static final Type INT16_TYPE = Types.typeForClass(Short.class); - public static final Type INT32_TYPE = Types.typeForClass(Integer.class); - public static final Type INT64_TYPE = Types.typeForClass(Long.class); - public static final Type STRING_TYPE = Types.typeForClass(String.class); - public static final Type DECIMAL64_TYPE = Types.typeForClass(Double.class); - public static final Type UINT8_TYPE = Types.typeForClass(Short.class); - public static final Type UINT16_TYPE = Types.typeForClass(Integer.class); - public static final Type UINT32_TYPE = Types.typeForClass(Long.class); - public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class); - public static final Type BINARY_TYPE = Types.primitiveType("byte[]"); - - static { - typeMap.put("boolean", BOOLEAN_TYPE); - typeMap.put("empty", EMPTY_TYPE); - typeMap.put("int8", INT8_TYPE); - typeMap.put("int16", INT16_TYPE); - typeMap.put("int32", INT32_TYPE); - typeMap.put("int64", INT64_TYPE); - typeMap.put("string", STRING_TYPE); - typeMap.put("decimal64", DECIMAL64_TYPE); - typeMap.put("uint8", UINT8_TYPE); - typeMap.put("uint16", UINT16_TYPE); - typeMap.put("uint32", UINT32_TYPE); - typeMap.put("uint64", UINT64_TYPE); - typeMap.put("binary", BINARY_TYPE); - } - - public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() { - - @Override - public Type javaTypeForYangType(String type) { - return typeMap.get(type); - } - - @Override - public Type javaTypeForSchemaDefinitionType(TypeDefinition type) { - if (type != null) { - return typeMap.get(type.getQName().getLocalName()); - } - - return null; - } - }; -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.sal.binding.yang.types; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.HashMap; +import java.util.Map; + +import org.opendaylight.yangtools.binding.generator.util.Types; +import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider; +import org.opendaylight.yangtools.sal.binding.model.api.Type; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; + +public final class BaseYangTypes { + + private static Map typeMap = new HashMap(); + + public static final Type BOOLEAN_TYPE = Types.typeForClass(Boolean.class); + public static final Type EMPTY_TYPE = Types.typeForClass(Boolean.class); + public static final Type INT8_TYPE = Types.typeForClass(Byte.class); + public static final Type INT16_TYPE = Types.typeForClass(Short.class); + public static final Type INT32_TYPE = Types.typeForClass(Integer.class); + public static final Type INT64_TYPE = Types.typeForClass(Long.class); + public static final Type STRING_TYPE = Types.typeForClass(String.class); + public static final Type DECIMAL64_TYPE = Types.typeForClass(BigDecimal.class); + public static final Type UINT8_TYPE = Types.typeForClass(Short.class); + public static final Type UINT16_TYPE = Types.typeForClass(Integer.class); + public static final Type UINT32_TYPE = Types.typeForClass(Long.class); + public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class); + public static final Type BINARY_TYPE = Types.primitiveType("byte[]"); + + static { + typeMap.put("boolean", BOOLEAN_TYPE); + typeMap.put("empty", EMPTY_TYPE); + typeMap.put("int8", INT8_TYPE); + typeMap.put("int16", INT16_TYPE); + typeMap.put("int32", INT32_TYPE); + typeMap.put("int64", INT64_TYPE); + typeMap.put("string", STRING_TYPE); + typeMap.put("decimal64", DECIMAL64_TYPE); + typeMap.put("uint8", UINT8_TYPE); + typeMap.put("uint16", UINT16_TYPE); + typeMap.put("uint32", UINT32_TYPE); + typeMap.put("uint64", UINT64_TYPE); + typeMap.put("binary", BINARY_TYPE); + } + + public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() { + + @Override + public Type javaTypeForYangType(String type) { + return typeMap.get(type); + } + + @Override + public Type javaTypeForSchemaDefinitionType(TypeDefinition type) { + if (type != null) { + return typeMap.get(type.getQName().getLocalName()); + } + + return null; + } + }; +} diff --git a/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/AugmentedTypeTest.java b/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/AugmentedTypeTest.java index 5c569d3b4f..b8b8cf7b15 100644 --- a/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/AugmentedTypeTest.java +++ b/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/AugmentedTypeTest.java @@ -1,199 +1,199 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.yangtools.sal.binding.generator.impl; - -import static org.junit.Assert.*; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator; -import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty; -import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject; -import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType; -import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature; -import org.opendaylight.yangtools.sal.binding.model.api.Type; -import org.opendaylight.yangtools.yang.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; -import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; - -public class AugmentedTypeTest { - - private final static List augmentModels = new ArrayList<>(); - private final static String augmentFolderPath = AugmentedTypeTest.class - .getResource("/augment-test-models").getPath(); - - @BeforeClass - public static void loadTestResources() { - final File augFolder = new File(augmentFolderPath); - - for (final File fileEntry : augFolder.listFiles()) { - if (fileEntry.isFile()) { - augmentModels.add(fileEntry); - } - } - } - - @Test - public void augmentedAbstractTopologyTest() { - final YangModelParser parser = new YangParserImpl(); - final Set modules = parser.parseYangModels(augmentModels); - final SchemaContext context = parser.resolveSchemaContext(modules); - - assertNotNull("context is null", context); - final BindingGenerator bindingGen = new BindingGeneratorImpl(); - final List genTypes = bindingGen.generateTypes(context); - - assertNotNull("genTypes is null", genTypes); - assertFalse("genTypes is empty", genTypes.isEmpty()); - - GeneratedTransferObject gtInterfaceKey = null; - GeneratedType gtInterface = null; - GeneratedType gtTunnel = null; - GeneratedTransferObject gtTunnelKey = null; - GeneratedType gtNetworkLink2 = null; - - for (final Type type : genTypes) { - if (type.getName().equals("InterfaceKey") && type.getPackageName().contains("augment._abstract.topology")) { - gtInterfaceKey = (GeneratedTransferObject) type; - } else if (type.getName().equals("Interface") && type.getPackageName().contains("augment._abstract.topology")) { - gtInterface = (GeneratedType) type; - } else if (type.getName().equals("Tunnel") && type.getPackageName().contains("augment._abstract.topology")) { - gtTunnel = (GeneratedType) type; - } else if (type.getName().equals("TunnelKey") && type.getPackageName().contains("augment._abstract.topology")) { - gtTunnelKey = (GeneratedTransferObject) type; - } else if (type.getName().equals("NetworkLink2") && type.getPackageName().contains("augment._abstract.topology")) { - gtNetworkLink2 = (GeneratedType) type; - } - } - - // 'Interface - assertNotNull("gtInterface is null", gtInterface); - final List gtInterfaceMethods = gtInterface.getMethodDefinitions(); - assertNotNull("gtInterfaceMethods is null", gtInterfaceMethods); - MethodSignature getIfcKeyMethod = null; - for (final MethodSignature method : gtInterfaceMethods) { - if (method.getName().equals("getInterfaceKey")) { - getIfcKeyMethod = method; - break; - } - } - assertNotNull("getIfcKeyMethod is null", getIfcKeyMethod); - assertNotNull("getIfcKeyMethod.getReturnType() is null", getIfcKeyMethod.getReturnType()); - assertFalse("getIfcKeyMethod.getReturnType() should not be Void", - getIfcKeyMethod.getReturnType().equals("java.lang.Void")); - assertTrue("getIfcKeyMethod.getReturnType().getName() must be InterfaceKey", - getIfcKeyMethod.getReturnType().getName().equals("InterfaceKey")); - - MethodSignature getHigherLayerIfMethod = null; - for (final MethodSignature method : gtInterfaceMethods) { - if (method.getName().equals("getHigherLayerIf")) { - getHigherLayerIfMethod = method; - break; - } - } - assertNotNull("getHigherLayerIfMethod is null", getHigherLayerIfMethod); - assertNotNull("getHigherLayerIfMethod.getReturnType() is null", - getHigherLayerIfMethod.getReturnType()); - assertFalse("getHigherLayerIfMethod.getReturnType() should not be Void", - getHigherLayerIfMethod.getReturnType().equals("java.lang.Void")); - assertTrue("getHigherLayerIfMethod.getReturnType().getName() must be List", - getHigherLayerIfMethod.getReturnType().getName().equals("List")); - - // 'InterfaceKey' - assertNotNull("gtInterfaceKey is null", gtInterfaceKey); - final List properties = gtInterfaceKey.getProperties(); - assertNotNull("properties is null", properties); - GeneratedProperty gtInterfaceId = null; - for (final GeneratedProperty property : properties) { - if (property.getName().equals("InterfaceId")) { - gtInterfaceId = property; - break; - } - } - assertNotNull("gtInterfaceId is null", gtInterfaceId); - assertNotNull("gtInterfaceId.getReturnType() is null", gtInterfaceId.getReturnType()); - assertFalse("gtInterfaceId.getReturnType() should not be Void", - gtInterfaceId.getReturnType().equals("java.lang.Void")); - assertTrue("gtInterfaceId.getReturnType().getName() must be String", - gtInterfaceId.getReturnType().getName().equals("String")); - - // 'Tunnel' - assertNotNull("gtTunnel is null", gtTunnel); - final List tunnelMethods = gtTunnel.getMethodDefinitions(); - assertNotNull("tunnelMethods is null", tunnelMethods); - MethodSignature getTunnelKeyMethod = null; - for (MethodSignature method : tunnelMethods) { - if (method.getName().equals("getTunnelKey")) { - getTunnelKeyMethod = method; - break; - } - } - assertNotNull("getTunnelKeyMethod is null", getTunnelKeyMethod); - assertNotNull("getTunnelKeyMethod.getReturnType()", - getTunnelKeyMethod.getReturnType()); - assertFalse("getTunnelKeyMethod.getReturnType() should not be Void", - getTunnelKeyMethod.getReturnType().equals("java.lang.Void")); - assertTrue("getTunnelKeyMethod.getReturnType().getName() must be TunnelKey", - getTunnelKeyMethod.getReturnType().getName().equals("TunnelKey")); - - // 'TunnelKey' - assertNotNull("gtTunnelKey is null", gtTunnelKey); - final List tunnelKeyProperties = gtTunnelKey.getProperties(); - assertNotNull("tunnelKeyProperties is null", tunnelKeyProperties); - - GeneratedProperty gtTunnelId = null; - for (final GeneratedProperty property : tunnelKeyProperties) { - if (property.getName().equals("TunnelId")) { - gtTunnelId = property; - } - } - assertNotNull("gtTunnelId is null", gtTunnelId); - assertNotNull("gtTunnelId.getReturnType() is null", - gtTunnelId.getReturnType()); - assertFalse("gtTunnelId.getReturnType() should not be Void", - gtTunnelId.getReturnType().equals("java.lang.Void")); - assertTrue("gtTunnelId.getReturnType().getName() must be Integer", - gtTunnelId.getReturnType().getName().equals("Integer")); - - // 'NetworkLink2' - assertNotNull("gtNetworkLink2 is null", gtNetworkLink2); - - final List networkLink2Methods = gtNetworkLink2.getMethodDefinitions(); - assertNotNull("networkLink2Methods is null", networkLink2Methods); - -// FIXME: in some cases getIfcMethod is null which causes test fail. fix ASAP -// MethodSignature getIfcMethod = null; -// for (MethodSignature method : networkLink2Methods) { -// if (method.getName().equals("getInterface")) { -// getIfcMethod = method; -// break; -// } -// } -// -// assertNotNull("getIfcMethod is null", getIfcMethod); -// assertNotNull("getIfcMethod.getReturnType() is null", getIfcMethod.getReturnType()); -// assertFalse("getIfcMethod.getReturnType() should not be Void", getIfcMethod.getReturnType().equals("java.lang.Void")); -// assertTrue("getIfcMethod.getReturnType().getName() must be String", getIfcMethod.getReturnType().getName().equals("String")); - } - - @Test - public void augmentedNetworkLinkTest() { - - } - - @Test - public void augmentedTopologyTunnelsTest() { - - } -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.sal.binding.generator.impl; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator; +import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty; +import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject; +import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType; +import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature; +import org.opendaylight.yangtools.sal.binding.model.api.Type; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; +import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; + +public class AugmentedTypeTest { + + private final static List augmentModels = new ArrayList<>(); + private final static String augmentFolderPath = AugmentedTypeTest.class + .getResource("/augment-test-models").getPath(); + + @BeforeClass + public static void loadTestResources() { + final File augFolder = new File(augmentFolderPath); + + for (final File fileEntry : augFolder.listFiles()) { + if (fileEntry.isFile()) { + augmentModels.add(fileEntry); + } + } + } + + @Test + public void augmentedAbstractTopologyTest() { + final YangModelParser parser = new YangParserImpl(); + final Set modules = parser.parseYangModels(augmentModels); + final SchemaContext context = parser.resolveSchemaContext(modules); + + assertNotNull("context is null", context); + final BindingGenerator bindingGen = new BindingGeneratorImpl(); + final List genTypes = bindingGen.generateTypes(context); + + assertNotNull("genTypes is null", genTypes); + assertFalse("genTypes is empty", genTypes.isEmpty()); + + GeneratedTransferObject gtInterfaceKey = null; + GeneratedType gtInterface = null; + GeneratedType gtTunnel = null; + GeneratedTransferObject gtTunnelKey = null; + GeneratedType gtNetworkLink2 = null; + + for (final Type type : genTypes) { + if (type.getName().equals("InterfaceKey") && type.getPackageName().contains("augment._abstract.topology")) { + gtInterfaceKey = (GeneratedTransferObject) type; + } else if (type.getName().equals("Interface") && type.getPackageName().contains("augment._abstract.topology")) { + gtInterface = (GeneratedType) type; + } else if (type.getName().equals("Tunnel") && type.getPackageName().contains("augment._abstract.topology")) { + gtTunnel = (GeneratedType) type; + } else if (type.getName().equals("TunnelKey") && type.getPackageName().contains("augment._abstract.topology")) { + gtTunnelKey = (GeneratedTransferObject) type; + } else if (type.getName().equals("NetworkLink2") && type.getPackageName().contains("augment._abstract.topology")) { + gtNetworkLink2 = (GeneratedType) type; + } + } + + // 'Interface + assertNotNull("gtInterface is null", gtInterface); + final List gtInterfaceMethods = gtInterface.getMethodDefinitions(); + assertNotNull("gtInterfaceMethods is null", gtInterfaceMethods); + MethodSignature getIfcKeyMethod = null; + for (final MethodSignature method : gtInterfaceMethods) { + if (method.getName().equals("getKey")) { + getIfcKeyMethod = method; + break; + } + } + assertNotNull("getIfcKeyMethod is null", getIfcKeyMethod); + assertNotNull("getIfcKeyMethod.getReturnType() is null", getIfcKeyMethod.getReturnType()); + assertFalse("getIfcKeyMethod.getReturnType() should not be Void", + getIfcKeyMethod.getReturnType().equals("java.lang.Void")); + assertTrue("getIfcKeyMethod.getReturnType().getName() must be InterfaceKey", + getIfcKeyMethod.getReturnType().getName().equals("InterfaceKey")); + + MethodSignature getHigherLayerIfMethod = null; + for (final MethodSignature method : gtInterfaceMethods) { + if (method.getName().equals("getHigherLayerIf")) { + getHigherLayerIfMethod = method; + break; + } + } + assertNotNull("getHigherLayerIfMethod is null", getHigherLayerIfMethod); + assertNotNull("getHigherLayerIfMethod.getReturnType() is null", + getHigherLayerIfMethod.getReturnType()); + assertFalse("getHigherLayerIfMethod.getReturnType() should not be Void", + getHigherLayerIfMethod.getReturnType().equals("java.lang.Void")); + assertTrue("getHigherLayerIfMethod.getReturnType().getName() must be List", + getHigherLayerIfMethod.getReturnType().getName().equals("List")); + + // 'InterfaceKey' + assertNotNull("gtInterfaceKey is null", gtInterfaceKey); + final List properties = gtInterfaceKey.getProperties(); + assertNotNull("properties is null", properties); + GeneratedProperty gtInterfaceId = null; + for (final GeneratedProperty property : properties) { + if (property.getName().equals("InterfaceId")) { + gtInterfaceId = property; + break; + } + } + assertNotNull("gtInterfaceId is null", gtInterfaceId); + assertNotNull("gtInterfaceId.getReturnType() is null", gtInterfaceId.getReturnType()); + assertFalse("gtInterfaceId.getReturnType() should not be Void", + gtInterfaceId.getReturnType().equals("java.lang.Void")); + assertTrue("gtInterfaceId.getReturnType().getName() must be String", + gtInterfaceId.getReturnType().getName().equals("String")); + + // 'Tunnel' + assertNotNull("gtTunnel is null", gtTunnel); + final List tunnelMethods = gtTunnel.getMethodDefinitions(); + assertNotNull("tunnelMethods is null", tunnelMethods); + MethodSignature getTunnelKeyMethod = null; + for (MethodSignature method : tunnelMethods) { + if (method.getName().equals("getKey")) { + getTunnelKeyMethod = method; + break; + } + } + assertNotNull("getTunnelKeyMethod is null", getTunnelKeyMethod); + assertNotNull("getTunnelKeyMethod.getReturnType()", + getTunnelKeyMethod.getReturnType()); + assertFalse("getTunnelKeyMethod.getReturnType() should not be Void", + getTunnelKeyMethod.getReturnType().equals("java.lang.Void")); + assertTrue("getTunnelKeyMethod.getReturnType().getName() must be TunnelKey", + getTunnelKeyMethod.getReturnType().getName().equals("TunnelKey")); + + // 'TunnelKey' + assertNotNull("gtTunnelKey is null", gtTunnelKey); + final List tunnelKeyProperties = gtTunnelKey.getProperties(); + assertNotNull("tunnelKeyProperties is null", tunnelKeyProperties); + + GeneratedProperty gtTunnelId = null; + for (final GeneratedProperty property : tunnelKeyProperties) { + if (property.getName().equals("TunnelId")) { + gtTunnelId = property; + } + } + assertNotNull("gtTunnelId is null", gtTunnelId); + assertNotNull("gtTunnelId.getReturnType() is null", + gtTunnelId.getReturnType()); + assertFalse("gtTunnelId.getReturnType() should not be Void", + gtTunnelId.getReturnType().equals("java.lang.Void")); + assertTrue("gtTunnelId.getReturnType().getName() must be Integer", + gtTunnelId.getReturnType().getName().equals("Integer")); + + // 'NetworkLink2' + assertNotNull("gtNetworkLink2 is null", gtNetworkLink2); + + final List networkLink2Methods = gtNetworkLink2.getMethodDefinitions(); + assertNotNull("networkLink2Methods is null", networkLink2Methods); + +// FIXME: in some cases getIfcMethod is null which causes test fail. fix ASAP +// MethodSignature getIfcMethod = null; +// for (MethodSignature method : networkLink2Methods) { +// if (method.getName().equals("getInterface")) { +// getIfcMethod = method; +// break; +// } +// } +// +// assertNotNull("getIfcMethod is null", getIfcMethod); +// assertNotNull("getIfcMethod.getReturnType() is null", getIfcMethod.getReturnType()); +// assertFalse("getIfcMethod.getReturnType() should not be Void", getIfcMethod.getReturnType().equals("java.lang.Void")); +// assertTrue("getIfcMethod.getReturnType().getName() must be String", getIfcMethod.getReturnType().getName().equals("String")); + } + + @Test + public void augmentedNetworkLinkTest() { + + } + + @Test + public void augmentedTopologyTunnelsTest() { + + } +} diff --git a/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/GeneratedTypesTest.java b/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/GeneratedTypesTest.java index e82b24ba40..0658adaec3 100644 --- a/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/GeneratedTypesTest.java +++ b/code-generator/binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/GeneratedTypesTest.java @@ -158,7 +158,7 @@ public class GeneratedTypesTest { MethodSignature getIfcKey = null; MethodSignature getHigherLayerIf = null; for (final MethodSignature method : gtIfcMethods) { - if (method.getName().equals("getInterfaceKey")) { + if (method.getName().equals("getKey")) { getIfcKey = method; } else if (method.getName().equals("getHigherLayerIf")) { getHigherLayerIf = method; @@ -226,7 +226,7 @@ public class GeneratedTypesTest { assertNotNull(gtTunnelMethods); MethodSignature getTunnelKey = null; for (MethodSignature method : gtTunnelMethods) { - if (method.getName().equals("getTunnelKey")) { + if (method.getName().equals("getKey")) { getTunnelKey = method; } } @@ -459,7 +459,7 @@ public class GeneratedTypesTest { simpleListMethodsCount = genType.getMethodDefinitions().size(); final List methods = genType.getMethodDefinitions(); for (final MethodSignature method : methods) { - if (method.getName().equals("getSimpleListKey")) { + if (method.getName().equals("getKey")) { getSimpleListKeyMethodCount++; getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName(); } else if (method.getName().equals("getListChildContainer")) { diff --git a/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Identifiable.java b/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Identifiable.java new file mode 100644 index 0000000000..4eb04e4dc6 --- /dev/null +++ b/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Identifiable.java @@ -0,0 +1,21 @@ +package org.opendaylight.yangtools.yang.binding; + + +/** + * Object is uniquely identifiable in its scope by key + * + * + * + * @author ttkacik + * + * @param Identifier class for this object + */ +public interface Identifiable>> { + + /** + * Returns an unique key for the object + * + * @return Key for the object + */ + T getKey(); +} diff --git a/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Identifier.java b/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Identifier.java new file mode 100644 index 0000000000..bdc386d7ea --- /dev/null +++ b/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Identifier.java @@ -0,0 +1,13 @@ +package org.opendaylight.yangtools.yang.binding; + +/** + * + * Object is unique identifier for another object + * + * @author ttkacik + * + * @param Class of object for which this object is identifier + */ +public interface Identifier> { + +} diff --git a/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java b/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java index 42cc11a554..832a9ed58a 100644 --- a/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java +++ b/yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java @@ -1,19 +1,81 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.yangtools.yang.binding; - -/** - * Created with IntelliJ IDEA. - * User: lsedlak - * Date: 27.6.2013 - * Time: 11:44 - * To change this template use File | Settings | File Templates. - */ -public class InstanceIdentifier { - -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.binding; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Uniquely identifies instance of data tree. + * + * + */ +public class InstanceIdentifier { + + + private final List path; + private final Class targetType; + + public InstanceIdentifier(Class type) { + path = Collections.emptyList(); + this.targetType = type; + } + + + public InstanceIdentifier(List path,Class type) { + this.path = Collections.unmodifiableList(new ArrayList<>(path)); + this.targetType = type; + } + + + /** + * + * @return + */ + public List getPath() { + return this.path; + } + + public Class getTargetType() { + return this.targetType; + } + + + /** + * Path argument of instance identifier. + * + * Interface which implementations are used as path components + * of the instance path. + * + * @author ttkacik + * + */ + public static interface PathArgument { + + } + + public static class IdentifiableItem,T extends Identifier> implements PathArgument { + + private final T key; + private final Class type; + + public IdentifiableItem(Class type, T key) { + this.type = type; + this.key = key; + } + + T getKey() { + return this.key; + } + + Class getType() { + return this.type; + } + } +}