From: Jozef Gloncak Date: Wed, 26 Jun 2013 09:45:12 +0000 (+0200) Subject: Added following mapping: X-Git-Tag: releasepom-0.1.0~327 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=a954bf75193626833a2bd8b7395514a8b1aec032 Added following mapping: from: - subelement >>PATTERN<< of YANG build-in type string to: - constant list string >>PATTERN_CONSTANTS<< which contains list of regular expressions specified in >>PATTENR<< - member class attribute >>patterns<< which is list of Pattern objects. Change-Id: I47ec9f4a5cd3e7eef85b78189a3788dd2f9e007f Signed-off-by: Jozef Gloncak --- diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/pom.xml b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/pom.xml index 18f29ed416..57fbd2c67b 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/pom.xml @@ -27,5 +27,10 @@ junit junit + + commons-lang + commons-lang + 2.1 + \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java index 9410ffe1de..6bb7907a74 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java @@ -7,7 +7,9 @@ */ package org.opendaylight.controller.sal.binding.yang.types; +import org.apache.commons.lang.StringEscapeUtils; import org.opendaylight.controller.binding.generator.util.ReferencedTypeImpl; +import org.opendaylight.controller.binding.generator.util.TypeConstants; 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; @@ -26,6 +28,7 @@ import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit; import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair; import org.opendaylight.controller.yang.model.util.ExtendedType; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -456,6 +459,23 @@ public final class TypeProviderImpl implements TypeProvider { genTOBuilder.addEqualsIdentity(genPropBuilder); genTOBuilder.addHashIdentity(genPropBuilder); genTOBuilder.addToStringProperty(genPropBuilder); + if (javaType == BaseYangTypes.STRING_TYPE) { + if (typedef instanceof ExtendedType) { + List patternConstraints = ((ExtendedType) typedef).getPatterns(); + List regularExpressions = new ArrayList(); + String regEx; + String modifiedRegEx; + for (PatternConstraint ptrnCons : patternConstraints) { + regEx = ptrnCons.getRegularExpression(); + modifiedRegEx = StringEscapeUtils.escapeJava(regEx); + regularExpressions.add(modifiedRegEx); + } + + genTOBuilder.addConstant(Types.listTypeFor(BaseYangTypes.STRING_TYPE), + TypeConstants.PATTERN_CONSTANT_NAME, regularExpressions); + } + } + return genTOBuilder.toInstance(); } return null; @@ -596,4 +616,5 @@ public final class TypeProviderImpl implements TypeProvider { } return genTOBuilder.toInstance(); } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneretedTypesBitsTest.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesBitsTest.java similarity index 67% rename from opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneretedTypesBitsTest.java rename to opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesBitsTest.java index 646efd1a78..af08ac2d0a 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneretedTypesBitsTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesBitsTest.java @@ -1,5 +1,7 @@ package org.opendaylight.controller.sal.binding.generator.impl; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.File; @@ -20,10 +22,9 @@ import org.opendaylight.controller.yang.model.api.SchemaContext; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; import org.opendaylight.controller.yang.parser.impl.YangParserImpl; -public class GeneretedTypesBitsTest { +public class GeneratedTypesBitsTest { - private SchemaContext resolveSchemaContextFromFiles( - final String... yangFiles) { + private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) { final YangModelParser parser = new YangParserImpl(); final List inputFiles = new ArrayList(); @@ -37,8 +38,7 @@ public class GeneretedTypesBitsTest { @Test public void testGeneretedTypesBitsTest() { - final String yangTypesPath = getClass().getResource( - "/simple-bits-demo.yang").getPath(); + final String yangTypesPath = getClass().getResource("/simple-bits-demo.yang").getPath(); final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath); assertTrue(context != null); @@ -65,12 +65,11 @@ public class GeneretedTypesBitsTest { for (final Type type : genTypes) { if (type instanceof GeneratedTransferObject) { // searching for - // ByteType + // ByteType final GeneratedTransferObject genTO = (GeneratedTransferObject) type; if (genTO.getName().equals("ByteType")) { byteTypeFound = true; - List genProperties = genTO - .getProperties(); + List genProperties = genTO.getProperties(); classPropertiesNumb = genProperties.size(); genProperties = null; @@ -90,31 +89,26 @@ public class GeneretedTypesBitsTest { // interface // LeafParameterContainer final GeneratedType genType = (GeneratedType) type; - if (genType.getName().compareTo("LeafParentContainer") == 0) { + if (genType.getName().equals("LeafParentContainer")) { leafParentFound = true; // check of methods methodSignaturesList = genType.getMethodDefinitions(); if (methodSignaturesList != null) { for (MethodSignature methodSignature : methodSignaturesList) { // loop - // through - // all - // methods - if (methodSignature.getName().compareTo( - "getByteLeaf") == 0) { + // through + // all + // methods + if (methodSignature.getName().equals("getByteLeaf")) { getByteLeafMethodFound = true; - nameReturnParamType = methodSignature - .getReturnType().getName(); - } else if (methodSignature.getName().compareTo( - "setByteLeaf") == 0) { + nameReturnParamType = methodSignature.getReturnType().getName(); + } else if (methodSignature.getName().equals("setByteLeaf")) { setByteLeafMethodFound = true; - List parameters = methodSignature - .getParameters(); + List parameters = methodSignature.getParameters(); setByteLeafMethodParamNum = parameters.size(); for (Parameter parameter : parameters) { - nameMethodeParamType = parameter.getType() - .getName(); + nameMethodeParamType = parameter.getType().getName(); } } @@ -126,33 +120,23 @@ public class GeneretedTypesBitsTest { } - assertTrue("type >ByteType< wasn't in YANG file", byteTypeFound == true); - - assertTrue("Incorrect number of bit properties in class", - classPropertiesNumb == 8); - - assertTrue("Incorrect number of properties in toString method", - toStringPropertiesNum == 8); - assertTrue("Incorrect number of properties in equals method", - equalPropertiesNum == 8); - assertTrue("Incorrect number of properties in hash method", - hashPropertiesNum == 8); - assertTrue("LeafParameterContainer container wasn't found", - leafParentFound == true); - - assertTrue("No methods were generated", methodSignaturesList != null); - - assertTrue("Method getByteLeaf wasn't generated.", - getByteLeafMethodFound == true); - assertTrue("Wrong returning parameter type of method getByteLeaf", - nameReturnParamType.compareTo("ByteType") == 0); - - assertTrue("Method setByteLeaf wasn't generated.", - setByteLeafMethodFound == true); - assertTrue("Incorrect number of input parameters for setByteLeaf", - setByteLeafMethodParamNum == 1); - assertTrue("Wrong input parameter type", - nameMethodeParamType.compareTo("ByteType") == 0); + assertTrue(byteTypeFound); + + assertEquals(classPropertiesNumb,8); + + assertEquals(toStringPropertiesNum,8); + assertEquals(equalPropertiesNum,8); + assertEquals(hashPropertiesNum,8); + assertTrue(leafParentFound); + + assertNotNull(methodSignaturesList); + + assertTrue(getByteLeafMethodFound); + assertEquals(nameReturnParamType,"ByteType"); + + assertTrue(setByteLeafMethodFound); + assertEquals(setByteLeafMethodParamNum,1); + assertEquals(nameMethodeParamType,"ByteType"); } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesStringTest.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesStringTest.java new file mode 100644 index 0000000000..ab290f7708 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesStringTest.java @@ -0,0 +1,116 @@ +package org.opendaylight.controller.sal.binding.generator.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +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.controller.sal.binding.generator.api.BindingGenerator; +import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl; +import org.opendaylight.controller.sal.binding.model.api.Constant; +import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject; +import org.opendaylight.controller.sal.binding.model.api.ParameterizedType; +import org.opendaylight.controller.sal.binding.model.api.Type; +import org.opendaylight.controller.yang.model.api.Module; +import org.opendaylight.controller.yang.model.api.SchemaContext; +import org.opendaylight.controller.yang.model.parser.api.YangModelParser; +import org.opendaylight.controller.yang.parser.impl.YangParserImpl; + +public class GeneratedTypesStringTest { + + private final static List testModels = new ArrayList(); + + @BeforeClass + public static void loadTestResources() { + final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang") + .getPath()); + testModels.add(listModelFile); + } + + @Test + public void constantGenerationTest() { + final YangModelParser parser = new YangParserImpl(); + final Set modules = parser.parseYangModels(testModels); + final SchemaContext context = parser.resolveSchemaContext(modules); + + assertNotNull(context); + final BindingGenerator bindingGen = new BindingGeneratorImpl(); + final List genTypes = bindingGen.generateTypes(context); + + boolean typedefStringFound = false; + boolean constantRegExListFound = false; + boolean constantRegExListTypeGeneric = false; + boolean constantRegExListTypeContainer = false; + boolean noStringInReqExListFound = false; + boolean constantRegExListValueOK = false; + boolean constantRegExListTypeOneGeneric = false; + for (final Type type : genTypes) { + if (type instanceof GeneratedTransferObject) { + final GeneratedTransferObject genTO = (GeneratedTransferObject) type; + + if (genTO.getName().equals("TypedefString")) { + typedefStringFound = true; + + List constants = genTO.getConstantDefinitions(); + for (Constant con : constants) { + if (con.getName().equals("PATTERN_CONSTANTS")) { + constantRegExListFound = true; + } else + break; + ParameterizedType pType; + if (con.getType() instanceof ParameterizedType) { + pType = (ParameterizedType) con.getType(); + } else + break; + + Type[] types; + if (pType.getName().equals("List")) { + constantRegExListTypeContainer = true; + types = pType.getActualTypeArguments(); + } else + break; + + if (types.length == 1) { + constantRegExListTypeOneGeneric = true; + } else + break; + + if (types[0].getName().equals("String")) { + constantRegExListTypeGeneric = true; + } else + break; + + if (con.getValue() instanceof List) { + constantRegExListValueOK = true; + } else + break; + + for (Object obj : (List) con.getValue()) { + if (!(obj instanceof String)) { + noStringInReqExListFound = true; + break; + } + } + + } + } + } + + } + + assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound); + assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound); + assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer); + assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric); + assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric); + assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK); + assertTrue("In list found other type than String", !noStringInReqExListFound); + + } + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesTest.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesTest.java index cfa74a005f..6d50975e8d 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/GeneratedTypesTest.java @@ -28,8 +28,7 @@ import org.opendaylight.controller.yang.parser.impl.YangParserImpl; public class GeneratedTypesTest { - private SchemaContext resolveSchemaContextFromFiles( - final String... yangFiles) { + private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) { final YangModelParser parser = new YangParserImpl(); final List inputFiles = new ArrayList(); @@ -43,57 +42,50 @@ public class GeneratedTypesTest { @Test public void testMultipleModulesResolving() { - final String topologyPath = getClass().getResource( - "/abstract-topology.yang").getPath(); - final String typesPath = getClass().getResource( - "/ietf-inet-types@2010-09-24.yang").getPath(); - final SchemaContext context = resolveSchemaContextFromFiles( - topologyPath, typesPath); - assertTrue(context != null); + final String topologyPath = getClass().getResource("/abstract-topology.yang").getPath(); + final String typesPath = getClass().getResource("/ietf-inet-types@2010-09-24.yang").getPath(); + final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, typesPath); + assertNotNull(context); final BindingGenerator bindingGen = new BindingGeneratorImpl(); final List genTypes = bindingGen.generateTypes(context); - assertTrue(genTypes != null); + assertNotNull(genTypes); assertEquals(29, genTypes.size()); } @Test public void testLeafrefResolving() { - final String topologyPath = getClass().getResource( - "/leafref-test-models/abstract-topology@2013-02-08.yang") + final String topologyPath = getClass().getResource("/leafref-test-models/abstract-topology@2013-02-08.yang") .getPath(); - final String interfacesPath = getClass().getResource( - "/leafref-test-models/ietf-interfaces@2012-11-15.yang") + final String interfacesPath = getClass().getResource("/leafref-test-models/ietf-interfaces@2012-11-15.yang") .getPath(); // final String ifTypePath = getClass().getResource( // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath(); - final String inetTypesPath = getClass().getResource( - "/leafref-test-models/ietf-inet-types@2010-09-24.yang") + final String inetTypesPath = getClass().getResource("/leafref-test-models/ietf-inet-types@2010-09-24.yang") .getPath(); - final String yangTypesPath = getClass().getResource( - "/leafref-test-models/ietf-yang-types@2010-09-24.yang") + final String yangTypesPath = getClass().getResource("/leafref-test-models/ietf-yang-types@2010-09-24.yang") .getPath(); - assertTrue(topologyPath != null); - assertTrue(interfacesPath != null); + assertNotNull(topologyPath); + assertNotNull(interfacesPath); // assertTrue(ifTypePath != null); - assertTrue(inetTypesPath != null); - assertTrue(yangTypesPath != null); + assertNotNull(inetTypesPath); + assertNotNull(yangTypesPath); // final SchemaContext context = resolveSchemaContextFromFiles( // topologyPath, interfacesPath, ifTypePath, inetTypesPath, // yangTypesPath); - final SchemaContext context = resolveSchemaContextFromFiles( - topologyPath, interfacesPath, inetTypesPath, yangTypesPath); - assertTrue(context != null); + final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, interfacesPath, inetTypesPath, + yangTypesPath); + assertNotNull(context); assertEquals(4, context.getModules().size()); final BindingGenerator bindingGen = new BindingGeneratorImpl(); final List genTypes = bindingGen.generateTypes(context); assertEquals(57, genTypes.size()); - assertTrue(genTypes != null); + assertNotNull(genTypes); GeneratedTransferObject gtIfcKey = null; GeneratedType gtIfc = null; @@ -142,7 +134,7 @@ public class GeneratedTypesTest { Type ifcIdPropType = ifcIdProp.getReturnType(); assertNotNull(ifcIdPropType); assertFalse(ifcIdPropType.equals("java.lang.Void")); - assertTrue(ifcIdPropType.getName().equals("String")); + assertEquals(ifcIdPropType.getName(), "String"); // Interface final List gtIfcMethods = gtIfc.getMethodDefinitions(); @@ -160,17 +152,16 @@ public class GeneratedTypesTest { Type getIfcKeyType = getIfcKey.getReturnType(); assertNotNull(getIfcKeyType); assertFalse(getIfcKeyType.equals("java.lang.Void")); - assertTrue(getIfcKeyType.getName().equals("InterfaceKey")); + assertEquals(getIfcKeyType.getName(), "InterfaceKey"); assertNotNull(getHigherLayerIf); Type getHigherLayerIfType = getHigherLayerIf.getReturnType(); assertNotNull(getHigherLayerIfType); assertFalse(getHigherLayerIfType.equals("java.lang.Void")); - assertTrue(getHigherLayerIfType.getName().equals("List")); + assertEquals(getHigherLayerIfType.getName(), "List"); // NetworkLink - final List gtNetworkLinkMethods = gtNetworkLink - .getMethodDefinitions(); + final List gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions(); assertNotNull(gtNetworkLinkMethods); MethodSignature getIfc = null; for (MethodSignature method : gtNetworkLinkMethods) { @@ -182,11 +173,10 @@ public class GeneratedTypesTest { Type getIfcType = getIfc.getReturnType(); assertNotNull(getIfcType); assertFalse(getIfcType.equals("java.lang.Void")); - assertTrue(getIfcType.getName().equals("String")); + assertEquals(getIfcType.getName(), "String"); // SourceNode - final List gtSourceMethods = gtSource - .getMethodDefinitions(); + final List gtSourceMethods = gtSource.getMethodDefinitions(); assertNotNull(gtSourceMethods); MethodSignature getIdSource = null; for (MethodSignature method : gtSourceMethods) { @@ -198,11 +188,10 @@ public class GeneratedTypesTest { Type getIdType = getIdSource.getReturnType(); assertNotNull(getIdType); assertFalse(getIdType.equals("java.lang.Void")); - assertTrue(getIdType.getName().equals("Uri")); + assertEquals(getIdType.getName(), "Uri"); // DestinationNode - final List gtDestMethods = gtDest - .getMethodDefinitions(); + final List gtDestMethods = gtDest.getMethodDefinitions(); assertNotNull(gtDestMethods); MethodSignature getIdDest = null; for (MethodSignature method : gtDestMethods) { @@ -214,11 +203,10 @@ public class GeneratedTypesTest { Type getIdDestType = getIdDest.getReturnType(); assertNotNull(getIdDestType); assertFalse(getIdDestType.equals("java.lang.Void")); - assertTrue(getIdDestType.getName().equals("Uri")); + assertEquals(getIdDestType.getName(), "Uri"); // Tunnel - final List gtTunnelMethods = gtTunnel - .getMethodDefinitions(); + final List gtTunnelMethods = gtTunnel.getMethodDefinitions(); assertNotNull(gtTunnelMethods); MethodSignature getTunnelKey = null; for (MethodSignature method : gtTunnelMethods) { @@ -230,11 +218,10 @@ public class GeneratedTypesTest { Type getTunnelKeyType = getTunnelKey.getReturnType(); assertNotNull(getTunnelKeyType); assertFalse(getTunnelKeyType.equals("java.lang.Void")); - assertTrue(getTunnelKeyType.getName().equals("TunnelKey")); + assertEquals(getTunnelKeyType.getName(), "TunnelKey"); // TunnelKey - final List gtTunnelKeyProps = gtTunnelKey - .getProperties(); + final List gtTunnelKeyProps = gtTunnelKey.getProperties(); assertNotNull(gtTunnelKeyProps); GeneratedProperty tunnelId = null; for (final GeneratedProperty property : gtTunnelKeyProps) { @@ -246,20 +233,19 @@ public class GeneratedTypesTest { Type tunnelIdType = tunnelId.getReturnType(); assertNotNull(tunnelIdType); assertFalse(tunnelIdType.equals("java.lang.Void")); - assertTrue(tunnelIdType.getName().equals("Uri")); + assertEquals(tunnelIdType.getName(), "Uri"); } @Test public void testContainerResolving() { - final String filePath = getClass().getResource( - "/simple-container-demo.yang").getPath(); + final String filePath = getClass().getResource("/simple-container-demo.yang").getPath(); final SchemaContext context = resolveSchemaContextFromFiles(filePath); - assertTrue(context != null); + assert (context != null); final BindingGenerator bindingGen = new BindingGeneratorImpl(); final List genTypes = bindingGen.generateTypes(context); - assertTrue(genTypes != null); + assertNotNull(genTypes); assertEquals(4, genTypes.size()); final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1); @@ -270,74 +256,117 @@ public class GeneratedTypesTest { assertEquals(5, simpleContainer.getMethodDefinitions().size()); assertEquals(4, nestedContainer.getMethodDefinitions().size()); - int methodsCount = 0; - for (final MethodSignature method : simpleContainer - .getMethodDefinitions()) { + int setFooMethodCounter = 0; + int getFooMethodCounter = 0; + int getBarMethodCounter = 0; + int getNestedContainerCounter = 0; + + String getFooMethodReturnTypeName = ""; + String setFooMethodInputParamName = ""; + String setFooMethodInputParamTypeName = ""; + String getBarMethodReturnTypeName = ""; + String getNestedContainerReturnTypeName = ""; + for (final MethodSignature method : simpleContainer.getMethodDefinitions()) { if (method.getName().equals("getFoo")) { - method.getReturnType().getName().equals("Integer"); - methodsCount++; + getFooMethodCounter++; + getFooMethodReturnTypeName = method.getReturnType().getName(); } if (method.getName().equals("setFoo")) { - methodsCount++; - final MethodSignature.Parameter param = method.getParameters() - .get(0); - assertEquals("foo", param.getName()); - assertEquals("Integer", param.getType().getName()); + setFooMethodCounter++; + final MethodSignature.Parameter param = method.getParameters().get(0); + setFooMethodInputParamName = param.getName(); + setFooMethodInputParamTypeName = param.getType().getName(); } if (method.getName().equals("getBar")) { - method.getReturnType().getName().equals("String"); - methodsCount++; + getBarMethodCounter++; + getBarMethodReturnTypeName = method.getReturnType().getName(); } if (method.getName().equals("getNestedContainer")) { - method.getReturnType().getName().equals("NestedContainer"); - methodsCount++; + getNestedContainerCounter++; + getNestedContainerReturnTypeName = method.getReturnType().getName(); } } - assertEquals(4, methodsCount); - methodsCount = 0; - for (final MethodSignature method : nestedContainer - .getMethodDefinitions()) { + assertEquals(getFooMethodCounter, 1); + assertEquals(getFooMethodReturnTypeName, "Integer"); + + assertEquals(setFooMethodCounter, 1); + assertEquals(setFooMethodInputParamName, "foo"); + assertEquals(setFooMethodInputParamTypeName, "Integer"); + + assertEquals(getBarMethodCounter, 1); + assertEquals(getBarMethodReturnTypeName, "String"); + + assertEquals(getNestedContainerCounter, 1); + assertEquals(getNestedContainerReturnTypeName, "NestedContainer"); + + setFooMethodCounter = 0; + getFooMethodCounter = 0; + getBarMethodCounter = 0; + int setBarMethodCounter = 0; + + getFooMethodReturnTypeName = ""; + setFooMethodInputParamName = ""; + setFooMethodInputParamTypeName = ""; + getBarMethodReturnTypeName = ""; + String setBarMethodInputParamName = ""; + String setBarMethodInputParamTypeName = ""; + + for (final MethodSignature method : nestedContainer.getMethodDefinitions()) { + if (method.getName().equals("getFoo")) { - method.getReturnType().getName().equals("Short"); - methodsCount++; + getFooMethodCounter++; + getFooMethodReturnTypeName = method.getReturnType().getName(); } if (method.getName().equals("setFoo")) { - methodsCount++; - final MethodSignature.Parameter param = method.getParameters() - .get(0); - assertEquals("foo", param.getName()); - assertEquals("Short", param.getType().getName()); + setFooMethodCounter++; + final MethodSignature.Parameter param = method.getParameters().get(0); + setFooMethodInputParamName = param.getName(); + setFooMethodInputParamTypeName = param.getType().getName(); } if (method.getName().equals("getBar")) { - method.getReturnType().getName().equals("String"); - methodsCount++; + getBarMethodCounter++; + getBarMethodReturnTypeName = method.getReturnType().getName(); } if (method.getName().equals("setBar")) { - method.getReturnType().getName().equals("String"); - methodsCount++; + setBarMethodCounter++; + final MethodSignature.Parameter param = method.getParameters().get(0); + setBarMethodInputParamName = param.getName(); + setBarMethodInputParamTypeName = param.getType().getName(); } } - assertEquals(4, methodsCount); + + assertEquals(1, getFooMethodCounter); + assertEquals(getFooMethodReturnTypeName, "Short"); + + assertEquals(1, setFooMethodCounter); + assertEquals(setFooMethodInputParamName, "foo"); + assertEquals(setFooMethodInputParamTypeName, "Short"); + + assertEquals(1, getBarMethodCounter); + assertEquals(getBarMethodReturnTypeName, "String"); + + assertEquals(1, setBarMethodCounter); + assertEquals(setBarMethodInputParamName, "bar"); + assertEquals(setBarMethodInputParamTypeName, "String"); } @Test public void testLeafListResolving() { - final String filePath = getClass().getResource( - "/simple-leaf-list-demo.yang").getPath(); + final String filePath = getClass().getResource("/simple-leaf-list-demo.yang").getPath(); final SchemaContext context = resolveSchemaContextFromFiles(filePath); - assertTrue(context != null); + assertNotNull(context); final BindingGenerator bindingGen = new BindingGeneratorImpl(); final List genTypes = bindingGen.generateTypes(context); - assertTrue(genTypes != null); + assertNotNull(genTypes); assertEquals(4, genTypes.size()); final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1); @@ -348,185 +377,270 @@ public class GeneratedTypesTest { assertEquals(5, simpleContainer.getMethodDefinitions().size()); assertEquals(3, nestedContainer.getMethodDefinitions().size()); - int methodsCount = 0; - for (final MethodSignature method : simpleContainer - .getMethodDefinitions()) { + int setFooMethodCounter = 0; + int getFooMethodCounter = 0; + int getBarMethodCounter = 0; + int getNestedContainerCounter = 0; + + String getFooMethodReturnTypeName = ""; + String setFooMethodInputParamName = ""; + String setFooMethodInputParamTypeName = ""; + String getBarMethodReturnTypeName = ""; + String getNestedContainerReturnTypeName = ""; + for (final MethodSignature method : simpleContainer.getMethodDefinitions()) { if (method.getName().equals("getFoo")) { - method.getReturnType().getName().equals("List"); - methodsCount++; + getFooMethodCounter++; + getFooMethodReturnTypeName = method.getReturnType().getName(); } if (method.getName().equals("setFoo")) { - methodsCount++; - final MethodSignature.Parameter param = method.getParameters() - .get(0); - assertEquals("foo", param.getName()); - assertEquals("List", param.getType().getName()); + setFooMethodCounter++; + final MethodSignature.Parameter param = method.getParameters().get(0); + setFooMethodInputParamName = param.getName(); + setFooMethodInputParamTypeName = param.getType().getName(); } if (method.getName().equals("getBar")) { - method.getReturnType().getName().equals("String"); - methodsCount++; + getBarMethodCounter++; + getBarMethodReturnTypeName = method.getReturnType().getName(); } if (method.getName().equals("getNestedContainer")) { - method.getReturnType().getName().equals("NestedContainer"); - methodsCount++; + getNestedContainerCounter++; + getNestedContainerReturnTypeName = method.getReturnType().getName(); } } - assertEquals(4, methodsCount); - methodsCount = 0; - for (final MethodSignature method : nestedContainer - .getMethodDefinitions()) { + assertEquals(1, getFooMethodCounter); + assertEquals(getFooMethodReturnTypeName, "List"); + + assertEquals(1, setFooMethodCounter); + assertEquals(setFooMethodInputParamName, "foo"); + assertEquals(setFooMethodInputParamTypeName, "List"); + + assertEquals(1, getBarMethodCounter); + assertEquals(getBarMethodReturnTypeName, "String"); + + assertEquals(1, getNestedContainerCounter); + assertEquals(getNestedContainerReturnTypeName, "NestedContainer"); + + setFooMethodCounter = 0; + getFooMethodCounter = 0; + getBarMethodCounter = 0; + + getFooMethodReturnTypeName = ""; + setFooMethodInputParamName = ""; + setFooMethodInputParamTypeName = ""; + getBarMethodReturnTypeName = ""; + + for (final MethodSignature method : nestedContainer.getMethodDefinitions()) { if (method.getName().equals("getFoo")) { - method.getReturnType().getName().equals("Short"); - methodsCount++; + getFooMethodCounter++; + getFooMethodReturnTypeName = method.getReturnType().getName(); } if (method.getName().equals("setFoo")) { - methodsCount++; - final MethodSignature.Parameter param = method.getParameters() - .get(0); - assertEquals("foo", param.getName()); - assertEquals("Short", param.getType().getName()); + setFooMethodCounter++; + final MethodSignature.Parameter param = method.getParameters().get(0); + setFooMethodInputParamName = param.getName(); + setFooMethodInputParamTypeName = param.getType().getName(); } if (method.getName().equals("getBar")) { - method.getReturnType().getName().equals("List"); - methodsCount++; + getBarMethodCounter++; + getBarMethodReturnTypeName = method.getReturnType().getName(); } } - assertEquals(3, methodsCount); + + assertEquals(1, getFooMethodCounter); + assertEquals(getFooMethodReturnTypeName, "Short"); + + assertEquals(1, setFooMethodCounter); + assertEquals(setFooMethodInputParamName, "foo"); + assertEquals(setFooMethodInputParamTypeName, "Short"); + + assertEquals(1, getBarMethodCounter); + assertEquals(getBarMethodReturnTypeName, "List"); } @Test public void testListResolving() { - final String filePath = getClass() - .getResource("/simple-list-demo.yang").getPath(); + final String filePath = getClass().getResource("/simple-list-demo.yang").getPath(); final SchemaContext context = resolveSchemaContextFromFiles(filePath); - assertTrue(context != null); + assertNotNull(context); final BindingGenerator bindingGen = new BindingGeneratorImpl(); final List genTypes = bindingGen.generateTypes(context); - assertTrue(genTypes != null); + assertNotNull(genTypes); assertEquals(6, genTypes.size()); int genTypesCount = 0; int genTOsCount = 0; + + int listParentContainerMethodsCount = 0; + int simpleListMethodsCount = 0; + int listChildContainerMethodsCount = 0; + int listKeyClassCount = 0; + + int getSimpleListKeyMethodCount = 0; + int getListChildContainerMethodCount = 0; + int getFooMethodCount = 0; + int setFooMethodCount = 0; + int getSimpleLeafListMethodCount = 0; + int setSimpleLeafListMethodCount = 0; + int getBarMethodCount = 0; + + String getSimpleListKeyMethodReturnTypeName = ""; + String getListChildContainerMethodReturnTypeName = ""; + + int listKeyClassPropertyCount = 0; + String listKeyClassPropertyName = ""; + String listKeyClassPropertyTypeName = ""; + boolean listKeyClassPropertyReadOnly = false; + + int hashMethodParameterCount = 0; + String hashMethodParameterName = ""; + String hashMethodParameterReturnTypeName = ""; + + int equalMethodParameterCount = 0; + String equalMethodParameterName = ""; + String equalMethodParameterReturnTypeName = ""; + for (final Type type : genTypes) { - if (type instanceof GeneratedType - && !(type instanceof GeneratedTransferObject)) { + if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) { final GeneratedType genType = (GeneratedType) type; if (genType.getName().equals("ListParentContainer")) { - assertEquals(2, genType.getMethodDefinitions().size()); + listParentContainerMethodsCount = genType.getMethodDefinitions().size(); genTypesCount++; } else if (genType.getName().equals("SimpleList")) { - assertEquals(8, genType.getMethodDefinitions().size()); - final List methods = genType - .getMethodDefinitions(); - int methodsCount = 0; + simpleListMethodsCount = genType.getMethodDefinitions().size(); + final List methods = genType.getMethodDefinitions(); for (final MethodSignature method : methods) { if (method.getName().equals("getSimpleListKey")) { - assertEquals("SimpleListKey", method - .getReturnType().getName()); - methodsCount++; - } else if (method.getName().equals( - "getListChildContainer")) { - assertEquals("ListChildContainer", method - .getReturnType().getName()); - methodsCount++; + getSimpleListKeyMethodCount++; + getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName(); + } else if (method.getName().equals("getListChildContainer")) { + getListChildContainerMethodCount++; + getListChildContainerMethodReturnTypeName = method.getReturnType().getName(); } else if (method.getName().equals("getFoo")) { - methodsCount++; + getFooMethodCount++; } else if (method.getName().equals("setFoo")) { - methodsCount++; + setFooMethodCount++; } else if (method.getName().equals("getSimpleLeafList")) { - methodsCount++; + getSimpleLeafListMethodCount++; } else if (method.getName().equals("setSimpleLeafList")) { - methodsCount++; + setSimpleLeafListMethodCount++; } else if (method.getName().equals("getBar")) { - methodsCount++; + getBarMethodCount++; } } - assertEquals(7, methodsCount); genTypesCount++; } else if (genType.getName().equals("ListChildContainer")) { - assertEquals(2, genType.getMethodDefinitions().size()); + listChildContainerMethodsCount = genType.getMethodDefinitions().size(); genTypesCount++; } } else if (type instanceof GeneratedTransferObject) { genTOsCount++; final GeneratedTransferObject genTO = (GeneratedTransferObject) type; - final List properties = genTO - .getProperties(); - final List hashProps = genTO - .getHashCodeIdentifiers(); - final List equalProps = genTO - .getEqualsIdentifiers(); - - assertEquals(1, properties.size()); - assertEquals("ListKey", properties.get(0).getName()); - assertEquals("Byte", properties.get(0).getReturnType() - .getName()); - assertEquals(true, properties.get(0).isReadOnly()); - assertEquals(1, hashProps.size()); - assertEquals("ListKey", hashProps.get(0).getName()); - assertEquals("Byte", hashProps.get(0).getReturnType().getName()); - assertEquals(1, equalProps.size()); - assertEquals("ListKey", equalProps.get(0).getName()); - assertEquals("Byte", equalProps.get(0).getReturnType() - .getName()); + final List properties = genTO.getProperties(); + final List hashProps = genTO.getHashCodeIdentifiers(); + final List equalProps = genTO.getEqualsIdentifiers(); + + listKeyClassCount++; + listKeyClassPropertyCount = properties.size(); + listKeyClassPropertyName = properties.get(0).getName(); + listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName(); + listKeyClassPropertyReadOnly = properties.get(0).isReadOnly(); + + hashMethodParameterCount = hashProps.size(); + hashMethodParameterName = hashProps.get(0).getName(); + hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName(); + + equalMethodParameterCount = equalProps.size(); + equalMethodParameterName = equalProps.get(0).getName(); + equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName(); + } } - assertEquals(3, genTypesCount); - assertEquals(1, genTOsCount); + + assertEquals(2, listParentContainerMethodsCount); + assertEquals(2, listChildContainerMethodsCount); + assertEquals(1, getSimpleListKeyMethodCount); + assertEquals(1, listKeyClassCount); + + assertEquals(1, listKeyClassPropertyCount); + assertEquals("ListKey", listKeyClassPropertyName); + assertEquals("Byte", listKeyClassPropertyTypeName); + assertEquals(true, listKeyClassPropertyReadOnly); + assertEquals(1, hashMethodParameterCount); + assertEquals("ListKey", hashMethodParameterName); + assertEquals("Byte", hashMethodParameterReturnTypeName); + assertEquals(1, equalMethodParameterCount); + assertEquals("ListKey", equalMethodParameterName); + assertEquals("Byte", equalMethodParameterReturnTypeName); + + assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName); + + assertEquals(1, getListChildContainerMethodCount); + assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName); + assertEquals(1, getFooMethodCount); + assertEquals(1, setFooMethodCount); + assertEquals(1, getSimpleLeafListMethodCount); + assertEquals(1, setSimpleLeafListMethodCount); + assertEquals(1, getBarMethodCount); + + assertEquals(8, simpleListMethodsCount); } @Test public void testListCompositeKeyResolving() { - final String filePath = getClass().getResource( - "/list-composite-key.yang").getPath(); + final String filePath = getClass().getResource("/list-composite-key.yang").getPath(); final SchemaContext context = resolveSchemaContextFromFiles(filePath); - assertTrue(context != null); + assertNotNull(context); final BindingGenerator bindingGen = new BindingGeneratorImpl(); final List genTypes = bindingGen.generateTypes(context); - assertTrue(genTypes != null); + assertNotNull(genTypes); assertEquals(8, genTypes.size()); int genTypesCount = 0; int genTOsCount = 0; + + int compositeKeyListKeyPropertyCount = 0; + int compositeKeyListKeyCount = 0; + int innerListKeyPropertyCount = 0; + for (final Type type : genTypes) { - if (type instanceof GeneratedType - && !(type instanceof GeneratedTransferObject)) { + if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) { genTypesCount++; } else if (type instanceof GeneratedTransferObject) { final GeneratedTransferObject genTO = (GeneratedTransferObject) type; if (genTO.getName().equals("CompositeKeyListKey")) { - final List properties = genTO - .getProperties(); - int propertyCount = 0; + compositeKeyListKeyCount++; + final List properties = genTO.getProperties(); for (final GeneratedProperty prop : properties) { if (prop.getName().equals("Key1")) { - propertyCount++; + compositeKeyListKeyPropertyCount++; } else if (prop.getName().equals("Key2")) { - propertyCount++; + compositeKeyListKeyPropertyCount++; } } - assertEquals(2, propertyCount); genTOsCount++; } else if (genTO.getName().equals("InnerListKey")) { - final List properties = genTO - .getProperties(); - assertEquals(1, properties.size()); + final List properties = genTO.getProperties(); + innerListKeyPropertyCount = properties.size(); genTOsCount++; } } } + assertEquals(1, compositeKeyListKeyCount); + assertEquals(2, compositeKeyListKeyPropertyCount); + + assertEquals(1, innerListKeyPropertyCount); assertEquals(6, genTypesCount); assertEquals(2, genTOsCount); @@ -534,22 +648,20 @@ public class GeneratedTypesTest { @Test public void testGeneratedTypes() { - final String filePath = getClass().getResource("/demo-topology.yang") - .getPath(); + final String filePath = getClass().getResource("/demo-topology.yang").getPath(); final SchemaContext context = resolveSchemaContextFromFiles(filePath); - assertTrue(context != null); + assertNotNull(context); final BindingGenerator bindingGen = new BindingGeneratorImpl(); final List genTypes = bindingGen.generateTypes(context); - assertTrue(genTypes != null); + assertNotNull(genTypes); assertEquals(15, genTypes.size()); int genTypesCount = 0; int genTOsCount = 0; for (final Type type : genTypes) { - if (type instanceof GeneratedType - && !(type instanceof GeneratedTransferObject)) { + if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) { genTypesCount++; } else if (type instanceof GeneratedTransferObject) { genTOsCount++; diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/simple-string-demo.yang b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/simple-string-demo.yang new file mode 100644 index 0000000000..687d2a11dd --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/simple-string-demo.yang @@ -0,0 +1,23 @@ +module simple-string-demo { + + namespace "urn:simple:string:demo"; + prefix "sbd"; + + organization "OPEN DAYLIGHT"; + contact "http://www.opendaylight.org/"; + + revision 2013-06-18 { + + } + + + typedef typedef-string { + type string { + length "40"; + pattern "[0-9A-F]*"; + pattern "[B-D]*"; + pattern "[4-7]*"; + } + } + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/BindingGeneratorUtil.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/BindingGeneratorUtil.java index b37f91fe44..a394edd906 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/BindingGeneratorUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/BindingGeneratorUtil.java @@ -159,6 +159,13 @@ public final class BindingGeneratorUtil { return validateParameterName(correctStr); } + public static String convertToCapitalLetters(final String token) { + String convertedStr = token.replace(" ", "_"); + convertedStr = convertedStr.replace(".", "_"); + convertedStr = convertedStr.toUpperCase(); + return convertedStr; + } + private static String parseToCamelCase(String token) { if (token == null) { throw new NullPointerException("Name can not be null"); diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/TypeConstants.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/TypeConstants.java new file mode 100644 index 0000000000..69443aa4d9 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/TypeConstants.java @@ -0,0 +1,9 @@ +package org.opendaylight.controller.binding.generator.util; + +public final class TypeConstants { + + public static final String PATTERN_CONSTANT_NAME = "PATTERN_CONSTANTS"; + + private TypeConstants() { + } +} diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/generated/type/builder/AbstractGeneratedType.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/generated/type/builder/AbstractGeneratedType.java index 8c7ee35344..2144597772 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/generated/type/builder/AbstractGeneratedType.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/generated/type/builder/AbstractGeneratedType.java @@ -7,7 +7,6 @@ */ package org.opendaylight.controller.binding.generator.util.generated.type.builder; - import org.opendaylight.controller.binding.generator.util.AbstractBaseType; import org.opendaylight.controller.sal.binding.model.api.*; import org.opendaylight.controller.sal.binding.model.api.type.builder.*; @@ -28,16 +27,11 @@ abstract class AbstractGeneratedType extends AbstractBaseType implements Generat private final List enclosedTypes; private final boolean isAbstract; - public AbstractGeneratedType(final Type parent, final String packageName, - final String name, final String comment, - final List annotationBuilders, - final boolean isAbstract, - final List implementsTypes, - final List enclosedGenTypeBuilders, - final List enclosedGenTOBuilders, - final List enumBuilders, - final List constants, - final List methodBuilders) { + public AbstractGeneratedType(final Type parent, final String packageName, final String name, final String comment, + final List annotationBuilders, final boolean isAbstract, + final List implementsTypes, final List enclosedGenTypeBuilders, + final List enclosedGenTOBuilders, final List enumBuilders, + final List constants, final List methodBuilders) { super(packageName, name); this.parent = parent; this.comment = comment; @@ -51,7 +45,7 @@ abstract class AbstractGeneratedType extends AbstractBaseType implements Generat } private List toUnmodifiableEnclosedTypes(final List enclosedGenTypeBuilders, - final List enclosedGenTOBuilders) { + final List enclosedGenTOBuilders) { final List enclosedTypes = new ArrayList<>(); for (final GeneratedTypeBuilder builder : enclosedGenTypeBuilders) { if (builder != null) { @@ -67,8 +61,7 @@ abstract class AbstractGeneratedType extends AbstractBaseType implements Generat return enclosedTypes; } - protected List toUnmodifiableAnnotations( - final List annotationBuilders) { + protected List toUnmodifiableAnnotations(final List annotationBuilders) { final List annotations = new ArrayList<>(); for (final AnnotationTypeBuilder builder : annotationBuilders) { annotations.add(builder.toInstance()); @@ -76,8 +69,7 @@ abstract class AbstractGeneratedType extends AbstractBaseType implements Generat return Collections.unmodifiableList(annotations); } - protected List toUnmodifiableMethods( - List methodBuilders) { + protected List toUnmodifiableMethods(List methodBuilders) { final List methods = new ArrayList<>(); for (final MethodSignatureBuilder methodBuilder : methodBuilders) { methods.add(methodBuilder.toInstance(this)); @@ -85,8 +77,7 @@ abstract class AbstractGeneratedType extends AbstractBaseType implements Generat return Collections.unmodifiableList(methods); } - protected List toUnmodifiableEnumerations( - List enumBuilders) { + protected List toUnmodifiableEnumerations(List enumBuilders) { final List enums = new ArrayList<>(); for (final EnumBuilder enumBuilder : enumBuilders) { enums.add(enumBuilder.toInstance(this)); @@ -111,10 +102,10 @@ abstract class AbstractGeneratedType extends AbstractBaseType implements Generat @Override public boolean isAbstract() { - return isAbstract; + return isAbstract; } - @Override + @Override public List getImplements() { return implementsTypes; } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/ClassCodeGenerator.java b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/ClassCodeGenerator.java index 877bcfce63..b95ac48928 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/ClassCodeGenerator.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/ClassCodeGenerator.java @@ -16,7 +16,9 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.opendaylight.controller.binding.generator.util.TypeConstants; import org.opendaylight.controller.sal.binding.model.api.CodeGenerator; +import org.opendaylight.controller.sal.binding.model.api.Constant; import org.opendaylight.controller.sal.binding.model.api.Enumeration; import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty; import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferIdentityObject; @@ -32,12 +34,13 @@ public final class ClassCodeGenerator implements CodeGenerator { final Writer writer = new StringWriter(); if (type instanceof GeneratedTransferObject) { - GeneratedTransferObject genTO = (GeneratedTransferObject) type; + GeneratedTransferObject genTO = (GeneratedTransferObject) type; imports = GeneratorUtil.createImports(genTO); - + final String currentPkg = genTO.getPackageName(); final List fields = genTO.getProperties(); final List enums = genTO.getEnumerations(); + final List consts = genTO.getConstantDefinitions(); writer.write(GeneratorUtil.createPackageDeclaration(currentPkg)); writer.write(NL); @@ -48,54 +51,61 @@ public final class ClassCodeGenerator implements CodeGenerator { } writer.write(NL); - writer.write(GeneratorUtil.createClassDeclaration(genTO, "", - imports, genTO.isAbstract())); + writer.write(GeneratorUtil.createClassDeclaration(genTO, "", imports, genTO.isAbstract())); writer.write(NL); writer.write(NL); - - if (enums != null) { - EnumGenerator enumGenerator = new EnumGenerator(); - for ( Enumeration e : enums ) { - writer.write(enumGenerator.generateInnerEnumeration(e, TAB).toString()); - writer.write(NL); - } + + if (consts != null) { + for (Constant con : consts) { + writer.write(GeneratorUtil.createConstant(con, TAB, imports, currentPkg)); + writer.write(NL); + } } - if (fields != null) { - for (GeneratedProperty field : fields) { - writer.write(GeneratorUtil.createField(field, TAB, imports, - currentPkg) + NL); + if (enums != null) { + EnumGenerator enumGenerator = new EnumGenerator(); + for (Enumeration e : enums) { + writer.write(enumGenerator.generateInnerEnumeration(e, TAB).toString()); + writer.write(NL); + } + } + + boolean memberPatternListCodeRequired = false; + memberPatternListCodeRequired = (GeneratorUtil.isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, genTO)); + if (fields != null || memberPatternListCodeRequired) { + if (fields != null) { + for (GeneratedProperty field : fields) { + writer.write(GeneratorUtil.createField(field, TAB, imports, currentPkg) + NL); + } + } + if (memberPatternListCodeRequired) { + writer.write(TAB + PRIVATE + GAP + "List" + GAP + MEMBER_PATTERN_LIST + GAP + ASSIGN + GAP + + "new ArrayList()" + GAP + SC + NL); + } writer.write(NL); - writer.write(GeneratorUtil.createConstructor(genTO, TAB, - imports, genTO.isAbstract()) + NL); + writer.write(GeneratorUtil.createConstructor(genTO, TAB, imports, genTO.isAbstract()) + NL); writer.write(NL); - for (GeneratedProperty field : fields) { - writer.write(GeneratorUtil.createGetter(field, TAB, - imports, currentPkg) + NL); - if (!field.isReadOnly()) { - writer.write(GeneratorUtil.createSetter(field, TAB, - imports, currentPkg) + NL); + if (fields != null) { + for (GeneratedProperty field : fields) { + writer.write(GeneratorUtil.createGetter(field, TAB, imports, currentPkg) + NL); + if (!field.isReadOnly()) { + writer.write(GeneratorUtil.createSetter(field, TAB, imports, currentPkg) + NL); + } } } writer.write(NL); if (!genTO.getHashCodeIdentifiers().isEmpty()) { - writer.write(GeneratorUtil.createHashCode( - genTO.getHashCodeIdentifiers(), TAB) - + NL); + writer.write(GeneratorUtil.createHashCode(genTO.getHashCodeIdentifiers(), TAB) + NL); } if (!genTO.getEqualsIdentifiers().isEmpty()) { - writer.write(GeneratorUtil.createEquals(genTO, - genTO.getEqualsIdentifiers(), TAB) - + NL); + writer.write(GeneratorUtil.createEquals(genTO, genTO.getEqualsIdentifiers(), TAB) + NL); } if (!genTO.getToStringIdentifiers().isEmpty()) { - writer.write(GeneratorUtil.createToString(genTO, - genTO.getToStringIdentifiers(), TAB) - + NL); + writer.write(GeneratorUtil.createToString(genTO, genTO.getToStringIdentifiers(), TAB) + NL); } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/Constants.java b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/Constants.java index e5cb97f1a6..f200538138 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/Constants.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/Constants.java @@ -19,11 +19,16 @@ final class Constants { public static final String LB = "("; public static final String RB = ")"; + public static final String LSB = "["; + public static final String RSB = "]"; + public static final String GAP = " "; public static final String COMMA = ","; public static final String NL = "\n"; public static final String SC = ";"; public static final String TAB = " "; + public static final String ASSIGN = "="; + public static final String DOUBLE_QUOTE = "\""; public static final String PUBLIC = "public"; public static final String PRIVATE = "private"; @@ -33,9 +38,13 @@ final class Constants { public static final String FINAL = "final"; public static final String EXTENDS = "extends"; public static final String IMPLEMENTS = "implements"; - + public static final String ENUMERATION_NAME = "value"; - public static final String ENUMERATION_TYPE = "int"; - - private Constants() {} + public static final String ENUMERATION_TYPE = "int"; + + public static final String STRING_PATTERN_LIST = "STRING_PATTERNS"; + public static final String MEMBER_PATTERN_LIST = "patterns"; + + private Constants() { + } } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java index 804ac99ff2..16074c7e98 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java @@ -15,6 +15,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.opendaylight.controller.binding.generator.util.TypeConstants; +import org.opendaylight.controller.binding.generator.util.Types; import org.opendaylight.controller.sal.binding.model.api.AnnotationType; import org.opendaylight.controller.sal.binding.model.api.Constant; import org.opendaylight.controller.sal.binding.model.api.Enumeration; @@ -33,30 +35,22 @@ public final class GeneratorUtil { private GeneratorUtil() { } - public static String createIfcDeclaration(final GeneratedType genType, - final String indent, + public static String createIfcDeclaration(final GeneratedType genType, final String indent, final Map> availableImports) { - return createFileDeclaration(IFC, genType, indent, availableImports, - false); + return createFileDeclaration(IFC, genType, indent, availableImports, false); } - public static String createClassDeclaration( - final GeneratedTransferObject genTransferObject, - final String indent, - final Map> availableImports, - boolean isIdentity) { - return createFileDeclaration(CLASS, genTransferObject, indent, - availableImports, isIdentity); + public static String createClassDeclaration(final GeneratedTransferObject genTransferObject, final String indent, + final Map> availableImports, boolean isIdentity) { + return createFileDeclaration(CLASS, genTransferObject, indent, availableImports, isIdentity); } public static String createPackageDeclaration(final String packageName) { return PKG + GAP + packageName + SC; } - private static String createFileDeclaration(final String type, - final GeneratedType genType, final String indent, - final Map> availableImports, - boolean isIdentity) { + private static String createFileDeclaration(final String type, final GeneratedType genType, final String indent, + final Map> availableImports, boolean isIdentity) { final StringBuilder builder = new StringBuilder(); final String currentPkg = genType.getPackageName(); @@ -70,11 +64,9 @@ public final class GeneratorUtil { if (isIdentity) { if (!(CLASS.equals(type))) { - throw new IllegalArgumentException( - "'identity' has to be generated as a class"); + throw new IllegalArgumentException("'identity' has to be generated as a class"); } - builder.append(PUBLIC + GAP + ABSTRACT + GAP + type + GAP - + genType.getName() + GAP); + builder.append(PUBLIC + GAP + ABSTRACT + GAP + type + GAP + genType.getName() + GAP); } else { builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP); } @@ -96,13 +88,11 @@ public final class GeneratorUtil { } else { builder.append(EXTENDS + GAP); } - builder.append(getExplicitType(genImplements.get(0), - availableImports, currentPkg)); + builder.append(getExplicitType(genImplements.get(0), availableImports, currentPkg)); for (int i = 1; i < genImplements.size(); ++i) { builder.append(", "); - builder.append(getExplicitType(genImplements.get(i), - availableImports, currentPkg)); + builder.append(getExplicitType(genImplements.get(i), availableImports, currentPkg)); } } @@ -110,8 +100,7 @@ public final class GeneratorUtil { return builder.toString(); } - private static StringBuilder appendAnnotations(final StringBuilder builder, - final List annotations) { + private static StringBuilder appendAnnotations(final StringBuilder builder, final List annotations) { if ((builder != null) && (annotations != null)) { for (final AnnotationType annotation : annotations) { builder.append("@"); @@ -121,8 +110,7 @@ public final class GeneratorUtil { if (annotation.containsParameters()) { builder.append("("); - final List parameters = annotation - .getParameters(); + final List parameters = annotation.getParameters(); appendAnnotationParams(builder, parameters); builder.append(")"); } @@ -131,8 +119,7 @@ public final class GeneratorUtil { return builder; } - private static StringBuilder appendAnnotationParams( - final StringBuilder builder, + private static StringBuilder appendAnnotationParams(final StringBuilder builder, final List parameters) { if (parameters != null) { int i = 0; @@ -165,23 +152,44 @@ public final class GeneratorUtil { return builder; } - public static String createConstant(final Constant constant, - final String indent, - final Map> availableImports, - final String currentPkg) { + public static String createConstant(final Constant constant, final String indent, + final Map> availableImports, final String currentPkg) { final StringBuilder builder = new StringBuilder(); + if (constant == null) + throw new IllegalArgumentException(); builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP); - builder.append(getExplicitType(constant.getType(), availableImports, - currentPkg) + GAP + constant.getName()); + builder.append(getExplicitType(constant.getType(), availableImports, currentPkg) + GAP + constant.getName()); builder.append(GAP + "=" + GAP); - builder.append(constant.getValue() + SC); + final Object constValue = constant.getValue(); + + if (constant.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) { + if (constant.getName() == null || constant.getType() == null || constant.getValue() == null) + throw new IllegalArgumentException(); + if (constValue instanceof List) { + builder.append("Arrays.asList" + LB); + final List constantValues = (List) constValue; + int stringsCount = 0; + for (Object value : constantValues) { + if (value instanceof String) { + if (stringsCount > 0) { + builder.append(COMMA); + } + stringsCount++; + builder.append(DOUBLE_QUOTE + (String) value + DOUBLE_QUOTE); + } + } + builder.append(RB); + } + } else { + builder.append(constant.getValue()); + } + builder.append(SC); + return builder.toString(); } - public static String createField(final GeneratedProperty property, - final String indent, - Map> availableImports, - final String currentPkg) { + public static String createField(final GeneratedProperty property, final String indent, + Map> availableImports, final String currentPkg) { final StringBuilder builder = new StringBuilder(); if (!property.getAnnotations().isEmpty()) { final List annotations = property.getAnnotations(); @@ -189,8 +197,8 @@ public final class GeneratorUtil { builder.append(NL); } builder.append(indent + PRIVATE + GAP); - builder.append(getExplicitType(property.getReturnType(), - availableImports, currentPkg) + GAP + property.getName()); + builder.append(getExplicitType(property.getReturnType(), availableImports, currentPkg) + GAP + + property.getName()); builder.append(SC); return builder.toString(); } @@ -202,15 +210,12 @@ public final class GeneratorUtil { * @param indent * @return */ - public static String createMethodDeclaration(final MethodSignature method, - final String indent, - Map> availableImports, - final String currentPkg) { + public static String createMethodDeclaration(final MethodSignature method, final String indent, + Map> availableImports, final String currentPkg) { final StringBuilder builder = new StringBuilder(); if (method == null) { - throw new IllegalArgumentException( - "Method Signature parameter MUST be specified and cannot be NULL!"); + throw new IllegalArgumentException("Method Signature parameter MUST be specified and cannot be NULL!"); } final String comment = method.getComment(); @@ -221,8 +226,7 @@ public final class GeneratorUtil { final Type type = method.getReturnType(); if (type == null) { - throw new IllegalStateException( - "Method Return type cannot be NULL!"); + throw new IllegalStateException("Method Return type cannot be NULL!"); } final List parameters = method.getParameters(); @@ -237,9 +241,7 @@ public final class GeneratorUtil { builder.append(NL); } - builder.append(indent - + getExplicitType(type, availableImports, currentPkg) + GAP - + name); + builder.append(indent + getExplicitType(type, availableImports, currentPkg) + GAP + name); builder.append(LB); for (int i = 0; i < parameters.size(); i++) { Parameter p = parameters.get(i); @@ -247,8 +249,7 @@ public final class GeneratorUtil { if (i + 1 == parameters.size()) { separator = ""; } - builder.append(getExplicitType(p.getType(), availableImports, - currentPkg) + GAP + p.getName() + separator); + builder.append(getExplicitType(p.getType(), availableImports, currentPkg) + GAP + p.getName() + separator); } builder.append(RB); builder.append(SC); @@ -256,19 +257,18 @@ public final class GeneratorUtil { return builder.toString(); } - public static String createConstructor( - GeneratedTransferObject genTransferObject, final String indent, - Map> availableImports, - boolean isIdentity) { + public static String createConstructor(GeneratedTransferObject genTransferObject, final String indent, + Map> availableImports, boolean isIdentity) { final StringBuilder builder = new StringBuilder(); final String currentPkg = genTransferObject.getPackageName(); - final List properties = genTransferObject - .getProperties(); + final List properties = genTransferObject.getProperties(); final List ctorParams = new ArrayList(); - for (final GeneratedProperty property : properties) { - if (property.isReadOnly()) { - ctorParams.add(property); + if (properties != null) { + for (final GeneratedProperty property : properties) { + if (property.isReadOnly()) { + ctorParams.add(property); + } } } @@ -279,15 +279,13 @@ public final class GeneratorUtil { builder.append(LB); if (!ctorParams.isEmpty()) { - builder.append(getExplicitType(ctorParams.get(0).getReturnType(), - availableImports, currentPkg)); + builder.append(getExplicitType(ctorParams.get(0).getReturnType(), availableImports, currentPkg)); builder.append(" "); builder.append(ctorParams.get(0).getName()); for (int i = 1; i < ctorParams.size(); ++i) { final GeneratedProperty param = ctorParams.get(i); builder.append(", "); - builder.append(getExplicitType(param.getReturnType(), - availableImports, currentPkg)); + builder.append(getExplicitType(param.getReturnType(), availableImports, currentPkg)); builder.append(GAP); builder.append(param.getName()); } @@ -305,15 +303,37 @@ public final class GeneratorUtil { builder.append(NL); } } + List consts = genTransferObject.getConstantDefinitions(); + for (Constant con : consts) { + if (con.getName() == null || con.getType() == null || con.getValue() == null) + continue; + if (con.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) { + Object values = con.getValue(); + if (values instanceof List) { + for (Object regEx : (List) values) { + if (regEx instanceof String) { + builder.append(indent + TAB + "for (String regEx : " + TypeConstants.PATTERN_CONSTANT_NAME + + ") {" + NL); + builder.append(indent + TAB + TAB + "this." + MEMBER_PATTERN_LIST + + ".add(Pattern.compile(regEx))" + SC + NL); + builder.append(indent + TAB + RCB + NL); + + break; + } + } + + } + } + + } + builder.append(indent); builder.append(RCB); return builder.toString(); } - public static String createGetter(final GeneratedProperty property, - final String indent, - Map> availableImports, - final String currentPkg) { + public static String createGetter(final GeneratedProperty property, final String indent, + Map> availableImports, final String currentPkg) { final StringBuilder builder = new StringBuilder(); final Type type = property.getReturnType(); @@ -321,9 +341,7 @@ public final class GeneratorUtil { final char first = Character.toUpperCase(varName.charAt(0)); final String methodName = "get" + first + varName.substring(1); - builder.append(indent + PUBLIC + GAP - + getExplicitType(type, availableImports, currentPkg) + GAP - + methodName); + builder.append(indent + PUBLIC + GAP + getExplicitType(type, availableImports, currentPkg) + GAP + methodName); builder.append(LB + RB + LCB + NL); String currentIndent = indent + TAB; @@ -334,10 +352,8 @@ public final class GeneratorUtil { return builder.toString(); } - public static String createSetter(final GeneratedProperty property, - final String indent, - Map> availableImports, - String currentPkg) { + public static String createSetter(final GeneratedProperty property, final String indent, + Map> availableImports, String currentPkg) { final StringBuilder builder = new StringBuilder(); final Type type = property.getReturnType(); @@ -346,17 +362,14 @@ public final class GeneratorUtil { final String methodName = "set" + first + varName.substring(1); builder.append(indent + PUBLIC + GAP + "void" + GAP + methodName); - builder.append(LB + getExplicitType(type, availableImports, currentPkg) - + GAP + varName + RB + LCB + NL); + builder.append(LB + getExplicitType(type, availableImports, currentPkg) + GAP + varName + RB + LCB + NL); String currentIndent = indent + TAB; - builder.append(currentIndent + "this." + varName + " = " + varName + SC - + NL); + builder.append(currentIndent + "this." + varName + " = " + varName + SC + NL); builder.append(indent + RCB); return builder.toString(); } - public static String createHashCode( - final List properties, final String indent) { + public static String createHashCode(final List properties, final String indent) { StringBuilder builder = new StringBuilder(); builder.append(indent + "public int hashCode() {" + NL); builder.append(indent + TAB + "final int prime = 31;" + NL); @@ -364,8 +377,7 @@ public final class GeneratorUtil { for (GeneratedProperty property : properties) { String fieldName = property.getName(); - builder.append(indent + TAB + "result = prime * result + ((" - + fieldName + " == null) ? 0 : " + fieldName + builder.append(indent + TAB + "result = prime * result + ((" + fieldName + " == null) ? 0 : " + fieldName + ".hashCode());" + NL); } @@ -374,8 +386,8 @@ public final class GeneratorUtil { return builder.toString(); } - public static String createEquals(final GeneratedTransferObject type, - final List properties, final String indent) { + public static String createEquals(final GeneratedTransferObject type, final List properties, + final String indent) { StringBuilder builder = new StringBuilder(); final String indent1 = indent + TAB; final String indent2 = indent1 + TAB; @@ -393,18 +405,15 @@ public final class GeneratorUtil { builder.append(indent1 + "}" + NL); String typeStr = type.getName(); - builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;" - + NL); + builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;" + NL); for (GeneratedProperty property : properties) { String fieldName = property.getName(); builder.append(indent1 + "if (" + fieldName + " == null) {" + NL); - builder.append(indent2 + "if (other." + fieldName + " != null) {" - + NL); + builder.append(indent2 + "if (other." + fieldName + " != null) {" + NL); builder.append(indent3 + "return false;" + NL); builder.append(indent2 + "}" + NL); - builder.append(indent1 + "} else if (!" + fieldName - + ".equals(other." + fieldName + ")) {" + NL); + builder.append(indent1 + "} else if (!" + fieldName + ".equals(other." + fieldName + ")) {" + NL); builder.append(indent2 + "return false;" + NL); builder.append(indent1 + "}" + NL); } @@ -415,8 +424,8 @@ public final class GeneratorUtil { return builder.toString(); } - public static String createToString(final GeneratedTransferObject type, - final List properties, final String indent) { + public static String createToString(final GeneratedTransferObject type, final List properties, + final String indent) { StringBuilder builder = new StringBuilder(); builder.append(indent); builder.append("public String toString() {"); @@ -474,12 +483,11 @@ public final class GeneratorUtil { return builder.toString(); } - public static String createEnum(final Enumeration enumeration, - final String indent) { + public static String createEnum(final Enumeration enumeration, final String indent) { if (enumeration == null || indent == null) throw new IllegalArgumentException(); - final StringBuilder builder = new StringBuilder(indent + PUBLIC + GAP - + ENUM + GAP + enumeration.getName() + GAP + LCB + NL); + final StringBuilder builder = new StringBuilder(indent + PUBLIC + GAP + ENUM + GAP + enumeration.getName() + + GAP + LCB + NL); String separator = COMMA + NL; final List values = enumeration.getValues(); @@ -488,21 +496,17 @@ public final class GeneratorUtil { if (i + 1 == values.size()) { separator = SC; } - builder.append(indent + TAB + values.get(i).getName() + LB - + values.get(i).getValue() + RB + separator); + builder.append(indent + TAB + values.get(i).getName() + LB + values.get(i).getValue() + RB + separator); } builder.append(NL); builder.append(NL); final String ENUMERATION_NAME = "value"; final String ENUMERATION_TYPE = "int"; - builder.append(indent + TAB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME - + SC); + builder.append(indent + TAB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + SC); builder.append(NL); - builder.append(indent + TAB + PRIVATE + GAP + enumeration.getName() - + LB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + RB + GAP - + LCB + NL); - builder.append(indent + TAB + TAB + "this." + ENUMERATION_NAME + GAP - + "=" + GAP + ENUMERATION_NAME + SC + NL); + builder.append(indent + TAB + PRIVATE + GAP + enumeration.getName() + LB + ENUMERATION_TYPE + GAP + + ENUMERATION_NAME + RB + GAP + LCB + NL); + builder.append(indent + TAB + TAB + "this." + ENUMERATION_NAME + GAP + "=" + GAP + ENUMERATION_NAME + SC + NL); builder.append(indent + TAB + RCB + NL); builder.append(indent + RCB); @@ -511,27 +515,21 @@ public final class GeneratorUtil { } private static String getExplicitType(final Type type, - Map> availableImports, - final String currentPkg) { + Map> availableImports, final String currentPkg) { if (type == null) { - throw new IllegalArgumentException( - "Type parameter MUST be specified and cannot be NULL!"); + throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!"); } String packageName = type.getPackageName(); - LinkedHashMap imports = availableImports.get(type - .getName()); + LinkedHashMap imports = availableImports.get(type.getName()); - if ((imports != null && packageName - .equals(findMaxValue(imports).get(0))) - || packageName.equals(currentPkg)) { + if ((imports != null && packageName.equals(findMaxValue(imports).get(0))) || packageName.equals(currentPkg)) { final StringBuilder builder = new StringBuilder(type.getName()); if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Type[] pTypes = pType.getActualTypeArguments(); builder.append("<"); - builder.append(getParameters(pTypes, availableImports, - currentPkg)); + builder.append(getParameters(pTypes, availableImports, currentPkg)); builder.append(">"); } if (builder.toString().equals("Void")) { @@ -554,8 +552,7 @@ public final class GeneratorUtil { ParameterizedType pType = (ParameterizedType) type; Type[] pTypes = pType.getActualTypeArguments(); builder.append("<"); - builder.append(getParameters(pTypes, availableImports, - currentPkg)); + builder.append(getParameters(pTypes, availableImports, currentPkg)); builder.append(">"); } if (builder.toString().equals("Void")) { @@ -566,8 +563,7 @@ public final class GeneratorUtil { } private static String getParameters(final Type[] pTypes, - Map> availableImports, - String currentPkg) { + Map> availableImports, String currentPkg) { final StringBuilder builder = new StringBuilder(); for (int i = 0; i < pTypes.length; i++) { Type t = pTypes[i]; @@ -576,20 +572,18 @@ public final class GeneratorUtil { if (i + 1 == pTypes.length) { separator = ""; } - + String wildcardParam = ""; - if(t instanceof WildcardType) { + if (t instanceof WildcardType) { wildcardParam = "? extends "; } - - builder.append(wildcardParam + getExplicitType(t, availableImports, currentPkg) - + separator); + + builder.append(wildcardParam + getExplicitType(t, availableImports, currentPkg) + separator); } return builder.toString(); } - private static List findMaxValue( - LinkedHashMap imports) { + private static List findMaxValue(LinkedHashMap imports) { final List result = new ArrayList(); int maxValue = 0; @@ -607,8 +601,7 @@ public final class GeneratorUtil { return result; } - private static void createComment(final StringBuilder builder, - final String comment, final String indent) { + private static void createComment(final StringBuilder builder, final String comment, final String indent) { if (comment != null && comment.length() > 0) { builder.append(indent + "/*" + NL); builder.append(indent + comment + NL); @@ -616,13 +609,12 @@ public final class GeneratorUtil { } } - public static Map> createImports( - GeneratedType genType) { + public static Map> createImports(GeneratedType genType) { final Map> imports = new HashMap>(); final String genTypePkg = genType.getPackageName(); final List constants = genType.getConstantDefinitions(); - final List methods = genType.getMethodDefinitions(); + final List methods = genType.getMethodDefinitions(); List impl = genType.getImplements(); // IMPLEMENTATIONS @@ -664,16 +656,23 @@ public final class GeneratorUtil { } } + // REGULAR EXPRESSION + if (genType instanceof GeneratedTransferObject) { + if (isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) { + addTypeToImports(Types.typeForClass(java.util.regex.Pattern.class), imports, genTypePkg); + addTypeToImports(Types.typeForClass(java.util.Arrays.class), imports, genTypePkg); + addTypeToImports(Types.typeForClass(java.util.ArrayList.class), imports, genTypePkg); + } + } + return imports; } - private static void addTypeToImports(Type type, - Map> importedTypes, + private static void addTypeToImports(Type type, Map> importedTypes, String genTypePkg) { String typeName = type.getName(); String typePkg = type.getPackageName(); - if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg) || - typePkg.isEmpty()) { + if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg) || typePkg.isEmpty()) { return; } LinkedHashMap packages = importedTypes.get(typeName); @@ -700,12 +699,10 @@ public final class GeneratorUtil { } } - public static List createImportLines( - Map> imports) { + public static List createImportLines(Map> imports) { List importLines = new ArrayList(); - for (Map.Entry> entry : imports - .entrySet()) { + for (Map.Entry> entry : imports.entrySet()) { String typeName = entry.getKey(); LinkedHashMap typePkgMap = entry.getValue(); String typePkg = typePkgMap.keySet().iterator().next(); @@ -714,4 +711,17 @@ public final class GeneratorUtil { return importLines; } + public static boolean isConstantInTO(String constName, GeneratedTransferObject genTO) { + if (constName == null || genTO == null) + throw new IllegalArgumentException(); + List consts = genTO.getConstantDefinitions(); + for (Constant cons : consts) { + if (cons.getName().equals(constName)) { + return true; + } + + } + return false; + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/samples/maven-code-gen-sample/src/main/yang/simple-string-demo.yang b/opendaylight/sal/yang-prototype/code-generator/samples/maven-code-gen-sample/src/main/yang/simple-string-demo.yang new file mode 100644 index 0000000000..c0f87dd910 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/samples/maven-code-gen-sample/src/main/yang/simple-string-demo.yang @@ -0,0 +1,22 @@ +module simple-string-demo { + + namespace "urn:simple:string:demo"; + prefix "sbd"; + + organization "OPEN DAYLIGHT"; + contact "http://www.opendaylight.org/"; + + revision 2013-06-18 { + + } + + typedef typedef-string { + type string { + length "40"; + pattern "[0-9A-F]\.*"; + pattern "[B-D]*"; + pattern "[4-7]*"; + } + } + +}