2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang2sources.plugin;
11 import java.util.Collection;
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;
23 import com.google.common.annotations.VisibleForTesting;
24 import com.google.common.collect.Maps;
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
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>
37 * <li>Instantiate using default constructor</li>
38 * <li>Call {@link ResourceGenerator#generateResourceFiles(Collection, File)}</li>
42 @Mojo(name = "generate-resources", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
43 public final class YangToResourcesMojo extends AbstractMojo {
45 private static final String LOG_PREFIX = "yang-to-resources:";
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.
53 @Parameter(required = true)
54 private ResourceProviderArg[] resourceProviders;
57 * Source directory that will be recursively searched for yang files (ending
60 @Parameter(required = true)
61 private String yangFilesRootDir;
64 YangToResourcesMojo(ResourceProviderArg[] resourceProviderArgs,
65 String yangFilesRootDir) {
67 this.resourceProviders = resourceProviderArgs;
68 this.yangFilesRootDir = yangFilesRootDir;
71 public YangToResourcesMojo() {
76 public void execute() throws MojoExecutionException, MojoFailureException {
78 if (resourceProviders.length == 0) {
80 Util.message("No resource provider classes provided",
85 Map<String, String> thrown = Maps.newHashMap();
86 Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
88 for (ResourceProviderArg resourceProvider : resourceProviders) {
91 provideResourcesWithOneProvider(yangFiles, resourceProvider);
93 } catch (Exception e) {
94 // try other generators, exception will be thrown after
97 "Unable to provide resources with %s resource provider",
99 resourceProvider.getResourceProviderClass()), e);
100 thrown.put(resourceProvider.getResourceProviderClass(), e
101 .getClass().getCanonicalName());
105 if (!thrown.isEmpty()) {
106 String message = Util
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);
116 * Instantiate provider from class and call required method
118 private void provideResourcesWithOneProvider(Collection<File> yangFiles,
119 ResourceProviderArg resourceProvider)
120 throws ClassNotFoundException, InstantiationException,
121 IllegalAccessException {
123 resourceProvider.check();
125 ResourceGenerator g = Util.getInstance(
126 resourceProvider.getResourceProviderClass(),
127 ResourceGenerator.class);
129 Util.message("Resource provider instantiated from %s",
130 LOG_PREFIX, resourceProvider.getResourceProviderClass()));
132 g.generateResourceFiles(yangFiles, resourceProvider.getOutputBaseDir());
134 Util.message("Resource provider %s call successful",
135 LOG_PREFIX, resourceProvider.getResourceProviderClass()));