Fix for dealing with multiple yang deps

Change-Id: I77fd4d1118db0b238549f50f4dac24c0616b5035
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
index 0a12189..a916717 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
@@ -67,11 +67,15 @@
 import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangDataTypes;
 import org.onosproject.yang.model.LeafObjectType;
 import org.onosproject.yang.model.SchemaId;
+import org.slf4j.Logger;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -111,11 +115,14 @@
 import static org.onosproject.yang.model.LeafObjectType.LONG;
 import static org.onosproject.yang.model.LeafObjectType.SHORT;
 import static org.onosproject.yang.model.LeafObjectType.STRING;
+import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Represents utilities for data model tree.
  */
 public final class DataModelUtils {
+    private static final Logger log = getLogger(DataModelUtils.class);
+
     public static final String TRUE = "true";
     public static final String FALSE = "false";
     public static final String FMT_NOT_EXIST =
@@ -865,49 +872,56 @@
     }
 
     /**
-     * Parses jar file and returns list of serialized file names.
+     * Extracts contents from jar file and returns path to YangMetaData.
      *
      * @param jarFile   jar file to be parsed
-     * @param directory directory where to search
-     * @return list of serialized files
+     * @param directory directory where to output
+     * @return path to serialized YangMetaData file copied in {@code directory}
      * @throws IOException when fails to do IO operations
      */
     public static File parseDepSchemaPath(String jarFile, String directory)
             throws IOException {
-        JarFile jar = new JarFile(jarFile);
-        Enumeration<?> enumEntries = jar.entries();
-        File serializedFile = null;
-        while (enumEntries.hasMoreElements()) {
-            JarEntry file = (JarEntry) enumEntries.nextElement();
-            if (file.getName().endsWith(".ser")) {
+        log.trace("From jarfile: {}", jarFile);
+        try (JarFile jar = new JarFile(jarFile)) {
+            Enumeration<?> enumEntries = jar.entries();
+            File serializedFile = null;
+            while (enumEntries.hasMoreElements()) {
+                JarEntry file = (JarEntry) enumEntries.nextElement();
+                if (file.getName().endsWith("YangMetaData.ser")) {
 
-                if (file.getName().contains(SLASH)) {
-                    String[] strArray = file.getName().split(SLASH);
-                    String tempPath = "";
-                    for (int i = 0; i < strArray.length - 1; i++) {
-                        tempPath = SLASH + tempPath + SLASH + strArray[i];
+                    Path jarRelPath = Paths.get(file.getName());
+                    Path outBase = Paths.get(directory);
+                    String inFilename = Paths.get(jarFile).getFileName().toString();
+                    String inBasename = inFilename.substring(0, inFilename.length() - ".jar".length());
+                    // inject input jar basename right before the filename.
+                    Path serializedPath = outBase
+                            .resolve(jarRelPath.getParent())
+                            .resolve(inBasename)
+                            .resolve(jarRelPath.getFileName());
+
+                    if (Files.isDirectory(serializedPath)) {
+                        Files.createDirectories(serializedPath.getParent());
+                        continue;
+                    } else {
+                        Files.createDirectories(serializedPath.getParent());
                     }
-                    File dir = new File(directory + tempPath);
-                    dir.mkdirs();
-                }
-                serializedFile = new File(directory + SLASH + file.getName());
-                if (file.isDirectory()) {
-                    serializedFile.mkdirs();
-                    continue;
-                }
-                InputStream inputStream = jar.getInputStream(file);
+                    serializedFile = serializedPath.toFile();
+                    log.trace(" writing {} to {}", file.getName(), serializedFile);
+                    InputStream inputStream = jar.getInputStream(file);
 
-                FileOutputStream fileOutputStream = new FileOutputStream(serializedFile);
-                IOUtils.copy(inputStream, fileOutputStream);
-                fileOutputStream.close();
-                inputStream.close();
-                //As of now only one metadata files will be there so if we
-                // found one then we should break the loop.
-                break;
+                    FileOutputStream fileOutputStream = new FileOutputStream(serializedFile);
+                    IOUtils.copy(inputStream, fileOutputStream);
+                    fileOutputStream.close();
+                    inputStream.close();
+
+                    //As of now only one metadata files will be there so if we
+                    // found one then we should break the loop.
+                    return serializedFile;
+                }
             }
         }
-        jar.close();
-        return serializedFile;
+        // Not found
+        return null;
     }
 
     /**
diff --git a/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/YangCompilerManager.java b/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/YangCompilerManager.java
index 8790df0..f2ae7e7 100644
--- a/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/YangCompilerManager.java
+++ b/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/YangCompilerManager.java
@@ -607,16 +607,18 @@
     }
 
     /**
-     * Parses jar file and returns YANG model.
+     * Extracts .yang files and YangMetaData from jar file to
+     * target directory and returns YangModel found.
      *
      * @param jarFile   jar file to be parsed
-     * @param directory directory where to search
-     * @return YANG model
+     * @param directory directory where to extract files to
+     * @return YangModel found
      * @throws IOException when fails to do IO operations
      */
     public static YangModel parseJarFile(String jarFile, String directory)
             throws IOException {
 
+        log.trace("Searching YangModel in {}", jarFile);
         YangModel model = null;
         try (JarFile jar = new JarFile(jarFile)) {
             Enumeration<?> enumEntries = jar.entries();
@@ -643,8 +645,13 @@
 
                         IOUtils.copy(inputStream, fileOutputStream);
                         fileOutputStream.close();
-                        if (serializedFile.getName().endsWith(YANG_META_DATA)) {
+                        // FIXME hack to return first model found
+                        if (model == null &&
+                            serializedFile.getName().endsWith(YANG_META_DATA)) {
                             model = deSerializeDataModel(serializedFile.toString());
+                            log.trace(" found {} at {}",
+                                      model.getYangModelId(),
+                                      serializedFile.getName());
                         }
                     }
                 }
diff --git a/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangGenerator.java b/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangGenerator.java
index 31f0347..9c25249 100644
--- a/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangGenerator.java
+++ b/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangGenerator.java
@@ -77,6 +77,8 @@
             //Need to get dependent schema paths to give inter jar dependencies.
             for (String jar : depJar) {
                 try {
+                    // Note: when there's multiple deps, it all gets copied to
+                    // same directory.
                     File path = parseDepSchemaPath(jar, outputDirectory);
                     if (path != null) {
                         bldr.addDependentSchema(Paths.get(path.getAbsolutePath()));
diff --git a/compiler/plugin/maven/src/main/java/org/onosproject/yang/compiler/plugin/maven/YangPluginUtils.java b/compiler/plugin/maven/src/main/java/org/onosproject/yang/compiler/plugin/maven/YangPluginUtils.java
index b791099..a0ec965 100644
--- a/compiler/plugin/maven/src/main/java/org/onosproject/yang/compiler/plugin/maven/YangPluginUtils.java
+++ b/compiler/plugin/maven/src/main/java/org/onosproject/yang/compiler/plugin/maven/YangPluginUtils.java
@@ -120,6 +120,8 @@
                 project, localRepository, remoteRepos);
         List<Path> serFilePaths = new ArrayList<>();
         for (String dependency : depJars) {
+            // Note: when there's multiple deps, it all gets copied to
+            // same directory.
             File path = parseDepSchemaPath(dependency, directory);
             if (path != null) {
                 serFilePaths.add(Paths.get(path.getAbsolutePath()));