janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 17 | package org.onosproject.yangutils.utils.io; |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 18 | |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame^] | 19 | import javax.tools.JavaCompiler; |
| 20 | import javax.tools.StandardJavaFileManager; |
| 21 | import javax.tools.ToolProvider; |
| 22 | import java.io.IOException; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Arrays; |
| 25 | import java.util.List; |
| 26 | |
| 27 | import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getJavaFiles; |
| 28 | |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 29 | /** |
| 30 | * Representation of plugin configurations required for YANG utils. |
| 31 | */ |
| 32 | public final class YangPluginConfig { |
| 33 | |
| 34 | /** |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 35 | * Contains the code generation directory. |
| 36 | */ |
| 37 | private String codeGenDir; |
| 38 | |
| 39 | /** |
| 40 | * Contains information of naming conflicts that can be resolved. |
| 41 | */ |
| 42 | private YangToJavaNamingConflictUtil conflictResolver; |
| 43 | |
| 44 | /** |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 45 | * Java code generation is for sbi. |
| 46 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 47 | private String codeGenerateForSbi; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 48 | |
| 49 | /** |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 50 | * Creates an object for YANG plugin config. |
| 51 | */ |
| 52 | public YangPluginConfig() { |
| 53 | } |
| 54 | |
| 55 | /** |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 56 | * Returns the string for code generation. |
| 57 | * |
| 58 | * @return returns the string for code generation. |
| 59 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 60 | public String getCodeGenerateForSbi() { |
| 61 | return codeGenerateForSbi; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Sets the string sbi or nbi for code generation. |
| 66 | * |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 67 | * @param codeGenerateForSbi generation is for sbi |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 68 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 69 | public void setCodeGenerateForSbi(String codeGenerateForSbi) { |
| 70 | this.codeGenerateForSbi = codeGenerateForSbi; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 74 | * Sets the path of the java code where it has to be generated. |
| 75 | * |
| 76 | * @param codeGenDir path of the directory |
| 77 | */ |
| 78 | public void setCodeGenDir(String codeGenDir) { |
| 79 | this.codeGenDir = codeGenDir; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the code generation directory path. |
| 84 | * |
| 85 | * @return code generation directory |
| 86 | */ |
| 87 | public String getCodeGenDir() { |
| 88 | return codeGenDir; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sets the object. |
| 93 | * |
| 94 | * @param conflictResolver object of the class |
| 95 | */ |
| 96 | public void setConflictResolver(YangToJavaNamingConflictUtil conflictResolver) { |
| 97 | this.conflictResolver = conflictResolver; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the object. |
| 102 | * |
| 103 | * @return object of the class |
| 104 | */ |
| 105 | public YangToJavaNamingConflictUtil getConflictResolver() { |
| 106 | return conflictResolver; |
| 107 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 108 | |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame^] | 109 | /** |
| 110 | * Compiles the generated code for unit tests. |
| 111 | * |
| 112 | * @param dir1 directory path |
| 113 | * @throws IOException when generated code has compilation errors. |
| 114 | */ |
| 115 | @SuppressWarnings("unchecked") |
| 116 | public static void compileCode(String dir1) throws IOException { |
| 117 | String classpath = System.getProperty("java.class.path"); |
| 118 | List<String> optionList = new ArrayList<>(); |
| 119 | optionList.addAll(Arrays.asList("-classpath", classpath)); |
| 120 | |
| 121 | List<String> files = getJavaFiles(dir1); |
| 122 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 123 | StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null); |
| 124 | Iterable fileObjects = manager.getJavaFileObjectsFromStrings(files); |
| 125 | JavaCompiler.CompilationTask task = compiler.getTask(null, null, |
| 126 | null, optionList, null, |
| 127 | fileObjects); |
| 128 | |
| 129 | boolean failOnError = !task.call(); |
| 130 | manager.close(); |
| 131 | if (failOnError) { |
| 132 | throw new IOException("Yang Error : compilation errors in " + |
| 133 | "generated code."); |
| 134 | } |
| 135 | } |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 136 | } |