861c820a822c416e05e67a8f80142d7cbc58d04b
[dcaegen2/platform/cli.git] / blueprint-generator / src / main / java / org / onap / blueprintgenerator / models / blueprint / Blueprint.java
1 /**============LICENSE_START======================================================= 
2  org.onap.dcae 
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 
9  
10       http://www.apache.org/licenses/LICENSE-2.0 
11  
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========================================================= 
18  
19  */
20
21 package org.onap.blueprintgenerator.models.blueprint;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
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;
32
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;
39
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;
46
47 import lombok.AllArgsConstructor;
48 import lombok.Builder;
49 import lombok.Getter;
50 import lombok.NoArgsConstructor;
51 import lombok.Setter;
52
53
54
55 @Getter @Setter
56 @JsonInclude(JsonInclude.Include.NON_NULL)
57
58 public class Blueprint {
59
60
61         private String tosca_definitions_version;
62
63
64         private ArrayList<String> imports;
65
66
67         private TreeMap<String, LinkedHashMap<String, Object>> inputs;
68
69
70         private TreeMap<String, Node> node_templates;
71         
72         public Blueprint createBlueprint(ComponentSpec cs, String name, char bpType, String importPath) {
73                 Blueprint bp = new Blueprint();
74                 
75                 if(bpType == 'o') {
76                         OnapBlueprint onap = new OnapBlueprint();
77                         bp = onap.createOnapBlueprint(cs, importPath);
78                         bp = bp.setQuotations(bp);
79                 }
80 //              if(bpType == 't') {
81 //                      bp.createBlueprintTemplate();
82 //              }
83                 return bp;
84         }
85         
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);
94                         }
95                 }
96                 
97                 return bp;
98         }
99         
100 //      public void createBlueprintTemplate() {
101 //              //set the tosca definition
102 //              this.setTosca_definitions_version("cloudify_dsl_1_3");
103 //              
104 //              //set the imports
105 //              Imports imps = new Imports();
106 //              this.setImports(imps.createOnapImports());
107 //              
108 //              //create the needed inputs and just add the default ones
109 //              TreeMap<String, LinkedHashMap<String, Object>> inputs = createTemplateInputs();
110 //              this.setInputs(inputs);
111 //              
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);
118 //              
119 //              
120 //      }
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>>();
124 //              
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);
129 //              
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);
135 //              
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);
141 //              
142 //              return inputs;
143 //      }
144
145         public void blueprintToYaml(String outputPath, String bluePrintName, ComponentSpec cs) {
146                 File outputFile;
147
148                 if(bluePrintName.equals("")) {
149                         String name = cs.getSelf().getName();
150                         if(name.contains(".")) {
151                                 name = name.replaceAll(Pattern.quote("."), "_");
152                         }
153                         if(name.contains(" ")) {
154                                 name = name.replaceAll(" ", "");
155                         }
156                         String file = name + ".yaml";
157
158
159                         outputFile = new File(outputPath, file);
160                         outputFile.getParentFile().mkdirs();
161                         try {
162                                 outputFile.createNewFile();
163                         } catch (IOException e) {
164                                 
165                                 throw new RuntimeException(e);
166                         }
167                 } else {
168                         if(bluePrintName.contains(" ") || bluePrintName.contains(".")) {
169                                 bluePrintName = bluePrintName.replaceAll(Pattern.quote("."), "_");
170                                 bluePrintName = bluePrintName.replaceAll(" ", "");
171                         }
172                         String file = bluePrintName + ".yaml";
173                         outputFile = new File(outputPath, file);
174                         outputFile.getParentFile().mkdirs();
175                         try {
176                                 outputFile.createNewFile();
177                         } catch (IOException e) {
178                                 throw new RuntimeException(e);
179                         }
180                 }
181
182                 String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
183                 String description = "#description: " + cs.getSelf().getDescription() + '\n';
184
185                 BufferedWriter writer = null;
186                 try {
187                         writer = new BufferedWriter(new FileWriter(outputFile, false));
188                 } catch (IOException e1) {
189                         throw new RuntimeException(e1);
190                 }
191                 if(writer != null) {
192                         try {
193                                 writer.write(description);
194                         } catch (IOException e) {
195                                 throw new RuntimeException(e);
196                         }
197                         try {
198                                 writer.write(version);
199                         } catch (IOException e) {
200                                 throw new RuntimeException(e);
201                         }
202                         try {
203                                 writer.close();
204                         } catch (IOException e) {
205                                 throw new RuntimeException(e);
206                         }
207                 }
208
209
210                 //read the translated blueprint into the file
211                 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
212
213                 PrintWriter out = null;
214                 try {
215                         out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
216                 } catch (IOException e) {
217                         throw new RuntimeException(e);
218                 }
219
220                 try {
221                         if(out != null) {
222                                 blueprintMapper.writeValue(out, this);
223                                 out.close();
224                         }
225                 } catch (IOException e) {
226                         
227                         throw new RuntimeException(e);
228                 }
229
230
231                 Fixes fix = new Fixes();
232                 try {
233                         fix.fixSingleQuotes(outputFile);
234                 } catch (IOException e) {
235                         throw new RuntimeException(e);
236                 }
237
238                 System.out.println("Blueprint created");
239         }
240
241
242         public String blueprintToString() {
243                 String ret = "";
244
245                 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
246                 try {
247                         ret = blueprintMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
248                 } catch (JsonProcessingException e) {
249                         throw new RuntimeException(e);
250                 }
251
252
253                 return ret;
254         }
255 }