BUG-3861 Detect RPC errors when committing netconf transaction
[controller.git] / opendaylight / config / yang-test-plugin / src / main / java / org / opendaylight / controller / config / yang / test / plugin / ProcessSources.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.yang.test.plugin;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.util.regex.Pattern;
13 import org.apache.commons.io.FileUtils;
14 import org.apache.maven.plugin.AbstractMojo;
15 import org.apache.maven.plugin.MojoExecutionException;
16 import org.apache.maven.plugin.MojoFailureException;
17
18 /**
19  * Add implementation code from stub.txt
20  *
21  * @goal process-sources
22  *
23  * @phase process-sources
24  *
25  */
26 public class ProcessSources extends AbstractMojo{
27     /**
28      * @parameter expression="${project.build.sourceDirectory}"
29      * @readOnly
30      * @required
31      */
32     private File directory;
33
34     @Override
35     public void execute() throws MojoExecutionException, MojoFailureException {
36         if (directory == null || !directory.exists()) {
37             super.getLog().error("Directory does not exists.");
38         }
39         File sourceDirectory = new File(directory.getPath() + Util.replaceDots(".org.opendaylight.controller.config.yang.test.impl"));
40         if (!sourceDirectory.exists()) {
41             super.getLog().error(String.format("Source directory does not exists %s", sourceDirectory.getPath()));
42         }
43
44         File[] sourceFiles = sourceDirectory.listFiles();
45         for (File sourceFile: sourceFiles) {
46             if (sourceFile.getName().endsWith(".java")) {
47                 String sourceContent;
48                 try {
49                     sourceContent = FileUtils.readFileToString(sourceFile);
50                 } catch (IOException e) {
51                     getLog().error(String.format("Cannot read %s", sourceFile.getAbsolutePath()), e);
52                     continue;
53                 }
54                 if (sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) {
55                     File stubFile = new File(sourceFile.getPath().replace(".java", "Stub.txt"));
56                     if (stubFile.exists()) {
57                         String stubContent = null;
58                         try {
59                             stubContent = FileUtils.readFileToString(stubFile);
60                         } catch (IOException e) {
61                             getLog().error(String.format("Cannot read %s", stubFile.getAbsolutePath()), e);
62                         }
63                         if (stubContent != null) {
64                             sourceContent = rewriteStub(sourceContent, stubContent);
65                         }
66                     }
67                 }
68                 // remove copyright headers as they can contain timestamp
69                 sourceContent = removeCopyrights(sourceContent);
70
71                 // replace the file content
72                 try {
73                     FileUtils.write(sourceFile, sourceContent);
74                 } catch (IOException e) {
75                     getLog().error(String.format("Cannot write %s", sourceFile.getAbsolutePath()), e);
76                 }
77             }
78
79         }
80     }
81
82     private static Pattern MULTILINE_COMMENT_PATTERN = Pattern.compile("/\\*.*\\*/", Pattern.MULTILINE | Pattern.DOTALL);
83     private static String removeCopyrights(String source) {
84         String target = MULTILINE_COMMENT_PATTERN.matcher(source).replaceAll("\n");
85         //FileUtils.write(sourceFile, target);
86         return target;
87     }
88
89     private static Pattern UNSUPPORTED_OP_PATTERN = Pattern.compile("^.*TODO.*\n.*throw new java.lang.UnsupportedOperationException.*$", Pattern.MULTILINE);
90
91     private static String rewriteStub(String source, String replaceTODOWith) {
92         String target = UNSUPPORTED_OP_PATTERN.matcher(source).replaceFirst(replaceTODOWith);
93         return target;
94     }
95 }