1 /**============LICENSE_START=======================================================
3 ================================================================================
4 Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
5 ================================================================================
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ============LICENSE_END=========================================================
21 package org.onap.blueprintgenerator.models.blueprint;
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
27 import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
28 import java.io.BufferedWriter;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.util.ArrayList;
34 import java.util.LinkedHashMap;
35 import java.util.TreeMap;
36 import java.util.regex.Pattern;
39 import org.onap.blueprintgenerator.core.Fixes;
40 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
41 import org.onap.blueprintgenerator.models.dmaapbp.DmaapBlueprint;
42 import org.onap.blueprintgenerator.models.onapbp.OnapBlueprint;
45 @JsonInclude(JsonInclude.Include.NON_NULL)
47 public class Blueprint {
50 private String tosca_definitions_version;
52 private String description;
54 private ArrayList<String> imports;
56 private TreeMap<String, LinkedHashMap<String, Object>> inputs;
58 private TreeMap<String, Node> node_templates;
60 public Blueprint createBlueprint(ComponentSpec cs, String name, char bpType, String importPath, String override) {
61 Blueprint bp = new Blueprint();
63 OnapBlueprint onap = new OnapBlueprint();
64 bp = onap.createOnapBlueprint(cs, importPath, override);
65 bp = bp.setQuotations(bp);
69 DmaapBlueprint dmaap = new DmaapBlueprint();
70 bp = dmaap.createDmaapBlueprint(cs, importPath, override);
71 bp = bp.setQuotations(bp);
76 public Blueprint setQuotations(Blueprint bp) {
77 for(String s: bp.getInputs().keySet()) {
78 LinkedHashMap<String, Object> temp = bp.getInputs().get(s);
79 if(temp.get("type") == "string") {
80 String def = (String) temp.get("default");
82 def = def.replaceAll("\"$", "").replaceAll("^\"", "");
84 def = '"' + def + '"';
85 temp.replace("default", def);
86 bp.getInputs().replace(s, temp);
93 public void blueprintToYaml(String outputPath, String bluePrintName, ComponentSpec cs) {
95 String name = bluePrintName.equals("") ? cs.getSelf().getName() : bluePrintName;
96 if(name.contains(".")) {
97 name = name.replaceAll(Pattern.quote("."), "_");
99 if(name.contains(" ")) {
100 name = name.replaceAll(" ", "");
102 String file = name + ".yaml";
103 outputFile = new File(outputPath, file);
104 outputFile.getParentFile().mkdirs();
106 outputFile.createNewFile();
107 } catch (IOException e) {
108 throw new RuntimeException(e);
111 String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
112 String description = "#description: " + cs.getSelf().getDescription() + '\n';
114 try(BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile, false))) {
115 writer.write(description);
116 writer.write(version);
117 } catch (IOException e1) {
118 throw new RuntimeException(e1);
121 //read the translated blueprint into the file
122 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
124 try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)))) {
125 blueprintMapper.writeValue(out, this);
126 } catch (IOException e) {
127 throw new RuntimeException(e);
131 Fixes.fixSingleQuotes(outputFile);
132 } catch (IOException e) {
133 throw new RuntimeException(e);
136 System.out.println("Blueprint created");
139 public String blueprintToString() {
142 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
144 ret = blueprintMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
145 } catch (JsonProcessingException e) {
146 throw new RuntimeException(e);
149 return Fixes.applyFixes(ret);