Added javadoc to maven-yang-plugin.
[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 /**
27  * Generate resources from yang files using user provided set of
28  * {@link ResourceGenerator}s. Can be used to copy yang files that served as
29  * blueprint for code generation into resources directory. Steps of this
30  * process:
31  * <ol>
32  * <li>List yang files from {@link #yangFilesRootDir} (If this goal is in the
33  * same execution as generate-sources, the same cached list will be used and the
34  * root folder will not be searched for yang files twice)</li>
35  * <li>For each {@link ResourceGenerator} from {@link #resourceProviders}:</li>
36  * <ol>
37  * <li>Instantiate using default constructor</li>
38  * <li>Call {@link ResourceGenerator#generateResourceFiles(Collection, File)}</li>
39  * </ol>
40  * </ol>
41  */
42 @Mojo(name = "generate-resources", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
43 public final class YangToResourcesMojo extends AbstractMojo {
44
45     private static final String LOG_PREFIX = "yang-to-resources:";
46
47     /**
48      * Classes implementing {@link ResourceGenerator} interface. An instance
49      * will be created out of every class using default constructor. Method
50      * {@link ResourceGenerator#generateResourceFiles(Collection, File)} will be
51      * called on every instance.
52      */
53     @Parameter(required = true)
54     private ResourceProviderArg[] resourceProviders;
55
56     /**
57      * Source directory that will be recursively searched for yang files (ending
58      * with .yang suffix).
59      */
60     @Parameter(required = true)
61     private String yangFilesRootDir;
62
63     @VisibleForTesting
64     YangToResourcesMojo(ResourceProviderArg[] resourceProviderArgs,
65             String yangFilesRootDir) {
66         super();
67         this.resourceProviders = resourceProviderArgs;
68         this.yangFilesRootDir = yangFilesRootDir;
69     }
70
71     public YangToResourcesMojo() {
72         super();
73     }
74
75     @Override
76     public void execute() throws MojoExecutionException, MojoFailureException {
77
78         if (resourceProviders.length == 0) {
79             getLog().warn(
80                     Util.message("No resource provider classes provided",
81                             LOG_PREFIX));
82             return;
83         }
84
85         Map<String, String> thrown = Maps.newHashMap();
86         Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
87
88         for (ResourceProviderArg resourceProvider : resourceProviders) {
89             try {
90
91                 provideResourcesWithOneProvider(yangFiles, resourceProvider);
92
93             } catch (Exception e) {
94                 // try other generators, exception will be thrown after
95                 getLog().error(
96                         Util.message(
97                                 "Unable to provide resources with %s resource provider",
98                                 LOG_PREFIX,
99                                 resourceProvider.getResourceProviderClass()), e);
100                 thrown.put(resourceProvider.getResourceProviderClass(), e
101                         .getClass().getCanonicalName());
102             }
103         }
104
105         if (!thrown.isEmpty()) {
106             String message = Util
107                     .message(
108                             "One or more code resource provider failed, including failed list(resourceProviderClass=exception) %s",
109                             LOG_PREFIX, thrown.toString());
110             getLog().error(message);
111             throw new MojoFailureException(message);
112         }
113     }
114
115     /**
116      * Instantiate provider from class and call required method
117      */
118     private void provideResourcesWithOneProvider(Collection<File> yangFiles,
119             ResourceProviderArg resourceProvider)
120             throws ClassNotFoundException, InstantiationException,
121             IllegalAccessException {
122
123         resourceProvider.check();
124
125         ResourceGenerator g = Util.getInstance(
126                 resourceProvider.getResourceProviderClass(),
127                 ResourceGenerator.class);
128         getLog().info(
129                 Util.message("Resource provider instantiated from %s",
130                         LOG_PREFIX, resourceProvider.getResourceProviderClass()));
131
132         g.generateResourceFiles(yangFiles, resourceProvider.getOutputBaseDir());
133         getLog().info(
134                 Util.message("Resource provider %s call successful",
135                         LOG_PREFIX, resourceProvider.getResourceProviderClass()));
136     }
137 }