summaryrefslogtreecommitdiffstats
path: root/mod/genprocessor/src/main/java/org/onap/dcae/genprocessor/App.java
blob: 9c509912a3f0d4c3818004f06af3036ddf118223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dcae.genprocessor;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;

import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.jar.Manifest;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.jar.Attributes;

import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.processor.Processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Hello world!
 *
 */
public class App {
    static final Logger LOG = LoggerFactory.getLogger(App.class);

    // NOTE: For each new processor, need to: change jar command, change meta-inf
    private static String createClassName(CompSpec compSpec) {
        return String.format("org.onap.dcae.%s", compSpec.nameJavaClass);
    }

    /**
     * Does a series of DCAEProcessor specific verification checks on the generated
     * class.
     * 
     * @param cc
     * @return true if verification is successful
     */
    private static boolean verifyGen(CtClass cc) {
        DCAEProcessor processor;
        try {
            processor = (DCAEProcessor) cc.toClass().newInstance();
        } catch (InstantiationException | IllegalAccessException | CannotCompileException e) {
            LOG.error(e.toString(), e);
            return false;
        }
        java.lang.annotation.Annotation[] anns = processor.getClass().getAnnotations();
        LOG.info(String.format("#Annotations on class: %d", anns.length));

        for (java.lang.annotation.Annotation ann : anns) {
            if (ann.annotationType().getName().contains("Description")) {
                LOG.info(String.format("CapabilityDescription: %s", ((CapabilityDescription) ann).value()));
            } else if (ann.annotationType().getName().contains("Tags")) {
                LOG.info(String.format("Tags: %s", String.join(", ", ((Tags) ann).value())));
            }
        }

        LOG.info(String.format("Processor getters:\nname=%s\nversion=%s\nid=%s\nselfUrl=%s", processor.getName(),
                processor.getVersion(), processor.getComponentId(), processor.getComponentUrl()));

        LOG.info(String.format("#Property descriptors: %d", processor.getPropertyDescriptors().size()));

        if (processor.getPropertyDescriptors().size() > 0) {
            LOG.info(processor.getPropertyDescriptors().get(0).toString());
        }

        LOG.info(String.format("#Relationships: %d", processor.getRelationships().size()));

        // Actually do checks
        return true;
    }

    /**
     * Generates a new DCAEProcessor class given a Component object and writes the
     * class file to a specified directory.
     * 
     * @param directoryName where generated class files will get written
     * @param comp          Component object to generate new DCAEProcessor classes
     */
    public static void generateClassFile(String directoryName, Comp comp) {
        LOG.info("Generating classes");

        try {
            ClassPool pool = ClassPool.getDefault();
            CtClass base = pool.get(DCAEProcessor.class.getName());

            CtClass cc = pool.makeClass(createClassName(comp.compSpec));
            cc.setSuperclass(base);

            String[] tags = ProcessorBuilder.createTags(comp.compSpec);

            ProcessorBuilder.addAnnotationsProcessor(cc, comp.compSpec.description, tags);
            ProcessorBuilder.setComponentPropertyGetters(cc, comp);
            ProcessorBuilder.setProcessorPropertyDescriptors(cc, comp.compSpec);
            ProcessorBuilder.setProcessorRelationships(cc, comp.compSpec);

            if (verifyGen(cc)) {
                cc.writeFile(directoryName);
            }
        } catch (Exception e) {
            LOG.error("Uhoh", e);
        }
    }

    private static List<String> generateManifestMF(CompSpec compSpec) {
        return Arrays.asList("Manifest-Version: 1.0", String.format("Id: %s", compSpec.name),
                String.format("Version: %s", compSpec.version)
                // TODO: Group is hardcoded here. Should it be?
                , "Group: org.onap.dcae");
    }

