Refactor frontend JS
[controller.git] / opendaylight / sal / yang-prototype / code-generator / maven-yang-plugin / src / main / java / org / opendaylight / controller / yang2sources / plugin / YangToResourcesMojo.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.yang2sources.plugin;
9
10 import java.io.File;
11 import java.util.Collection;
12 import java.util.Map;
13
14 import org.apache.maven.plugin.AbstractMojo;
15 import org.apache.maven.plugin.MojoExecutionException;
16 import org.apache.maven.plugin.MojoFailureException;
17 import org.apache.maven.plugins.annotations.LifecyclePhase;
18 import org.apache.maven.plugins.annotations.Mojo;
19 import org.apache.maven.plugins.annotations.Parameter;
20 import org.opendaylight.controller.yang2sources.plugin.ConfigArg.ResourceProviderArg;
21 import org.opendaylight.controller.yang2sources.spi.ResourceGenerator;
22
23 import com.google.common.annotations.VisibleForTesting;
24 import com.google.common.collect.Maps;
25
26 @Mojo(name = "generate-resources", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
27 public final class YangToResourcesMojo extends AbstractMojo {
28
29     private static final String LOG_PREFIX = "yang-to-resources:";
30
31     @Parameter(required = true)
32     private ResourceProviderArg[] resourceProviders;
33
34     @Parameter(required = true)
35     private String yangFilesRootDir;
36
37     @VisibleForTesting
38     YangToResourcesMojo(ResourceProviderArg[] resourceProviderArgs,
39             String yangFilesRootDir) {
40         super();
41         this.resourceProviders = resourceProviderArgs;
42         this.yangFilesRootDir = yangFilesRootDir;
43     }
44
45     public YangToResourcesMojo() {
46         super();
47     }
48
49     @Override
50     public void execute() throws MojoExecutionException, MojoFailureException {
51
52         if (resourceProviders.length == 0) {
53             getLog().warn(
54                     Util.message("No resource provider classes provided",
55                             LOG_PREFIX));
56             return;
57         }
58
59         Map<String, String> thrown = Maps.newHashMap();
60         Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
61
62         for (ResourceProviderArg resourceProvider : resourceProviders) {
63             try {
64
65                 provideResourcesWithOneProvider(yangFiles, resourceProvider);
66
67             } catch (Exception e) {
68                 // try other generators, exception will be thrown after
69                 getLog().error(
70                         Util.message(
71                                 "Unable to provide resources with %s resource provider",
72                                 LOG_PREFIX,
73                                 resourceProvider.getResourceProviderClass()), e);
74                 thrown.put(resourceProvider.getResourceProviderClass(), e
75                         .getClass().getCanonicalName());
76             }
77         }
78
79         if (!thrown.isEmpty()) {
80             String message = Util
81                     .message(
82                             "One or more code resource provider failed, including failed list(resourceProviderClass=exception) %s",
83                             LOG_PREFIX, thrown.toString());
84             getLog().error(message);
85             throw new MojoFailureException(message);
86         }
87     }
88
89     /**
90      * Instantiate provider from class and call required method
91      */
92     private void provideResourcesWithOneProvider(Collection<File> yangFiles,
93             ResourceProviderArg resourceProvider)
94             throws ClassNotFoundException, InstantiationException,
95             IllegalAccessException {
96
97         resourceProvider.check();
98
99         ResourceGenerator g = Util.getInstance(
100                 resourceProvider.getResourceProviderClass(),
101                 ResourceGenerator.class);
102         getLog().info(
103                 Util.message("Resource provider instantiated from %s",
104                         LOG_PREFIX, resourceProvider.getResourceProviderClass()));
105
106         g.generateResourceFiles(yangFiles, resourceProvider.getOutputBaseDir());
107         getLog().info(
108                 Util.message("Resource provider %s call successful",
109                         LOG_PREFIX, resourceProvider.getResourceProviderClass()));
110     }
111 }