Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / common / src / main / java / org / onap / blueprintgenerator / service / base / FixesService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
7  *  *  Copyright (c) 2021 Nokia. All rights reserved.
8  *  *  ================================================================================
9  *  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  *  you may not use this file except in compliance with the License.
11  *  *  You may obtain a copy of the License at
12  *  *
13  *  *   http://www.apache.org/licenses/LICENSE-2.0
14  *  *
15  *  *  Unless required by applicable law or agreed to in writing, software
16  *  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  *  See the License for the specific language governing permissions and
19  *  *  limitations under the License.
20  *  *  ============LICENSE_END=========================================================
21  *
22  *
23  */
24
25 package org.onap.blueprintgenerator.service.base;
26
27 import org.onap.blueprintgenerator.exception.FixesException;
28 import org.springframework.stereotype.Service;
29
30 import java.io.BufferedReader;
31 import java.io.BufferedWriter;
32 import java.io.FileWriter;
33 import java.io.PrintWriter;
34 import java.io.FileReader;
35 import java.io.File;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40  * @author : Ravi Mantena
41  * @date 10/16/2020 Application: DCAE/ONAP - Blueprint Generator Common Module: Used by both ONAp
42  * and DCAE Blueprint Applications Service: For Blueprint Quotes Fixes
43  */
44 @Service
45 public class FixesService {
46
47     /**
48      * Interface to fix Single Quotes in the generated Blueprint
49      *
50      * @param file File
51      * @return
52      */
53     public void fixDcaeSingleQuotes(File file) {
54         List<String> lines = new ArrayList<>();
55         try {
56             FileReader fr = new FileReader(file);
57             BufferedReader br = new BufferedReader(fr);
58             for (String line = br.readLine(); line != null; line = br.readLine()) {
59                 String newLine = line;
60                 if (newLine.contains("'")) {
61                     newLine = processLine(newLine);
62                 }
63                 if (newLine.contains("get_input") || newLine.contains("get_secret") || newLine
64                     .contains("envs")) {
65                     newLine = newLine.replace("'", "");
66                 }
67                 lines.add(newLine);
68             }
69
70             fr.close();
71             br.close();
72
73             FileWriter fw = new FileWriter(file);
74             PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
75             for (String s : lines) {
76                 out.println();
77                 out.write(s);
78                 out.flush();
79             }
80             out.close();
81             fw.close();
82         } catch (Exception e) {
83             throw new FixesException("Unable to Fix Single Quotes in Final DCAE Blueprint", e);
84         }
85     }
86
87     /**
88      * Interface to fix String Quotes in the generated Blueprint
89      *
90      * @param string String
91      * @return
92      */
93     public String fixStringQuotes(String string) {
94         String[] sLines = string.split("\n");
95         StringBuilder ret = new StringBuilder();
96         for (String line : sLines) {
97             if (line.contains("get_input")
98                 || line.contains("get_secret")
99                 || ((line.contains("concat")
100                 || line.contains("default: ")
101                 || line.contains("description")
102                 || line.contains("dmaap")
103                 || line.contains(".\"'"))
104                 && line.contains("'"))) {
105                 line = line.replace("'", "");
106             }
107
108             if (line.contains("'")) {
109                 line = processLine(line);
110             }
111             ret.append("\n");
112             ret.append(line);
113         }
114         return ret.toString();
115     }
116
117     /**
118      * Interface to fix Single Quotes in the generated ONAP Blueprint
119      *
120      * @param file File
121      * @return
122      */
123     public void fixOnapSingleQuotes(File file) {
124         List<String> lines = new ArrayList<>();
125         try {
126             FileReader fr = new FileReader(file);
127             BufferedReader br = new BufferedReader(fr);
128             for (String line = br.readLine(); line != null; line = br.readLine()) {
129                 String newLine = line;
130                 if (newLine.contains("'")) {
131                     newLine = newLine.replace("'", "");
132                 }
133                 if (newLine.contains("\"\"") && (newLine.contains("m") || newLine.contains("M"))) {
134                     newLine = newLine.replace("\"\"", "\"");
135                 }
136                 lines.add(newLine);
137             }
138             fr.close();
139             br.close();
140             FileWriter fw = new FileWriter(file);
141             PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
142             for (String s : lines) {
143                 out.println();
144                 out.write(s);
145                 out.flush();
146             }
147
148             out.close();
149             fw.close();
150
151         } catch (Exception e) {
152             throw new FixesException("Unable to Fix Single Quotes in final ONAP Blueprint", e);
153         }
154     }
155
156     /**
157      * Interface to fix Single Quotes for given line
158      *
159      * @param line Line
160      * @return
161      */
162     private String ensureNoSingleQuotes(String line) {
163         if ((line.contains("concat")
164             || line.contains("default: ")
165             || line.contains("description")
166             || line.contains("dmaap")
167             || line.contains(".\"'"))
168             && line.contains("'")) {
169             return line.replace("'", "");
170         } else {
171             return line;
172         }
173     }
174
175     /**
176      * Interface to Applt fixes for Quotes
177      *
178      * @param bp Blueprint
179      * @return
180      */
181     public String applyFixes(String bp) {
182         List<String> lines = new ArrayList<>();
183         String[] linesPre = bp.split("\n");
184         for (String line : linesPre) {
185             lines.add(ensureNoSingleQuotes(line));
186         }
187         return String.join("\n", lines);
188     }
189
190     private String processLine(String line) {
191         return line.replace("'\\{", "{")
192             .replace("}'", "}")
193             .replace("'\\[", "[")
194             .replace("]'", "]")
195             .replace("'''''", "'")
196             .replace("'''", "'")
197             .replace("'''", "")
198             .replace("''\\{", "'{")
199             .replace("}''", "}'")
200             .replace("''\\[", "'[")
201             .replace("]''", "]'")
202             .replace("\"''", "'")
203             .replace("''\"", "'");
204     }
205 }