    private static void writeManifestThing(File dirTarget, List<String> lines, String subDir, String fileName) {
        File dirManifest = new File(dirTarget, subDir);

        if (dirManifest.exists() || dirManifest.mkdirs()) {
            Path path = Paths.get(dirManifest.getAbsolutePath(), fileName);
            try {
                Files.write(path, lines, StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new RuntimeException("Could not create Manifest directory");
        }
    }

    private static boolean copyProcessorClassFile(String classResourceName, File dirBuild) {
        File dirSandbox = new File(dirBuild, "org/onap/dcae/genprocessor");

        if (dirSandbox.exists() || dirSandbox.mkdir()) {
            try (InputStream asStream = App.class.getResourceAsStream(classResourceName)) {
                File dest = new File(dirSandbox, classResourceName);
                Files.copy(asStream, dest.toPath());
                return true;
            } catch (FileAlreadyExistsException e) {
                // Do nothing, class file already exists
            } catch (IOException e) {
                throw new RuntimeException(e);
            } 
        }

        return false;
    }

    private static File getDirectoryForJars(File dirWorking) {
        return new File(dirWorking, "nifi-jars");
    }

    private static boolean packageJar(File dirWorking, File dirBuild, String jarName) {
        LOG.info("Package into jar");

        try {
            File dirJars = getDirectoryForJars(dirWorking);

            if (dirJars.exists() || dirJars.mkdir()) {
                String argDashC = String.format("-C %s", dirBuild.getAbsolutePath());
                String cmd = String.join(" ", new String[] {
                    "jar cvfm"
                    , String.format("%s/%s.jar", dirJars.getAbsolutePath(), jarName)
                    , String.format("%s/META-INF/MANIFEST.MF", dirBuild.getAbsolutePath())
                    , argDashC, "org"
                    , argDashC, "org/onap/dcae/genprocessor"
                    , argDashC, "META-INF"
                });
                LOG.debug(String.format("Jar command: %s", cmd));

                if (Runtime.getRuntime().exec(cmd).waitFor() == 0) {
                    return true;
                }
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Error while creating jar", e);
        } catch (IOException e) {
            throw new RuntimeException("Error while creating jar", e);
        }

        return false;
    }

    private static boolean doesJarExist(File dirWorking, String jarName) {
        File dirJars = getDirectoryForJars(dirWorking);
        String[] jars = dirJars.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.contains(jarName);
            }
        });
        return jars.length > 0;
    }

    /**
     * Looks for the MANIFEST.MF and extracts-to-print expected values (group, id,
     * version)
     * 
     * @param classLoader
     */
    private static void checkManifest(ClassLoader classLoader) {
        try {
            URL url = ((URLClassLoader) classLoader).findResource("META-INF/MANIFEST.MF");
            Manifest manifest = new Manifest(url.openStream());

            final Attributes attributes = manifest.getMainAttributes();
            final String group = attributes.getValue("Group");
            final String id = attributes.getValue("Id");
            final String version = attributes.getValue("Version");
            LOG.info(String.format("group=%s, id=%s, version=%s", group, id, version));
        } catch (IOException e) {
            throw new RuntimeException("Error while reading manifest", e);
        }
    }

    /**
     * Given a URL to a index.json file, fetches the file and generates a list of
     * URLs for DCAE jars that has Processors packaged.
     * 
     * @param indexDCAEJars
     * @return
     */
    public static List<URL> getDCAEJarsURLs(URI indexDCAEJars) {
        JsonFactory jf = new JsonFactory();
        ObjectMapper om = new ObjectMapper();

        try {
            List<Object> urls = om.readValue(jf.createParser(indexDCAEJars.toURL()), List.class);

            return urls.stream().map(u -> {
                try {
                    Map<String, Object> foo = (Map<String, Object>) u;
                    String name = (String) foo.get("name");
                    String url = String.format("%s/%s", indexDCAEJars.toString(), name);
                    return new URL(url);
                } catch (MalformedURLException e) {
                    // Hopefully you never come here...
                    return null;
                }
            }).collect(Collectors.toList());
        } catch (Exception e) {
            throw new RuntimeException("Error while getting jar URIs", e);
        }
    }

