Merge "Policy Model Distribution POC Issue-ID: DCAEGEN2-1868"
[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
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
42  * Application: DCAE/ONAP - Blueprint Generator
43  * Common Module: Used by both ONAp and DCAE Blueprint Applications
44  * Service: For Blueprint Quotes Fixes
45  */
46
47 @Service
48 public class FixesService {
49
50  public void fixDcaeSingleQuotes(File file) {
51   List<String> lines = new ArrayList<>();
52   try {
53  FileReader fr = new FileReader(file);
54  BufferedReader br = new BufferedReader(fr);
55  for (String line = br.readLine(); line != null; line = br.readLine()){
56   if(line.contains("'")) {
57    line = line.replaceAll("'\\{", "{");
58    line = line.replaceAll("}'", "}");
59    line = line.replaceAll("'\\[", "[");
60    line = line.replaceAll("]'", "]");
61    line = line.replaceAll("'''''", "'");
62    line = line.replaceAll("'''", "'");
63    line = line.replaceAll("'''", "");
64    line = line.replaceAll("''\\{", "'{");
65    line = line.replaceAll("}''", "}'");
66    line = line.replaceAll("''\\[", "'[");
67    line = line.replaceAll("]''", "]'");
68    line = line.replaceAll("\"''", "'");
69    line = line.replaceAll("''\"", "'");
70   }
71   if(line.contains("get_input") || line.contains("get_secret") || line.contains("envs"))
72    line = line.replaceAll("'", "");
73
74   lines.add(line);
75  }
76
77  fr.close();
78  br.close();
79
80  FileWriter fw = new FileWriter(file);
81  PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
82  for(String s: lines) {
83   out.println();
84   out.write(s);
85   out.flush();
86  }
87  out.close();
88  fw.close();
89   } catch (Exception e) {
90  throw new FixesException("Unable to Fix Single Quotes in Final DCAE Blueprint", e);
91   }
92  }
93
94  public String fixStringQuotes(String string) {
95   String sLines[] = string.split("\n");
96   String ret = "";
97   for(String line: sLines) {
98  if(line.contains("get_input") || line.contains("get_secret") || ((line.contains("concat") || line.contains("default: ") || line.contains("description") || line.contains("dmaap") || line.contains(".\"'")) && line.contains("'")))
99   line = line.replaceAll("'", "");
100
101  if(line.contains("'")) {
102   line = line.replaceAll("'\\{", "{");
103   line = line.replaceAll("}'", "}");
104   line = line.replaceAll("'\\[", "[");
105   line = line.replaceAll("]'", "]");
106   line = line.replaceAll("'''''", "'");
107   line = line.replaceAll("'''", "'");
108   line = line.replaceAll("'''", "");
109   line = line.replaceAll("''\\{", "'{");
110   line = line.replaceAll("}''", "}'");
111   line = line.replaceAll("''\\[", "'[");
112   line = line.replaceAll("]''", "]'");
113   line = line.replaceAll("\"''", "'");
114   line = line.replaceAll("''\"", "'");
115  }
116  ret = ret + "\n" + line;
117   }
118   return ret;
119  }
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  }
136  fr.close();
137  br.close();
138  FileWriter fw = new FileWriter(file);
139  PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
140  for(String s: lines) {
141   out.println();
142   out.write(s);
143   out.flush();
144  }
145
146  out.close();
147  fw.close();
148
149   } catch (Exception e) {
150  throw new FixesException("Unable to Fix Single Quotes in final ONAP Blueprint", e);
151   }
152  }
153
154  private String ensureNoSingleQuotes(String line) {
155   if ((line.contains("concat") || line.contains("default: ") || line.contains("description") || line.contains("dmaap") || line.contains(".\"'")) && line.contains("'"))
156  return line.replace("'", "");
157   else
158  return line;
159  }
160
161  public String applyFixes(String bp) {
162   List<String> lines = new ArrayList<>();
163   String[] linesPre = bp.split("\n");
164   for (String line : linesPre) {
165  lines.add(ensureNoSingleQuotes(line));
166   }
167   return String.join("\n", lines);
168  }
169  
170 }