Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.maven; |
| 17 | |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 19 | import com.thoughtworks.qdox.JavaProjectBuilder; |
| 20 | import com.thoughtworks.qdox.model.JavaAnnotation; |
| 21 | import com.thoughtworks.qdox.model.JavaClass; |
| 22 | import com.thoughtworks.qdox.model.JavaField; |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 23 | import com.thoughtworks.qdox.model.expression.Add; |
| 24 | import com.thoughtworks.qdox.model.expression.AnnotationValue; |
| 25 | import com.thoughtworks.qdox.model.expression.AnnotationValueList; |
| 26 | import com.thoughtworks.qdox.model.expression.FieldRef; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 27 | import org.apache.maven.plugin.AbstractMojo; |
| 28 | import org.apache.maven.plugin.MojoExecutionException; |
| 29 | import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 30 | import org.apache.maven.plugins.annotations.Mojo; |
| 31 | import org.apache.maven.plugins.annotations.Parameter; |
| 32 | |
| 33 | import java.io.File; |
| 34 | import java.io.FileWriter; |
| 35 | import java.io.IOException; |
| 36 | import java.io.PrintWriter; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 39 | import java.util.Map; |
| 40 | import java.util.Optional; |
| 41 | import java.util.jar.JarEntry; |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Produces ONOS component configuration catalogue resources. |
| 45 | */ |
| 46 | @Mojo(name = "cfg", defaultPhase = LifecyclePhase.GENERATE_RESOURCES) |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 47 | @java.lang.SuppressWarnings("squid:S1148") |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 48 | public class OnosCfgMojo extends AbstractMojo { |
| 49 | |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 50 | /** |
| 51 | * The directory where the generated catalogue file will be put. |
| 52 | */ |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 53 | @Parameter(defaultValue = "${basedir}") |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 54 | protected File srcDirectory; |
| 55 | |
| 56 | /** |
| 57 | * The directory where the generated catalogue file will be put. |
| 58 | */ |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 59 | @Parameter(defaultValue = "${project.build.outputDirectory}") |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 60 | protected File dstDirectory; |
| 61 | |
| 62 | @Override |
| 63 | public void execute() throws MojoExecutionException { |
| 64 | getLog().info("Generating ONOS component configuration catalogues..."); |
| 65 | try { |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 66 | CfgDefGenerator gen = new CfgDefGenerator(dstDirectory, srcDirectory); |
| 67 | gen.analyze(); |
| 68 | gen.generate(); |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 69 | } catch (Exception e) { |
| 70 | e.printStackTrace(); |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 71 | throw new MojoExecutionException("Unable to generate property catalog", e); |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 75 | |
Carmelo Cascone | afdddc1 | 2019-07-15 18:51:32 -0700 | [diff] [blame] | 76 | private class CfgDefGenerator { |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 77 | |
Daniele Moro | 462547a | 2019-11-22 18:00:29 -0800 | [diff] [blame] | 78 | private static final String COMPONENT = "Component"; |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 79 | private static final String PROPERTY = "property"; |
| 80 | private static final String SEP = "|"; |
| 81 | private static final String UTF_8 = "UTF-8"; |
| 82 | private static final String JAVA = ".java"; |
| 83 | private static final String EXT = ".cfgdef"; |
| 84 | private static final String STRING = "STRING"; |
| 85 | private static final String NO_DESCRIPTION = "no description provided"; |
| 86 | |
| 87 | private final File dstDir; |
| 88 | private final JavaProjectBuilder builder; |
| 89 | |
| 90 | private final Map<String, String> constants = Maps.newHashMap(); |
| 91 | |
| 92 | |
| 93 | private CfgDefGenerator(File dstDir, File srcDir) { |
| 94 | this.dstDir = dstDir; |
| 95 | this.builder = new JavaProjectBuilder(); |
| 96 | builder.addSourceTree(new File(srcDir, "src/main/java")); |
| 97 | |
| 98 | // Arrays.stream(sourceFilePaths).forEach(filename -> { |
| 99 | // try { |
| 100 | // if (filename.endsWith(JAVA)) |
| 101 | // builder.addSource(new File(filename)); |
| 102 | // } catch (ParseException e) { |
| 103 | // // When unable to parse, skip the source; leave it to javac to fail. |
| 104 | // } catch (IOException e) { |
| 105 | // throw new IllegalArgumentException("Unable to open file", e); |
| 106 | // } |
| 107 | // }); |
| 108 | } |
| 109 | |
| 110 | public void analyze() { |
| 111 | builder.getClasses().forEach(this::collectConstants); |
| 112 | } |
| 113 | |
| 114 | private void collectConstants(JavaClass javaClass) { |
| 115 | javaClass.getFields().stream() |
| 116 | .filter(f -> f.isStatic() && f.isFinal() && !f.isPrivate()) |
| 117 | .forEach(f -> constants.put(f.getName(), f.getInitializationExpression())); |
| 118 | } |
| 119 | |
| 120 | public void generate() throws IOException { |
| 121 | for (JavaClass javaClass : builder.getClasses()) { |
| 122 | processClass(javaClass); |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 125 | |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 126 | private void processClass(JavaClass javaClass) throws IOException { |
| 127 | Optional<JavaAnnotation> annotation = javaClass.getAnnotations().stream() |
Daniele Moro | 462547a | 2019-11-22 18:00:29 -0800 | [diff] [blame] | 128 | .filter(ja -> ja.getType().getName().endsWith(COMPONENT)) |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 129 | .findFirst(); |
| 130 | if (annotation.isPresent()) { |
| 131 | AnnotationValue property = annotation.get().getProperty(PROPERTY); |
| 132 | List<String> lines = new ArrayList<>(); |
| 133 | if (property instanceof AnnotationValueList) { |
| 134 | AnnotationValueList list = (AnnotationValueList) property; |
| 135 | list.getValueList().forEach(v -> processProperty(lines, javaClass, v)); |
| 136 | } else { |
| 137 | processProperty(lines, javaClass, property); |
| 138 | } |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 139 | |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 140 | if (!lines.isEmpty()) { |
| 141 | writeCatalog(javaClass, lines); |
| 142 | } |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 143 | } |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private void processProperty(List<String> lines, JavaClass javaClass, |
| 147 | AnnotationValue value) { |
| 148 | String s = elaborate(value); |
| 149 | String[] pex = s.split("=", 2); |
| 150 | |
| 151 | if (pex.length == 2) { |
| 152 | String[] rex = pex[0].split(":", 2); |
| 153 | String name = rex[0]; |
| 154 | String type = rex.length == 2 ? rex[1].toUpperCase() : STRING; |
| 155 | String def = pex[1]; |
| 156 | String desc = description(javaClass, name); |
| 157 | |
| 158 | if (desc != null) { |
Daniele Moro | e917a76 | 2019-12-03 11:50:31 -0800 | [diff] [blame] | 159 | String cleanedDesc = desc.trim().replace("\n", " ").replace(" ", " "); |
| 160 | String line = name + SEP + type + SEP + def + SEP + cleanedDesc; |
Carmelo Cascone | afdddc1 | 2019-07-15 18:51:32 -0700 | [diff] [blame] | 161 | getLog().info("Processing property " + line + " ..."); |
| 162 | lines.add(line + "\n"); |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Retrieve description from a comment preceding the field named the same |
| 168 | // as the property or |
| 169 | // TODO: from an annotated comment. |
| 170 | private String description(JavaClass javaClass, String name) { |
| 171 | if (name.startsWith("_")) { |
| 172 | // Static property - just leave it as is, not for inclusion in the cfg defs |
| 173 | return null; |
| 174 | } |
| 175 | JavaField field = javaClass.getFieldByName(name); |
| 176 | if (field != null) { |
| 177 | String comment = field.getComment(); |
| 178 | return comment != null ? comment : NO_DESCRIPTION; |
| 179 | } |
| 180 | throw new IllegalStateException("cfgdef could not find a variable named " + name + " in " + javaClass.getName()); |
| 181 | } |
| 182 | |
| 183 | private String elaborate(AnnotationValue value) { |
| 184 | if (value instanceof Add) { |
| 185 | return elaborate(((Add) value).getLeft()) + elaborate(((Add) value).getRight()); |
| 186 | } else if (value instanceof FieldRef) { |
| 187 | return elaborate((FieldRef) value); |
| 188 | } else if (value != null) { |
| 189 | return stripped(value.toString()); |
| 190 | } else { |
| 191 | return ""; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private String elaborate(FieldRef field) { |
| 196 | String name = field.getName(); |
| 197 | String value = constants.get(name); |
| 198 | if (value != null) { |
| 199 | return stripped(value); |
| 200 | } |
| 201 | throw new IllegalStateException("Constant " + name + " cannot be elaborated;" + |
| 202 | " value not in the same compilation context"); |
| 203 | } |
| 204 | |
| 205 | private String stripped(String s) { |
| 206 | return s.trim().replaceFirst("^[^\"]*\"", "").replaceFirst("\"$", ""); |
| 207 | } |
| 208 | |
| 209 | private void writeCatalog(JavaClass javaClass, List<String> lines) { |
| 210 | File dir = new File(dstDir, javaClass.getPackageName().replace('.', '/')); |
| 211 | dir.mkdirs(); |
| 212 | |
| 213 | File cfgDef = new File(dir, javaClass.getName().replace('.', '/') + ".cfgdef"); |
| 214 | try (FileWriter fw = new FileWriter(cfgDef); |
| 215 | PrintWriter pw = new PrintWriter(fw)) { |
| 216 | pw.println("# This file is auto-generated by onos-maven-plugin"); |
| 217 | lines.forEach(pw::println); |
| 218 | } catch (IOException e) { |
| 219 | System.err.println("Unable to write catalog for " + javaClass.getName()); |
| 220 | e.printStackTrace(); |
| 221 | } |
| 222 | } |
| 223 | |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 224 | } |
Thomas Vachuska | d6f88c6 | 2019-03-30 10:37:36 -0700 | [diff] [blame] | 225 | } |