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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import java.io.FileReader;
30 import org.onap.blueprintgenerator.model.base.Blueprint;
31 import org.onap.blueprintgenerator.model.common.Input;
32 import org.onap.blueprintgenerator.model.componentspec.base.ComponentSpec;
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Qualifier;
37 import org.springframework.stereotype.Service;
38 import org.springframework.util.StringUtils;
41 import java.io.IOException;
42 import java.io.FileWriter;
43 import java.io.BufferedWriter;
44 import java.io.PrintWriter;
45 import java.util.LinkedHashMap;
47 import java.util.regex.Pattern;
48 import java.util.Date;
49 import org.apache.maven.model.Model;
50 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
53 * @author : Ravi Mantena
54 * @date 10/16/2020 Application: DCAE/ONAP - Blueprint Generator Common Module: Used by both ONAp
55 * and DCAE Blueprint Applications Service: For Adding Quotes and Converting Blueprint to
59 public class BlueprintService {
61 private static final String TYPE_KEY = "type";
62 private static final String DEFAULT_KEY = "default";
63 private static final Logger logger = LoggerFactory.getLogger(BlueprintService.class);
66 protected FixesService fixesService;
68 @Qualifier("objectMapper")
70 protected ObjectMapper objectMapper;
72 @Qualifier("yamlObjectMapper")
74 protected ObjectMapper yamlObjectMapper;
77 * Convertes blueprint to Yaml for given ComponentSpec, Blueprint and input
79 * @param cs ComponentSpec
80 * @param blueprint Blueprint
84 public void blueprintToYaml(ComponentSpec cs, Blueprint blueprint, Input input) {
85 String bluePrintName = input.getBluePrintName();
86 String outputPath = input.getOutputPath();
87 String comment = "# " + input.getComment() + '\n';
91 StringUtils.isEmpty(bluePrintName) ? cs.getSelf().getName() : bluePrintName;
92 if (name.contains(".")) {
93 name = name.replaceAll(Pattern.quote("."), "_");
95 if (name.contains(" ")) {
96 name = name.replace(" ", "");
99 File outputFile = createFile(outputPath, name);
100 String appVersion = readAppVersion();
102 String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
103 String description = "#description: " + cs.getSelf().getDescription() + '\n';
104 String date = "#blueprint_created_date: " + new Date() + '\n';
106 BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile, false));
107 writer.write(description);
108 writer.write(version);
110 writer.write(appVersion);
112 if (isBpTypeMatches(input)) {
113 writer.write(comment);
118 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
119 yamlObjectMapper.writeValue(out, blueprint);
122 if (isBpTypeMatches(input)) {
123 fixesService.fixDcaeSingleQuotes(outputFile);
125 fixesService.fixOnapSingleQuotes(outputFile);
128 logger.debug("Blueprint is created with valid YAML Format");
129 } catch (Exception ex) {
130 throw new RuntimeException(
131 "Unable to generate YAML file from Blueprint or the generated YAML is not valid",
137 * Add quotes for given input
139 * @param inputs Inputs
142 public Map<String, LinkedHashMap<String, Object>> addQuotes(
143 Map<String, LinkedHashMap<String, Object>> inputs) {
147 if (value.get(TYPE_KEY) != null) {
148 if (value.get(TYPE_KEY).equals("string")
149 && value.get(DEFAULT_KEY) != null
150 && !key.contains("policies")) {
151 value.replace(DEFAULT_KEY, "'" + value.get(DEFAULT_KEY).toString() + "'");
152 } else if (value.get(TYPE_KEY).equals("map") || value.get(TYPE_KEY)
154 // Commented the Code as we need to read the object as is for Map and List. If the
155 // List object is to be converted to string uncomment the below code.
156 /* if(inputs.get(s).get("type").equals("list")) {
157 String temp = inputs.get(s).get("default").toString();
158 inputs.get(s).replace("default", temp);
160 inputs.get(key).remove(TYPE_KEY);
169 * Converts Blueprint to String format
171 * @param componentSpec ComponentSpec
172 * @param blueprint Blueprint
176 public String blueprintToString(ComponentSpec componentSpec, Blueprint blueprint, Input input) {
179 ret = yamlObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(blueprint);
180 } catch (JsonProcessingException e) {
183 if (isBpTypeMatches(input)) {
186 + componentSpec.getSelf().getDescription()
189 + componentSpec.getSelf().getVersion()
195 ret = fixesService.fixStringQuotes(ret);
197 ret = fixesService.applyFixes(ret);
202 private boolean isBpTypeMatches(Input input) {
203 return input.getBpType().equals("dti")
204 || input.getBpType().equals("m")
205 || input.getBpType().equals("k");
208 private File createFile(String outputPath, String name) {
210 String file = name + ".yaml";
211 outputFile = new File(outputPath, file);
212 outputFile.getParentFile().mkdirs();
214 boolean isCreated = outputFile.createNewFile();
216 logger.debug("The file " + file + " was successfully created.");
218 logger.debug("The file " + file + " already existed.");
220 } catch (IOException e) {
221 throw new RuntimeException(e);
226 private String readAppVersion() {
227 String appVersion = "";
229 MavenXpp3Reader reader = new MavenXpp3Reader();
230 Model model = reader.read(new FileReader("pom.xml"));
231 appVersion = "#bpgen_application_version: " + model.getVersion() + '\n';
232 } catch (Exception e) {