blob: 86e4500c20b464ee64019dcf1af0bdd293b14cce [file] [log] [blame]
janani b1c6acc42016-04-15 16:18:30 +05301/*
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 saraswale50edca2016-08-05 01:58:25 +053017package org.onosproject.yangutils.utils.io;
janani b1c6acc42016-04-15 16:18:30 +053018
Bharat saraswal0663aff2016-10-18 23:16:14 +053019import javax.tools.JavaCompiler;
20import javax.tools.StandardJavaFileManager;
21import javax.tools.ToolProvider;
22import java.io.IOException;
23import java.util.ArrayList;
24import java.util.Arrays;
25import java.util.List;
26
27import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getJavaFiles;
28
janani b1c6acc42016-04-15 16:18:30 +053029/**
30 * Representation of plugin configurations required for YANG utils.
31 */
32public final class YangPluginConfig {
33
34 /**
janani b1c6acc42016-04-15 16:18:30 +053035 * 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 saraswal8beac342016-08-04 02:00:03 +053045 * Java code generation is for sbi.
46 */
Bharat saraswale50edca2016-08-05 01:58:25 +053047 private String codeGenerateForSbi;
Bharat saraswal8beac342016-08-04 02:00:03 +053048
49 /**
Vidyashree Rama13960652016-04-26 15:06:06 +053050 * Creates an object for YANG plugin config.
51 */
52 public YangPluginConfig() {
53 }
54
55 /**
Shankara-Huaweia1039e52016-07-14 16:53:09 +053056 * Returns the string for code generation.
57 *
58 * @return returns the string for code generation.
59 */
Bharat saraswale50edca2016-08-05 01:58:25 +053060 public String getCodeGenerateForSbi() {
61 return codeGenerateForSbi;
Shankara-Huaweia1039e52016-07-14 16:53:09 +053062 }
63
64 /**
65 * Sets the string sbi or nbi for code generation.
66 *
Bharat saraswale50edca2016-08-05 01:58:25 +053067 * @param codeGenerateForSbi generation is for sbi
Shankara-Huaweia1039e52016-07-14 16:53:09 +053068 */
Bharat saraswale50edca2016-08-05 01:58:25 +053069 public void setCodeGenerateForSbi(String codeGenerateForSbi) {
70 this.codeGenerateForSbi = codeGenerateForSbi;
Shankara-Huaweia1039e52016-07-14 16:53:09 +053071 }
72
73 /**
janani b1c6acc42016-04-15 16:18:30 +053074 * 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 saraswald14cbe82016-07-14 13:26:18 +0530108
Bharat saraswal0663aff2016-10-18 23:16:14 +0530109 /**
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 b1c6acc42016-04-15 16:18:30 +0530136}