Merge "Refactor, fix code formatting and add unittests"
[dcaegen2/platform.git] / mod / bpgenerator / 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 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;
29 import java.io.File;
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;
37 import lombok.Getter;
38 import lombok.Setter;
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;
43
44 @Getter @Setter
45 @JsonInclude(JsonInclude.Include.NON_NULL)
46
47 public class Blueprint {
48
49
50         private String tosca_definitions_version;
51
52         private String description;
53
54         private ArrayList<String> imports;
55
56         private TreeMap<String, LinkedHashMap<String, Object>> inputs;
57
58         private TreeMap<String, Node> node_templates;
59
60         public Blueprint createBlueprint(ComponentSpec cs, String name, char bpType, String importPath, String override) {
61                 Blueprint bp = new Blueprint();
62                 if(bpType == 'o') {
63                         OnapBlueprint onap = new OnapBlueprint();
64                         bp = onap.createOnapBlueprint(cs, importPath, override);
65                         bp = bp.setQuotations(bp);
66                 }
67
68                 if(bpType == 'd') {
69                         DmaapBlueprint dmaap = new DmaapBlueprint();
70                         bp = dmaap.createDmaapBlueprint(cs, importPath, override);
71                         bp = bp.setQuotations(bp);
72                 }
73                 return bp;
74         }
75
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");
81                                 if(def != null){
82                                         def = def.replaceAll("\"$", "").replaceAll("^\"", "");
83                                 }
84                                 def = '"' + def + '"';
85                                 temp.replace("default", def);
86                                 bp.getInputs().replace(s, temp);
87                         }
88                 }
89
90                 return bp;
91         }
92
93         public void blueprintToYaml(String outputPath, String bluePrintName, ComponentSpec cs) {
94                 File outputFile;
95                 String name = bluePrintName.equals("") ? cs.getSelf().getName() : bluePrintName;
96                 if(name.contains(".")) {
97                         name = name.replaceAll(Pattern.quote("."), "_");
98                 }
99                 if(name.contains(" ")) {
100                         name = name.replaceAll(" ", "");
101                 }
102                 String file = name + ".yaml";
103                 outputFile = new File(outputPath, file);
104                 outputFile.getParentFile().mkdirs();
105                 try {
106                         outputFile.createNewFile();
107                 } catch (IOException e) {
108                         throw new RuntimeException(e);
109                 }
110
111                 String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
112                 String description = "#description: " + cs.getSelf().getDescription() + '\n';
113
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);
119                 }
120
121                 //read the translated blueprint into the file
122                 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
123
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);
128                 }
129
130                 try {
131                         Fixes.fixSingleQuotes(outputFile);
132                 } catch (IOException e) {
133                         throw new RuntimeException(e);
134                 }
135
136                 System.out.println("Blueprint created");
137         }
138
139         public String blueprintToString() {
140                 String ret = "";
141
142                 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
143                 try {
144                         ret = blueprintMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
145                 } catch (JsonProcessingException e) {
146                         throw new RuntimeException(e);
147                 }
148
149                 return Fixes.applyFixes(ret);
150         }
151 }