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 java.io.BufferedWriter;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.TreeMap;
31 import java.util.regex.Pattern;
33 import org.onap.blueprintgenerator.core.Fixes;
34 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
35 import org.onap.blueprintgenerator.models.componentspec.Parameters;
36 import org.onap.blueprintgenerator.models.componentspec.Publishes;
37 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
38 import org.onap.blueprintgenerator.models.onapbp.OnapBlueprint;
40 import com.fasterxml.jackson.annotation.JsonInclude;
41 import com.fasterxml.jackson.core.JsonProcessingException;
42 //import com.fasterxml.jackson.databind.ObjectMapper;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
45 import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
47 import lombok.AllArgsConstructor;
48 import lombok.Builder;
50 import lombok.NoArgsConstructor;
56 @JsonInclude(JsonInclude.Include.NON_NULL)
58 public class Blueprint {
61 private String tosca_definitions_version;
64 private ArrayList<String> imports;
67 private TreeMap<String, LinkedHashMap<String, Object>> inputs;
70 private TreeMap<String, Node> node_templates;
72 public Blueprint createBlueprint(ComponentSpec cs, String name, char bpType, String importPath) {
73 Blueprint bp = new Blueprint();
76 OnapBlueprint onap = new OnapBlueprint();
77 bp = onap.createOnapBlueprint(cs, importPath);
78 bp = bp.setQuotations(bp);
80 // if(bpType == 't') {
81 // bp.createBlueprintTemplate();
86 public Blueprint setQuotations(Blueprint bp) {
87 for(String s: bp.getInputs().keySet()) {
88 LinkedHashMap<String, Object> temp = bp.getInputs().get(s);
89 if(temp.get("type") == "string") {
90 String def = (String) temp.get("default");
91 def = '"' + def + '"';
92 temp.replace("default", def);
93 bp.getInputs().replace(s, temp);
100 // public void createBlueprintTemplate() {
101 // //set the tosca definition
102 // this.setTosca_definitions_version("cloudify_dsl_1_3");
105 // Imports imps = new Imports();
106 // this.setImports(imps.createOnapImports());
108 // //create the needed inputs and just add the default ones
109 // TreeMap<String, LinkedHashMap<String, Object>> inputs = createTemplateInputs();
110 // this.setInputs(inputs);
112 // //create a node template
113 // TreeMap<String, Node> nodeTemplate = new TreeMap<String, Node>();
114 // TemplateNode template = new TemplateNode();
115 // template.createTemplateNode();
116 // nodeTemplate.put("Blueprint_Template", template);
117 // this.setNode_template(nodeTemplate);
121 // //add tag, externam port, and replicas since they are in all the bps
122 // public TreeMap<String, LinkedHashMap<String, Object>> createTemplateInputs() {
123 // TreeMap<String, LinkedHashMap<String, Object>> inputs = new TreeMap<String, LinkedHashMap<String, Object>>();
125 // LinkedHashMap<String, Object> tag = new LinkedHashMap<String, Object>();
126 // tag.put("type", "string");
127 // tag.put("default", "{{ ONAPTEMPLATE_DOCKERREGURL_org_onap_dcaegen2_releases }}/onap/org.onap.dcaegen2.collectors.ves.vescollector:1.3.1");
128 // inputs.put("tag_version", tag);
130 // LinkedHashMap<String, Object> port = new LinkedHashMap<String, Object>();
131 // port.put("type", "string");
132 // port.put("description", "Kubernetes node port on which collector is exposed");
133 // port.put("default", "30235");
134 // inputs.put("external_port", port);
136 // LinkedHashMap<String, Object> rep = new LinkedHashMap<String, Object>();
137 // rep.put("type", "integer");
138 // rep.put("description", "number of instances");
139 // rep.put("default", 1);
140 // inputs.put("replicas", rep);
145 public void blueprintToYaml(String outputPath, String bluePrintName, ComponentSpec cs) {
148 if(bluePrintName.equals("")) {
149 String name = cs.getSelf().getName();
150 if(name.contains(".")) {
151 name = name.replaceAll(Pattern.quote("."), "_");
153 if(name.contains(" ")) {
154 name = name.replaceAll(" ", "");
156 String file = name + ".yaml";
159 outputFile = new File(outputPath, file);
160 outputFile.getParentFile().mkdirs();
162 outputFile.createNewFile();
163 } catch (IOException e) {
165 throw new RuntimeException(e);
168 if(bluePrintName.contains(" ") || bluePrintName.contains(".")) {
169 bluePrintName = bluePrintName.replaceAll(Pattern.quote("."), "_");
170 bluePrintName = bluePrintName.replaceAll(" ", "");
172 String file = bluePrintName + ".yaml";
173 outputFile = new File(outputPath, file);
174 outputFile.getParentFile().mkdirs();
176 outputFile.createNewFile();
177 } catch (IOException e) {
178 throw new RuntimeException(e);
182 String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
183 String description = "#description: " + cs.getSelf().getDescription() + '\n';
185 BufferedWriter writer = null;
187 writer = new BufferedWriter(new FileWriter(outputFile, false));
188 } catch (IOException e1) {
189 throw new RuntimeException(e1);
193 writer.write(description);
194 } catch (IOException e) {
195 throw new RuntimeException(e);
198 writer.write(version);
199 } catch (IOException e) {
200 throw new RuntimeException(e);
204 } catch (IOException e) {
205 throw new RuntimeException(e);
210 //read the translated blueprint into the file
211 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
213 PrintWriter out = null;
215 out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
216 } catch (IOException e) {
217 throw new RuntimeException(e);
222 blueprintMapper.writeValue(out, this);
225 } catch (IOException e) {
227 throw new RuntimeException(e);
231 Fixes fix = new Fixes();
233 fix.fixSingleQuotes(outputFile);
234 } catch (IOException e) {
235 throw new RuntimeException(e);
238 System.out.println("Blueprint created");
242 public String blueprintToString() {
245 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
247 ret = blueprintMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
248 } catch (JsonProcessingException e) {
249 throw new RuntimeException(e);