[ONOS-3906],[ONOS-3910] Implementation of YANG module and leaf/leaf-list translator.
Change-Id: If1a8a991ffafa14b51211f97c435176ee1bf856f
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
index 8b7e732..3ba2fc6 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
@@ -33,9 +33,11 @@
public final class CopyrightHeader {
private static final Logger log = getLogger(CopyrightHeader.class);
- private static final int NULL = -1;
+ private static final int EOF = -1;
private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
+ private static String copyrightHeader;
+
/**
* Default constructor.
*/
@@ -43,21 +45,30 @@
}
/**
- * Returns Copyright file header.
+ * Returns copyright file header.
*
- * @return Copyright file header
+ * @return copyright file header
+ * @throws IOException when fails to parse copyright header.
*/
- public static String getCopyrightHeader() {
- return parseOnosHeader();
+ public static String getCopyrightHeader() throws IOException {
+ return copyrightHeader;
+ }
+
+ /**
+ * Sets the copyright header.
+ *
+ * @param header copyright header
+ */
+ private static void setCopyrightHeader(String header) {
+ copyrightHeader = header;
}
/**
* parse Copyright to the temporary file.
*
- * @param file generated file
- * @param stream input stream
+ * @throws IOException when fails to get the copyright header.
*/
- private static String parseOnosHeader() {
+ public static void parseCopyrightHeader() throws IOException {
File temp = new File("temp.txt");
@@ -65,17 +76,18 @@
InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt");
OutputStream out = new FileOutputStream(temp);
int index;
- while ((index = stream.read()) != NULL) {
+ while ((index = stream.read()) != EOF) {
out.write(index);
}
out.close();
- return convertToString(temp.toString());
+ stream.close();
+ getStringFileContent(temp);
+ setCopyrightHeader(getStringFileContent(temp));
} catch (IOException e) {
- log.info("Failed to insert onos header in files.");
+ throw new IOException("failed to parse the Copyright header");
} finally {
temp.delete();
}
- return "";
}
/**
@@ -85,7 +97,8 @@
* @return string of file.
* @throws IOException when fails to convert to string
*/
- private static String convertToString(String toAppend) throws IOException {
+ private static String getStringFileContent(File toAppend) throws IOException {
+
BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
try {
StringBuilder stringBuilder = new StringBuilder();
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
new file mode 100644
index 0000000..f7e3556
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import org.onosproject.yangutils.translator.CachedFileHandle;
+import org.onosproject.yangutils.translator.GeneratedFileType;
+import org.onosproject.yangutils.translator.tojava.CachedJavaFileHandle;
+import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
+import org.onosproject.yangutils.utils.UtilConstants;
+
+/**
+ * Utility to handle file system operations.
+ */
+public final class FileSystemUtil {
+ /**
+ * Hiding constructor of a utility class.
+ */
+ private FileSystemUtil() {
+ }
+
+ /**
+ * Check if the package directory structure created.
+ *
+ * @param pkg Package to check if it is created.
+ * @return existence status of package.
+ */
+ public static boolean doesPackageExist(File pkg) {
+ if (pkg.exists()) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Create a package structure with package info java file if not present.
+ *
+ * @param pkg java package string
+ * @param pkgInfo description of package
+ * @throws IOException any IO exception
+ */
+ public static void createPackage(String pkg, String pkgInfo) throws IOException {
+ if (!doesPackageExist(new File(pkg))) {
+ try {
+ File pack = YangIoUtils
+ .createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
+ YangIoUtils.addPackageInfo(pack, pkgInfo, pkg);
+ } catch (IOException e) {
+ throw new IOException("failed to create package-info file");
+ }
+ }
+ }
+
+ /**
+ * Create a java source file in the specified package.
+ *
+ * @param pkg java package under which the interface file needs to be created.
+ * @param yangName YANG name of the node for which java file needs to be created.
+ * @param types types of files to be created.
+ * @throws IOException when fails to create interface file.
+ * @return the cached java file handle, which can be used to further add methods.
+ */
+ public static CachedFileHandle createSourceFiles(String pkg, String yangName, GeneratedFileType types)
+ throws IOException {
+ //if (!doesPackageExist(new File(pkg))) {
+ // throw new IOException("package does not exist.");
+ //}
+ yangName = JavaIdentifierSyntax.getCamelCase(yangName);
+ CachedFileHandle handler = new CachedJavaFileHandle(pkg, yangName, types);
+
+ return handler;
+ }
+
+ /**
+ * Read the contents from source file and append its contents to append
+ * file.
+ *
+ * @param toAppend destination file in which the contents of source file is
+ * appended.
+ * @param srcFile source file from which data is read and added to to append
+ * file.
+ * @throws IOException any IO errors.
+ */
+ public static void appendFileContents(File toAppend, File srcFile) throws IOException {
+
+ insertStringInFile(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()));
+ //TODO: read the contents from src file and append its contents to append file.
+ return;
+ }
+
+ /**
+ * Reads file and convert it to string.
+ *
+ * @param toAppend file to be converted.
+ * @return string of file.
+ * @throws IOException when fails to convert to string
+ */
+ private static String readAppendFile(String toAppend) throws IOException {
+ BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
+ try {
+ StringBuilder stringBuilder = new StringBuilder();
+ String line = bufferReader.readLine();
+
+ while (line != null) {
+ stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line);
+ stringBuilder.append("\n");
+ line = bufferReader.readLine();
+ }
+ return stringBuilder.toString();
+ } finally {
+ bufferReader.close();
+ }
+ }
+
+ /**
+ * Insert content to the generated file.
+ *
+ * @param inputFile input file
+ * @param contentTobeAdded content to be appended to the file.
+ * @throws IOException when fails to append content to the file.
+ */
+ public static void insertStringInFile(File inputFile, String contentTobeAdded) throws IOException {
+ FileWriter fileWriter = new FileWriter(inputFile, true);
+ PrintWriter outputPrintWriter = new PrintWriter(fileWriter);
+ outputPrintWriter.write(contentTobeAdded);
+ outputPrintWriter.flush();
+ outputPrintWriter.close();
+
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
new file mode 100644
index 0000000..635acf1
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
@@ -0,0 +1,250 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
+import org.onosproject.yangutils.utils.UtilConstants;
+
+/**
+ * Provides javadoc for the generated classes.
+ */
+public final class JavaDocGen {
+
+ /**
+ * Default Constructor.
+ */
+ private JavaDocGen() {
+ }
+
+ /**
+ * JavaDocs types.
+ */
+ public static enum JavaDocType {
+
+ /**
+ * For class.
+ */
+ IMPL_CLASS,
+
+ /**
+ * For builder class.
+ */
+ BUILDER_CLASS,
+
+ /**
+ * For interface.
+ */
+ INTERFACE,
+
+ /**
+ * For builder interface.
+ */
+ BUILDER_INTERFACE,
+
+ /**
+ * For package-info.
+ */
+ PACKAGE_INFO,
+
+ /**
+ * For getters.
+ */
+ GETTER,
+
+ /**
+ * For setters.
+ */
+ SETTER,
+
+ /**
+ * For default constructor.
+ */
+ DEFAULT_CONSTRUCTOR,
+
+ /**
+ * For constructor.
+ */
+ CONSTRUCTOR,
+
+ /**
+ * For build.
+ */
+ BUILD
+ }
+
+ /**
+ * Returns java docs.
+ *
+ * @param type java doc type
+ * @param name name of the YangNode
+ * @return javadocs.
+ */
+ public static String getJavaDoc(JavaDocType type, String name) {
+ name = JavaIdentifierSyntax.getCamelCase(name);
+ String javaDoc = "";
+ if (type.equals(JavaDocType.IMPL_CLASS)) {
+ javaDoc = generateForImplClass(name);
+ } else if (type.equals(JavaDocType.BUILDER_CLASS)) {
+ javaDoc = generateForBuilderClass(name);
+ } else if (type.equals(JavaDocType.INTERFACE)) {
+ javaDoc = generateForInterface(name);
+ } else if (type.equals(JavaDocType.BUILDER_INTERFACE)) {
+ javaDoc = generateForBuilderInterface(name);
+ } else if (type.equals(JavaDocType.PACKAGE_INFO)) {
+ javaDoc = generateForPackage(name);
+ } else if (type.equals(JavaDocType.GETTER)) {
+ javaDoc = generateForGetters(name);
+ } else if (type.equals(JavaDocType.SETTER)) {
+ javaDoc = generateForSetters(name);
+ } else if (type.equals(JavaDocType.DEFAULT_CONSTRUCTOR)) {
+ javaDoc = generateForDefaultConstructors();
+ } else if (type.equals(JavaDocType.BUILD)) {
+ javaDoc = generateForBuild(name);
+ } else if (type.equals(JavaDocType.CONSTRUCTOR)) {
+ javaDoc = generateForConstructors(name);
+ }
+ return javaDoc;
+ }
+
+ /**
+ * Generate javaDocs for getter method.
+ *
+ * @param attribute attribute
+ * @return javaDocs
+ */
+ private static String generateForGetters(String attribute) {
+ return (UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_GETTERS + attribute
+ + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ + UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN
+ + attribute + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generates javaDocs for setter method.
+ *
+ * @param attribute attribute
+ * @return javaDocs
+ */
+ private static String generateForSetters(String attribute) {
+ return (UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_SETTERS + attribute
+ + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ + UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
+ + attribute + UtilConstants.SPACE + attribute + UtilConstants.NEW_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN + UtilConstants.BUILDER_OBJECT
+ + attribute + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generate javaDocs for the impl class.
+ *
+ * @param className class name
+ * @return javaDocs.
+ */
+ private static String generateForImplClass(String className) {
+ return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.IMPL_CLASS_JAVA_DOC + className + UtilConstants.PERIOD
+ + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generate javaDocs for the builder class.
+ *
+ * @param className class name
+ * @return javaDocs.
+ */
+ private static String generateForBuilderClass(String className) {
+ return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC
+ + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generate javaDoc for the interface.
+ *
+ * @param interfaceName interface name
+ * @return javaDocs.
+ */
+ private static String generateForInterface(String interfaceName) {
+ return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC
+ + interfaceName + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generate javaDoc for the builder interface.
+ *
+ * @param builderforName builder for name
+ * @return javaDocs.
+ */
+ private static String generateForBuilderInterface(String builderforName) {
+ return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_INTERFACE_JAVA_DOC + builderforName
+ + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generate javaDocs for package-info.
+ *
+ * @param packageName package name
+ * @return javaDocs
+ */
+ private static String generateForPackage(String packageName) {
+ return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.PACKAGE_INFO_JAVADOC + packageName
+ + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generate javaDocs for default constructor.
+ *
+ * @return javaDocs
+ */
+ private static String generateForDefaultConstructors() {
+ return (UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generate javaDocs for constructor with parameters.
+ *
+ * @param params list of parameters
+ * @param className class name
+ * @return javaDocs
+ */
+ private static String generateForConstructors(String className) {
+ return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ + UtilConstants.JAVA_DOC_SETTERS + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
+ + (className.substring(0, 1).toLowerCase() + className.substring(1)) + UtilConstants.OBJECT
+ + UtilConstants.SPACE + UtilConstants.BUILDER_OBJECT + UtilConstants.SPACE + className
+ + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE);
+ }
+
+ /**
+ * Generate javaDocs for build.
+ *
+ * @return javaDocs
+ */
+ private static String generateForBuild(String buildName) {
+ return (UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_BUILD + buildName + UtilConstants.PERIOD
+ + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN
+ + UtilConstants.JAVA_DOC_BUILD_RETURN + buildName + UtilConstants.PERIOD + UtilConstants.NEW_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE);
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java
new file mode 100644
index 0000000..d33e1fe
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java
@@ -0,0 +1,203 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.util.List;
+
+/**
+ * Provides storage for serialized data while traversing data model tree for code generation.
+ */
+public final class SerializedDataStore {
+
+ /**
+ * Data Store types.
+ */
+ public static enum SerializedDataStoreType {
+
+ /**
+ * Methods.
+ */
+ INTERFACE_METHODS,
+
+ /**
+ * Methods.
+ */
+ BUILDER_METHODS,
+
+ /**
+ * Methods.
+ */
+ BUILDER_INTERFACE_METHODS,
+
+ /**
+ * Methods.
+ */
+ IMPL_METHODS,
+
+ /**
+ * Attributes.
+ */
+ ATTRIBUTE,
+
+ /**
+ * Imports.
+ */
+ IMPORT
+ }
+
+ /**
+ * File name string for serialized files of methods.
+ */
+ private static final String INTERFACE_METHOD_FILE_NAME = "SerializedInterfaceMethodDataStore";
+
+ /**
+ * File name string for serialized files of methods.
+ */
+ private static final String BUILDER_METHOD_FILE_NAME = "SerializedBuilderMethodDataStore";
+
+ /**
+ * File name string for serialized files of methods.
+ */
+ private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "SerializedBuilderInterfaceMethodDataStore";
+
+ /**
+ * File name string for serialized files of methods.
+ */
+ private static final String IMPL_METHOD_FILE_NAME = "SerializedImplMethodDataStore";
+
+ /**
+ * File name string for serialized files of attributes.
+ */
+ private static final String ATTRIBUTE_FILE_NAME = "SerializedAttributeDataStore";
+
+ /**
+ * File name string for serialized files of imports.
+ */
+ private static final String IMPORT_FILE_NAME = "SerializedImportDataStore";
+
+ /**
+ * File extension of serialized files.
+ */
+ private static final String SERIALIZE_FILE_EXTENSION = ".ser";
+
+ /**
+ * Buffer size.
+ */
+ private static final int BUFFER_SIZE = 8 * 1024;
+
+ /**
+ * Default constructor.
+ */
+ private SerializedDataStore() {
+ }
+
+ /**
+ * Writes specific info to a serialized file.
+ *
+ * @param data data to be stored
+ * @param type type of serialized data store
+ * @throws IOException when fails to create a serialized data file.
+ */
+ public static void setSerializeData(String data, SerializedDataStoreType type) throws IOException {
+
+ String fileName = "";
+ if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
+ fileName = ATTRIBUTE_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
+ fileName = INTERFACE_METHOD_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
+ fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
+ fileName = BUILDER_METHOD_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
+ fileName = IMPL_METHOD_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.IMPORT)) {
+ fileName = IMPORT_FILE_NAME;
+ } else {
+ throw new IOException("Unresolved file type.");
+ }
+
+ try {
+ OutputStream file = new FileOutputStream(fileName + SERIALIZE_FILE_EXTENSION);
+ OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
+
+ ObjectOutput output = new ObjectOutputStream(buffer);
+ try {
+ output.writeObject(data);
+ } finally {
+ output.close();
+ }
+ } catch (IOException ex) {
+ throw new IOException("failed to serialize data");
+ }
+ }
+
+ /**
+ * Get the serialized data.
+ *
+ * @param type type of serialized data store
+ * @return list of attribute info.
+ * @throws IOException when fails to read from the file.
+ * @throws ClassNotFoundException when file is missing.
+ */
+ @SuppressWarnings("unchecked")
+ public static List<String> getSerializeData(SerializedDataStoreType type)
+ throws IOException, ClassNotFoundException {
+
+ String fileName = "";
+ if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
+ fileName = ATTRIBUTE_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
+ fileName = INTERFACE_METHOD_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
+ fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
+ fileName = BUILDER_METHOD_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
+ fileName = IMPL_METHOD_FILE_NAME;
+ } else if (type.equals(SerializedDataStoreType.IMPORT)) {
+ fileName = IMPORT_FILE_NAME;
+ } else {
+ throw new IOException("Unresolved file type.");
+ }
+
+ try {
+ InputStream file = new FileInputStream(fileName + SERIALIZE_FILE_EXTENSION);
+ InputStream buffer = new BufferedInputStream(file);
+ ObjectInput input = new ObjectInputStream(buffer);
+ try {
+ List<String> recoveredData = (List<String>) input.readObject();
+ return recoveredData;
+ } finally {
+ input.close();
+ }
+ } catch (ClassNotFoundException ex) {
+ throw new ClassNotFoundException("failed to fetch the serialized data file.");
+ }
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
index ca5da57..a9006d2 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
@@ -34,14 +34,39 @@
}
/**
- * Returns the list of yang files.
+ * Returns the list of YANG files.
*
* @param root specified directory
- * @return list of yang files.
- * @throws IOException when files get deleted while performing the operations.
+ * @return list of YANG files.
+ * @throws IOException when files get deleted while performing the
+ * operations.
*/
public static List<String> getYangFiles(String root) throws IOException {
+ return getFiles(root, ".yang");
+ }
+ /**
+ * Returns the list of java files.
+ *
+ * @param root specified directory
+ * @return list of java files.
+ * @throws IOException when files get deleted while performing the
+ * operations.
+ */
+ public static List<String> getJavaFiles(String root) throws IOException {
+ return getFiles(root, ".java");
+ }
+
+ /**
+ * Returns the list of required files.
+ *
+ * @param root specified directory
+ * @param extension file extension.
+ * @return list of required files.
+ * @throws IOException when files get deleted while performing the
+ * operations.
+ */
+ public static List<String> getFiles(String root, String extension) throws IOException {
List<String> store = new LinkedList<>();
Stack<String> stack = new Stack<>();
stack.push(root);
@@ -60,7 +85,7 @@
stack.push(current.toString());
} else {
String yangFile = current.getCanonicalPath();
- if (yangFile.endsWith(".yang")) {
+ if (yangFile.endsWith(extension)) {
store.add(yangFile);
}
}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
new file mode 100644
index 0000000..af83de4
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+
+import org.sonatype.plexus.build.incremental.BuildContext;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.model.Resource;
+
+import org.onosproject.yangutils.utils.UtilConstants;
+
+import static org.slf4j.LoggerFactory.getLogger;
+import org.slf4j.Logger;
+
+/**
+ * Provides common utility functionalities for code generation.
+ */
+public final class YangIoUtils {
+
+ private static final Logger log = getLogger(YangIoUtils.class);
+
+ /**
+ * Default constructor.
+ */
+ private YangIoUtils() {
+ }
+
+ /**
+ * Creates the directory structure.
+ *
+ * @param path directory path
+ * @return directory structure
+ */
+ public static File createDirectories(String path) {
+
+ File generatedDir = new File(UtilConstants.YANG_GEN_DIR + File.separator + path);
+ generatedDir.mkdirs();
+ return generatedDir;
+ }
+
+ /**
+ * Adds package info file for the created directory.
+ *
+ * @param path directory path
+ * @param classInfo class info for the package
+ * @param pack package of the directory
+ * @throws IOException when fails to create package info file.
+ */
+ public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
+
+ try {
+
+ File packageInfo = new File(path + File.separator + "package-info.java");
+ packageInfo.createNewFile();
+ if (packageInfo.exists()) {
+
+ FileWriter fileWriter = null;
+ BufferedWriter bufferedWriter = null;
+ fileWriter = new FileWriter(packageInfo);
+ bufferedWriter = new BufferedWriter(fileWriter);
+ bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
+ bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo));
+ bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
+ bufferedWriter.close();
+ }
+ } catch (IOException e) {
+ throw new IOException("Exception occured while creating package info file.");
+ }
+ }
+
+ /**
+ * Cleans the generated directory if already exist in source folder.
+ *
+ * @param baseDir generated directory in previous build.
+ */
+ public static void clean(String baseDir) {
+ File generatedDirectory = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR);
+ if (generatedDirectory.exists()) {
+ List<String> javafiles;
+ try {
+ javafiles = YangFileScanner.getJavaFiles(generatedDirectory.toString());
+ for (String file : javafiles) {
+ File currentFile = new File(file);
+ currentFile.delete();
+ }
+ } catch (IOException e) {
+ log.info("Failed to delete the generated files in " + generatedDirectory + " directory");
+ }
+ generatedDirectory.delete();
+ }
+ }
+
+ /**
+ * Adds generated source directory to the compilation root.
+ *
+ * @param source directory
+ * @param project current maven project
+ * @param context current build context
+ */
+ public static void addToSource(String source, MavenProject project, BuildContext context) {
+
+ project.addCompileSourceRoot(source);
+ Resource rsc = new Resource();
+ rsc.setDirectory(source);
+ project.addResource(rsc);
+ context.refresh(project.getBasedir());
+ log.info("Source directory added to compilation root: " + source);
+ }
+
+}