Document QNAME constant
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / BaseTemplate.xtend
index 27292a96a23d6468b4e13a6b774e8ac8959afad4..5d3c27f224730650771a90f5eed3ddc1873e0583 100644 (file)
@@ -7,58 +7,41 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator
 
-import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
+import static extension org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil.encodeAngleBrackets
+import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
+import static org.opendaylight.mdsal.binding.model.ri.Types.objectType;
 
 import com.google.common.base.CharMatcher
-import com.google.common.base.MoreObjects
 import com.google.common.base.Splitter
-import com.google.common.collect.Iterables
 import java.util.Collection
 import java.util.List
+import java.util.Locale
 import java.util.Map.Entry
 import java.util.StringTokenizer
 import java.util.regex.Pattern
+import org.eclipse.jdt.annotation.NonNull;
+import org.gaul.modernizer_maven_annotations.SuppressModernizer
+import org.opendaylight.mdsal.binding.model.api.AnnotationType
 import org.opendaylight.mdsal.binding.model.api.ConcreteType
 import org.opendaylight.mdsal.binding.model.api.Constant
 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
 import org.opendaylight.mdsal.binding.model.api.GeneratedType
-import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
 import org.opendaylight.mdsal.binding.model.api.MethodSignature
 import org.opendaylight.mdsal.binding.model.api.Restrictions
 import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.api.TypeMember
-import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Single
-import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Multiple
-import org.opendaylight.mdsal.binding.model.util.Types
-import org.opendaylight.yangtools.yang.binding.BindingMapping
-import org.opendaylight.yangtools.yang.binding.CodeHelpers
-import org.opendaylight.yangtools.yang.common.QName
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
-import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition
-import org.opendaylight.yangtools.yang.model.api.SchemaNode
-import org.opendaylight.yangtools.yang.model.api.YangStmtMapping
-import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement
-import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement
-import org.opendaylight.yangtools.yang.model.export.DeclaredStatementFormatter
+import org.opendaylight.mdsal.binding.model.api.TypeMemberComment
+import org.opendaylight.mdsal.binding.model.ri.TypeConstants
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
+import org.opendaylight.yangtools.yang.binding.BaseIdentity
 
