blob: 57a0d98c9df91d07967147624c7cac72bd986958 [file] [log] [blame]
Thomas Vachuska8c8b0372015-03-10 11:11:24 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska8c8b0372015-03-10 11:11:24 -07003 *
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 */
16package org.onosproject.maven;
17
18import com.thoughtworks.qdox.JavaProjectBuilder;
19import com.thoughtworks.qdox.model.JavaAnnotation;
20import com.thoughtworks.qdox.model.JavaClass;
21import com.thoughtworks.qdox.model.JavaField;
22import org.apache.maven.plugin.AbstractMojo;
23import org.apache.maven.plugin.MojoExecutionException;
24import org.apache.maven.plugins.annotations.LifecyclePhase;
25import org.apache.maven.plugins.annotations.Mojo;
26import org.apache.maven.plugins.annotations.Parameter;
27
28import java.io.File;
29import java.io.FileWriter;
30import java.io.IOException;
31import java.io.PrintWriter;
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * Produces ONOS component configuration catalogue resources.
37 */
38@Mojo(name = "cfg", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
Ray Milkey4fd3ceb2015-12-10 14:43:08 -080039@java.lang.SuppressWarnings("squid:S1148")
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070040public class OnosCfgMojo extends AbstractMojo {
41
42 private static final String COMPONENT = "org.apache.felix.scr.annotations.Component";
43 private static final String PROPERTY = "org.apache.felix.scr.annotations.Property";
44 private static final String SEP = "|";
45
46 /**
47 * The directory where the generated catalogue file will be put.
48 */
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070049 @Parameter(defaultValue = "${basedir}")
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070050 protected File srcDirectory;
51
52 /**
53 * The directory where the generated catalogue file will be put.
54 */
Thomas Vachuskaa98ae7f2015-04-14 14:00:36 -070055 @Parameter(defaultValue = "${project.build.outputDirectory}")
Thomas Vachuska8c8b0372015-03-10 11:11:24 -070056 protected File dstDirectory;
57
58 @Override
59 public void execute() throws MojoExecutionException {
60 getLog().info("Generating ONOS component configuration catalogues...");
61 try {
62 JavaProjectBuilder builder = new JavaProjectBuilder();
63 builder.addSourceTree(new File(srcDirectory, "src/main/java"));
64 builder.getClasses().forEach(this::processClass);
65 } catch (Exception e) {
66 e.printStackTrace();
67 throw e;
68 }
69 }
70
71 private void processClass(JavaClass javaClass) {
72 boolean isComponent = javaClass.getAnnotations().stream()
73 .map(ja -> ja.getType().getName().equals(COMPONENT))
74 .findFirst().isPresent();
75 if (isComponent) {
76 List<String> lines = new ArrayList<>();
77 javaClass.getFields().forEach(field -> processField(lines, javaClass, field));
78 if (!lines.isEmpty()) {
79 writeCatalog(javaClass, lines);
80 }
81 }
82 }
83
84 private void writeCatalog(JavaClass javaClass, List<String> lines) {
85 File dir = new File(dstDirectory, javaClass.getPackageName().replace('.', '/'));
86 dir.mkdirs();
87
88 File cfgDef = new File(dir, javaClass.getName().replace('.', '/') + ".cfgdef");
89 try (FileWriter fw = new FileWriter(cfgDef);
90 PrintWriter pw = new PrintWriter(fw)) {
91 pw.println("# This file is auto-generated by onos-maven-plugin");
92 lines.forEach(pw::println);
93 } catch (IOException e) {
94 System.err.println("Unable to write catalog for " + javaClass.getName());
95 e.printStackTrace();
96 }
97 }
98
99 private void processField(List<String> lines, JavaClass javaClass, JavaField field) {
100 field.getAnnotations().forEach(ja -> {
101 if (ja.getType().getName().equals(PROPERTY)) {
102 lines.add(expand(javaClass, ja.getNamedParameter("name").toString()) +
103 SEP + type(field) +
104 SEP + defaultValue(javaClass, field, ja) +
105 SEP + description(ja));
106 }
107 });
108 }
109
110 // TODO: Stuff below is very much hack-ish and should be redone; it works for now though.
111
112 private String description(JavaAnnotation annotation) {
113 String description = (String) annotation.getNamedParameter("label");
114 return description.replaceAll("\" \\+ \"", "")
115 .replaceFirst("^[^\"]*\"", "").replaceFirst("\"$", "");
116 }
117
118 private String type(JavaField field) {
119 String ft = field.getType().getName().toUpperCase();
120 return ft.equals("INT") ? "INTEGER" : ft;
121 }
122
123 private String defaultValue(JavaClass javaClass, JavaField field,
124 JavaAnnotation annotation) {
125 String ft = field.getType().getName().toLowerCase();
126 String defValueName = ft.equals("boolean") ? "boolValue" :
127 ft.equals("string") ? "value" : ft + "Value";
128 Object dv = annotation.getNamedParameter(defValueName);
129 return dv == null ? "" : expand(javaClass, dv.toString());
130 }
131
132 private String stripQuotes(String string) {
133 return string.trim().replaceFirst("^[^\"]*\"", "").replaceFirst("\"$", "");
134 }
135
136 private String expand(JavaClass javaClass, String value) {
137 JavaField field = javaClass.getFieldByName(value);
138 return field == null ? stripQuotes(value) :
139 stripQuotes(field.getCodeBlock().replaceFirst(".*=", "").replaceFirst(";$", ""));
140 }
141
142}