3 * * ============LICENSE_START=======================================================
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
12 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.blueprintgenerator.service.base;
26 import org.onap.blueprintgenerator.exception.FixesException;
27 import org.springframework.stereotype.Service;
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;
35 import java.util.ArrayList;
36 import java.util.List;
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
44 public class FixesService {
47 * Interface to fix Single Quotes in the generated Blueprint
52 public void fixDcaeSingleQuotes(File file) {
53 List<String> lines = new ArrayList<>();
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);
61 if (line.contains("get_input") || line.contains("get_secret") || line
63 line = line.replaceAll("'", "");
72 FileWriter fw = new FileWriter(file);
73 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
74 for (String s : lines) {
81 } catch (Exception e) {
82 throw new FixesException("Unable to Fix Single Quotes in Final DCAE Blueprint", e);
87 * Interface to fix String Quotes in the generated Blueprint
89 * @param string String
92 public String fixStringQuotes(String string) {
93 String sLines[] = string.split("\n");
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("'", "");
107 if (line.contains("'")) {
108 line = processLine(line);
110 ret = ret + "\n" + line;
116 * Interface to fix Single Quotes in the generated ONAP Blueprint
121 public void fixOnapSingleQuotes(File file) {
122 List<String> lines = new ArrayList<>();
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("'", "");
130 if (line.contains("\"\"") && (line.contains("m") || line.contains("M"))) {
131 line = line.replaceAll("\"\"", "\"");
137 FileWriter fw = new FileWriter(file);
138 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
139 for (String s : lines) {
148 } catch (Exception e) {
149 throw new FixesException("Unable to Fix Single Quotes in final ONAP Blueprint", e);
154 * Interface to fix Single Quotes for given line
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("'", "");
173 * Interface to Applt fixes for Quotes
175 * @param bp Blueprint
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));
184 return String.join("\n", lines);
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("''\"", "'");