/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang2sources.plugin;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
import org.sonatype.plexus.build.incremental.BuildContext;

import com.google.common.annotations.VisibleForTesting;

/**
 * Generate sources from yang files using user provided set of
 * {@link CodeGenerator}s. Steps of this process:
 * <ol>
 * <li>List yang files from {@link #yangFilesRootDir}</li>
 * <li>Process yang files using {@link YangParserImpl}</li>
 * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:
 * <ol>
 * <li>Instantiate using default constructor</li>
 * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File, Set)}</li>
 * </ol></li>
 * </ol>
 */
@Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
public final class YangToSourcesMojo extends AbstractMojo {
    public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";

    /**
     * Classes implementing {@link CodeGenerator} interface. An instance will be
     * created out of every class using default constructor. Method {@link
     * CodeGenerator#generateSources(SchemaContext, File, Set<String>
     * yangModulesNames)} will be called on every instance.
     */
    @Parameter(required = false)
    private CodeGeneratorArg[] codeGenerators;

    /**
     * Source directory that will be recursively searched for yang files (ending
     * with .yang suffix).
     */
    @Parameter(required = false)
    // defaults to ${basedir}/src/main/yang
    private String yangFilesRootDir;

    @Parameter(required = false)
    private String[] excludeFiles;

    @Parameter(property = "project", required = true, readonly = true)
    private MavenProject project;

    @Parameter(property = "inspectDependencies", required = true, readonly = true)
    private boolean inspectDependencies;

    @Component
    private BuildContext buildContext;

    private YangToSourcesProcessor yangToSourcesProcessor;

    @Component
    private RepositorySystem repoSystem;

    @Parameter( readonly = true, defaultValue = "${localRepository}" )
    private ArtifactRepository localRepository;

    @Parameter( readonly = true, defaultValue = "${project.remoteArtifactRepositories}" )
    private List<ArtifactRepository> remoteRepos;


    public YangToSourcesMojo() {
    }

    public void setProject(MavenProject project) {
        this.project = project;
    }

    @VisibleForTesting
    YangToSourcesMojo(YangToSourcesProcessor processor) {
        this.yangToSourcesProcessor = processor;
    }

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        Util.checkClasspath(project, repoSystem, localRepository, remoteRepos, getLog());

        if (yangToSourcesProcessor == null) {
            List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);

            // defaults to ${basedir}/src/main/yang
            File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
            File[] excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);

            yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, getLog(), yangFilesRootFile,
                    excludedFiles, codeGeneratorArgs, project, inspectDependencies);
        }
        yangToSourcesProcessor.execute();
    }

    private static List<CodeGeneratorArg> processCodeGenerators(CodeGeneratorArg[] codeGenerators) {
        List<CodeGeneratorArg> codeGeneratorArgs;
        if (codeGenerators == null) {
            codeGeneratorArgs = Collections.emptyList();
        } else {
            codeGeneratorArgs = Arrays.asList(codeGenerators);
        }
        return codeGeneratorArgs;
    }

    private static File processYangFilesRootDir(String yangFilesRootDir, File baseDir) {
        File yangFilesRootFile;
        if (yangFilesRootDir == null) {
            yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
        } else {
            File file = new File(yangFilesRootDir);
            if (file.isAbsolute()) {
                yangFilesRootFile = file;
            } else {
                yangFilesRootFile = new File(baseDir, file.getPath());
            }
        }
        return yangFilesRootFile;
    }

    private static File[] processExcludeFiles(String[] excludeFiles, File baseDir) {
        if (excludeFiles == null) {
            return new File[] {};
        }
        File[] result = new File[excludeFiles.length];
        for (int i = 0; i < excludeFiles.length; i++) {
            result[i] = new File(baseDir, excludeFiles[i]);
        }

        return result;
    }

}

@@ -358,16 +358,72 @@
 lugin;%0A%0A
+import com.google.common.annotations.VisibleForTesting;%0A
 import j
@@ -535,17 +535,16 @@
 il.Set;%0A
-%0A
 import o
@@ -1396,16 +1396,21 @@
 ces.spi.
+Basic
 CodeGene
@@ -1480,65 +1480,8 @@
 t;%0A%0A
-import com.google.common.annotations.VisibleForTesting;%0A%0A
 /**%0A
@@ -1545,32 +1545,37 @@
 et of%0A * %7B@link 
+Basic
 CodeGenerator%7Ds.
@@ -1740,32 +1740,37 @@
 For each %7B@link 
+Basic
 CodeGenerator%7D f
@@ -1867,32 +1867,37 @@
 %3Cli%3ECall %7B@link 
+Basic
 CodeGenerator#ge
@@ -2327,16 +2327,21 @@
  %7B@link 
+Basic
 CodeGene
@@ -3712,16 +3712,22 @@
 Project(
+final 
 MavenPro
@@ -3826,16 +3826,22 @@
 cesMojo(
+final 
 YangToSo
@@ -4809,16 +4809,22 @@
 erators(
+final 
 CodeGene
@@ -5176,16 +5176,22 @@
 RootDir(
+final 
 String y
@@ -5206,16 +5206,22 @@
 RootDir,
+ final
  File ba
@@ -5762,16 +5762,22 @@
 deFiles(
+final 
 String%5B%5D
@@ -5790,16 +5790,22 @@
 deFiles,
+ final
  File ba

Migrate to BasicGenerator