-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.controller.sal.binding.yang.types;\r
-\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-import java.util.Queue;\r
-import java.util.Set;\r
-\r
-import org.opendaylight.controller.binding.generator.util.Types;\r
-import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider;\r
-import org.opendaylight.controller.sal.binding.model.api.Type;\r
-import org.opendaylight.controller.yang.common.QName;\r
-import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.DataNodeContainer;\r
-import org.opendaylight.controller.yang.model.api.DataSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.LeafSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.ListSchemaNode;\r
-import org.opendaylight.controller.yang.model.api.Module;\r
-import org.opendaylight.controller.yang.model.api.ModuleImport;\r
-import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;\r
-import org.opendaylight.controller.yang.model.api.SchemaContext;\r
-import org.opendaylight.controller.yang.model.api.SchemaPath;\r
-import org.opendaylight.controller.yang.model.api.TypeDefinition;\r
-import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition;\r
-import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition;\r
-import org.opendaylight.controller.yang.model.util.Leafref;\r
-\r
-public class TypeProviderImpl implements TypeProvider {\r
-\r
- private SchemaContext schemaContext;\r
-\r
- public TypeProviderImpl(SchemaContext schemaContext) {\r
- this.schemaContext = schemaContext;\r
- }\r
-\r
- /*\r
- * (non-Javadoc)\r
- * \r
- * @see org.opendaylight.controller.yang.model.type.provider.TypeProvider#\r
- * javaTypeForYangType(java.lang.String)\r
- */\r
- @Override\r
- public Type javaTypeForYangType(String type) {\r
- Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER\r
- .javaTypeForYangType(type);\r
- return t;\r
- }\r
-\r
- @Override\r
- public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type) {\r
- Type returnType = null;\r
- if (type != null) {\r
- if (type instanceof Leafref) {\r
- final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) type;\r
- returnType = provideTypeForLeafref(leafref);\r
- } else if (type instanceof IdentityrefTypeDefinition) {\r
-\r
- } else {\r
- returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER\r
- .javaTypeForSchemaDefinitionType(type);\r
- }\r
- }\r
- return returnType;\r
- }\r
-\r
- public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) {\r
- Type returnType = null;\r
- if ((leafrefType != null) && (leafrefType.getPathStatement() != null)) {\r
- final RevisionAwareXPath xpath = leafrefType.getPathStatement();\r
- final String strXPath = xpath.toString();\r
-\r
- if (strXPath != null) {\r
- if (strXPath.matches(".*//[.* | .*//].*")) {\r
- returnType = Types.typeForClass(Object.class);\r
- } else {\r
- final Module module = resolveModuleFromSchemaContext(leafrefType\r
- .getPath());\r
- if (module != null) {\r
- Queue<String> leafrefPath;\r
- if (!xpath.isAbsolute()) {\r
- leafrefPath = resolveRelativeXPath(xpath,\r
- leafrefType.getPath());\r
- } else {\r
- leafrefPath = xpathToPrefixedPath(strXPath, module.getName());\r
- }\r
-\r
- if (leafrefPath != null) {\r
- final DataSchemaNode dataNode = findSchemaNodeForGivenPath(\r
- module, leafrefPath);\r
- returnType = resolveTypeFromDataSchemaNode(dataNode);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- return returnType;\r
- }\r
-\r
- private Type resolveTypeFromDataSchemaNode(final DataSchemaNode dataNode) {\r
- Type returnType = null;\r
- if (dataNode != null) {\r
- if (dataNode instanceof LeafSchemaNode) {\r
- final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;\r
- returnType = javaTypeForSchemaDefinitionType(leaf.getType());\r
- } else if (dataNode instanceof LeafListSchemaNode) {\r
- final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;\r
- returnType = javaTypeForSchemaDefinitionType(leafList.getType());\r
- }\r
- }\r
- return returnType;\r
- }\r
-\r
- /**\r
- * Search which starts from root of Module.\r
- * \r
- * @param module\r
- * @param prefixedPath\r
- * @return\r
- */\r
- private DataSchemaNode findSchemaNodeForGivenPath(final Module module,\r
- final Queue<String> prefixedPath) {\r
- if ((module != null) && (prefixedPath != null)) {\r
- final String modulePrefix = module.getPrefix();\r
- String childNodeName = prefixedPath.poll();\r
- DataNodeContainer nextContainer = null;\r
-\r
- if ((childNodeName != null)\r
- && childNodeName.equals(module.getName())) {\r
- nextContainer = module;\r
- }\r
-\r
- DataSchemaNode schemaNode = null;\r
- while ((nextContainer != null) && (prefixedPath.size() > 0)) {\r
- childNodeName = prefixedPath.poll();\r
- if (childNodeName.contains(":")) {\r
- final String[] prefixedChildNode = childNodeName.split(":");\r
- if ((modulePrefix != null)\r
- && modulePrefix.equals(prefixedChildNode[0])) {\r
- \r
- childNodeName = prefixedChildNode[1];\r
- } else {\r
- final Module nextModule = resolveModuleForPrefix(\r
- prefixedChildNode[0], module);\r
- final Queue<String> nextModulePrefixedPath = new LinkedList<String>();\r
- \r
- nextModulePrefixedPath.add(nextModule.getName());\r
- nextModulePrefixedPath.add(childNodeName);\r
- nextModulePrefixedPath.addAll(prefixedPath);\r
- prefixedPath.clear();\r
- \r
- schemaNode = findSchemaNodeForGivenPath(nextModule,\r
- nextModulePrefixedPath);\r
- \r
- return schemaNode;\r
- }\r
- }\r
- \r
- schemaNode = nextContainer.getDataChildByName(childNodeName);\r
- if (schemaNode instanceof ContainerSchemaNode) {\r
- nextContainer = (ContainerSchemaNode) schemaNode;\r
- } else if (schemaNode instanceof ListSchemaNode) {\r
- nextContainer = (ListSchemaNode) schemaNode;\r
- } else {\r
- return schemaNode;\r
- }\r
- }\r
- }\r
-\r
- return null;\r
- }\r
-\r
- private Module resolveModuleFromSchemaContext(final SchemaPath schemaPath) {\r
- final Set<Module> modules = schemaContext.getModules();\r
- final String moduleName = resolveModuleName(schemaPath);\r
- if ((moduleName != null) && (modules != null)) {\r
- for (final Module module : modules) {\r
- if (module.getName().equals(moduleName)) {\r
- return module;\r
- }\r
- }\r
- }\r
- return null;\r
- }\r
-\r
- private String resolveModuleName(final SchemaPath schemaPath) {\r
- if ((schemaPath != null) && (schemaPath.getPath() != null)) {\r
- final QName qname = schemaPath.getPath().get(0);\r
- if ((qname != null) && (qname.getLocalName() != null)) {\r
- return qname.getLocalName();\r
- }\r
- }\r
- return "";\r
- }\r
-\r
- private Queue<String> xpathToPrefixedPath(final String xpath, final String moduleName) {\r
- final Queue<String> retQueue = new LinkedList<String>();\r
- if ((xpath != null) && (moduleName != null)) {\r
- final String[] prefixedPath = xpath.split("/");\r
- \r
- retQueue.add(moduleName);\r
- if (prefixedPath != null) {\r
- for (int i = 0; i < prefixedPath.length; ++i) {\r
- if (!prefixedPath[i].isEmpty()) {\r
- retQueue.add(prefixedPath[i]);\r
- }\r
- }\r
- }\r
- }\r
- return retQueue;\r
- }\r
-\r
- private Module resolveModuleForPrefix(final String prefix,\r
- final Module parent) {\r
- if ((prefix != null) && (parent != null)) {\r
- final Set<ModuleImport> imports = parent.getImports();\r
-\r
- if (imports != null) {\r
- for (final ModuleImport impModule : imports) {\r
- final String impModPrefix = impModule.getPrefix();\r
- if ((impModPrefix != null) && prefix.equals(impModPrefix)) {\r
- return resolveModuleFromContext(prefix,\r
- impModule.getModuleName());\r
- }\r
- }\r
- }\r
- }\r
- return null;\r
- }\r
-\r
- private Module resolveModuleFromContext(final String prefix,\r
- final String moduleName) {\r
- final Set<Module> modules = schemaContext.getModules();\r
-\r
- if ((prefix != null) && (moduleName != null) && (modules != null)) {\r
- for (Module module : modules) {\r
- if ((module != null) && prefix.equals(module.getPrefix())\r
- && moduleName.equals(module.getName())) {\r
- return module;\r
- }\r
- }\r
- }\r
- return null;\r
- }\r
-\r
- private Queue<String> resolveRelativeXPath(\r
- final RevisionAwareXPath relativeXPath,\r
- final SchemaPath leafrefSchemaPath) {\r
- final Queue<String> absolutePath = new LinkedList<String>();\r
-\r
- if ((relativeXPath != null) && !relativeXPath.isAbsolute()\r
- && (leafrefSchemaPath != null)) {\r
- final String strXPath = relativeXPath.toString();\r
- if (strXPath != null) {\r
- final String[] xpaths = strXPath.split("/");\r
-\r
- if (xpaths != null) {\r
- int colCount = 0;\r
- while (xpaths[colCount].contains("..")) {\r
- ++colCount;\r
- }\r
- final List<QName> path = leafrefSchemaPath.getPath();\r
- if (path != null) {\r
- int lenght = path.size() - colCount;\r
- for (int i = 0; i < lenght; ++i) {\r
- absolutePath.add(path.get(i).getLocalName());\r
- }\r
- for (int i = colCount; i < xpaths.length; ++i) {\r
- absolutePath.add(xpaths[i]);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- return absolutePath;\r
- }\r
-}\r
+/*
+ * 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.controller.sal.binding.yang.types;
+
+import org.opendaylight.controller.binding.generator.util.ReferencedTypeImpl;
+import org.opendaylight.controller.binding.generator.util.Types;
+import org.opendaylight.controller.binding.generator.util.generated.type.builder.EnumerationBuilderImpl;
+import org.opendaylight.controller.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
+import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider;
+import org.opendaylight.controller.sal.binding.model.api.Enumeration;
+import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.controller.sal.binding.model.api.Type;
+import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;
+import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
+import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;
+import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
+import org.opendaylight.controller.yang.model.api.*;
+import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition;
+import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair;
+import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition;
+import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition;
+import org.opendaylight.controller.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.controller.yang.model.util.ExtendedType;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.opendaylight.controller.binding.generator.util.BindingGeneratorUtil.*;
+import static org.opendaylight.controller.yang.model.util.SchemaContextUtil.*;
+
+public final class TypeProviderImpl implements TypeProvider {
+
+ private final SchemaContext schemaContext;
+ private Map<String, Map<String, Type>> genTypeDefsContextMap;
+ private final Map<SchemaPath, Type> referencedTypes;
+
+ public TypeProviderImpl(final SchemaContext schemaContext) {
+ if (schemaContext == null) {
+ throw new IllegalArgumentException("Schema Context cannot be null!");
+ }
+
+ this.schemaContext = schemaContext;
+ this.genTypeDefsContextMap = new HashMap<>();
+ this.referencedTypes = new HashMap<>();
+ resolveTypeDefsFromContext();
+ }
+
+ public void putReferencedType(final SchemaPath refTypePath,
+ final Type refType) {
+ if (refTypePath == null) {
+ throw new IllegalArgumentException("Path reference of " +
+ "Enumeration Type Definition cannot be NULL!");
+ }
+
+ if (refType == null) {
+ throw new IllegalArgumentException("Reference to Enumeration " +
+ "Type cannot be NULL!");
+ }
+ referencedTypes.put(refTypePath, refType);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.opendaylight.controller.yang.model.type.provider.TypeProvider#
+ * javaTypeForYangType(java.lang.String)
+ */
+ @Override
+ public Type javaTypeForYangType(String type) {
+ Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
+ .javaTypeForYangType(type);
+ return t;
+ }
+
+ @Override
+ public Type javaTypeForSchemaDefinitionType(
+ final TypeDefinition<?> typeDefinition) {
+ Type returnType = null;
+ if (typeDefinition == null) {
+ throw new IllegalArgumentException("Type Definition cannot be " +
+ "NULL!");
+ }
+ if (typeDefinition.getQName() == null) {
+ throw new IllegalArgumentException("Type Definition cannot have " +
+ "non specified QName (QName cannot be NULL!)");
+ }
+ if (typeDefinition.getQName().getLocalName() == null) {
+ throw new IllegalArgumentException("Type Definitions Local Name " +
+ "cannot be NULL!");
+ }
+ final String typedefName = typeDefinition.getQName().getLocalName();
+ if (typeDefinition instanceof ExtendedType) {
+ final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
+
+ if (baseTypeDef instanceof LeafrefTypeDefinition) {
+ final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) baseTypeDef;
+ returnType = provideTypeForLeafref(leafref);
+ } else if (baseTypeDef instanceof IdentityrefTypeDefinition) {
+
+ } else if (baseTypeDef instanceof EnumTypeDefinition) {
+ final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDef;
+ returnType = resolveEnumFromTypeDefinition(enumTypeDef,
+ typedefName);
+ } else {
+ final Module module = findParentModuleForTypeDefinition(schemaContext,
+ typeDefinition);
+ if (module != null) {
+ final Map<String, Type> genTOs = genTypeDefsContextMap
+ .get(module.getName());
+ if (genTOs != null) {
+ returnType = genTOs.get(typedefName);
+ }
+ if (returnType == null) {
+ returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
+ .javaTypeForSchemaDefinitionType(baseTypeDef);
+ }
+ }
+ }
+ } else {
+ if (typeDefinition instanceof LeafrefTypeDefinition) {
+ final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) typeDefinition;
+ returnType = provideTypeForLeafref(leafref);
+ } else if (typeDefinition instanceof IdentityrefTypeDefinition) {
+
+ } else {
+ returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
+ .javaTypeForSchemaDefinitionType(typeDefinition);
+ }
+ }
+ //TODO: add throw exception when we will be able to resolve ALL yang
+ // types!
+// if (returnType == null) {
+// throw new IllegalArgumentException("Type Provider can't resolve " +
+// "type for specified Type Definition " + typedefName);
+// }
+ return returnType;
+ }
+
+ public Type generatedTypeForExtendedDefinitionType(
+ final TypeDefinition<?> typeDefinition) {
+ Type returnType = null;
+ if (typeDefinition == null) {
+ throw new IllegalArgumentException("Type Definition cannot be " +
+ "NULL!");
+ }
+ if (typeDefinition.getQName() == null) {
+ throw new IllegalArgumentException("Type Definition cannot have " +
+ "non specified QName (QName cannot be NULL!)");
+ }
+ if (typeDefinition.getQName() == null) {
+ throw new IllegalArgumentException("Type Definitions Local Name " +
+ "cannot be NULL!");
+ }
+
+ final String typedefName = typeDefinition.getQName().getLocalName();
+ if (typeDefinition instanceof ExtendedType) {
+ final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
+
+ if (!(baseTypeDef instanceof LeafrefTypeDefinition)
+ && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
+ final Module module = findParentModuleForTypeDefinition(schemaContext,
+ typeDefinition);
+
+ if (module != null) {
+ final Map<String, Type> genTOs = genTypeDefsContextMap
+ .get(module.getName());
+ if (genTOs != null) {
+ returnType = genTOs.get(typedefName);
+ }
+ }
+ }
+ }
+ return returnType;
+ }
+
+ private TypeDefinition<?> baseTypeDefForExtendedType(
+ final TypeDefinition<?> extendTypeDef) {
+ if (extendTypeDef == null) {
+ throw new IllegalArgumentException("Type Definiition reference " +
+ "cannot be NULL!");
+ }
+ final TypeDefinition<?> baseTypeDef = extendTypeDef.getBaseType();
+ if (baseTypeDef instanceof ExtendedType) {
+ return baseTypeDefForExtendedType(baseTypeDef);
+ } else {
+ return baseTypeDef;
+ }
+
+ }
+
+ public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) {
+ Type returnType = null;
+ if (leafrefType == null) {
+ throw new IllegalArgumentException("Leafref Type Definition " +
+ "reference cannot be NULL!");
+ }
+
+ if (leafrefType.getPathStatement() == null) {
+ throw new IllegalArgumentException("The Path Statement for " +
+ "Leafref Type Definition cannot be NULL!");
+ }
+
+ final RevisionAwareXPath xpath = leafrefType.getPathStatement();
+ final String strXPath = xpath.toString();
+
+ if (strXPath != null) {
+ if (strXPath.matches(".*//[.* | .*//].*")) {
+ returnType = Types.typeForClass(Object.class);
+ } else {
+ final Module module = findParentModuleForTypeDefinition(schemaContext, leafrefType);
+ if (module != null) {
+ final DataSchemaNode dataNode;
+ if (xpath.isAbsolute()) {
+ dataNode = findDataSchemaNode(schemaContext, module,
+ xpath);
+ } else {
+ dataNode = findDataSchemaNodeForRelativeXPath(schemaContext,
+ module, leafrefType, xpath);
+ }
+
+ if (leafContainsEnumDefinition(dataNode)) {
+ returnType = referencedTypes.get(dataNode.getPath());
+ } else if (leafListContainsEnumDefinition(dataNode)) {
+ returnType = Types.listTypeFor(referencedTypes.get(
+ dataNode.getPath()));
+ } else {
+ returnType = resolveTypeFromDataSchemaNode(dataNode);
+ }
+ }
+ }
+ }
+ return returnType;
+ }
+
+ private boolean leafContainsEnumDefinition(final DataSchemaNode dataNode) {
+ if (dataNode instanceof LeafSchemaNode) {
+ final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
+ if (leaf.getType() instanceof EnumTypeDefinition) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean leafListContainsEnumDefinition(
+ final DataSchemaNode dataNode) {
+ if (dataNode instanceof LeafListSchemaNode) {
+ final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
+ if (leafList.getType() instanceof EnumTypeDefinition) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private Enumeration resolveEnumFromTypeDefinition(
+ final EnumTypeDefinition enumTypeDef, final String enumName) {
+ if (enumTypeDef == null) {
+ throw new IllegalArgumentException("EnumTypeDefinition reference " +
+ "cannot be NULL!");
+ }
+ if (enumTypeDef.getValues() == null) {
+ throw new IllegalArgumentException("EnumTypeDefinition MUST " +
+ "contain at least ONE value definition!");
+ }
+ if (enumTypeDef.getQName() == null) {
+ throw new IllegalArgumentException("EnumTypeDefinition MUST " +
+ "contain NON-NULL QName!");
+ }
+ if (enumTypeDef.getQName().getLocalName() == null) {
+ throw new IllegalArgumentException("Local Name in " +
+ "EnumTypeDefinition QName cannot be NULL!");
+ }
+
+ final String enumerationName = parseToClassName(enumName);
+
+ Module module = findParentModuleForTypeDefinition(schemaContext, enumTypeDef);
+ final String basePackageName = moduleNamespaceToPackageName(module);
+
+ final EnumBuilder enumBuilder = new EnumerationBuilderImpl(
+ basePackageName, enumerationName);
+ updateEnumPairsFromEnumTypeDef(enumTypeDef, enumBuilder);
+ return enumBuilder.toInstance(null);
+ }
+
+ private EnumBuilder resolveInnerEnumFromTypeDefinition(
+ final EnumTypeDefinition enumTypeDef, final String enumName,
+ final GeneratedTypeBuilder typeBuilder) {
+ if (enumTypeDef == null) {
+ throw new IllegalArgumentException("EnumTypeDefinition reference " +
+ "cannot be NULL!");
+ }
+ if (enumTypeDef.getValues() == null) {
+ throw new IllegalArgumentException("EnumTypeDefinition MUST " +
+ "contain at least ONE value definition!");
+ }
+ if (enumTypeDef.getQName() == null) {
+ throw new IllegalArgumentException("EnumTypeDefinition MUST " +
+ "contain NON-NULL QName!");
+ }
+ if (enumTypeDef.getQName().getLocalName() == null) {
+ throw new IllegalArgumentException("Local Name in " +
+ "EnumTypeDefinition QName cannot be NULL!");
+ }
+ if (typeBuilder == null) {
+ throw new IllegalArgumentException("Generated Type Builder " +
+ "reference cannot be NULL!");
+ }
+
+ final String enumerationName = parseToClassName(enumName);
+ final EnumBuilder enumBuilder = typeBuilder
+ .addEnumeration(enumerationName);
+
+ updateEnumPairsFromEnumTypeDef(enumTypeDef, enumBuilder);
+
+ return enumBuilder;
+ }
+
+ private void updateEnumPairsFromEnumTypeDef(
+ final EnumTypeDefinition enumTypeDef,
+ final EnumBuilder enumBuilder) {
+ if (enumBuilder != null) {
+ final List<EnumPair> 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++;
+ }
+ }
+ }
+ }
+ }
+
+ private Type resolveTypeFromDataSchemaNode(final DataSchemaNode dataNode) {
+ Type returnType = null;
+ if (dataNode != null) {
+ if (dataNode instanceof LeafSchemaNode) {
+ final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
+ returnType = javaTypeForSchemaDefinitionType(leaf.getType());
+ } else if (dataNode instanceof LeafListSchemaNode) {
+ final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
+ returnType = javaTypeForSchemaDefinitionType(leafList.getType());
+ }
+ }
+ return returnType;
+ }
+
+ private void resolveTypeDefsFromContext() {
+ final Set<Module> modules = schemaContext.getModules();
+ if (modules == null) {
+ throw new IllegalArgumentException("Sef of Modules cannot be " +
+ "NULL!");
+ }
+ for (final Module module : modules) {
+ if (module == null) {
+ continue;
+ }
+ final String moduleName = module.getName();
+ final String basePackageName = moduleNamespaceToPackageName(module);
+
+ final Set<TypeDefinition<?>> typeDefinitions = module
+ .getTypeDefinitions();
+
+ final Map<String, Type> typeMap = new HashMap<>();
+ genTypeDefsContextMap.put(moduleName, typeMap);
+
+ if ((typeDefinitions != null) && (basePackageName != null)) {
+ for (final TypeDefinition<?> typedef : typeDefinitions) {
+ typedefToGeneratedType(basePackageName, moduleName, typedef);
+ }
+ final List<ExtendedType> extUnions = UnionDependencySort
+ .sort(typeDefinitions);
+ for (final ExtendedType extUnionType : extUnions) {
+ addUnionGeneratedTypeDefinition(basePackageName, extUnionType);
+ }
+ }
+ }
+ }
+
+ private Type typedefToGeneratedType(final String basePackageName,
+ final String moduleName, final TypeDefinition<?> typedef) {
+ if ((basePackageName != null) && (moduleName != null)
+ && (typedef != null) && (typedef.getQName() != null)) {
+
+
+ final String typedefName = typedef.getQName().getLocalName();
+ final TypeDefinition<?> baseTypeDefinition = baseTypeDefForExtendedType(typedef);
+ if (!(baseTypeDefinition instanceof LeafrefTypeDefinition)
+ && !(baseTypeDefinition instanceof IdentityrefTypeDefinition)) {
+ Type returnType;
+ if (baseTypeDefinition instanceof EnumTypeDefinition) {
+ final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDefinition;
+ returnType = resolveEnumFromTypeDefinition(enumTypeDef,
+ typedefName);
+
+ } else {
+ final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
+ .javaTypeForSchemaDefinitionType(baseTypeDefinition);
+
+ returnType = wrapJavaTypeIntoTO(basePackageName, typedef,
+ javaType);
+ }
+ if (returnType != null) {
+ final Map<String, Type> typeMap = genTypeDefsContextMap.get
+ (moduleName);
+ if (typeMap != null) {
+ typeMap.put(typedefName, returnType);
+ }
+ return returnType;
+ }
+ }
+ }
+ return null;
+ }
+
+ private GeneratedTransferObject wrapJavaTypeIntoTO(
+ final String basePackageName, final TypeDefinition<?> typedef,
+ final Type javaType) {
+ if (javaType != null) {
+ final String typedefName = typedef.getQName().getLocalName();
+ final String propertyName = parseToValidParamName(typedefName);
+
+ final GeneratedTOBuilder genTOBuilder = typedefToTransferObject(
+ basePackageName, typedef);
+
+ final GeneratedPropertyBuilder genPropBuilder = genTOBuilder
+ .addProperty(propertyName);
+
+ genPropBuilder.addReturnType(javaType);
+ genTOBuilder.addEqualsIdentity(genPropBuilder);
+ genTOBuilder.addHashIdentity(genPropBuilder);
+ genTOBuilder.addToStringProperty(genPropBuilder);
+ return genTOBuilder.toInstance();
+ }
+ return null;
+ }
+
+ private void addUnionGeneratedTypeDefinition(final String basePackageName,
+ final TypeDefinition<?> typedef) {
+ if (basePackageName == null) {
+ throw new IllegalArgumentException("Base Package Name cannot be " +
+ "NULL!");
+ }
+ if (typedef == null) {
+ throw new IllegalArgumentException("Type Definition cannot be " +
+ "NULL!");
+ }
+ if (typedef.getQName() == null) {
+ throw new IllegalArgumentException("Type Definition cannot have " +
+ "non specified QName (QName cannot be NULL!)");
+ }
+
+ final TypeDefinition<?> baseTypeDefinition = typedef.getBaseType();
+ if ((baseTypeDefinition != null)
+ && (baseTypeDefinition instanceof UnionTypeDefinition)) {
+ final UnionTypeDefinition unionTypeDef = (UnionTypeDefinition) baseTypeDefinition;
+ final List<TypeDefinition<?>> unionTypes = unionTypeDef
+ .getTypes();
+ final Module parentModule = findParentModuleForTypeDefinition(schemaContext,
+ typedef);
+
+ Map<String, Type> genTOsMap = null;
+ if (parentModule != null && parentModule.getName() != null) {
+ genTOsMap = genTypeDefsContextMap.get(parentModule.getName());
+ }
+
+ final GeneratedTOBuilder unionGenTransObject = typedefToTransferObject(
+ basePackageName, typedef);
+ if ((unionTypes != null) && (unionGenTransObject != null)) {
+ for (final TypeDefinition<?> unionType : unionTypes) {
+ final String typeName = unionType.getQName()
+ .getLocalName();
+ if (unionType instanceof ExtendedType) {
+ final Module unionTypeModule = findParentModuleForTypeDefinition(schemaContext,
+ unionType);
+ if (unionTypeModule != null && unionTypeModule.getName() != null) {
+ final Map<String, Type> innerGenTOs = genTypeDefsContextMap
+ .get(unionTypeModule.getName());
+
+ final GeneratedTransferObject genTransferObject =
+ (GeneratedTransferObject) innerGenTOs.get(typeName);
+ if (genTransferObject != null) {
+ updateUnionTypeAsProperty(unionGenTransObject,
+ genTransferObject,
+ genTransferObject.getName());
+ }
+ }
+ } else if (unionType instanceof EnumTypeDefinition) {
+ final EnumBuilder
+ enumBuilder = resolveInnerEnumFromTypeDefinition(
+ (EnumTypeDefinition) unionType, typeName,
+ unionGenTransObject);
+ final Type enumRefType = new ReferencedTypeImpl(
+ enumBuilder.getPackageName(),
+ enumBuilder.getName());
+ updateUnionTypeAsProperty(unionGenTransObject,
+ enumRefType, typeName);
+ } else {
+ final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
+ .javaTypeForSchemaDefinitionType(unionType);
+ if (javaType != null) {
+ updateUnionTypeAsProperty(unionGenTransObject,
+ javaType, typeName);
+ }
+ }
+ }
+ genTOsMap.put(typedef.getQName().getLocalName(),
+ unionGenTransObject.toInstance());
+ }
+ }
+ }
+
+ private void updateUnionTypeAsProperty(
+ final GeneratedTOBuilder unionGenTransObject, final Type type,
+ final String propertyName) {
+ if (unionGenTransObject != null && type != null) {
+ final GeneratedPropertyBuilder propBuilder =
+ unionGenTransObject.addProperty(parseToValidParamName(
+ propertyName));
+ propBuilder.addReturnType(type);
+ propBuilder.setReadOnly(false);
+
+ if (!(type instanceof Enumeration)) {
+ unionGenTransObject.addEqualsIdentity(propBuilder);
+ unionGenTransObject.addHashIdentity(propBuilder);
+ unionGenTransObject.addToStringProperty(propBuilder);
+ }
+ }
+ }
+
+ private GeneratedTOBuilder typedefToTransferObject(
+ final String basePackageName, final TypeDefinition<?> typedef) {
+
+ final String packageName = packageNameForGeneratedType(basePackageName,
+ typedef.getPath());
+ final String typeDefTOName = typedef.getQName().getLocalName();
+
+ if ((packageName != null) && (typedef != null)
+ && (typeDefTOName != null)) {
+ final String genTOName = parseToClassName(typeDefTOName);
+ final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(
+ packageName, genTOName);
+
+ return newType;
+ }
+ return null;
+ }
+}