blob: f02ed1ad1b01de87fb3c0ec47a46ac13cfaf15e2 [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 /**
VinodKumarS-Huawei618fdfa2016-11-14 11:50:48 +053050 * 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 Rama13960652016-04-26 15:06:06 +053057 * Creates an object for YANG plugin config.
58 */
59 public YangPluginConfig() {
60 }
61
62 /**
Shankara-Huaweia1039e52016-07-14 16:53:09 +053063 * Returns the string for code generation.
64 *
65 * @return returns the string for code generation.
66 */
Bharat saraswale50edca2016-08-05 01:58:25 +053067 public String getCodeGenerateForSbi() {
68 return codeGenerateForSbi;
Shankara-Huaweia1039e52016-07-14 16:53:09 +053069 }
70
71 /**
72 * Sets the string sbi or nbi for code generation.
73 *
Bharat saraswale50edca2016-08-05 01:58:25 +053074 * @param codeGenerateForSbi generation is for sbi
Shankara-Huaweia1039e52016-07-14 16:53:09 +053075 */
Bharat saraswale50edca2016-08-05 01:58:25 +053076 public void setCodeGenerateForSbi(String codeGenerateForSbi) {
77 this.codeGenerateForSbi = codeGenerateForSbi;
Shankara-Huaweia1039e52016-07-14 16:53:09 +053078 }
79
80 /**
janani b1c6acc42016-04-15 16:18:30 +053081 * 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 saraswald14cbe82016-07-14 13:26:18 +0530115
VinodKumarS-Huawei618fdfa2016-11-14 11:50:48 +0530116
117 public String resourceGenDir() {
118 return resourceGenDir;
119 }
120
121 public void resourceGenDir(String resourceGenDir) {
122 this.resourceGenDir = resourceGenDir;
123 }
124
Bharat saraswal0663aff2016-10-18 23:16:14 +0530125 /**
VinodKumarS-Huawei618fdfa2016-11-14 11:50:48 +0530126 * TODO: delete me, it is not part of config, it needs to be updated for
127 * test scripts
Bharat saraswal0663aff2016-10-18 23:16:14 +0530128 * 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 b1c6acc42016-04-15 16:18:30 +0530154}