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