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;
26 @Mojo(name = "generate-resources", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
27 public final class YangToResourcesMojo extends AbstractMojo {
29 private static final String LOG_PREFIX = "yang-to-resources:";
31 @Parameter(required = true)
32 private ResourceProviderArg[] resourceProviders;
34 @Parameter(required = true)
35 private String yangFilesRootDir;
38 YangToResourcesMojo(ResourceProviderArg[] resourceProviderArgs,
39 String yangFilesRootDir) {
41 this.resourceProviders = resourceProviderArgs;
42 this.yangFilesRootDir = yangFilesRootDir;
45 public YangToResourcesMojo() {
50 public void execute() throws MojoExecutionException, MojoFailureException {
52 if (resourceProviders.length == 0) {
54 Util.message("No resource provider classes provided",
59 Map<String, String> thrown = Maps.newHashMap();
60 Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
62 for (ResourceProviderArg resourceProvider : resourceProviders) {
65 provideResourcesWithOneProvider(yangFiles, resourceProvider);
67 } catch (Exception e) {
68 // try other generators, exception will be thrown after
71 "Unable to provide resources with %s resource provider",
73 resourceProvider.getResourceProviderClass()), e);
74 thrown.put(resourceProvider.getResourceProviderClass(), e
75 .getClass().getCanonicalName());
79 if (!thrown.isEmpty()) {
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);
90 * Instantiate provider from class and call required method
92 private void provideResourcesWithOneProvider(Collection<File> yangFiles,
93 ResourceProviderArg resourceProvider)
94 throws ClassNotFoundException, InstantiationException,
95 IllegalAccessException {
97 resourceProvider.check();
99 ResourceGenerator g = Util.getInstance(
100 resourceProvider.getResourceProviderClass(),
101 ResourceGenerator.class);
103 Util.message("Resource provider instantiated from %s",
104 LOG_PREFIX, resourceProvider.getResourceProviderClass()));
106 g.generateResourceFiles(yangFiles, resourceProvider.getOutputBaseDir());
108 Util.message("Resource provider %s call successful",
109 LOG_PREFIX, resourceProvider.getResourceProviderClass()));