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 | /** |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 50 | * Path to generate the resource files, which will be used by the plugin |
| 51 | * to add it as part of the resource, implicitly by the path location, or |
| 52 | * some explicit way to add to bundle. |
| 53 | */ |
| 54 | private String resourceGenDir; |
| 55 | |
| 56 | /** |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 57 | * Creates an object for YANG plugin config. |
| 58 | */ |
| 59 | public YangPluginConfig() { |
| 60 | } |
| 61 | |
| 62 | /** |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 63 | * Returns the string for code generation. |
| 64 | * |
| 65 | * @return returns the string for code generation. |
| 66 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 67 | public String getCodeGenerateForSbi() { |
| 68 | return codeGenerateForSbi; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets the string sbi or nbi for code generation. |
| 73 | * |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 74 | * @param codeGenerateForSbi generation is for sbi |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 75 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 76 | public void setCodeGenerateForSbi(String codeGenerateForSbi) { |
| 77 | this.codeGenerateForSbi = codeGenerateForSbi; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 81 | * Sets the path of the java code where it has to be generated. |
| 82 | * |
| 83 | * @param codeGenDir path of the directory |
| 84 | */ |
| 85 | public void setCodeGenDir(String codeGenDir) { |
| 86 | this.codeGenDir = codeGenDir; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the code generation directory path. |
| 91 | * |
| 92 | * @return code generation directory |
| 93 | */ |
| 94 | public String getCodeGenDir() { |
| 95 | return codeGenDir; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sets the object. |
| 100 | * |
| 101 | * @param conflictResolver object of the class |
| 102 | */ |
| 103 | public void setConflictResolver(YangToJavaNamingConflictUtil conflictResolver) { |
| 104 | this.conflictResolver = conflictResolver; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the object. |
| 109 | * |
| 110 | * @return object of the class |
| 111 | */ |
| 112 | public YangToJavaNamingConflictUtil getConflictResolver() { |
| 113 | return conflictResolver; |
| 114 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 115 | |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 116 | |
| 117 | public String resourceGenDir() { |
| 118 | return resourceGenDir; |
| 119 | } |
| 120 | |
| 121 | public void resourceGenDir(String resourceGenDir) { |
| 122 | this.resourceGenDir = resourceGenDir; |
| 123 | } |
| 124 | |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 125 | /** |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 126 | * TODO: delete me, it is not part of config, it needs to be updated for |
| 127 | * test scripts |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 128 | * Compiles the generated code for unit tests. |
| 129 | * |
| 130 | * @param dir1 directory path |
| 131 | * @throws IOException when generated code has compilation errors. |
| 132 | */ |
| 133 | @SuppressWarnings("unchecked") |
| 134 | public static void compileCode(String dir1) throws IOException { |
| 135 | String classpath = System.getProperty("java.class.path"); |
| 136 | List<String> optionList = new ArrayList<>(); |
| 137 | optionList.addAll(Arrays.asList("-classpath", classpath)); |
| 138 | |
| 139 | List<String> files = getJavaFiles(dir1); |
| 140 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 141 | StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null); |
| 142 | Iterable fileObjects = manager.getJavaFileObjectsFromStrings(files); |
| 143 | JavaCompiler.CompilationTask task = compiler.getTask(null, null, |
| 144 | null, optionList, null, |
| 145 | fileObjects); |
| 146 | |
| 147 | boolean failOnError = !task.call(); |
| 148 | manager.close(); |
| 149 | if (failOnError) { |
| 150 | throw new IOException("Yang Error : compilation errors in " + |
| 151 | "generated code."); |
| 152 | } |
| 153 | } |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 154 | } |