<groupId>junit</groupId>\r
<artifactId>junit</artifactId>\r
</dependency>\r
+ <dependency>\r
+ <groupId>commons-lang</groupId>\r
+ <artifactId>commons-lang</artifactId>\r
+ <version>2.1</version>\r
+ </dependency>\r
</dependencies>\r
</project>
\ No newline at end of file
*/
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;
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;
genTOBuilder.addEqualsIdentity(genPropBuilder);
genTOBuilder.addHashIdentity(genPropBuilder);
genTOBuilder.addToStringProperty(genPropBuilder);
+ if (javaType == BaseYangTypes.STRING_TYPE) {
+ if (typedef instanceof ExtendedType) {
+ List<PatternConstraint> patternConstraints = ((ExtendedType) typedef).getPatterns();
+ List<String> regularExpressions = new ArrayList<String>();
+ 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;
}
return genTOBuilder.toInstance();
}
+
}
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;
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<File> inputFiles = new ArrayList<File>();
@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);
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<GeneratedProperty> genProperties = genTO
- .getProperties();
+ List<GeneratedProperty> genProperties = genTO.getProperties();
classPropertiesNumb = genProperties.size();
genProperties = null;
// 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<Parameter> parameters = methodSignature
- .getParameters();
+ List<Parameter> parameters = methodSignature.getParameters();
setByteLeafMethodParamNum = parameters.size();
for (Parameter parameter : parameters) {
- nameMethodeParamType = parameter.getType()
- .getName();
+ nameMethodeParamType = parameter.getType().getName();
}
}
}
- 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");
}
--- /dev/null
+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<File> testModels = new ArrayList<File>();
+
+ @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<Module> modules = parser.parseYangModels(testModels);
+ final SchemaContext context = parser.resolveSchemaContext(modules);
+
+ assertNotNull(context);
+ final BindingGenerator bindingGen = new BindingGeneratorImpl();
+ final List<Type> 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<Constant> 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);
+
+ }
+
+}
public class GeneratedTypesTest {
- private SchemaContext resolveSchemaContextFromFiles(
- final String... yangFiles) {
+ private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
final YangModelParser parser = new YangParserImpl();
final List<File> inputFiles = new ArrayList<File>();
@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<Type> 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<Type> genTypes = bindingGen.generateTypes(context);
assertEquals(57, genTypes.size());
- assertTrue(genTypes != null);
+ assertNotNull(genTypes);
GeneratedTransferObject gtIfcKey = null;
GeneratedType gtIfc = null;
Type ifcIdPropType = ifcIdProp.getReturnType();
assertNotNull(ifcIdPropType);
assertFalse(ifcIdPropType.equals("java.lang.Void"));
- assertTrue(ifcIdPropType.getName().equals("String"));
+ assertEquals(ifcIdPropType.getName(), "String");
// Interface
final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
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<MethodSignature> gtNetworkLinkMethods = gtNetworkLink
- .getMethodDefinitions();
+ final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
assertNotNull(gtNetworkLinkMethods);
MethodSignature getIfc = null;
for (MethodSignature method : gtNetworkLinkMethods) {
Type getIfcType = getIfc.getReturnType();
assertNotNull(getIfcType);
assertFalse(getIfcType.equals("java.lang.Void"));
- assertTrue(getIfcType.getName().equals("String"));
+ assertEquals(getIfcType.getName(), "String");
// SourceNode
- final List<MethodSignature> gtSourceMethods = gtSource
- .getMethodDefinitions();
+ final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
assertNotNull(gtSourceMethods);
MethodSignature getIdSource = null;
for (MethodSignature method : gtSourceMethods) {
Type getIdType = getIdSource.getReturnType();
assertNotNull(getIdType);
assertFalse(getIdType.equals("java.lang.Void"));
- assertTrue(getIdType.getName().equals("Uri"));
+ assertEquals(getIdType.getName(), "Uri");
// DestinationNode
- final List<MethodSignature> gtDestMethods = gtDest
- .getMethodDefinitions();
+ final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
assertNotNull(gtDestMethods);
MethodSignature getIdDest = null;
for (MethodSignature method : gtDestMethods) {
Type getIdDestType = getIdDest.getReturnType();
assertNotNull(getIdDestType);
assertFalse(getIdDestType.equals("java.lang.Void"));
- assertTrue(getIdDestType.getName().equals("Uri"));
+ assertEquals(getIdDestType.getName(), "Uri");
// Tunnel
- final List<MethodSignature> gtTunnelMethods = gtTunnel
- .getMethodDefinitions();
+ final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
assertNotNull(gtTunnelMethods);
MethodSignature getTunnelKey = null;
for (MethodSignature method : gtTunnelMethods) {
Type getTunnelKeyType = getTunnelKey.getReturnType();
assertNotNull(getTunnelKeyType);
assertFalse(getTunnelKeyType.equals("java.lang.Void"));
- assertTrue(getTunnelKeyType.getName().equals("TunnelKey"));
+ assertEquals(getTunnelKeyType.getName(), "TunnelKey");
// TunnelKey
- final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey
- .getProperties();
+ final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
assertNotNull(gtTunnelKeyProps);
GeneratedProperty tunnelId = null;
for (final GeneratedProperty property : gtTunnelKeyProps) {
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<Type> genTypes = bindingGen.generateTypes(context);
- assertTrue(genTypes != null);
+ assertNotNull(genTypes);
assertEquals(4, genTypes.size());
final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
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<Type> genTypes = bindingGen.generateTypes(context);
- assertTrue(genTypes != null);
+ assertNotNull(genTypes);
assertEquals(4, genTypes.size());
final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
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<Type> 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<MethodSignature> methods = genType
- .getMethodDefinitions();
- int methodsCount = 0;
+ simpleListMethodsCount = genType.getMethodDefinitions().size();
+ final List<MethodSignature> 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<GeneratedProperty> properties = genTO
- .getProperties();
- final List<GeneratedProperty> hashProps = genTO
- .getHashCodeIdentifiers();
- final List<GeneratedProperty> 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<GeneratedProperty> properties = genTO.getProperties();
+ final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
+ final List<GeneratedProperty> 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<Type> 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<GeneratedProperty> properties = genTO
- .getProperties();
- int propertyCount = 0;
+ compositeKeyListKeyCount++;
+ final List<GeneratedProperty> 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<GeneratedProperty> properties = genTO
- .getProperties();
- assertEquals(1, properties.size());
+ final List<GeneratedProperty> properties = genTO.getProperties();
+ innerListKeyPropertyCount = properties.size();
genTOsCount++;
}
}
}
+ assertEquals(1, compositeKeyListKeyCount);
+ assertEquals(2, compositeKeyListKeyPropertyCount);
+
+ assertEquals(1, innerListKeyPropertyCount);
assertEquals(6, genTypesCount);
assertEquals(2, genTOsCount);
@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<Type> 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++;
--- /dev/null
+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]*";
+ }
+ }
+
+}
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");
--- /dev/null
+package org.opendaylight.controller.binding.generator.util;
+
+public final class TypeConstants {
+
+ public static final String PATTERN_CONSTANT_NAME = "PATTERN_CONSTANTS";
+
+ private TypeConstants() {
+ }
+}
*/
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.*;
private final List<GeneratedType> enclosedTypes;
private final boolean isAbstract;
- public AbstractGeneratedType(final Type parent, final String packageName,
- final String name, final String comment,
- final List<AnnotationTypeBuilder> annotationBuilders,
- final boolean isAbstract,
- final List<Type> implementsTypes,
- final List<GeneratedTypeBuilder> enclosedGenTypeBuilders,
- final List<GeneratedTOBuilder> enclosedGenTOBuilders,
- final List<EnumBuilder> enumBuilders,
- final List<Constant> constants,
- final List<MethodSignatureBuilder> methodBuilders) {
+ public AbstractGeneratedType(final Type parent, final String packageName, final String name, final String comment,
+ final List<AnnotationTypeBuilder> annotationBuilders, final boolean isAbstract,
+ final List<Type> implementsTypes, final List<GeneratedTypeBuilder> enclosedGenTypeBuilders,
+ final List<GeneratedTOBuilder> enclosedGenTOBuilders, final List<EnumBuilder> enumBuilders,
+ final List<Constant> constants, final List<MethodSignatureBuilder> methodBuilders) {
super(packageName, name);
this.parent = parent;
this.comment = comment;
}
private List<GeneratedType> toUnmodifiableEnclosedTypes(final List<GeneratedTypeBuilder> enclosedGenTypeBuilders,
- final List<GeneratedTOBuilder> enclosedGenTOBuilders) {
+ final List<GeneratedTOBuilder> enclosedGenTOBuilders) {
final List<GeneratedType> enclosedTypes = new ArrayList<>();
for (final GeneratedTypeBuilder builder : enclosedGenTypeBuilders) {
if (builder != null) {
return enclosedTypes;
}
- protected List<AnnotationType> toUnmodifiableAnnotations(
- final List<AnnotationTypeBuilder> annotationBuilders) {
+ protected List<AnnotationType> toUnmodifiableAnnotations(final List<AnnotationTypeBuilder> annotationBuilders) {
final List<AnnotationType> annotations = new ArrayList<>();
for (final AnnotationTypeBuilder builder : annotationBuilders) {
annotations.add(builder.toInstance());
return Collections.unmodifiableList(annotations);
}
- protected List<MethodSignature> toUnmodifiableMethods(
- List<MethodSignatureBuilder> methodBuilders) {
+ protected List<MethodSignature> toUnmodifiableMethods(List<MethodSignatureBuilder> methodBuilders) {
final List<MethodSignature> methods = new ArrayList<>();
for (final MethodSignatureBuilder methodBuilder : methodBuilders) {
methods.add(methodBuilder.toInstance(this));
return Collections.unmodifiableList(methods);
}
- protected List<Enumeration> toUnmodifiableEnumerations(
- List<EnumBuilder> enumBuilders) {
+ protected List<Enumeration> toUnmodifiableEnumerations(List<EnumBuilder> enumBuilders) {
final List<Enumeration> enums = new ArrayList<>();
for (final EnumBuilder enumBuilder : enumBuilders) {
enums.add(enumBuilder.toInstance(this));
@Override
public boolean isAbstract() {
- return isAbstract;
+ return isAbstract;
}
- @Override
+ @Override
public List<Type> getImplements() {
return implementsTypes;
}
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;
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<GeneratedProperty> fields = genTO.getProperties();
final List<Enumeration> enums = genTO.getEnumerations();
+ final List<Constant> consts = genTO.getConstantDefinitions();
writer.write(GeneratorUtil.createPackageDeclaration(currentPkg));
writer.write(NL);
}
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<Pattern>" + GAP + MEMBER_PATTERN_LIST + GAP + ASSIGN + GAP
+ + "new ArrayList<Pattern>()" + 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);
}
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";
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() {
+ }
}
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;
private GeneratorUtil() {
}
- public static String createIfcDeclaration(final GeneratedType genType,
- final String indent,
+ public static String createIfcDeclaration(final GeneratedType genType, final String indent,
final Map<String, LinkedHashMap<String, Integer>> 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<String, LinkedHashMap<String, Integer>> availableImports,
- boolean isIdentity) {
- return createFileDeclaration(CLASS, genTransferObject, indent,
- availableImports, isIdentity);
+ public static String createClassDeclaration(final GeneratedTransferObject genTransferObject, final String indent,
+ final Map<String, LinkedHashMap<String, Integer>> 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<String, LinkedHashMap<String, Integer>> availableImports,
- boolean isIdentity) {
+ private static String createFileDeclaration(final String type, final GeneratedType genType, final String indent,
+ final Map<String, LinkedHashMap<String, Integer>> availableImports, boolean isIdentity) {
final StringBuilder builder = new StringBuilder();
final String currentPkg = genType.getPackageName();
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);
}
} 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));
}
}
return builder.toString();
}
- private static StringBuilder appendAnnotations(final StringBuilder builder,
- final List<AnnotationType> annotations) {
+ private static StringBuilder appendAnnotations(final StringBuilder builder, final List<AnnotationType> annotations) {
if ((builder != null) && (annotations != null)) {
for (final AnnotationType annotation : annotations) {
builder.append("@");
if (annotation.containsParameters()) {
builder.append("(");
- final List<AnnotationType.Parameter> parameters = annotation
- .getParameters();
+ final List<AnnotationType.Parameter> parameters = annotation.getParameters();
appendAnnotationParams(builder, parameters);
builder.append(")");
}
return builder;
}
- private static StringBuilder appendAnnotationParams(
- final StringBuilder builder,
+ private static StringBuilder appendAnnotationParams(final StringBuilder builder,
final List<AnnotationType.Parameter> parameters) {
if (parameters != null) {
int i = 0;
return builder;
}
- public static String createConstant(final Constant constant,
- final String indent,
- final Map<String, LinkedHashMap<String, Integer>> availableImports,
- final String currentPkg) {
+ public static String createConstant(final Constant constant, final String indent,
+ final Map<String, LinkedHashMap<String, Integer>> 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<String, LinkedHashMap<String, Integer>> availableImports,
- final String currentPkg) {
+ public static String createField(final GeneratedProperty property, final String indent,
+ Map<String, LinkedHashMap<String, Integer>> availableImports, final String currentPkg) {
final StringBuilder builder = new StringBuilder();
if (!property.getAnnotations().isEmpty()) {
final List<AnnotationType> annotations = property.getAnnotations();
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();
}
* @param indent
* @return
*/
- public static String createMethodDeclaration(final MethodSignature method,
- final String indent,
- Map<String, LinkedHashMap<String, Integer>> availableImports,
- final String currentPkg) {
+ public static String createMethodDeclaration(final MethodSignature method, final String indent,
+ Map<String, LinkedHashMap<String, Integer>> 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();
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<Parameter> parameters = method.getParameters();
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);
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);
return builder.toString();
}
- public static String createConstructor(
- GeneratedTransferObject genTransferObject, final String indent,
- Map<String, LinkedHashMap<String, Integer>> availableImports,
- boolean isIdentity) {
+ public static String createConstructor(GeneratedTransferObject genTransferObject, final String indent,
+ Map<String, LinkedHashMap<String, Integer>> availableImports, boolean isIdentity) {
final StringBuilder builder = new StringBuilder();
final String currentPkg = genTransferObject.getPackageName();
- final List<GeneratedProperty> properties = genTransferObject
- .getProperties();
+ final List<GeneratedProperty> properties = genTransferObject.getProperties();
final List<GeneratedProperty> ctorParams = new ArrayList<GeneratedProperty>();
- 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);
+ }
}
}
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());
}
builder.append(NL);
}
}
+ List<Constant> 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<String, LinkedHashMap<String, Integer>> availableImports,
- final String currentPkg) {
+ public static String createGetter(final GeneratedProperty property, final String indent,
+ Map<String, LinkedHashMap<String, Integer>> availableImports, final String currentPkg) {
final StringBuilder builder = new StringBuilder();
final Type type = property.getReturnType();
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;
return builder.toString();
}
- public static String createSetter(final GeneratedProperty property,
- final String indent,
- Map<String, LinkedHashMap<String, Integer>> availableImports,
- String currentPkg) {
+ public static String createSetter(final GeneratedProperty property, final String indent,
+ Map<String, LinkedHashMap<String, Integer>> availableImports, String currentPkg) {
final StringBuilder builder = new StringBuilder();
final Type type = property.getReturnType();
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<GeneratedProperty> properties, final String indent) {
+ public static String createHashCode(final List<GeneratedProperty> properties, final String indent) {
StringBuilder builder = new StringBuilder();
builder.append(indent + "public int hashCode() {" + NL);
builder.append(indent + TAB + "final int prime = 31;" + NL);
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);
}
return builder.toString();
}
- public static String createEquals(final GeneratedTransferObject type,
- final List<GeneratedProperty> properties, final String indent) {
+ public static String createEquals(final GeneratedTransferObject type, final List<GeneratedProperty> properties,
+ final String indent) {
StringBuilder builder = new StringBuilder();
final String indent1 = indent + TAB;
final String indent2 = indent1 + TAB;
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);
}
return builder.toString();
}
- public static String createToString(final GeneratedTransferObject type,
- final List<GeneratedProperty> properties, final String indent) {
+ public static String createToString(final GeneratedTransferObject type, final List<GeneratedProperty> properties,
+ final String indent) {
StringBuilder builder = new StringBuilder();
builder.append(indent);
builder.append("public String toString() {");
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<Pair> values = enumeration.getValues();
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);
}
private static String getExplicitType(final Type type,
- Map<String, LinkedHashMap<String, Integer>> availableImports,
- final String currentPkg) {
+ Map<String, LinkedHashMap<String, Integer>> 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<String, Integer> imports = availableImports.get(type
- .getName());
+ LinkedHashMap<String, Integer> 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")) {
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")) {
}
private static String getParameters(final Type[] pTypes,
- Map<String, LinkedHashMap<String, Integer>> availableImports,
- String currentPkg) {
+ Map<String, LinkedHashMap<String, Integer>> availableImports, String currentPkg) {
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < pTypes.length; i++) {
Type t = pTypes[i];
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<String> findMaxValue(
- LinkedHashMap<String, Integer> imports) {
+ private static List<String> findMaxValue(LinkedHashMap<String, Integer> imports) {
final List<String> result = new ArrayList<String>();
int maxValue = 0;
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);
}
}
- public static Map<String, LinkedHashMap<String, Integer>> createImports(
- GeneratedType genType) {
+ public static Map<String, LinkedHashMap<String, Integer>> createImports(GeneratedType genType) {
final Map<String, LinkedHashMap<String, Integer>> imports = new HashMap<String, LinkedHashMap<String, Integer>>();
final String genTypePkg = genType.getPackageName();
final List<Constant> constants = genType.getConstantDefinitions();
- final List<MethodSignature> methods = genType.getMethodDefinitions();
+ final List<MethodSignature> methods = genType.getMethodDefinitions();
List<Type> impl = genType.getImplements();
// IMPLEMENTATIONS
}
}
+ // 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<String, LinkedHashMap<String, Integer>> importedTypes,
+ private static void addTypeToImports(Type type, Map<String, LinkedHashMap<String, Integer>> 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<String, Integer> packages = importedTypes.get(typeName);
}
}
- public static List<String> createImportLines(
- Map<String, LinkedHashMap<String, Integer>> imports) {
+ public static List<String> createImportLines(Map<String, LinkedHashMap<String, Integer>> imports) {
List<String> importLines = new ArrayList<String>();
- for (Map.Entry<String, LinkedHashMap<String, Integer>> entry : imports
- .entrySet()) {
+ for (Map.Entry<String, LinkedHashMap<String, Integer>> entry : imports.entrySet()) {
String typeName = entry.getKey();
LinkedHashMap<String, Integer> typePkgMap = entry.getValue();
String typePkg = typePkgMap.keySet().iterator().next();
return importLines;
}
+ public static boolean isConstantInTO(String constName, GeneratedTransferObject genTO) {
+ if (constName == null || genTO == null)
+ throw new IllegalArgumentException();
+ List<Constant> consts = genTO.getConstantDefinitions();
+ for (Constant cons : consts) {
+ if (cons.getName().equals(constName)) {
+ return true;
+ }
+
+ }
+ return false;
+ }
+
}
--- /dev/null
+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]*";
+ }
+ }
+
+}