3 * * ============LICENSE_START=======================================================
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
13 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
25 package org.onap.blueprintgenerator.service.base;
27 import org.onap.blueprintgenerator.exception.FixesException;
28 import org.springframework.stereotype.Service;
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;
36 import java.util.ArrayList;
37 import java.util.List;
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
45 public class FixesService {
48 * Interface to fix Single Quotes in the generated Blueprint
53 public void fixDcaeSingleQuotes(File file) {
54 List<String> lines = new ArrayList<>();
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);
63 if (newLine.contains("get_input") || newLine.contains("get_secret") || newLine
65 newLine = newLine.replace("'", "");
73 FileWriter fw = new FileWriter(file);
74 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
75 for (String s : lines) {
82 } catch (Exception e) {
83 throw new FixesException("Unable to Fix Single Quotes in Final DCAE Blueprint", e);
88 * Interface to fix String Quotes in the generated Blueprint
90 * @param string String
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("'", "");
108 if (line.contains("'")) {
109 line = processLine(line);
114 return ret.toString();
118 * Interface to fix Single Quotes in the generated ONAP Blueprint
123 public void fixOnapSingleQuotes(File file) {
124 List<String> lines = new ArrayList<>();
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("'", "");
133 if (newLine.contains("\"\"") && (newLine.contains("m") || newLine.contains("M"))) {
134 newLine = newLine.replace("\"\"", "\"");
140 FileWriter fw = new FileWriter(file);
141 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
142 for (String s : lines) {
151 } catch (Exception e) {
152 throw new FixesException("Unable to Fix Single Quotes in final ONAP Blueprint", e);
157 * Interface to fix Single Quotes for given line
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("'", "");
176 * Interface to Applt fixes for Quotes
178 * @param bp Blueprint
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));
187 return String.join("\n", lines);
190 private String processLine(String line) {
191 return line.replace("'\\{", "{")
193 .replace("'\\[", "[")
195 .replace("'''''", "'")
198 .replace("''\\{", "'{")
199 .replace("}''", "}'")
200 .replace("''\\[", "'[")
201 .replace("]''", "]'")
202 .replace("\"''", "'")
203 .replace("''\"", "'");