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;
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.TreeMap;
27 import java.util.regex.Pattern;
29 import org.onap.blueprintgenerator.core.Fixes;
30 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
31 import org.onap.blueprintgenerator.models.componentspec.Parameters;
32 import org.onap.blueprintgenerator.models.componentspec.Publishes;
33 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
34 import org.onap.blueprintgenerator.models.dmaapbp.DmaapBlueprint;
35 import org.onap.blueprintgenerator.models.onapbp.OnapBlueprint;
37 import com.fasterxml.jackson.annotation.JsonInclude;
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 //import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
42 import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
44 import lombok.AllArgsConstructor;
45 import lombok.Builder;
47 import lombok.NoArgsConstructor;
49 import org.yaml.snakeyaml.Yaml;
53 @JsonInclude(JsonInclude.Include.NON_NULL)
55 public class Blueprint {
58 private String tosca_definitions_version;
60 private String description;
62 private ArrayList<String> imports;
64 private TreeMap<String, LinkedHashMap<String, Object>> inputs;
66 private TreeMap<String, Node> node_templates;
68 public Blueprint createBlueprint(ComponentSpec cs, String name, char bpType, String importPath, String override) {
69 Blueprint bp = new Blueprint();
71 OnapBlueprint onap = new OnapBlueprint();
72 bp = onap.createOnapBlueprint(cs, importPath, override);
73 bp = bp.setQuotations(bp);
77 DmaapBlueprint dmaap = new DmaapBlueprint();
78 bp = dmaap.createDmaapBlueprint(cs, importPath, override);
79 bp = bp.setQuotations(bp);
84 public Blueprint setQuotations(Blueprint bp) {
85 for(String s: bp.getInputs().keySet()) {
86 LinkedHashMap<String, Object> temp = bp.getInputs().get(s);
87 if(temp.get("type") == "string") {
88 String def = (String) temp.get("default");
90 def = def.replaceAll("\"$", "").replaceAll("^\"", "");
92 def = '"' + def + '"';
93 temp.replace("default", def);
94 bp.getInputs().replace(s, temp);
101 public void blueprintToYaml(String outputPath, String bluePrintName, ComponentSpec cs) {
104 if(bluePrintName.equals("")) {
105 String name = cs.getSelf().getName();
106 if(name.contains(".")) {
107 name = name.replaceAll(Pattern.quote("."), "_");
109 if(name.contains(" ")) {
110 name = name.replaceAll(" ", "");
112 String file = name + ".yaml";
115 outputFile = new File(outputPath, file);
116 outputFile.getParentFile().mkdirs();
118 outputFile.createNewFile();
119 } catch (IOException e) {
121 throw new RuntimeException(e);
124 if(bluePrintName.contains(" ") || bluePrintName.contains(".")) {
125 bluePrintName = bluePrintName.replaceAll(Pattern.quote("."), "_");
126 bluePrintName = bluePrintName.replaceAll(" ", "");
128 String file = bluePrintName + ".yaml";
129 outputFile = new File(outputPath, file);
130 outputFile.getParentFile().mkdirs();
132 outputFile.createNewFile();
133 } catch (IOException e) {
134 throw new RuntimeException(e);
138 String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
139 String description = "#description: " + cs.getSelf().getDescription() + '\n';
141 BufferedWriter writer = null;
143 writer = new BufferedWriter(new FileWriter(outputFile, false));
144 } catch (IOException e1) {
145 throw new RuntimeException(e1);
149 writer.write(description);
150 } catch (IOException e) {
151 throw new RuntimeException(e);
154 writer.write(version);
155 } catch (IOException e) {
156 throw new RuntimeException(e);
160 } catch (IOException e) {
161 throw new RuntimeException(e);
166 //read the translated blueprint into the file
167 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
169 PrintWriter out = null;
171 out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
172 } catch (IOException e) {
173 throw new RuntimeException(e);
178 blueprintMapper.writeValue(out, this);
181 } catch (IOException e) {
183 throw new RuntimeException(e);
187 Fixes fix = new Fixes();
189 fix.fixSingleQuotes(outputFile);
190 } catch (IOException e) {
191 throw new RuntimeException(e);
194 System.out.println("Blueprint created");
197 public String blueprintToString() {
200 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
202 ret = blueprintMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
203 } catch (JsonProcessingException e) {
204 throw new RuntimeException(e);
207 return Fixes.applyFixes(ret);