blob: 9339e4bb0bad1330a762eb7ea6e7cae7ad3b3cb1 [file] [log] [blame]
Vinod Kumar S7a004de2016-02-05 16:15:09 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S7a004de2016-02-05 16:15:09 +05303 *
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
17package org.onosproject.yangutils.plugin.manager;
18
Bharat saraswal6ef0b762016-04-05 12:45:45 +053019import java.io.IOException;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053020import java.util.HashSet;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053021import java.util.Iterator;
22import java.util.List;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053023import java.util.Set;
Bharat saraswal33dfa012016-05-17 19:59:16 +053024
Bharat saraswal96dfef02016-06-16 00:29:12 +053025import org.apache.maven.artifact.repository.ArtifactRepository;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053026import org.apache.maven.plugin.AbstractMojo;
27import org.apache.maven.plugin.MojoExecutionException;
28import org.apache.maven.plugin.MojoFailureException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053029import org.apache.maven.plugins.annotations.Component;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053030import org.apache.maven.plugins.annotations.Mojo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053031import org.apache.maven.plugins.annotations.Parameter;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053032import org.apache.maven.project.MavenProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053033import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053034import org.onosproject.yangutils.linker.YangLinker;
35import org.onosproject.yangutils.linker.exceptions.LinkerException;
36import org.onosproject.yangutils.linker.impl.YangLinkerManager;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053037import org.onosproject.yangutils.parser.YangUtilsParser;
38import org.onosproject.yangutils.parser.exceptions.ParserException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053039import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
janani bde4ffab2016-04-15 16:18:30 +053040import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
41import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053042import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
Bharat saraswal870c56f2016-02-20 21:57:16 +053043import org.sonatype.plexus.build.incremental.BuildContext;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053044
Bharat saraswale2d51d62016-03-23 19:40:35 +053045import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
46import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
Vinod Kumar S38046502016-03-23 15:30:27 +053047import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053048import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler;
Bharat saraswal2f11f652016-03-25 18:19:46 +053049import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswale2d51d62016-03-23 19:40:35 +053050import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
51import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
52import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswal96dfef02016-06-16 00:29:12 +053053import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToCompilationRoot;
Vinod Kumar S38046502016-03-23 15:30:27 +053054import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053055import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Vinod Kumar S38046502016-03-23 15:30:27 +053056import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
Bharat saraswal96dfef02016-06-16 00:29:12 +053057import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.resolveInterJarDependencies;
58import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.serializeDataModel;
Vinod Kumar S38046502016-03-23 15:30:27 +053059
Vinod Kumar S7a004de2016-02-05 16:15:09 +053060/**
Bharat saraswald9822e92016-04-05 15:13:44 +053061 * Represents ONOS YANG utility maven plugin.
Bharat saraswalef2e6392016-04-19 19:50:32 +053062 * Goal of plugin is yang2java.
63 * Execution phase is generate-sources.
64 * requiresDependencyResolution at compile time.
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053065 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053066@Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE,
Bharat saraswal96dfef02016-06-16 00:29:12 +053067 requiresProject = true)
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053068public class YangUtilManager
69 extends AbstractMojo {
Vinod Kumar S7a004de2016-02-05 16:15:09 +053070
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053071 private YangNode rootNode;
72 // YANG file information set.
73 private Set<YangFileInfo> yangFileInfoSet = new HashSet<>();
74 private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
75 private YangLinker yangLinker = new YangLinkerManager();
Bharat saraswal33dfa012016-05-17 19:59:16 +053076 private YangFileInfo curYangFileInfo = new YangFileInfo();
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053077
78 private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
79
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053080 /**
81 * Source directory for YANG files.
82 */
83 @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang")
84 private String yangFilesDir;
85
86 /**
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053087 * Source directory for generated files.
88 */
89 @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
90 private String genFilesDir;
91
92 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053093 * Base directory for project.
94 */
95 @Parameter(property = "basedir", defaultValue = "${basedir}")
96 private String baseDir;
97
98 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053099 * Output directory.
100 */
101 @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
Vinod Kumar S38046502016-03-23 15:30:27 +0530102 private String outputDirectory;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530103
104 /**
105 * Current maven project.
106 */
107 @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}")
108 private MavenProject project;
109
110 /**
janani bde4ffab2016-04-15 16:18:30 +0530111 * Replacement required for period special character in the identifier.
112 */
113 @Parameter(property = "replacementForPeriod")
114 private String replacementForPeriod;
115
116 /**
117 * Replacement required for underscore special character in the identifier.
118 */
119 @Parameter(property = "replacementForUnderscore")
120 private String replacementForUnderscore;
121
122 /**
123 * Replacement required for hyphen special character in the identifier.
124 */
125 @Parameter(property = "replacementForHyphen")
126 private String replacementForHyphen;
127
128 /**
janani bdd1314f2016-05-19 17:39:50 +0530129 * Prefix which is required for adding with the identifier.
130 */
131 @Parameter(property = "prefixForIdentifier")
132 private String prefixForIdentifier;
133
134 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530135 * Build context.
136 */
137 @Component
138 private BuildContext context;
139
Bharat saraswal96dfef02016-06-16 00:29:12 +0530140 @Parameter(readonly = true, defaultValue = "${localRepository}")
141 private ArtifactRepository localRepository;
142
143 @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}")
144 private List<ArtifactRepository> remoteRepository;
145
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530146 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530147 public void execute()
148 throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530149
150 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530151
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530152 /*
Bharat saraswal870c56f2016-02-20 21:57:16 +0530153 * For deleting the generated code in previous build.
154 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530155 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
156 deleteDirectory(getDirectory(baseDir, outputDirectory));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530157
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530158 String searchDir = getDirectory(baseDir, yangFilesDir);
159 String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530160
161 // Creates conflict resolver and set values to it.
janani bde4ffab2016-04-15 16:18:30 +0530162 YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
163 conflictResolver.setReplacementForPeriod(replacementForPeriod);
164 conflictResolver.setReplacementForHyphen(replacementForHyphen);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530165 conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
janani bdd1314f2016-05-19 17:39:50 +0530166 conflictResolver.setPrefixForIdentifier(prefixForIdentifier);
janani bde4ffab2016-04-15 16:18:30 +0530167 YangPluginConfig yangPlugin = new YangPluginConfig();
168 yangPlugin.setCodeGenDir(codeGenDir);
169 yangPlugin.setConflictResolver(conflictResolver);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530170
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530171 /*
172 * Obtain the YANG files at a path mentioned in plugin and creates
173 * YANG file information set.
174 */
175 createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530176
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530177 // Check if there are any file to translate, if not return.
178 if (getYangFileInfoSet() == null || getYangFileInfoSet().isEmpty()) {
179 // No files to translate
180 return;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530181 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530182 // Resolve inter jar dependency.
183 resolveInterJardependency();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530184
Bharat saraswal33dfa012016-05-17 19:59:16 +0530185 // Carry out the parsing for all the YANG files.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530186 parseYangFileInfoSet();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530187
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530188 // Resolve dependencies using linker.
189 resolveDependenciesUsingLinker();
190
191 // Perform translation to JAVA.
192 translateToJava(getYangFileInfoSet(), yangPlugin);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530193
Bharat saraswal96dfef02016-06-16 00:29:12 +0530194 // Serialize data model.
195 serializeDataModel(getDirectory(baseDir, outputDirectory), getYangFileInfoSet(), project, true);
196
197 addToCompilationRoot(getDirectory(baseDir, genFilesDir), project, context);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530198
199 copyYangFilesToTarget(getYangFileInfoSet(), getDirectory(baseDir, outputDirectory), project);
janani bdd1314f2016-05-19 17:39:50 +0530200 } catch (IOException | ParserException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530201 getLog().info(e);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530202 String fileName = "";
Bharat saraswal33dfa012016-05-17 19:59:16 +0530203 if (getCurYangFileInfo() != null) {
204 fileName = getCurYangFileInfo().getYangFileName();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530205 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530206 try {
207 translatorErrorHandler(getRootNode());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530208 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530209 } catch (IOException ex) {
210 throw new MojoExecutionException(
211 "Error handler failed to delete files for data model node.");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530212 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530213 throw new MojoExecutionException(
214 "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName
215 + " YANG file.");
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530216 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530217 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530218
219 /**
Bharat saraswal96dfef02016-06-16 00:29:12 +0530220 * Resolved inter-jar dependencies.
221 *
222 * @throws IOException when fails to do IO operations
223 */
224 public void resolveInterJardependency() throws IOException {
225 try {
226 List<YangNode> interJarResolvedNodes = resolveInterJarDependencies(project, localRepository,
227 remoteRepository, getDirectory(baseDir, outputDirectory));
228 for (YangNode node : interJarResolvedNodes) {
229 YangFileInfo dependentFileInfo = new YangFileInfo();
230 dependentFileInfo.setRootNode(node);
231 dependentFileInfo.setForTranslator(false);
232 dependentFileInfo.setYangFileName(node.getName());
233 getYangFileInfoSet().add(dependentFileInfo);
234 }
235 } catch (IOException e) {
236 throw new IOException("failed to resolve in interjar scenario.");
237 }
238 }
239
240 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530241 * Links all the provided with the YANG file info set.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530242 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530243 * @throws MojoExecutionException a violation in mojo excecution
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530244 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530245 public void resolveDependenciesUsingLinker()
246 throws MojoExecutionException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530247 for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530248 setCurYangFileInfo(yangFileInfo);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530249 try {
250 yangLinker.resolveDependencies(getYangFileInfoSet());
251 } catch (LinkerException e) {
252 throw new MojoExecutionException(e.getMessage());
253 }
254 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530255 }
256
257 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530258 * Parses all the provided YANG files and generates YANG data model tree.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530259 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530260 * @throws IOException a violation in IO
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530261 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530262 public void parseYangFileInfoSet()
263 throws IOException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530264 for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530265 setCurYangFileInfo(yangFileInfo);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530266 if (yangFileInfo.isForTranslator()) {
267 try {
268 YangNode yangNode = yangUtilsParser.getDataModel(yangFileInfo.getYangFileName());
269 yangFileInfo.setRootNode(yangNode);
270 setRootNode(yangNode);
271 } catch (ParserException e) {
272 String logInfo = "Error in file: " + e.getFileName();
273 if (e.getLineNumber() != 0) {
274 logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
275 + e.getCharPositionInLine();
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530276
Bharat saraswal96dfef02016-06-16 00:29:12 +0530277 }
278 if (e.getMessage() != null) {
279 logInfo = logInfo + NEW_LINE + e.getMessage();
280 }
281 getLog().info(logInfo);
282 throw e;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530283 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530284 }
285 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530286 }
287
288 /**
289 * Returns current root YANG node of data-model tree.
290 *
291 * @return current root YANG node of data-model tree
292 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530293 private YangNode getRootNode() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530294 return rootNode;
295 }
296
297 /**
298 * Sets current root YANG node of data-model tree.
299 *
300 * @param rootNode current root YANG node of data-model tree
301 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530302 private void setRootNode(YangNode rootNode) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530303 this.rootNode = rootNode;
304 }
305
Vidyashree Rama1db15562016-05-17 16:16:15 +0530306 /**
307 * Translates to java code corresponding to the YANG schema.
308 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530309 * @param yangFileInfoSet YANG file information
310 * @param yangPlugin YANG plugin config
Vidyashree Rama1db15562016-05-17 16:16:15 +0530311 * @throws IOException when fails to generate java code file the current
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530312 * node
Vidyashree Rama1db15562016-05-17 16:16:15 +0530313 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530314 public void translateToJava(Set<YangFileInfo> yangFileInfoSet, YangPluginConfig yangPlugin)
Vidyashree Rama1db15562016-05-17 16:16:15 +0530315 throws IOException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530316 Iterator<YangFileInfo> yangFileIterator = yangFileInfoSet.iterator();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530317 while (yangFileIterator.hasNext()) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530318 YangFileInfo yangFileInfo = yangFileIterator.next();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530319 setCurYangFileInfo(yangFileInfo);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530320 if (yangFileInfo.isForTranslator()) {
321 generateJavaCode(yangFileInfo.getRootNode(), yangPlugin);
322 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530323 }
324 }
325
326 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530327 * Creates a YANG file info set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530328 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530329 * @param yangFileList YANG files list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530330 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530331 public void createYangFileInfoSet(List<String> yangFileList) {
332 for (String yangFile : yangFileList) {
333 YangFileInfo yangFileInfo = new YangFileInfo();
334 yangFileInfo.setYangFileName(yangFile);
335 getYangFileInfoSet().add(yangFileInfo);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530336 }
337 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530338
339 /**
340 * Returns the YANG file info set.
341 *
342 * @return the YANG file info set
343 */
344 public Set<YangFileInfo> getYangFileInfoSet() {
345 return yangFileInfoSet;
346 }
347
348 /**
349 * Sets the YANG file info set.
350 *
351 * @param yangFileInfoSet the YANG file info set
352 */
353 public void setYangFileInfoSet(Set<YangFileInfo> yangFileInfoSet) {
354 this.yangFileInfoSet = yangFileInfoSet;
355 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530356
357 /**
358 * Returns current YANG file's info.
359 *
360 * @return the yangFileInfo
361 */
362 public YangFileInfo getCurYangFileInfo() {
363 return curYangFileInfo;
364 }
365
366 /**
367 * Sets current YANG file's info.
368 *
369 * @param yangFileInfo the yangFileInfo to set
370 */
371 public void setCurYangFileInfo(YangFileInfo yangFileInfo) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530372 curYangFileInfo = yangFileInfo;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530373 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530374}