Merge "Make Yang compilation error more readable"
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 f56e742..7888e66 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
@@ -69,6 +69,7 @@
 import static org.onosproject.yang.compiler.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler;
 import static org.onosproject.yang.compiler.utils.UtilConstants.NEW_LINE;
 import static org.onosproject.yang.compiler.utils.UtilConstants.YANG_META_DATA;
+import static org.onosproject.yang.compiler.utils.UtilConstants.YANG_RESOURCES;
 import static org.onosproject.yang.compiler.utils.io.impl.YangFileScanner.getJavaFiles;
 import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.createDirectories;
 import static org.slf4j.LoggerFactory.getLogger;
@@ -120,15 +121,28 @@
             if (!fromUt) {
                 serializeModuleMetaData(serFile, node);
             }
-            YangModule module = new YangModuleExtendedInfo(
-                    id, new File(node.getFileName()), new File(serFile));
-            ((YangModuleExtendedInfo) module).setSchema(node);
+            //take the absolute jar path and make a new path for our yang files.
+            String fileName = getFileName(node.getFileName());
+            YangModuleExtendedInfo module = new YangModuleExtendedInfo(
+                    id, new File(path + fileName), new File(serFile));
+            module.setSchema(node);
             b.addModule(id, module);
         }
         return b.addModelId(modelId).build();
     }
 
     /**
+     * Returns the file name from provided absolute path.
+     *
+     * @param absPath absolute path
+     * @return file name
+     */
+    private static String getFileName(String absPath) {
+        String[] file = absPath.split(SLASH);
+        return file[file.length - 1];
+    }
+
+    /**
      * Serializes YANG Node.
      *
      * @param serFileName path of resource directory
@@ -517,7 +531,7 @@
     }
 
     /**
-     * Returs the set of YANG nodes from a given YANG model.
+     * Returns the set of YANG nodes from a given YANG model.
      *
      * @param model YANG model
      * @return set of YANG nodes
@@ -548,20 +562,18 @@
         JarFile jar = new JarFile(jarFile);
         Enumeration<?> enumEntries = jar.entries();
 
+        File dir = new File(directory + SLASH + YANG_RESOURCES);
+        if (!dir.exists()) {
+            dir.mkdirs();
+        }
+
         while (enumEntries.hasMoreElements()) {
             JarEntry file = (JarEntry) enumEntries.nextElement();
-            if (file.getName().endsWith(YANG_META_DATA)) {
-
-                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];
-                    }
-                    File dir = new File(directory + tempPath);
-                    dir.mkdirs();
-                }
-                File serializedFile = new File(directory + SLASH + file.getName());
+            if (file.getName().endsWith(YANG_META_DATA) ||
+                    file.getName().endsWith(".yang")) {
+                String name = getFileName(file.getName());
+                File serializedFile = new File(directory + SLASH +
+                                                       YANG_RESOURCES + SLASH + name);
                 if (file.isDirectory()) {
                     serializedFile.mkdirs();
                     continue;
@@ -574,10 +586,9 @@
                 }
                 fileOutputStream.close();
                 inputStream.close();
-                model = deSerializeDataModel(serializedFile.toString());
-                //As of now only one metadata files will be there so if we
-                // found one then we should break the loop.
-                break;
+                if (serializedFile.getName().endsWith(YANG_META_DATA)) {
+                    model = deSerializeDataModel(serializedFile.toString());
+                }
             }
         }
         jar.close();
diff --git a/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/UtilConstants.java b/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/UtilConstants.java
index 427524c..dfc3bf0 100644
--- a/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/UtilConstants.java
+++ b/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/UtilConstants.java
@@ -19,7 +19,8 @@
 import java.io.File;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
 
 /**
  * Represents utilities constants which are used while generating java files.
@@ -1130,20 +1131,35 @@
      * List of keywords in java, this is used for checking if the input does not
      * contain these keywords.
      */
