import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
+import com.google.common.collect.BiMap;
+import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.common.io.BaseEncoding;
// Pattern string is the key, XSD regex is the value. The reason for this choice is that the pattern carries
// also negation information and hence guarantees uniqueness.
final Map<String, String> expressions = new HashMap<>();
+ final Map<TypeDefinition, String> propertyNames = resolveUnionMemberTypePropertyNames(unionTypes);
for (TypeDefinition<?> unionType : unionTypes) {
- final String unionTypeName = unionType.getQName().getLocalName();
+ final String propertyName = propertyNames.get(unionType);
// If we have a base type we should follow the type definition backwards, except for identityrefs, as those
// do not follow type encapsulation -- we use the general case for that.
if (unionType.getBaseType() != null && !(unionType instanceof IdentityrefTypeDefinition)) {
- resolveExtendedSubtypeAsUnion(unionGenTOBuilder, unionType, expressions, parentNode);
+ resolveExtendedSubtypeAsUnion(unionGenTOBuilder, unionType, expressions, parentNode,
+ propertyName);
} else if (unionType instanceof UnionTypeDefinition) {
generatedTOBuilders.addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder,
(UnionTypeDefinition) unionType, parentNode));
} else if (unionType instanceof EnumTypeDefinition) {
final Enumeration enumeration = addInnerEnumerationToTypeBuilder((EnumTypeDefinition) unionType,
- unionTypeName, unionGenTOBuilder);
- updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, unionTypeName);
+ propertyName, unionGenTOBuilder);
+ updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, propertyName);
} else {
final Type javaType = javaTypeForSchemaDefinitionType(unionType, parentNode);
- updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
+ updateUnionTypeAsProperty(unionGenTOBuilder, javaType, propertyName);
}
}
addStringRegExAsConstant(unionGenTOBuilder, expressions);
return generatedTOBuilders;
}
+ private Map<TypeDefinition, String> resolveUnionMemberTypePropertyNames(final List<TypeDefinition<?>> unionTypes) {
+ final BiMap<String, TypeDefinition> propertyNames = HashBiMap.create();
+ String propertyName;
+ Integer suffix = 0;
+ for (TypeDefinition<?> type : unionTypes) {
+ propertyName = type.getQName().getLocalName();
+ if (propertyNames.containsKey(propertyName)) {
+ propertyName = propertyName + (++suffix);
+ }
+
+ propertyNames.put(propertyName, type);
+ }
+
+ return propertyNames.inverse();
+ }
+
/**
* Wraps code which handles the case when union subtype is also of the type <code>UnionType</code>.
*
*
*/
private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
- final TypeDefinition<?> unionSubtype, final Map<String, String> expressions, final SchemaNode parentNode) {
+ final TypeDefinition<?> unionSubtype, final Map<String, String> expressions, final SchemaNode parentNode,
+ final String propertyName) {
final String unionTypeName = unionSubtype.getQName().getLocalName();
final Type genTO = findGenTO(unionTypeName, unionSubtype);
if (genTO != null) {
final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType,
parentNode, BindingGeneratorUtil.getRestrictions(unionSubtype));
if (javaType != null) {
- updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
+ updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, propertyName);
}
} else if (baseType instanceof LeafrefTypeDefinition) {
final Type javaType = javaTypeForSchemaDefinitionType(baseType, parentNode);
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.mdsal.binding.generator.impl;
+
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.model.util.Types;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class Mdsal361Test {
+
+ @Test
+ public void mdsal361Test() {
+ final SchemaContext context = YangParserTestUtils.parseYangResource("/mdsal361.yang");
+
+ final List<Type> generateTypes = new BindingGeneratorImpl().generateTypes(context);
+ assertNotNull(generateTypes);
+ assertEquals(4, generateTypes.size());
+ for (final Type type : generateTypes) {
+ if (type.getName().equals("PceId")) {
+ final GeneratedType gt = (GeneratedType) type;
+ assertNotNull(gt);
+ assertEquals(2, gt.getProperties().size());
+ }
+
+ if (type.getName().equals("PceId2")) {
+ final GeneratedType gt = (GeneratedType) type;
+ assertNotNull(gt);
+ assertEquals(2, gt.getEnumerations().size());
+ assertEquals(2, gt.getProperties().size());
+ }
+ }
+ }
+}