Merge "Five more Equals/HashCode/StringBuilder replacements"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / maven-yang-plugin / src / main / java / org / opendaylight / controller / yang2sources / plugin / YangToSourcesMojo.java
index 6a11042cb9c1075f2ca2ee96d904fffdc9e2cf41..f5ac8f904c2475ff569d162496eb00b5ff4cdb8b 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.controller.yang2sources.plugin;
 import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -35,7 +36,7 @@ import org.apache.maven.project.MavenProject;
 import org.opendaylight.controller.yang.model.api.Module;
 import org.opendaylight.controller.yang.model.api.SchemaContext;
 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
-import org.opendaylight.controller.yang.model.parser.impl.YangParserImpl;
+import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
 import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
 import org.opendaylight.controller.yang2sources.plugin.ConfigArg.ResourceProviderArg;
 import org.opendaylight.controller.yang2sources.spi.CodeGenerator;
@@ -44,6 +45,7 @@ import org.opendaylight.controller.yang2sources.spi.ResourceGenerator;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import com.google.common.io.Files;
 
 /**
  * Generate sources from yang files using user provided set of
@@ -61,7 +63,7 @@ import com.google.common.collect.Maps;
 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
 public final class YangToSourcesMojo extends AbstractMojo {
     private static final String LOG_PREFIX = "yang-to-sources:";
-    private static final String INPUT_RESOURCE_DIR = "META-INF/yangs/";
+    private static final String INPUT_RESOURCE_DIR = "META-INF/yang/";
     private static final String OUTPUT_RESOURCE_DIR = "/target/external-resources/";
 
     /**
@@ -130,8 +132,9 @@ public final class YangToSourcesMojo extends AbstractMojo {
 
             if (yangFiles.isEmpty()) {
                 getLog().warn(
-                        Util.message("No %s file found in %s", LOG_PREFIX,
-                                Util.YANG_SUFFIX, yangFilesRootDir));
+                        Util.message(
+                                "No %s file found in %s or in dependencies",
+                                LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir));
                 return null;
             }
 
@@ -177,8 +180,8 @@ public final class YangToSourcesMojo extends AbstractMojo {
         yangFiles.addAll(getFilesFromYangRoot());
 
         // load files from dependencies
-        yangFiles.addAll(getFilesFromDependencies());
-
+        Collection<File> filesFromDependencies = getFilesFromDependencies();
+        yangFiles.addAll(filesFromDependencies);
 
         for (ResourceProviderArg resourceProvider : resourceProviders) {
             try {
@@ -206,16 +209,35 @@ public final class YangToSourcesMojo extends AbstractMojo {
     }
 
     private Collection<File> getFilesFromYangRoot() {
-        Collection<File> yangFilesLoaded = Util.listFiles(yangFilesRootDir);
+        Collection<File> yangFilesLoaded = null;
+
+        File rootDir = new File(yangFilesRootDir);
+        try {
+            if (rootDir.isAbsolute()) {
+                yangFilesLoaded = Util.listFiles(yangFilesRootDir);
+            } else {
+                String path = project.getBasedir().getAbsolutePath()
+                        + File.separator + yangFilesRootDir;
+                yangFilesLoaded = Util.listFiles(path);
+            }
+        } catch (FileNotFoundException e) {
+            getLog().warn(
+                    "yangFilesRootDir[" + rootDir.getAbsolutePath()
+                            + "] does not exists.");
+            yangFilesLoaded = new ArrayList<File>();
+        }
+
         Collection<File> yangFiles = new ArrayList<File>(yangFilesLoaded);
 
         try {
-            for(File yangFile : yangFilesLoaded) {
+            for (File yangFile : yangFilesLoaded) {
                 InputStream is = new FileInputStream(yangFile);
-                yangFiles.add(createFileFromStream(is, project.getBasedir().getAbsolutePath() + OUTPUT_RESOURCE_DIR + yangFile.getName()));
+                yangFiles.add(createFileFromStream(is,
+                        project.getBasedir().getAbsolutePath()
+                                + OUTPUT_RESOURCE_DIR + yangFile.getName()));
                 resources.add(is);
             }
-        } catch(IOException e) {
+        } catch (IOException e) {
             getLog().warn("Exception while loading yang files.", e);
         }
         return yangFiles;
@@ -242,8 +264,11 @@ public final class YangToSourcesMojo extends AbstractMojo {
                             continue;
                         }
                         InputStream entryStream = zip.getInputStream(entry);
-                        String newEntryName = entryName.substring(INPUT_RESOURCE_DIR.length());
-                        File f = createFileFromStream(entryStream, project.getBasedir().getAbsolutePath() + OUTPUT_RESOURCE_DIR + newEntryName);
+                        String newEntryName = entryName
+                                .substring(INPUT_RESOURCE_DIR.length());
+                        File tmp = Files.createTempDir();
+                        File f = createFileFromStream(entryStream,
+                                tmp.getAbsolutePath() + newEntryName);
                         yangFiles.add(f);
 
                         resources.add(entryStream);
@@ -258,9 +283,10 @@ public final class YangToSourcesMojo extends AbstractMojo {
         return yangFiles;
     }
 
-    private File createFileFromStream(InputStream is, String absoluteName) throws IOException {
+    private File createFileFromStream(InputStream is, String absoluteName)
+            throws IOException {
         File f = new File(absoluteName);
-        if(!f.exists()) {
+        if (!f.exists()) {
             f.getParentFile().mkdirs();
         }
         f.createNewFile();
@@ -378,8 +404,8 @@ public final class YangToSourcesMojo extends AbstractMojo {
                     ZipEntry entry = entries.nextElement();
                     String entryName = entry.getName();
 
-                    if(entryName.startsWith(INPUT_RESOURCE_DIR)) {
-                        if(entry.isDirectory()) {
+                    if (entryName.startsWith(INPUT_RESOURCE_DIR)) {
+                        if (entry.isDirectory()) {
                             continue;
                         }
                         if (!Util.acceptedFilter(entryName, filter)) {
@@ -408,7 +434,7 @@ public final class YangToSourcesMojo extends AbstractMojo {
             try {
                 resource.close();
             } catch (IOException e) {
-                getLog().warn("Failed to close resources: "+ resource, e);
+                getLog().warn("Failed to close resources: " + resource, e);
             }
         }
     }