-    public static final List<String> JAVA_KEY_WORDS = Collections
-            .unmodifiableList(Arrays.asList("abstract", "assert", "boolean", "break", "byte",
-                                            "case", "catch", "char", "class", "const", "continue",
-                                            "default", "do", "double", "else", "extends", "false",
-                                            "final", "finally", "float", "for", "goto", "if",
-                                            "implements", "import", "instanceof", "enum", "int",
-                                            "interface", "long", "native", "new", "null",
-                                            "package", "private", "protected", "public", "return",
-                                            "short", "static", "strictfp", "super", "switch",
-                                            "synchronized", "this", "throw", "throws", "transient",
-                                            "true", "try", "void", "volatile", "while", "list",
-                                            "map", "arrayList", "hashMap", "linkedList", "notify",
-                                            "notifyAll", "Method",
-                                            "collections"));
+    public static final Set<String> JAVA_KEY_WORDS =
+            Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
+                 "abstract", "assert", "boolean", "break", "byte",
+                 "case", "catch", "char", "class", "const", "continue",
+                 "default", "do", "double", "else", "extends", "false",
+                 "final", "finally", "float", "for", "goto", "if",
+                 "implements", "import", "instanceof", "enum", "int",
+                 "interface", "long", "native", "new", "null",
+                 "package", "private", "protected", "public", "return",
+                 "short", "static", "strictfp", "super", "switch",
+                 "synchronized", "this", "throw", "throws", "transient",
+                 "true", "try", "void", "volatile", "while",
+                 "list", // Not a Java keyword
+                 "map", // Not a Java keyword
+                 "arrayList", // Not a Java keyword
+                 "hashMap", // Not a Java keyword
+                 "linkedList", // Not a Java keyword
+                 "notify", // method on Object
+                 "notifyAll", // method on Object
+                 "wait", // method on Object
+                 "getClass", // method on Object
+                 "hashCode", // method on Object
+                 "equals", // method on Object
+                 "toString", // method on Object
+                 "clone", // method on Object
+                 "finalize", // method on Object
+                 "Method", // Not a Java keyword
+                 "collections") // Not a Java keyword
+            ));
 
     /**
      * Static attribute for regex for all the special characters.
diff --git a/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/io/impl/CopyrightHeader.java b/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/io/impl/CopyrightHeader.java
index 5cc4bbb..4d4864a 100644
--- a/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/io/impl/CopyrightHeader.java
+++ b/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/io/impl/CopyrightHeader.java
@@ -16,16 +16,7 @@
 
 package org.onosproject.yang.compiler.utils.io.impl;
 
-import org.onosproject.yang.compiler.utils.UtilConstants;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileReader;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
 import static java.util.Calendar.YEAR;
 import static java.util.Calendar.getInstance;
 
@@ -34,11 +25,26 @@
  */
 public final class CopyrightHeader {
 
-    private static final int EOF = -1;
-    private static final String COPYRIGHT_HEADER_FILE = "CopyrightHeader.txt";
-    private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + getInstance().get(YEAR)
-            + "-present Open Networking Foundation\n";
-    private static final String TEMP_FILE = "temp.txt";
+    /**
+     * ONF standard Apache 2.0 license header for .java files.
+     */
+    public static final String COPYRIGHT_HEADER =
+        "/*\n" +
+        " * Copyright " + getInstance().get(YEAR) + "-present Open Networking Foundation\n" +
+        " *\n" +
+        " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
+        " * you may not use this file except in compliance with the License.\n" +
+        " * You may obtain a copy of the License at\n" +
+        " *\n" +
+        " *     http://www.apache.org/licenses/LICENSE-2.0\n" +
+        " *\n" +
+        " * Unless required by applicable law or agreed to in writing, software\n" +
+        " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
+        " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
+        " * See the License for the specific language governing permissions and\n" +
+        " * limitations under the License.\n" +
+        " */\n" +
+        "\n";
 
     /**
      * Creates an instance of copyright header.
@@ -51,55 +57,10 @@
      *
      * @return string of file.
      * @throws IOException when fails to get the copyright header
+     * @see CopyrightHeader#COPYRIGHT_HEADER
      */
     public static String parseCopyrightHeader() throws IOException {
 
-        File temp = new File(TEMP_FILE);
-
-        try {
-            InputStream stream = CopyrightHeader.class.getClassLoader()
-                    .getResourceAsStream(COPYRIGHT_HEADER_FILE);
-            OutputStream out = new FileOutputStream(temp);
-
-            int index;
-            out.write(COPYRIGHTS_FIRST_LINE.getBytes());
-            while ((index = stream.read()) != EOF) {
-                out.write(index);
-            }
-            out.close();
-            stream.close();
-            return getStringFileContent(temp);
-        } catch (IOException e) {
-            throw new IOException("failed to parse the Copyright header");
-        } finally {
-            temp.delete();
-        }
-    }
-
-    /**
-     * Converts it to string.
-     *
-     * @param toAppend file to be converted.
-     * @return string of file.
-     * @throws IOException when fails to convert to string
-     */
-    private static String getStringFileContent(File toAppend) throws IOException {
-
-        FileReader fileReader = new FileReader(toAppend);
-        BufferedReader bufferReader = new BufferedReader(fileReader);
-        try {
-            StringBuilder stringBuilder = new StringBuilder();
-            String line = bufferReader.readLine();
-
-            while (line != null) {
-                stringBuilder.append(line);
-                stringBuilder.append(UtilConstants.NEW_LINE);
-                line = bufferReader.readLine();
-            }
-            return stringBuilder.toString();
-        } finally {
-            fileReader.close();
-            bufferReader.close();
-        }
+        return COPYRIGHT_HEADER;
     }
 }
diff --git a/compiler/base/utils/src/main/resources/CopyrightHeader.txt b/compiler/base/utils/src/main/resources/CopyrightHeader.txt
deleted file mode 100644
index 2cbed45..0000000
--- a/compiler/base/utils/src/main/resources/CopyrightHeader.txt
+++ /dev/null
@@ -1,14 +0,0 @@
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/helperutils/YangApacheUtils.java b/runtime/src/main/java/org/onosproject/yang/runtime/helperutils/YangApacheUtils.java
index edefc76..b75ad53 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/helperutils/YangApacheUtils.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/helperutils/YangApacheUtils.java
@@ -42,7 +42,6 @@
     private static final String SLASH = File.separator;
     private static final String HYPHEN = "-";
     private static final String PERIOD = ".";
-    private static final String YANG_META_DATA = "YangMetaData.ser";
     private static final String YANG_RESOURCES = "yang/resources";
     private static final String SYSTEM = SLASH + "system" + SLASH;
     private static final String MAVEN = "mvn:";
@@ -69,7 +68,7 @@
             String metaPath;
             jarPath = getJarPathFromBundleLocation(
                     bundle.getLocation(), context.getProperty(USER_DIRECTORY));
-            metaPath = jarPath + SLASH + YANG_RESOURCES + SLASH + YANG_META_DATA;
+            metaPath = jarPath + SLASH + YANG_RESOURCES + SLASH;
             YangModel model = processJarParsingOperations(jarPath);
             if (model != null) {
                 curNodes.addAll(getYangNodes(model));