Bug 1411-5 #5: MDSAL Binding2 Java API Generator 92/46892/18
authorMartin Ciglan <mciglan@cisco.com>
Tue, 22 Nov 2016 10:14:53 +0000 (11:14 +0100)
committerRobert Varga <nite@hq.sk>
Tue, 13 Dec 2016 14:58:27 +0000 (14:58 +0000)
- generator to java file
- enum generator, renderer and template
- bit of cleanup

Change-Id: I79f96631e765c2d7a9b4d96c7e19c1a288bb7139
Signed-off-by: Filip Gregor <fgregor@cisco.com>
Signed-off-by: Martin Ciglan <mciglan@cisco.com>
binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/EnumGenerator.java [new file with mode: 0644]
binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/GeneratorJavaFile.java [new file with mode: 0644]
binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/renderers/EnumRenderer.java [new file with mode: 0644]
binding2/mdsal-binding2-java-api-generator/src/main/twirl/org/opendaylight/mdsal/binding2/enumTemplate.scala.txt [new file with mode: 0644]

diff --git a/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/EnumGenerator.java b/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/EnumGenerator.java
new file mode 100644 (file)
index 0000000..f9b2e71
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2016 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.mdsal.binding2.java.api.generator;
+
+import com.google.common.annotations.Beta;
+import org.opendaylight.mdsal.binding2.java.api.generator.renderers.EnumRenderer;
+import org.opendaylight.mdsal.binding2.model.api.CodeGenerator;
+import org.opendaylight.mdsal.binding2.model.api.Enumeration;
+import org.opendaylight.mdsal.binding2.model.api.Type;
+import org.opendaylight.mdsal.binding2.model.api.UnitName;
+import org.opendaylight.yangtools.concepts.Identifier;
+
+/**
+ * Transformer of the data from the virtual form to JAVA source code. The
+ * result source code represents JAVA enumeration. For generation of the source
+ * code is used the template written in Twirl (Scala based) language.
+ */
+@Beta
+public class EnumGenerator implements CodeGenerator {
+
+    @Override
+    public String generate(Type type) {
+        if (type instanceof Enumeration) {
+            final Enumeration enums = (Enumeration) type;
+            return new EnumRenderer(enums).generateTemplate();
+        }
+        return "";
+    }
+
+    @Override
+    public boolean isAcceptable(Type type) {
+        return type instanceof Enumeration;
+    }
+
+    @Override
+    public Identifier getUnitName(Type type) {
+        return new UnitName(type.getName());
+    }
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/GeneratorJavaFile.java b/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/GeneratorJavaFile.java
new file mode 100644 (file)
index 0000000..8945545
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2016 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.mdsal.binding2.java.api.generator;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.opendaylight.mdsal.binding2.model.api.CodeGenerator;
+import org.opendaylight.mdsal.binding2.model.api.Type;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonatype.plexus.build.incremental.BuildContext;
+
+/**
+ * Generates files with JAVA source code for every specified type.
+ */
+@Beta
+public final class GeneratorJavaFile {
+
+    private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
+
+    /**
+     * List of <code>CodeGenerator</code> instances.
+     */
+    private final List<CodeGenerator> generators = new ArrayList<>();
+
+    /**
+     * Set of <code>Type</code> instances for which the JAVA code is generated.
+     */
+    private final Collection<? extends Type> types;
+
+    /**
+     * BuildContext used for instantiating files
+     */
+    private final BuildContext buildContext;
+
+    /**
+     * Creates instance of this class with the set of <code>types</code> for
+     * which the JAVA code is generated.
+     *
+     * The instances of concrete JAVA code generator are created.
+     *
+     * @param buildContext
+     *            build context to use for accessing files
+     * @param types
+     *            set of types for which JAVA code should be generated
+     */
+    public GeneratorJavaFile(final BuildContext buildContext, final Collection<? extends Type> types) {
+        this.buildContext = Preconditions.checkNotNull(buildContext);
+        this.types = Preconditions.checkNotNull(types);
+        generators.add(new EnumGenerator());
+        //TODO: finish generators
+//        generators.add(new InterfaceGenerator());
+//        generators.add(new TOGenerator());
+//        generators.add(new BuilderGenerator());
+    }
+
+}
diff --git a/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/renderers/EnumRenderer.java b/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding2/java/api/generator/renderers/EnumRenderer.java
new file mode 100644 (file)
index 0000000..633a2dd
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 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.mdsal.binding2.java.api.generator.renderers;
+
+import static org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.asJavadoc;
+import static org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.encodeAngleBrackets;
+
+import java.util.LinkedList;
+import java.util.List;
+import org.opendaylight.mdsal.binding2.model.api.Enumeration;
+import org.opendaylight.mdsal.binding2.txt.enumTemplate;
+
+public class EnumRenderer extends BaseRenderer {
+    private final Enumeration enums;
+
+    public EnumRenderer(final Enumeration type) {
+        super(type);
+        enums = type;
+    }
+
+    @Override
+    protected String body() {
+        String importedName = importedName(String.class);
+        return enumTemplate.render(enums, importedName).body();
+    }
+
+    /**
+     * @param enumeration
+     * @return List of enumeration pairs with javadoc
+     */
+    public static String writeEnumeration(final Enumeration enumeration) {
+        final List<CharSequence> strings = new LinkedList<>();
+        if (!enumeration.getValues().isEmpty()) {
+            for (Enumeration.Pair pair : enumeration.getValues()) {
+                final StringBuilder sb = new StringBuilder();
+                sb.append(asJavadoc(encodeAngleBrackets(pair.getDescription())));
+                sb.append("\n");
+                sb.append(pair.getMappedName());
+                sb.append('(');
+                sb.append(pair.getValue());
+                sb.append(", \"");
+                sb.append(pair.getName());
+                sb.append("\")");
+                strings.add(sb);
+            }
+        }
+        return String.join(",\n", strings).concat(";");
+    }
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-java-api-generator/src/main/twirl/org/opendaylight/mdsal/binding2/enumTemplate.scala.txt b/binding2/mdsal-binding2-java-api-generator/src/main/twirl/org/opendaylight/mdsal/binding2/enumTemplate.scala.txt
new file mode 100644 (file)
index 0000000..7966a32
--- /dev/null
@@ -0,0 +1,64 @@
+@*
+ * Copyright (c) 2016 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
+ *@
+
+@import org.opendaylight.mdsal.binding2.java.api.generator.renderers.EnumRenderer.writeEnumeration
+@import org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.formatDataForJavaDoc
+@import org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.wrapToDocumentation
+@import org.opendaylight.mdsal.binding2.model.api.Enumeration
+@import org.opendaylight.mdsal.binding2.model.api.GeneratedType
+
+@(genType: GeneratedType, importedName: String)
+@if(genType != null) {
+@{wrapToDocumentation(formatDataForJavaDoc(genType))}
+public enum @{genType.getName} {
+@{writeEnumeration(genType.asInstanceOf[Enumeration])}
+
+    @{importedName} name;
+    int value;
+    private static final java.util.Map<java.lang.Integer, @{genType.getName}> VALUE_MAP;
+
+    static {
+        final com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, @{genType.getName}> b = com.google.common.collect.ImmutableMap.builder();
+        for (@{genType.getName} enumItem : @{genType.getName}.values())
+        {
+            b.put(enumItem.value, enumItem);
+        }
+
+        VALUE_MAP = b.build();
+    }
+
+    private @{genType.getName}(int value, @{importedName} name) {
+        this.value = value;
+        this.name = name;
+    }
+
+    /**
+     * Returns the name of the enumeration item as it is specified in the input yang.
+     *
+     * @@return the name of the enumeration item as it is specified in the input yang
+     */
+    public @{importedName} getName() {
+        return name;
+    }
+
+    /**
+     * @@return integer value
+     */
+    public int getIntValue() {
+        return value;
+    }
+
+    /**
+     * @@param valueArg
+     * @@return corresponding @{genType.getName} item
+     */
+    public static @{genType.getName} forValue(int valueArg) {
+        return VALUE_MAP.get(valueArg);
+    }
+}
+}
\ No newline at end of file