+@SuppressModernizer
 abstract class BaseTemplate extends JavaFileTemplate {
-    private static final char NEW_LINE = '\n'
-    private static val AMP_MATCHER = CharMatcher.is('&')
-    private static val NL_MATCHER = CharMatcher.is(NEW_LINE)
-    private static val TAB_MATCHER = CharMatcher.is('\t')
-    private static val SPACES_PATTERN = Pattern.compile(" +")
-    private static val NL_SPLITTER = Splitter.on(NL_MATCHER)
-    private static val TAIL_COMMENT_PATTERN = Pattern.compile("*/", Pattern.LITERAL);
-    private static val YANG_FORMATTER = DeclaredStatementFormatter.builder()
-        .addIgnoredStatement(YangStmtMapping.CONTACT)
-        .addIgnoredStatement(YangStmtMapping.DESCRIPTION)
-        .addIgnoredStatement(YangStmtMapping.REFERENCE)
-        .addIgnoredStatement(YangStmtMapping.ORGANIZATION)
-        .build();
+    static final char NEW_LINE = '\n'
+    static final char SPACE = ' '
+    static val WS_MATCHER = CharMatcher.anyOf("\n\t")
+    static val SPACES_PATTERN = Pattern.compile(" +")
+    static val NL_SPLITTER = Splitter.on(NEW_LINE)
 
     new(GeneratedType type) {
         super(type)
@@ -68,7 +51,7 @@ abstract class BaseTemplate extends JavaFileTemplate {
         super(javaType, type)
     }
 
-    final public def generate() {
+    final def generate() {
         val _body = body()
         '''
             package «type.packageName»;
@@ -81,22 +64,8 @@ abstract class BaseTemplate extends JavaFileTemplate {
     protected abstract def CharSequence body();
 
     // Helper patterns
-    final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
-
-    final protected def propertyNameFromGetter(MethodSignature getter) {
-        var int prefix;
-        if (getter.name.startsWith("is")) {
-            prefix = 2
-        } else if (getter.name.startsWith("get")) {
-            prefix = 3
-        } else {
-            throw new IllegalArgumentException("Not a getter")
-        }
-        return getter.name.substring(prefix).toFirstLower;
-    }
-
-    final protected def isAccessor(MethodSignature maybeGetter) {
-        return maybeGetter.name.startsWith("is") || maybeGetter.name.startsWith("get");
+    final protected def fieldName(GeneratedProperty property) {
+        "_" + property.name
     }
 
     /**
@@ -106,21 +75,20 @@ abstract class BaseTemplate extends JavaFileTemplate {
      * generated property with data about field which is generated as the getter method
      * @return string with the getter method source code in JAVA format
      */
-    protected def getterMethod(GeneratedProperty field) {
-        '''
-            public «field.returnType.importedName» «field.getterMethodName»() {
-                «IF field.returnType.importedName.contains("[]")»
-                return «field.fieldName» == null ? null : «field.fieldName».clone();
-                «ELSE»
-                return «field.fieldName»;
-                «ENDIF»
-            }
-        '''
-    }
+    protected def getterMethod(GeneratedProperty field) '''
+        «val methodName = field.getterMethodName»
+        public «field.returnType.importedName» «methodName»() {
+            «val fieldName = field.fieldName»
+            «IF field.returnType.name.endsWith("[]")»
+            return «fieldName» == null ? null : «fieldName».clone();
+            «ELSE»
+            return «fieldName»;
+            «ENDIF»
+        }
+    '''
 
     final protected def getterMethodName(GeneratedProperty field) {
-        val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
-        return '''«prefix»«field.name.toFirstUpper»'''
+        return '''«BindingMapping.GETTER_PREFIX»«field.name.toFirstUpper»'''
     }
 
     /**
@@ -148,6 +116,17 @@ abstract class BaseTemplate extends JavaFileTemplate {
     def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
         returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
 
+    /**
+     * Template method which generates method parameters with their types from <code>parameters</code>, annotating them
+     * with {@link NonNull}.
+     *
+     * @param parameters group of generated property instances which are transformed to the method parameters
+     * @return string with the list of the method parameters with their types in JAVA format
+     */
+    def final protected asNonNullArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»
+        «FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedNonNull» «parameter
+        .fieldName»«ENDFOR»«ENDIF»'''
+
     /**
      * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
      *
@@ -155,7 +134,7 @@ abstract class BaseTemplate extends JavaFileTemplate {
      * group of generated property instances which are transformed to the sequence of parameter names
      * @return string with the list of the parameter names of the <code>parameters</code>
      */
-    def final protected asArguments(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
+    def final protected asArguments(Collection<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
         fieldName»«ENDFOR»«ENDIF»'''
 
     /**
@@ -164,19 +143,20 @@ abstract class BaseTemplate extends JavaFileTemplate {
      * @param comment string with the comment for whole JAVA class
      * @return string with comment in JAVA format
      */
-    def protected CharSequence asJavadoc(String comment) {
-        if(comment === null) return ''
-        var txt = comment
+    def final protected asJavadoc(TypeMemberComment comment) {
+        if (comment === null) {
+            return ''
+        }
+        return wrapToDocumentation('''
+           «comment.contractDescription»
 
-        txt = comment.trim
-        txt = formatToParagraph(txt)
+           «comment.referenceDescription.formatReference»
 
-        return '''
-            «wrapToDocumentation(txt)»
-        '''
+           «comment.typeSignature»
+        ''')
     }
 
-    def String wrapToDocumentation(String text) {
+    def static String wrapToDocumentation(String text) {
         if (text.empty)
             return ""
 
@@ -184,8 +164,7 @@ abstract class BaseTemplate extends JavaFileTemplate {
         for (String t : NL_SPLITTER.split(text)) {
             sb.append(" *")
             if (!t.isEmpty()) {
-                sb.append(' ');
-                sb.append(t)
+                sb.append(SPACE).append(t)
             }
             sb.append(NEW_LINE)
         }
@@ -210,14 +189,6 @@ abstract class BaseTemplate extends JavaFileTemplate {
         '''.toString
     }
 
-    def static encodeJavadocSymbols(String description) {
-        if (description.nullOrEmpty) {
-            return description;
-        }
-
-        return TAIL_COMMENT_PATTERN.matcher(AMP_MATCHER.replaceFrom(description, "&amp;")).replaceAll("&#42;&#47;")
-    }
-
     def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
         val comment = type.comment
         if (comment === null) {
@@ -239,103 +210,21 @@ abstract class BaseTemplate extends JavaFileTemplate {
         '''
     }
 
-    def private static void appendSnippet(StringBuilder sb, GeneratedType type) {
-        val optDef = type.yangSourceDefinition
-        if (optDef.present) {
-            val def = optDef.get
-            sb.append(NEW_LINE)
-
-            if (def instanceof Single) {
-                val node = def.node
-                sb.append("<p>\n")
-                .append("This class represents the following YANG schema fragment defined in module <b>")
-                .append(def.module.argument).append("</b>\n")
-                .append("<pre>\n")
-                appendYangSnippet(sb, def.module, (node as EffectiveStatement<?, ?>).declared)
-                sb.append("</pre>")
-
-                if (node instanceof SchemaNode) {
-                    sb.append("The schema path to identify an instance is\n")
-                    .append("<i>")
-                    .append(formatSchemaPath(def.module.argument, node.path.pathFromRoot))
-                    .append("</i>\n")
-
-                    if (hasBuilderClass(node)) {
-                        val builderName = type.name + "Builder";
-
-                        sb.append("\n<p>To create instances of this class use {@link ").append(builderName)
-                        .append("}.\n")
-                        .append("@see ").append(builderName).append('\n')
-                        if (node instanceof ListSchemaNode) {
-                            val keyDef = node.keyDefinition
-                            if (keyDef !== null && !keyDef.empty) {
-                                sb.append("@see ").append(type.name).append("Key")
-                            }
-                            sb.append('\n');
-                        }
-                    }
-                }
-            } else if (def instanceof Multiple) {
-                sb.append("<pre>\n")
-                for (SchemaNode node : def.nodes) {
-                    appendYangSnippet(sb, def.module, (node as EffectiveStatement<?, ?>).declared)
-                }
-                sb.append("</pre>\n")
-            }
-        }
-    }
-
-    def private static void appendYangSnippet(StringBuilder sb, ModuleEffectiveStatement module,
-            DeclaredStatement<?> stmt) {
-        for (String str : YANG_FORMATTER.toYangTextSnippet(module, stmt)) {
-            sb.append(encodeAngleBrackets(encodeJavadocSymbols(str)))
-        }
-    }
-
-    def private static boolean hasBuilderClass(SchemaNode schemaNode) {
-        return schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode
-                || schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition;
-    }
-
-    def private static String formatSchemaPath(String moduleName, Iterable<QName> schemaPath) {
-        val sb = new StringBuilder().append(moduleName);
+    def static formatReference(String reference) '''
+        «IF reference !== null»
+            <pre>
+                <code>
+                    «reference.encodeAngleBrackets.formatToParagraph»
+                </code>
+            </pre>
 
-        var currentElement = Iterables.getFirst(schemaPath, null);
-        for (QName pathElement : schemaPath) {
-            sb.append('/')
-            if (!currentElement.namespace.equals(pathElement.namespace)) {
-                currentElement = pathElement
-                sb.append(pathElement)
-            } else {
-                sb.append(pathElement.getLocalName())
-            }
-        }
-        return sb.toString();
-    }
-
-    def protected String formatDataForJavaDoc(TypeMember type, String additionalComment) {
-        val StringBuilder typeDescriptionBuilder = new StringBuilder();
-        if (!type.comment.nullOrEmpty) {
-            typeDescriptionBuilder.append(formatToParagraph(type.comment))
-            typeDescriptionBuilder.append(NEW_LINE)
-            typeDescriptionBuilder.append(NEW_LINE)
-            typeDescriptionBuilder.append(NEW_LINE)
-        }
-        typeDescriptionBuilder.append(additionalComment)
-        var typeDescription = wrapToDocumentation(typeDescriptionBuilder.toString)
-        return '''
-            «typeDescription»
-        '''.toString
-    }
-
-    def asCode(String text) {
-        return "<code>" + text + "</code>"
-    }
+        «ENDIF»
+    '''
 
     def asLink(String text) {
         val StringBuilder sb = new StringBuilder()
         var tempText = text
-        var char lastChar = ' '
+        var char lastChar = SPACE
         var boolean badEnding = false
 
         if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
@@ -343,83 +232,51 @@ abstract class BaseTemplate extends JavaFileTemplate {
             lastChar = text.charAt(text.length - 1)
             badEnding = true
         }
-        sb.append("<a href = \"")
-        sb.append(tempText)
-        sb.append("\">")
-        sb.append(tempText)
-        sb.append("</a>")
+        sb.append("<a href = \"").append(tempText).append("\">").append(tempText).append("</a>")
 
-        if(badEnding)
+        if (badEnding)
             sb.append(lastChar)
 
         return sb.toString
     }
 
-    protected def formatToParagraph(String text) {
-        if(text === null || text.isEmpty)
-            return text
-
-        var formattedText = text
+    protected static def formatToParagraph(String inputText) {
         val StringBuilder sb = new StringBuilder();
         var StringBuilder lineBuilder = new StringBuilder();
         var boolean isFirstElementOnNewLineEmptyChar = false;
 
-        formattedText = encodeJavadocSymbols(formattedText)
-        formattedText = NL_MATCHER.removeFrom(formattedText)
-        formattedText = TAB_MATCHER.removeFrom(formattedText)
+        var formattedText = WS_MATCHER.replaceFrom(inputText.encodeJavadocSymbols, SPACE)
         formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
 
-        val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
-
-        while (tokenizer.hasMoreElements) {
-            val nextElement = tokenizer.nextElement.toString
+        val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true)
+        while (tokenizer.hasMoreTokens) {
+            val nextElement = tokenizer.nextToken
 
             if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
-                if (lineBuilder.charAt(lineBuilder.length - 1) == ' ') {
-                    lineBuilder.setLength(0)
-                    lineBuilder.append(lineBuilder.substring(0, lineBuilder.length - 1))
+                if (lineBuilder.charAt(lineBuilder.length - 1) == SPACE) {
+                    lineBuilder.setLength(lineBuilder.length - 1)
                 }
-                if (lineBuilder.charAt(0) == ' ') {
-                    lineBuilder.setLength(0)
-                    lineBuilder.append(lineBuilder.substring(1))
+                if (lineBuilder.length != 0 && lineBuilder.charAt(0) == SPACE) {
+                    lineBuilder.deleteCharAt(0)
                 }
 
-                sb.append(lineBuilder);
+                sb.append(lineBuilder).append(NEW_LINE)
                 lineBuilder.setLength(0)
-                sb.append(NEW_LINE)
 
-                if(nextElement.toString == ' ') {
+                if (nextElement == " ") {
                     isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
                 }
             }
-
             if (isFirstElementOnNewLineEmptyChar) {
                 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
-            }
-
-            else {
+            } else {
                 lineBuilder.append(nextElement)
             }
         }
-        sb.append(lineBuilder)
-        sb.append(NEW_LINE)
 
-        return sb.toString
+        return sb.append(lineBuilder).append(NEW_LINE).toString
     }
 
-    def protected generateToString(Collection<GeneratedProperty> properties) '''
-        «IF !properties.empty»
-            @«Override.importedName»
-            public «String.importedName» toString() {
-                final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper(«type.importedName».class);
-                «FOR property : properties»
-                    «CodeHelpers.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
-                «ENDFOR»
-                return helper.toString();
-            }
-        «ENDIF»
-    '''
-
     /**
      * Template method which generates method parameters with their types from <code>parameters</code>.
      *
@@ -438,19 +295,95 @@ abstract class BaseTemplate extends JavaFileTemplate {
     def protected emitConstant(Constant c) '''
         «IF BindingMapping.QNAME_STATIC_FIELD_NAME.equals(c.name)»
             «val entry = c.value as Entry<JavaTypeName, String>»
-            public static final «c.type.importedName» «c.name» = «entry.key.importedName».«BindingMapping.MODULE_INFO_QNAMEOF_METHOD_NAME»("«entry.value»");
+            /**
+             * YANG identifier of the statement represented by this class.
+             */
+            public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«BindingMapping.MODULE_INFO_QNAMEOF_METHOD_NAME»("«entry.value»");
+        «ELSEIF BindingMapping.VALUE_STATIC_FIELD_NAME.equals(c.name) && BaseIdentity.equals(c.value)»
+            «val typeName = c.type.importedName»
+            «val override = OVERRIDE.importedName»
+            /**
+             * Singleton value representing the {@link «typeName»} identity.
+             */
+            public static final «c.type.importedNonNull» «c.name» = new «typeName»() {
+                @«override»
+                public «CLASS.importedName»<«typeName»> «BindingMapping.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»() {
+                    return «typeName».class;
+                }
+
+                @«override»
+                public int hashCode() {
+                    return «typeName».class.hashCode();
+                }
+
+                @«override»
+                public boolean equals(final «objectType.importedName» obj) {
+                    return obj == this || obj instanceof «typeName»
+                        && «typeName».class.equals(((«typeName») obj).«BindingMapping.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»());
+                }
+
+                @«override»
+                public «STRING.importedName» toString() {
+                    return «MOREOBJECTS.importedName».toStringHelper("«c.type.name»").add("qname", QNAME).toString();
+                }
+            };
         «ELSE»
             public static final «c.type.importedName» «c.name» = «c.value»;
         «ENDIF»
     '''
 
-    def static Restrictions getRestrictions(Type type) {
-        if (type instanceof ConcreteType) {
-            return type.restrictions
-        }
-        if (type instanceof GeneratedTransferObject) {
-            return type.restrictions
-        }
-        return null
-    }
+    def protected generateCheckers(GeneratedProperty field, Restrictions restrictions, Type actualType) '''
+       «IF restrictions.rangeConstraint.present»
+           «AbstractRangeGenerator.forType(actualType).generateRangeChecker(field.name.toFirstUpper,
+               restrictions.rangeConstraint.get, this)»
+       «ENDIF»
+       «IF restrictions.lengthConstraint.present»
+           «LengthGenerator.generateLengthChecker(field.fieldName, actualType, restrictions.lengthConstraint.get, this)»
+       «ENDIF»
+    '''
+
+    def protected checkArgument(GeneratedProperty property, Restrictions restrictions, Type actualType, String value) '''
+       «IF restrictions.getRangeConstraint.isPresent»
+           «IF actualType instanceof ConcreteType»
+               «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value)»
+           «ELSE»
+               «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value + ".getValue()")»
+           «ENDIF»
+       «ENDIF»
+       «val fieldName = property.fieldName»
+       «IF restrictions.getLengthConstraint.isPresent»
+           «IF actualType instanceof ConcreteType»
+               «LengthGenerator.generateLengthCheckerCall(fieldName, value)»
+           «ELSE»
+               «LengthGenerator.generateLengthCheckerCall(fieldName, value + ".getValue()")»
+           «ENDIF»
+       «ENDIF»
+
+       «val fieldUpperCase = fieldName.toUpperCase(Locale.ENGLISH)»
+       «FOR currentConstant : type.getConstantDefinitions»
+           «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
+               && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
+           «CODEHELPERS.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«fieldName», «Constants.MEMBER_REGEX_LIST»«fieldName»);
+           «ENDIF»
+       «ENDFOR»
+    '''
+
+    def protected hashCodeResult(Collection<? extends GeneratedProperty> properties) '''
+        final int prime = 31;
+        int result = 1;
+        «FOR property : properties»
+            result = prime * result + «property.importedUtilClass».hashCode(«property.fieldName»);
+        «ENDFOR»
+    '''
+
+    def protected final generateAnnotation(AnnotationType annotation) '''
+        @«annotation.importedName»
+        «IF annotation.parameters !== null && !annotation.parameters.empty»
+        (
+        «FOR param : annotation.parameters SEPARATOR ","»
+            «param.name»=«param.value»
+        «ENDFOR»
+        )
+        «ENDIF»
+    '''
 }