    /**
     * Loads all the Processor classes from the list of jar URLs and does a
     * validation check that prints to screen.
     * 
     * @param jarURLs
     */
    public static void loadFromJars(URL[] jarURLs) {
        URLClassLoader urlClassLoader = new URLClassLoader(jarURLs);
        checkManifest(urlClassLoader);

        final ServiceLoader<?> serviceLoader = ServiceLoader.load(Processor.class, urlClassLoader);

        for (final Object o : serviceLoader) {
            LOG.info(o.getClass().getName());
            DCAEProcessor proc = ((DCAEProcessor) o);
            proc.ping();
            LOG.info(String.format("%s: %s", proc.getName(), proc.getComponentUrl()));
        }

        // TODO: Can fetch the comp spec with the component url to do further
        // validation..
    }

    private static boolean init(File dirWorking) {
        File dirJars = getDirectoryForJars(dirWorking);

        if (dirJars.exists() || dirJars.mkdirs()) {
            return true;
        }

        return false;
    }

    public static void main(String[] args) throws InterruptedException, URISyntaxException {
        if (args.length == 0) {
            args = new String[] { "gen" };
            String sleepstr = System.getenv("GENPROC_SLEEP_SEC");
            long sleepdur = (sleepstr != null)? 1000 * Long.parseLong(sleepstr): 0;
            do {
		try {
			main2(args);
		} catch (Exception e) {
			LOG.error(e.toString(), e);
		}
                Thread.sleep(sleepdur);
            } while (sleepdur > 0);
            return;
        } else {
            main2(args);
        }
    }


    public static void main2(String[] args) throws URISyntaxException {
        String argsStr = String.join(", ", args);
        if (argsStr.contains("-h")) {
            LOG.info("Here are the possible args:");
            LOG.info("<gen> <load>");
            return;
        }

        boolean shouldGenerate = argsStr.contains("gen") ? true : false;
        boolean shouldLoad = argsStr.contains("load") ? true : false;
        boolean shouldPackage = argsStr.contains("package") ? true : false;

        // Config from env variables
        File dirWorking = new File(System.getenv("GENPROC_WORKING_DIR"));
        String hostOnboardingAPI = System.getenv("GENPROC_ONBOARDING_API_HOST");
        String urlToJarIndex = System.getenv("GENPROC_JAR_INDEX_URL");

        String[] paramsToPrint = new String[] {
            String.format("shouldGenerate=%b", shouldGenerate)
            , String.format("shouldLoad=%b", shouldLoad)
            , String.format("Working directory=%s", dirWorking.getName())
        };
        LOG.info(String.format("Genprocessor configuration: \n\t%s",
            String.join("\n\t", paramsToPrint)));

        init(dirWorking);

        // TODO: Need a way to load from file again

        if (shouldGenerate) {
            CompList compList = OnboardingAPIClient.getComponents(hostOnboardingAPI);
            LOG.info(String.format("Components retrieved: %d", compList.components.size()));

            for (CompList.CompShort cs : compList.components) {
                Comp comp = OnboardingAPIClient.getComponent(cs.getComponentUrlAsURI());
                LOG.info(String.format("Component spec: \n\t%s", comp.compSpec.toString("\n\t")));

                String jarName = Utils.formatNameForJar(comp.compSpec);

                if (doesJarExist(dirWorking, jarName)) {
                    LOG.info(String.format("Jar exists: %s.jar", jarName));
                    continue;
                }

                File dirBuild = new File(dirWorking, jarName);

                if (dirBuild.exists() || dirBuild.mkdir()) {
                    generateClassFile(dirBuild.getAbsolutePath(), comp);
                    writeManifestThing(dirBuild, generateManifestMF(comp.compSpec), "META-INF", "MANIFEST.MF");
                    writeManifestThing(dirBuild, Arrays.asList(createClassName(comp.compSpec)), "META-INF/services",
                            "org.apache.nifi.processor.Processor");
                    copyProcessorClassFile("DCAEProcessor.class", dirBuild);
                    packageJar(dirWorking, dirBuild, jarName);
                }
            }
        }

        if (shouldLoad) {
            List<URL> jarURLs;
            try {
                jarURLs = getDCAEJarsURLs(new URI(urlToJarIndex));
                LOG.info(jarURLs.toString());
            } catch (URISyntaxException e) {
                throw new RuntimeException("URL to index.json is bad");
            }

            for (URL jarURL : jarURLs) {
                loadFromJars(new URL[] {jarURL});
            }
        }
    }
}