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.model.base.Blueprint;
27 import org.onap.blueprintgenerator.model.common.Input;
28 import org.onap.blueprintgenerator.model.componentspec.base.ComponentSpec;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.beans.factory.annotation.Qualifier;
33 import org.springframework.stereotype.Service;
34 import org.springframework.util.StringUtils;
38 import java.io.IOException;
39 import java.io.FileWriter;
40 import java.io.BufferedWriter;
41 import java.io.PrintWriter;
42 import java.util.LinkedHashMap;
44 import java.util.regex.Pattern;
47 * @author : Ravi Mantena
49 * Application: DCAE/ONAP - Blueprint Generator
50 * Common Module: Used by both ONAp and DCAE Blueprint Applications
51 * Service: For Adding Quotes and Converting Blueprint to Yaml/String
55 public abstract class BlueprintService {
58 protected FixesService fixesService;
60 @Qualifier("objectMapper")
62 protected ObjectMapper objectMapper;
64 @Qualifier("yamlObjectMapper")
66 protected ObjectMapper yamlObjectMapper;
68 public void blueprintToYaml(ComponentSpec cs, Blueprint blueprint, Input input) {
69 String bluePrintName = input.getBluePrintName();
70 String outputPath = input.getOutputPath();
71 String comment = "# " + input.getComment() + '\n';
75 String name = StringUtils.isEmpty(bluePrintName) ? cs.getSelf().getName() : bluePrintName;
76 if(name.contains(".")) {
77 name = name.replaceAll(Pattern.quote("."), "_");
79 if(name.contains(" ")) {
80 name = name.replaceAll(" ", "");
82 String file = name + ".yaml";
83 outputFile = new File(outputPath, file);
84 outputFile.getParentFile().mkdirs();
86 outputFile.createNewFile();
87 } catch (IOException e) {
88 throw new RuntimeException(e);
91 String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
92 String description = "#description: " + cs.getSelf().getDescription() + '\n';
94 BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile, false));
95 writer.write(description);
96 writer.write(version);
98 if(input.getBpType().equals("dti") || input.getBpType().equals("m") || input.getBpType().equals("k"))
99 writer.write(comment);
103 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
104 yamlObjectMapper.writeValue(out, blueprint);
107 if(input.getBpType().equals("dti") || input.getBpType().equals("m") || input.getBpType().equals("k"))
108 fixesService.fixDcaeSingleQuotes(outputFile);
110 fixesService.fixOnapSingleQuotes(outputFile);
112 //new Yaml().load(new FileInputStream(outputFile));
114 System.out.println("Blueprint is created with valid YAML Format");
115 } catch (Exception ex) {
116 throw new RuntimeException("Unable to generate YAML file from Blueprint or the generated YAML is not valid", ex);
121 public Map<String, LinkedHashMap<String, Object>> addQuotes(Map<String, LinkedHashMap<String, Object>> inputs) {
123 inputs.forEach((key,value) -> {
124 if(value.get("type") != null){
125 if (value.get("type").equals("string") && value.get("default") != null && !key.contains("policies")){
126 value.replace("default", "'" + value.get("default").toString() + "'");
127 } else if (value.get("type").equals("map") || value.get("type").equals("list")) {
128 // Commented the Code as we need to read the object as is for Map and List. If the List object is to be converted to string uncomment the below code.
129 /* if(inputs.get(s).get("type").equals("list")) {
130 String temp = inputs.get(s).get("default").toString();
131 inputs.get(s).replace("default", temp);
133 inputs.get(key).remove("type");
141 public String blueprintToString(ComponentSpec componentSpec, Blueprint blueprint, Input input) {
144 ret = yamlObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(blueprint);
145 } catch (JsonProcessingException e) {
148 if(input.getBpType().equals("dti") || input.getBpType().equals("m") || input.getBpType().equals("k")){
149 ret = "# " + componentSpec.getSelf().getDescription() + "\n" + "# " + componentSpec.getSelf().getVersion() + "\n" + "# " + input.getComment() + "\n" + ret;
150 ret = fixesService.fixStringQuotes(ret);
152 ret = fixesService.applyFixes(ret);