use of org.opendaylight.mdsal.binding.model.api.Enumeration in project mdsal by opendaylight.
the class GenEnumResolvingTest method testLeafEnumResolving.
@Test
public void testLeafEnumResolving() {
final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResources(GenEnumResolvingTest.class, "/enum-test-models/ietf-interfaces@2012-11-15.yang", "/ietf-models/iana-if-type.yang"));
assertNotNull(genTypes);
assertEquals(7, genTypes.size());
GeneratedType genInterface = null;
for (final GeneratedType type : genTypes) {
if (type.getName().equals("Interface")) {
genInterface = type;
}
}
assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
Enumeration linkUpDownTrapEnable = null;
Enumeration operStatus = null;
final List<Enumeration> enums = genInterface.getEnumerations();
assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
assertEquals("Generated Type Interface MUST contain 2 Enumeration Types", 2, enums.size());
for (final Enumeration e : enums) {
if (e.getName().equals("LinkUpDownTrapEnable")) {
linkUpDownTrapEnable = e;
} else if (e.getName().equals("OperStatus")) {
operStatus = e;
}
}
assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!", linkUpDownTrapEnable.getValues());
assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
final List<MethodSignature> methods = genInterface.getMethodDefinitions();
assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
// FIXME: split this into getter/default/static asserts
assertEquals(32, methods.size());
Enumeration ianaIfType = null;
for (final MethodSignature method : methods) {
if (method.getName().equals("getType")) {
if (method.getReturnType() instanceof Enumeration) {
ianaIfType = (Enumeration) method.getReturnType();
}
}
}
assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
}
use of org.opendaylight.mdsal.binding.model.api.Enumeration in project mdsal by opendaylight.
the class Bug6135Test method bug6135Test.
@Test
public void bug6135Test() {
final List<GeneratedType> generateTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource("/bug6135.yang"));
assertEquals(5, generateTypes.size());
GeneratedType genInterface = null;
for (final GeneratedType type : generateTypes) {
if (type.getName().equals("TestLeafrefData")) {
genInterface = type;
break;
}
}
assertNotNull(genInterface);
final List<Enumeration> enums = genInterface.getEnumerations();
assertEquals(2, enums.size());
}
use of org.opendaylight.mdsal.binding.model.api.Enumeration in project mdsal by opendaylight.
the class AbstractTypeObjectGenerator method createUnion.
@NonNull
private static GeneratedTransferObject createUnion(final List<GeneratedType> auxiliaryGeneratedTypes, final TypeBuilderFactory builderFactory, final EffectiveStatement<?, ?> definingStatement, final UnionDependencies dependencies, final JavaTypeName typeName, final ModuleGenerator module, final TypeEffectiveStatement<?> type, final boolean isTypedef, final TypeDefinition<?> typedef) {
final GeneratedUnionBuilder builder = builderFactory.newGeneratedUnionBuilder(typeName);
builder.addImplementsType(BindingTypes.TYPE_OBJECT);
builder.setIsUnion(true);
// builder.setSchemaPath(typedef.getPath());
builder.setModuleName(module.statement().argument().getLocalName());
builderFactory.addCodegenInformation(definingStatement, builder);
annotateDeprecatedIfNecessary(definingStatement, builder);
// 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<>();
// Linear list of properties generated from subtypes. We need this information for runtime types, as it allows
// direct mapping of type to corresponding property -- without having to resort to re-resolving the leafrefs
// again.
final List<String> typeProperties = new ArrayList<>();
for (EffectiveStatement<?, ?> stmt : type.effectiveSubstatements()) {
if (stmt instanceof TypeEffectiveStatement) {
final TypeEffectiveStatement<?> subType = (TypeEffectiveStatement<?>) stmt;
final QName subName = subType.argument();
final String localName = subName.getLocalName();
String propSource = localName;
final Type generatedType;
if (TypeDefinitions.UNION.equals(subName)) {
final JavaTypeName subUnionName = typeName.createEnclosed(provideAvailableNameForGenTOBuilder(typeName.simpleName()));
final GeneratedTransferObject subUnion = createUnion(auxiliaryGeneratedTypes, builderFactory, definingStatement, dependencies, subUnionName, module, subType, isTypedef, subType.getTypeDefinition());
builder.addEnclosingTransferObject(subUnion);
propSource = subUnionName.simpleName();
generatedType = subUnion;
} else if (TypeDefinitions.ENUMERATION.equals(subName)) {
final Enumeration subEnumeration = createEnumeration(builderFactory, typeName.createEnclosed(BindingMapping.getClassName(localName), "$"), module, (EnumTypeDefinition) subType.getTypeDefinition());
builder.addEnumeration(subEnumeration);
generatedType = subEnumeration;
} else if (TypeDefinitions.BITS.equals(subName)) {
final GeneratedTransferObject subBits = createBits(builderFactory, typeName.createEnclosed(BindingMapping.getClassName(localName), "$"), module, subType.getTypeDefinition(), isTypedef);
builder.addEnclosingTransferObject(subBits);
generatedType = subBits;
} else if (TypeDefinitions.IDENTITYREF.equals(subName)) {
generatedType = verifyNotNull(dependencies.identityTypes.get(stmt), "Cannot resolve identityref %s in %s", stmt, definingStatement).methodReturnType(builderFactory);
} else if (TypeDefinitions.LEAFREF.equals(subName)) {
generatedType = verifyNotNull(dependencies.leafTypes.get(stmt), "Cannot resolve leafref %s in %s", stmt, definingStatement).methodReturnType(builderFactory);
} else {
Type baseType = SIMPLE_TYPES.get(subName);
if (baseType == null) {
// This has to be a reference to a typedef, let's lookup it up and pick up its type
final AbstractTypeObjectGenerator<?, ?> baseGen = verifyNotNull(dependencies.baseTypes.get(subName), "Cannot resolve base type %s in %s", subName, definingStatement);
baseType = baseGen.methodReturnType(builderFactory);
// FIXME: This is legacy behaviour for leafrefs:
if (baseGen.refType instanceof TypeReference.Leafref) {
// if there already is a compatible property, do not generate a new one
final Type search = baseType;
final String matching = builder.getProperties().stream().filter(prop -> search == ((GeneratedPropertyBuilderImpl) prop).getReturnType()).findFirst().map(GeneratedPropertyBuilder::getName).orElse(null);
if (matching != null) {
typeProperties.add(matching);
continue;
}
// ... otherwise generate this weird property name
propSource = BindingMapping.getUnionLeafrefMemberName(builder.getName(), baseType.getName());
}
}
expressions.putAll(resolveRegExpressions(subType.getTypeDefinition()));
generatedType = restrictType(baseType, BindingGeneratorUtil.getRestrictions(type.getTypeDefinition()), builderFactory);
}
final String propName = BindingMapping.getPropertyName(propSource);
typeProperties.add(propName);
if (builder.containsProperty(propName)) {
/*
* FIXME: this is not okay, as we are ignoring multiple base types. For example in the case of:
*
* type union {
* type string {
* length 1..5;
* }
* type string {
* length 8..10;
* }
* }
*
* We are ending up losing the information about 8..10 being an alternative. This is also the case
* for leafrefs -- we are performing property compression as well (see above). While it is alluring
* to merge these into 'length 1..5|8..10', that may not be generally feasible.
*
* We should resort to a counter of conflicting names, i.e. the second string would be mapped to
* 'string1' or similar.
*/
continue;
}
final GeneratedPropertyBuilder propBuilder = builder.addProperty(propName).setReturnType(generatedType);
builder.addEqualsIdentity(propBuilder);
builder.addHashIdentity(propBuilder);
builder.addToStringProperty(propBuilder);
}
}
// Record property names if needed
builder.setTypePropertyNames(typeProperties);
addStringRegExAsConstant(builder, expressions);
addUnits(builder, typedef);
makeSerializable(builder);
final GeneratedTransferObject ret = builder.build();
// Define a corresponding union builder. Typedefs are always anchored at a Java package root,
// so we are placing the builder alongside the union.
final GeneratedTOBuilder unionBuilder = builderFactory.newGeneratedTOBuilder(unionBuilderName(typeName));
unionBuilder.setIsUnionBuilder(true);
unionBuilder.addMethod("getDefaultInstance").setAccessModifier(AccessModifier.PUBLIC).setStatic(true).setReturnType(ret).addParameter(Types.STRING, "defaultValue");
auxiliaryGeneratedTypes.add(unionBuilder.build());
return ret;
}
use of org.opendaylight.mdsal.binding.model.api.Enumeration in project mdsal by opendaylight.
the class GenEnumResolvingTest method testTypedefEnumResolving.
@Test
public void testTypedefEnumResolving() {
final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource("/ietf-models/iana-if-type.yang"));
assertNotNull(genTypes);
assertEquals(2, genTypes.size());
final GeneratedType type = genTypes.get(1);
assertThat(type, instanceOf(Enumeration.class));
final Enumeration enumer = (Enumeration) type;
assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
}
use of org.opendaylight.mdsal.binding.model.api.Enumeration in project mdsal by opendaylight.
the class GenEnumResolvingTest method testLeafrefEnumResolving.
@Test
public void testLeafrefEnumResolving() {
final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResources(GenEnumResolvingTest.class, "/enum-test-models/abstract-topology@2013-02-08.yang", "/enum-test-models/ietf-interfaces@2012-11-15.yang", "/ietf-models/iana-if-type.yang"));
assertNotNull(genTypes);
assertEquals(25, genTypes.size());
GeneratedType genInterface = null;
for (final GeneratedType type : genTypes) {
if (type.getName().equals("Interface") && type.getPackageName().equals("org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")) {
genInterface = type;
}
}
assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
Type linkUpDownTrapEnable = null;
Type operStatus = null;
final List<MethodSignature> methods = genInterface.getMethodDefinitions();
assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
// FIXME: split this into getter/default/static asserts
assertEquals(13, methods.size());
for (final MethodSignature method : methods) {
if (method.getName().equals("getLinkUpDownTrapEnable")) {
linkUpDownTrapEnable = method.getReturnType();
} else if (method.getName().equals("getOperStatus")) {
operStatus = method.getReturnType();
}
}
assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
assertThat(linkUpDownTrapEnable, instanceOf(Enumeration.class));
assertEquals("org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface", linkUpDownTrapEnable.getIdentifier().immediatelyEnclosingClass().get().toString());
assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
assertThat(operStatus, instanceOf(Enumeration.class));
assertEquals("org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface", operStatus.getIdentifier().immediatelyEnclosingClass().get().toString());
}
Aggregations