- Implemented type length restriction in generated code (Bug 93).
- Setters for binary and string fields contains check for valid length.
- Added new "fieldsFrom(DataObject arg)" method to Builder classes.
- Method accepts class generated from grouping and serves as setter of properties based on grouping type.
Change-Id: I402ef1d76a68cdde3f4a8a8c74d2100ac38a6487
Signed-off-by: Martin Vitez <mvitez@cisco.com>
import java.util.Collection
import org.opendaylight.yangtools.yang.model.api.YangNode
import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
+import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
public class BindingGeneratorImpl implements BindingGenerator {
if (genTOBuilder !== null) {
returnType = new ReferencedTypeImpl(genTOBuilder.packageName, genTOBuilder.name);
}
- } else {
- returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
+ } else {\r
+ val Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);\r
+ returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
}
if (returnType !== null) {
val MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
*/\r
package org.opendaylight.yangtools.sal.binding.generator.spi;\r
\r
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;\r
import org.opendaylight.yangtools.sal.binding.model.api.Type;\r
import org.opendaylight.yangtools.yang.model.api.SchemaNode;\r
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;\r
* @return Resolved Type\r
*/\r
Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode);\r
+\r
+ Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode, final Restrictions restrictions);\r
}\r
import java.text.DateFormat;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
+import org.opendaylight.yangtools.yang.model.util.ExtendedType;
/**
* Contains the methods for converting strings to valid JAVA language strings
}
return sb.toString();
}
+
+ public static Restrictions getRestrictions(TypeDefinition<?> type) {
+ final List<LengthConstraint> length = new ArrayList<>();
+ final List<PatternConstraint> pattern = new ArrayList<>();
+ final List<RangeConstraint> range = new ArrayList<>();
+
+ if (type instanceof ExtendedType) {
+ ExtendedType ext = (ExtendedType)type;
+ length.addAll(ext.getLengthConstraints());
+ pattern.addAll(ext.getPatternConstraints());
+ range.addAll(ext.getRangeConstraints());
+ }
+
+ return new Restrictions() {
+ @Override
+ public List<RangeConstraint> getRangeConstraints() {
+ return range;
+ }
+ @Override
+ public List<PatternConstraint> getPatternConstraints() {
+ return pattern;
+ }
+ @Override
+ public List<LengthConstraint> getLengthConstraints() {
+ return length;
+ }
+ @Override
+ public boolean isEmpty() {
+ return range.isEmpty() && pattern.isEmpty() && length.isEmpty();
+ }
+ };
+ }
+
}
*/
package org.opendaylight.yangtools.binding.generator.util;
-import java.math.BigDecimal;
-import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
import org.opendaylight.yangtools.sal.binding.model.api.Type;
import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
import org.opendaylight.yangtools.yang.binding.Augmentable;
private static final Type SET_TYPE = typeForClass(Set.class);
private static final Type LIST_TYPE = typeForClass(List.class);
private static final Type MAP_TYPE = typeForClass(Map.class);
- public static final ConcreteType NUMBER = typeForClass(Number.class);
- public static final ConcreteType BIG_DECIMAL = typeForClass(BigDecimal.class);
- public static final ConcreteType BIG_INTEGER = typeForClass(BigInteger.class);
- public static final ConcreteType BYTE = typeForClass(Byte.class);
+
public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
- public static final ConcreteType DOUBLE = typeForClass(Double.class);
- public static final ConcreteType FLOAT = typeForClass(Float.class);
- public static final ConcreteType INTEGER = typeForClass(Integer.class);
- public static final ConcreteType LONG = typeForClass(Long.class);
- public static final ConcreteType SHORT = typeForClass(Short.class);
- public static final ConcreteType STRING = typeForClass(String.class);
- public static final ConcreteType CHAR_SEQUENCE = typeForClass(CharSequence.class);
- public static final ConcreteType THREAD = typeForClass(Thread.class);
public static final ConcreteType FUTURE = typeForClass(Future.class);
- public static final ConcreteType CALLABLE = typeForClass(Callable.class);
public static final ConcreteType VOID = typeForClass(Void.class);
- public static final ConcreteType THROWABLE = typeForClass(Throwable.class);
- public static final ConcreteType EXCEPTION = typeForClass(Exception.class);
/**
* It is not desirable to create instance of this class
* Creates the instance of type
* {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
* ConcreteType} which represents JAVA <code>void</code> type.
- *
+ *
* @return <code>ConcreteType</code> instance which represents JAVA
* <code>void</code>
*/
* {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
* ConcreteType} which represents primitive JAVA type for which package
* doesn't exist.
- *
+ *
* @param primitiveType
* string containing programaticall construction based on
* primitive type (e.g byte[])
* @return <code>ConcreteType</code> instance which represents programatic
* construction with primitive JAVA type
*/
- public static Type primitiveType(final String primitiveType) {
- return new ConcreteTypeImpl("", primitiveType);
+ public static Type primitiveType(final String primitiveType, final Restrictions restrictions) {
+ return new ConcreteTypeImpl("", primitiveType, restrictions);
}
/**
* Returns an instance of {@link ConcreteType} describing the class
- *
+ *
* @param cls
* Class to describe
* @return Description of class
*/
public static ConcreteType typeForClass(Class<?> cls) {
- return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName());
+ return typeForClass(cls, null);
+ }
+
+ public static ConcreteType typeForClass(Class<?> cls, Restrictions restrictions) {
+ return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
}
/**
* Returns an instance of {@link ParameterizedType} describing the typed
* {@link Map}<K,V>
- *
+ *
* @param keyType
* Key Type
* @param valueType
/**
* Returns an instance of {@link ParameterizedType} describing the typed
* {@link Set}<V> with concrete type of value.
- *
+ *
* @param valueType
* Value Type
* @return Description of generic type instance of Set
/**
* Returns an instance of {@link ParameterizedType} describing the typed
* {@link List}<V> with concrete type of value.
- *
+ *
* @param valueType
* Value Type
* @return Description of type instance of List
/**
* Creates generated transfer object for
* {@link org.opendaylight.yangtools.yang.binding.BaseIdentity BaseIdentity}
- *
+ *
* @return generated transfer object which is used as extension when YANG
* <code>identity</code> is mapped to generated TO
*/
* Creates instance of type
* {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
* ParameterizedType}
- *
+ *
* @param type
* JAVA <code>Type</code> for raw type
* @param parameters
* Creates instance of type
* {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
* WildcardType}
- *
+ *
* @param packageName
* string with the package name
* @param typeName
* ParameterizedType} where raw type is
* {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
* parameter is <code>valueType</code>.
- *
+ *
* @param valueType
* JAVA <code>Type</code> with actual parameter
* @return <code>ParametrizedType</code> reprezentation of raw type
* ParameterizedType} where raw type is
* {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
* parameter is <code>valueType</code>.
- *
+ *
* @param valueType
* JAVA <code>Type</code> with actual parameter
* @return <code>ParametrizedType</code> reprezentation of raw type
}
/**
- *
+ *
* Represents concrete JAVA type.
- *
+ *
*/
private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
+ private final Restrictions restrictions;
+
/**
* Creates instance of this class with package <code>pkName</code> and
* with the type name <code>name</code>.
- *
+ *
* @param pkName
* string with package name
* @param name
* string with the name of the type
*/
- private ConcreteTypeImpl(String pkName, String name) {
+ private ConcreteTypeImpl(String pkName, String name, Restrictions restrictions) {
super(pkName, name);
+ this.restrictions = restrictions;
+ }
+
+ @Override
+ public Restrictions getRestrictions() {
+ return restrictions;
}
}
/**
- *
+ *
* Represents parametrized JAVA type.
- *
+ *
*/
private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
/**
/**
* Creates instance of this class with concrete rawType and array of
* actual parameters.
- *
+ *
* @param rawType
* JAVA <code>Type</code> for raw type
* @param actTypes
}
/**
- *
+ *
* Represents JAVA bounded wildcard type.
- *
+ *
*/
private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
/**
* Creates instance of this class with concrete package and type name.
- *
+ *
* @param packageName
* string with the package name
* @param typeName
\r
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;\r
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;\r
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;\r
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;\r
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;\r
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;\r
private final List<GeneratedPropertyBuilder> hashProperties = new ArrayList<>();\r
private final List<GeneratedPropertyBuilder> toStringProperties = new ArrayList<>();\r
private boolean isUnionType = false;\r
+ private Restrictions restrictions;\r
\r
public GeneratedTOBuilderImpl(String packageName, String name) {\r
super(packageName, name);\r
* {@link MethodSignatureBuilder#setAbstract(boolean)},\r
* {TypeMemberBuilder#setFinal(boolean)} and\r
* {TypeMemberBuilder#setAccessModifier(boolean)}\r
- * \r
+ *\r
* @param name\r
* Name of Method\r
* @return <code>new</code> instance of Method Signature Builder.\r
return this;\r
}\r
\r
+ @Override\r
+ public void setRestrictions(Restrictions restrictions) {\r
+ this.restrictions = restrictions;\r
+ }\r
+\r
@Override\r
public GeneratedTransferObject toInstance() {\r
return new GeneratedTransferObjectImpl(this);\r
private final List<GeneratedProperty> stringProperties;\r
private final GeneratedTransferObject extendsType;\r
private final boolean isUnionType;\r
+ private final Restrictions restrictions;\r
\r
public GeneratedTransferObjectImpl(GeneratedTOBuilderImpl builder) {\r
super(builder);\r
this.hashCodeProperties = toUnmodifiableProperties(builder.hashProperties);\r
this.stringProperties = toUnmodifiableProperties(builder.toStringProperties);\r
this.isUnionType = builder.isUnionType;\r
+ this.restrictions = builder.restrictions;\r
}\r
\r
@Override\r
return stringProperties;\r
}\r
\r
+ @Override\r
+ public Restrictions getRestrictions() {\r
+ return restrictions;\r
+ }\r
+\r
@Override\r
public String toString() {\r
StringBuilder builder = new StringBuilder();\r
import org.opendaylight.yangtools.binding.generator.util.Types
import com.google.common.base.Splitter
import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature
+import com.google.common.collect.Range
+import java.util.ArrayList
+import java.util.List
+import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
abstract class BaseTemplate {
**/
'''
}
+
+ def generateLengthRestrictions(Type type, String paramName) '''
+ «IF type instanceof ConcreteType»
+ «val restrictions = (type as ConcreteType).restrictions»
+ «IF restrictions !== null && !restrictions.lengthConstraints.empty»
+ «generateLengthRestriction(type, restrictions, paramName)»
+ «ENDIF»
+ «ENDIF»
+ «IF type instanceof GeneratedTransferObject»
+ «val restrictions = (type as GeneratedTransferObject).restrictions»
+ «IF restrictions !== null && !restrictions.lengthConstraints.empty»
+ «generateLengthRestriction(type, restrictions, paramName)»
+ «ENDIF»
+ «ENDIF»
+ '''
+
+ def generateLengthRestriction(Type type, Restrictions restrictions, String paramName) '''
+ boolean isValidLength = false;
+ «List.importedName»<«Range.importedName»<«Integer.importedName»>> lengthConstraints = new «ArrayList.importedName»<>();
+ «FOR r : restrictions.lengthConstraints»
+ lengthConstraints.add(«Range.importedName».closed(«r.min», «r.max»));
+ «ENDFOR»
+ for («Range.importedName»<«Integer.importedName»> r : lengthConstraints) {
+ «IF type.name.contains("[")»
+ if (r.contains(«paramName».length)) {
+ «ELSE»
+ if (r.contains(«paramName».length())) {
+ «ENDIF»
+ isValidLength = true;
+ }
+ }
+ if (!isValidLength) {
+ throw new IllegalArgumentException("illegal length");
+ }
+ '''
+
}
\ No newline at end of file
import org.opendaylight.yangtools.yang.binding.Augmentable\r
import static org.opendaylight.yangtools.binding.generator.util.Types.*\r
import java.util.HashMap\r
-import java.util.Collections\r
-\r
+import java.util.Collections\rimport org.opendaylight.yangtools.yang.binding.DataObject
+import java.util.ArrayList
+
/**\r
* Template for generating JAVA builder classes. \r
*/\r
\r
«generateFields(false)»\r
\r
- «generateBuilderConstructor(type)»\r
+ «generateConstructorsFromIfcs(type)»\r
+\r
+ «generateSetterFromIfcs(type)»\r
\r
«generateGetters(false)»\r
\r
}\r
'''\r
\r
-\r
- def private generateBuilderConstructor(Type type) '''\r
+ def private generateConstructorsFromIfcs(Type type) '''\r
public «type.name»«BUILDER»() {\r
- }\r
+ } \r
«IF (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject))»\r
«val ifc = type as GeneratedType»\r
«FOR impl : ifc.implements»\r
- «generateSingleBuilderConstructor(impl)»\r
+ «generateConstructorFromIfc(impl)»\r
«ENDFOR»\r
«ENDIF»\r
'''\r
\r
- def private generateSingleBuilderConstructor(Type impl) '''\r
+ def private generateConstructorFromIfc(Type impl) '''\r
«IF (impl instanceof GeneratedType) && !((impl as GeneratedType).methodDefinitions.empty)»\r
- \r
«val implType = impl as GeneratedType»\r
+\r
public «type.name»«BUILDER»(«implType.fullyQualifiedName» arg) {\r
- «printBuilderConstructorProperties(implType)»\r
+ «printConstructorProperties(implType)»\r
}\r
«FOR implTypeImplement : implType.implements»\r
- «generateSingleBuilderConstructor(implTypeImplement)»\r
+ «generateConstructorFromIfc(implTypeImplement)»\r
«ENDFOR»\r
«ENDIF»\r
'''\r
\r
- def private printBuilderConstructorProperties(Type implementedIfc) '''\r
+ def private printConstructorProperties(Type implementedIfc) '''\r
«IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»\r
«val ifc = implementedIfc as GeneratedType»\r
«FOR getter : ifc.methodDefinitions»\r
this._«getter.propertyNameFromGetter» = arg.«getter.name»();\r
«ENDFOR»\r
«FOR impl : ifc.implements»\r
- «printBuilderConstructorProperties(impl)»\r
+ «printConstructorProperties(impl)»\r
+ «ENDFOR»\r
+ «ENDIF»\r
+ '''\r
+\r
+ def private generateSetterFromIfcs(Type type) '''\r
+ «IF (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject))»\r
+ «val ifc = type as GeneratedType»\r
+ «val List<Type> done = new ArrayList()»\r
+ public void fieldsFrom(«DataObject.importedName» arg) {\r
+ «FOR impl : ifc.implements»\r
+ «generateSettersForIfc(impl, done)»\r
+ «ENDFOR»\r
+ }\r
+ «ENDIF»\r
+ '''\r
+\r
+ def private generateSettersForIfc(Type impl, List<Type> done) '''\r
+ «IF (impl instanceof GeneratedType) && !((impl as GeneratedType).methodDefinitions.empty)»\r
+ «val implType = impl as GeneratedType»\r
+ «val boolean added = done.contains(impl)»\r
+ «IF !(added)»\r
+ if (arg instanceof «implType.fullyQualifiedName») {\r
+ «printSetterProperties(implType, done)»\r
+ }\r
+ «ENDIF»\r
+ «FOR implTypeImplement : implType.implements»\r
+ «generateSettersForIfc(implTypeImplement, done)»\r
+ «ENDFOR»\r
+ «ENDIF»\r
+ '''\r
+\r
+ def private printSetterProperties(Type implementedIfc, List<Type> done) '''\r
+ «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»\r
+ «val ifc = implementedIfc as GeneratedType»\r
+ «val boolean added = done.contains(ifc)»\r
+ «IF !(added)»\r
+ «FOR getter : ifc.methodDefinitions»\r
+ this._«getter.propertyNameFromGetter» = ((«implementedIfc.fullyQualifiedName»)arg).«getter.name»();\r
+ «ENDFOR»\r
+ «val add = done.add(ifc)»\r
+ «ENDIF»\r
+ «FOR impl : ifc.implements»\r
+ «printSetterProperties(impl, done)»\r
«ENDFOR»\r
«ENDIF»\r
'''\r
def private generateSetters() '''\r
«FOR field : properties SEPARATOR '\n'»\r
public «type.name»«BUILDER» set«field.name.toFirstUpper»(«field.returnType.importedName» value) {\r
+ «generateLengthRestrictions(field.returnType, "value")»\r
+\r
this.«field.fieldName» = value;\r
return this;\r
}\r
}\r
«ENDIF»\r
'''\r
- \r
+\r
/**\r
* Template method which generate constructor for IMPL class.\r
* \r
super(«parentProperties.asArguments»);\r
«ENDIF»\r
«FOR p : properties» \r
+ «generateLengthRestrictions(p.returnType, p.fieldName.toString)»\r
this.«p.fieldName» = «p.fieldName»;\r
«ENDFOR»\r
}\r
import java.io.File;
import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashSet;
assertNotNull(usesBarGr1);
assertNotNull(usesBazGr1);
+ Method fieldsFromMethod = null;
+ for (Method m : nodesBuilderClass.getDeclaredMethods()) {
+ String methodName = m.getName();
+ if ("fieldsFrom".equals(methodName)) {
+ fieldsFromMethod = m;
+ }
+ }
+ assertNotNull(fieldsFromMethod);
+ assertEquals(1, fieldsFromMethod.getParameterTypes().length);
+
cleanUp(sourcesOutputDir, compiledOutputDir);
}
*/
public interface ConcreteType extends Type {
+ Restrictions getRestrictions();
+
}
* definitions. For this purpose retrieve definitions through
* {@link #getEqualsIdentifiers ()}, {@link #getHashCodeIdentifiers()} and
* {@link #getToStringIdentifiers ()}.
- *
+ *
*/
public interface GeneratedTransferObject extends GeneratedType {
/**
* Returns the extending Generated Transfer Object or <code>null</code> if
* there is no extending Generated Transfer Object.
- *
+ *
* @return the extending Generated Transfer Object or <code>null</code> if
* there is no extending Generated Transfer Object.
*/
/**
* Returns List of Properties that are designated to define equality for
* Generated Transfer Object.
- *
+ *
* @return List of Properties that are designated to define equality for
* Generated Transfer Object.
*/
/**
* Returns List of Properties that are designated to define identity for
* Generated Transfer Object.
- *
+ *
* @return List of Properties that are designated to define identity for
* Generated Transfer Object.
*/
/**
* Returns List of Properties that will be members of toString definition
* for Generated Transfer Object.
- *
+ *
* @return List of Properties that will be members of toString definition
* for Generated Transfer Object.
*/
/**
* Return boolean value which describe whether Generated Transfer Object
* was/wasn't created from union YANG type.
- *
+ *
* @return true value if Generated Transfer Object was created from union
* YANG type.
*/
@Deprecated
boolean isUnionType();
+
+ Restrictions getRestrictions();
}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.binding.model.api;
+
+import java.util.List;
+
+import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
+
+public interface Restrictions {
+
+ List<LengthConstraint> getLengthConstraints();
+ List<PatternConstraint> getPatternConstraints();
+ List<RangeConstraint> getRangeConstraints();
+ boolean isEmpty();
+
+}
package org.opendaylight.yangtools.sal.binding.model.api.type.builder;
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
/**
* Generated Transfer Object Builder is interface that contains methods to build
* and instantiate Generated Transfer Object definition.
- *
+ *
* @see GeneratedTransferObject
*/
public interface GeneratedTOBuilder extends GeneratedTypeBuilderBase<GeneratedTOBuilder> {
* be overwritten by lastly added Generated Transfer Object. <br>
* If Generated Transfer Object is <code>null</code> the method SHOULD throw
* {@link IllegalArgumentException}
- *
+ *
* @param genTransObj
* Generated Transfer Object
* @return This instance of builder
* Add Property that will be part of <code>equals</code> definition. <br>
* If Generated Property Builder is <code>null</code> the method SHOULD
* throw {@link IllegalArgumentException}
- *
+ *
* @param property
* Generated Property Builder
* @return This instance of builder
* Add Property that will be part of <code>hashCode</code> definition. <br>
* If Generated Property Builder is <code>null</code> the method SHOULD
* throw {@link IllegalArgumentException}
- *
+ *
* @param property
* Generated Property Builder
* @return This instance of builder
* Add Property that will be part of <code>toString</code> definition. <br>
* If Generated Property Builder is <code>null</code> the method SHOULD
* throw {@link IllegalArgumentException}
- *
+ *
* @param property
* Generated Property Builder
* @return This instance of builder
*/
GeneratedTOBuilder addToStringProperty(final GeneratedPropertyBuilder property);
+ void setRestrictions(Restrictions restrictions);
+
/**
* Returns instance of <code>GeneratedTransferObject</code> which data are
* build from the data of this builder
- *
+ *
* @return generated transfer object instance
*/
GeneratedTransferObject toInstance();
/**
- *
+ *
* @param isUnion
*/
void setIsUnion(boolean isUnion);
\r
import org.opendaylight.yangtools.binding.generator.util.Types;\r
import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;\r
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;\r
import org.opendaylight.yangtools.sal.binding.model.api.Type;\r
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
import org.opendaylight.yangtools.yang.model.api.SchemaNode;\r
/**\r
* <code>Type</code> representation of <code>binary</code> YANG type\r
*/\r
- public static final Type BINARY_TYPE = Types.primitiveType("byte[]");\r
+ public static final Type BINARY_TYPE = Types.primitiveType("byte[]", null);\r
\r
public static final Type INSTANCE_IDENTIFIER = Types.parameterizedTypeFor(Types\r
.typeForClass(InstanceIdentifier.class));\r
\r
return null;\r
}\r
+\r
+ @Override\r
+ public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode, Restrictions restrictions) {\r
+ String typeName = type.getQName().getLocalName();\r
+ switch (typeName) {\r
+ case "binary" : return Types.primitiveType("byte[]", restrictions);\r
+ case "decimal64": return Types.typeForClass(BigDecimal.class, restrictions);\r
+ case "int8": return Types.typeForClass(Byte.class, restrictions);\r
+ case "int16": return Types.typeForClass(Short.class, restrictions);\r
+ case "int32": return Types.typeForClass(Integer.class, restrictions);\r
+ case "int64": return Types.typeForClass(Long.class, restrictions);\r
+ case "string": return Types.typeForClass(String.class, restrictions);\r
+ case "uint8": return Types.typeForClass(Short.class, restrictions);\r
+ case "uint16": Types.typeForClass(Integer.class, restrictions);\r
+ case "uint32": Types.typeForClass(Long.class, restrictions);\r
+ case "uint64": Types.typeForClass(BigInteger.class, restrictions);\r
+ default: return javaTypeForSchemaDefinitionType(type, parentNode);\r
+ }\r
+ }\r
};\r
}\r
import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.EnumerationBuilderImpl;
import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
+import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
import org.opendaylight.yangtools.sal.binding.model.api.Type;
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
/**
* Creates new instance of class <code>TypeProviderImpl</code>.
- *
+ *
* @param schemaContext
* contains the schema data red from YANG files
* @throws IllegalArgumentException
/**
* Puts <code>refType</code> to map with key <code>refTypePath</code>
- *
+ *
* @param refTypePath
* schema path used as the map key
* @param refType
* <li>if <code>refTypePath</code> equal null</li>
* <li>if <code>refType</code> equal null</li>
* </ul>
- *
+ *
*/
public void putReferencedType(final SchemaPath refTypePath, final Type refType) {
Preconditions.checkArgument(refTypePath != null,
}
/**
- *
+ *
* Converts basic YANG type <code>type</code> to JAVA <code>Type</code>.
- *
+ *
* @param type
* string with YANG name of type
* @return JAVA <code>Type</code> for YANG type <code>type</code>
return BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(type);
}
+ @Override
+ public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode) {
+ return javaTypeForSchemaDefinitionType(typeDefinition, parentNode, null);
+ }
+
/**
* Converts schema definition type <code>typeDefinition</code> to JAVA
* <code>Type</code>
- *
+ *
* @param typeDefinition
* type definition which is converted to JAVA type
* @throws IllegalArgumentException
* </ul>
*/
@Override
- public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode) {
+ public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode, Restrictions r) {
Type returnType = null;
Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
if (typeDefinition.getQName() == null) {
returnType = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode);
if (returnType == null) {
returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition,
- parentNode);
+ parentNode, r);
}
}
// TODO: add throw exception when we will be able to resolve ALL yang
* Returns JAVA <code>Type</code> for instances of the type
* <code>LeafrefTypeDefinition</code> or
* <code>IdentityrefTypeDefinition</code>.
- *
+ *
* @param typeDefinition
* type definition which is converted to JAVA <code>Type</code>
* @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
/**
* Returns JAVA <code>Type</code> for instances of the type
* <code>ExtendedType</code>.
- *
+ *
* @param typeDefinition
* type definition which is converted to JAVA <code>Type</code>
* @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
returnType = provideTypeForEnum(enumTypeDef, typedefName, typeDefinition);
} else {
final Module module = findParentModule(schemaContext, typeDefinition);
+ Restrictions r = BindingGeneratorUtil.getRestrictions(typeDefinition);
if (module != null) {
final Map<String, Type> genTOs = genTypeDefsContextMap.get(module.getName());
if (genTOs != null) {
}
if (returnType == null) {
returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
- baseTypeDef, typeDefinition);
+ baseTypeDef, typeDefinition, r);
}
}
}
* Seeks for identity reference <code>idref</code> the JAVA
* <code>type</code>.<br />
* <br />
- *
+ *
* <i>Example:<br />
* If identy which is referenced via <code>idref</code> has name <b>Idn</b>
* then returning type is <b>{@code Class<? extends Idn>}</b></i>
- *
+ *
* @param idref
* identityref type definition for which JAVA <code>Type</code>
* is sought
/**
* Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
- *
+ *
* @param typeDefinition
* type definition which should be converted to JAVA
* <code>Type</code>
/**
* Gets base type definition for <code>extendTypeDef</code>. The method is
* recursivelly called until non <code>ExtendedType</code> type is found.
- *
+ *
* @param extendTypeDef
* type definition for which is the base type definition sought
* @return type definition which is base type for <code>extendTypeDef</code>
/**
* Converts <code>leafrefType</code> to JAVA <code>Type</code>.
- *
+ *
* The path of <code>leafrefType</code> is followed to find referenced node
* and its <code>Type</code> is returned.
- *
+ *
* @param leafrefType
* leafref type definition for which is the type sought
* @return JAVA <code>Type</code> of data schema node which is referenced in
* <li>if <code>leafrefType</code> equal null</li>
* <li>if path statement of <code>leafrefType</code> equal null</li>
* </ul>
- *
+ *
*/
public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType, final SchemaNode parentNode) {
Type returnType = null;
/**
* Checks if <code>dataNode</code> is <code>LeafSchemaNode</code> and if it
* so then checks if it is of type <code>EnumTypeDefinition</code>.
- *
+ *
* @param dataNode
* data schema node for which is checked if it is leaf and if it
* is of enum type
/**
* Checks if <code>dataNode</code> is <code>LeafListSchemaNode</code> and if
* it so then checks if it is of type <code>EnumTypeDefinition</code>.
- *
+ *
* @param dataNode
* data schema node for which is checked if it is leaflist and if
* it is of enum type
* Converts <code>enumTypeDef</code> to
* {@link org.opendaylight.yangtools.sal.binding.model.api.Enumeration
* enumeration}.
- *
+ *
* @param enumTypeDef
* enumeration type definition which is converted to enumeration
* @param enumName
/**
* Adds enumeration to <code>typeBuilder</code>. The enumeration data are
* taken from <code>enumTypeDef</code>.
- *
+ *
* @param enumTypeDef
* enumeration type definition is source of enumeration data for
* <code>typeBuilder</code>
* <li>if name of <code>enumTypeDef</code> equal null</li>
* <li>if name of <code>typeBuilder</code> equal null</li>
* </ul>
- *
+ *
*/
private Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef, final String enumName,
final GeneratedTypeBuilderBase<?> typeBuilder) {
/**
* Converts <code>dataNode</code> to JAVA <code>Type</code>.
- *
+ *
* @param dataNode
* contains information about YANG type
* @return JAVA <code>Type</code> representation of <code>dataNode</code>
/**
* Passes through all modules and through all its type definitions and
* convert it to generated types.
- *
+ *
* The modules are firstly sorted by mutual dependencies. The modules are
* sequentially passed. All type definitions of a module are at the
* beginning sorted so that type definition with less amount of references
* {@link TypeProviderImpl#genTypeDefsContextMap genTypeDefsContextMap}
* which map current module name to the map which maps type names to
* returned types (generated types).
- *
+ *
*/
private void resolveTypeDefsFromContext() {
final Set<Module> modules = schemaContext.getModules();
}
/**
- *
+ *
* @param basePackageName
* string with name of package to which the module belongs
* @param moduleName
Type returnType = null;
if (innerTypeDefinition instanceof ExtendedType) {
ExtendedType innerExtendedType = (ExtendedType) innerTypeDefinition;
- returnType = provideGeneratedTOFromExtendedType(innerExtendedType, basePackageName, typedefName);
+ returnType = provideGeneratedTOFromExtendedType(typedef, innerExtendedType, basePackageName);
} else if (innerTypeDefinition instanceof UnionTypeDefinition) {
final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(basePackageName,
(UnionTypeDefinition) innerTypeDefinition, typedefName, typedef);
returnType = genTOBuilder.toInstance();
} else {
+ Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
- innerTypeDefinition, typedef);
+ innerTypeDefinition, typedef, r);
returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType);
}
/**
* Wraps base YANG type to generated TO.
- *
+ *
* @param basePackageName
* string with name of package to which the module belongs
* @param typedef
genTOBuilder.addEqualsIdentity(genPropBuilder);
genTOBuilder.addHashIdentity(genPropBuilder);
genTOBuilder.addToStringProperty(genPropBuilder);
- if (javaType == BaseYangTypes.STRING_TYPE && typedef instanceof ExtendedType) {
+ if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef instanceof ExtendedType) {
final List<String> regExps = resolveRegExpressionsFromTypedef((ExtendedType) typedef);
addStringRegExAsConstant(genTOBuilder, regExps);
}
/**
* Converts output list of generated TO builders to one TO builder (first
* from list) which contains the remaining builders as its enclosing TO.
- *
+ *
* @param basePackageName
* string with name of package to which the module belongs
* @param typedef
* Converts <code>typedef</code> to generated TO with
* <code>typeDefName</code>. Every union type from <code>typedef</code> is
* added to generated TO builder as property.
- *
+ *
* @param basePackageName
* string with name of package to which the module belongs
* @param typedef
/**
* Wraps code which handle case when union subtype is also of the type
* <code>UnionType</code>.
- *
+ *
* In this case the new generated TO is created for union subtype (recursive
* call of method
* {@link #provideGeneratedTOBuildersForUnionTypeDef(String, TypeDefinition, String)
* provideGeneratedTOBuilderForUnionTypeDef} and in parent TO builder
* <code>parentUnionGenTOBuilder</code> is created property which type is
* equal to new generated TO.
- *
+ *
* @param parentUnionGenTOBuilder
* generated TO builder to which is the property with the child
* union subtype added
/**
* Wraps code which handle case when union subtype is of the type
* <code>ExtendedType</code>.
- *
+ *
* If TO for this type already exists it is used for the creation of the
* property in <code>parentUnionGenTOBuilder</code>. In other case the base
* type is used for the property creation.
- *
+ *
* @param parentUnionGenTOBuilder
* generated TO builder in which new property is created
* @param unionSubtype
/**
* Searches for generated TO for <code>searchedTypeDef</code> type
* definition in {@link #genTypeDefsContextMap genTypeDefsContextMap}
- *
+ *
* @param searchedTypeName
* string with name of <code>searchedTypeDef</code>
* @return generated TO for <code>searchedTypeDef</code> or
* Stores generated TO created from <code>genTOBuilder</code> for
* <code>newTypeDef</code> to {@link #genTypeDefsContextMap
* genTypeDefsContextMap} if the module for <code>newTypeDef</code> exists
- *
+ *
* @param newTypeDef
* type definition for which is <code>genTOBuilder</code> created
* @param genTOBuilder
/**
* Adds a new property with the name <code>propertyName</code> and with type
* <code>type</code> to <code>unonGenTransObject</code>.
- *
+ *
* @param unionGenTransObject
* generated TO to which should be property added
* @param type
/**
* Converts <code>typedef</code> to the generated TO builder.
- *
+ *
* @param basePackageName
* string with name of package to which the module belongs
* @param typedef
/**
* Converts <code>typeDef</code> which should be of the type
* <code>BitsTypeDefinition</code> to <code>GeneratedTOBuilder</code>.
- *
+ *
* All the bits of the typeDef are added to returning generated TO as
* properties.
- *
+ *
* @param basePackageName
* string with name of package to which the module belongs
* @param typeDef
/**
* Converts the pattern constraints from <code>typedef</code> to the list of
* the strings which represents these constraints.
- *
+ *
* @param typedef
* extended type in which are the pattern constraints sought
* @return list of strings which represents the constraint patterns
* @throws IllegalArgumentException
* if <code>typedef</code> equals null
- *
+ *
*/
private List<String> resolveRegExpressionsFromTypedef(ExtendedType typedef) {
final List<String> regExps = new ArrayList<String>();
}
/**
- *
+ *
* Adds to the <code>genTOBuilder</code> the constant which contains regular
* expressions from the <code>regularExpressions</code>
- *
+ *
* @param genTOBuilder
* generated TO builder to which are
* <code>regular expressions</code> added
* <code>innerExtendedType</code>, about the package name
* <code>typedefName</code> and about the generated TO name
* <code>typedefName</code>.
- *
+ *
* It is supposed that <code>innerExtendedType</code> is already present in
* {@link TypeProviderImpl#genTypeDefsContextMap genTypeDefsContextMap} to
* be possible set it as extended type for the returning generated TO.
- *
+ *
* @param innerExtendedType
* extended type which is part of some other extended type
* @param basePackageName
* <li>if <code>typedefName</code> equals null</li>
* </ul>
*/
- private GeneratedTransferObject provideGeneratedTOFromExtendedType(final ExtendedType innerExtendedType,
- final String basePackageName, final String typedefName) {
-
+ private GeneratedTransferObject provideGeneratedTOFromExtendedType(final TypeDefinition<?> typedef, final ExtendedType innerExtendedType,
+ final String basePackageName) {
Preconditions.checkArgument(innerExtendedType != null, "Extended type cannot be NULL!");
Preconditions.checkArgument(basePackageName != null, "String with base package name cannot be NULL!");
- Preconditions.checkArgument(typedefName != null, "String with type definition name cannot be NULL!");
+ final String typedefName = typedef.getQName().getLocalName();
final String classTypedefName = parseToClassName(typedefName);
final String innerTypeDef = innerExtendedType.getQName().getLocalName();
final GeneratedTOBuilder genTOBuilder = new GeneratedTOBuilderImpl(basePackageName, classTypedefName);
+ Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
+ genTOBuilder.setRestrictions(r);
Map<String, Type> typeMap = null;
final Module parentModule = findParentModule(schemaContext, innerExtendedType);
* equal depth. In next step are lists from this map concatenated to one
* list in ascending order according to their depth. All type definitions
* are in the list behind all type definitions on which depends.
- *
+ *
* @param unsortedTypeDefinitions
* list of type definitions which should be sorted by depth
* @return list of type definitions sorted according their each other
/**
* Returns how many immersion is necessary to get from the type definition
* to the base type.
- *
+ *
* @param typeDefinition
* type definition for which is depth sought.
* @return number of immersions which are necessary to get from the type
* Returns string which contains the same value as <code>name</code> but
* integer suffix is incremented by one. If <code>name</code> contains no
* number suffix then number 1 is added.
- *
+ *
* @param name
* string with name of augmented node
* @return string with the number suffix incremented by one (or 1 is added)
<artifactId>yang-common</artifactId>\r
<version>0.5.9-SNAPSHOT</version>\r
</dependency>\r
+ <dependency>\r
+ <groupId>com.google.guava</groupId>\r
+ <artifactId>guava</artifactId>\r
+ </dependency>\r
</dependencies>\r
<version>0.6.0-SNAPSHOT</version>\r
</project>\r