bp-gen code clone from cli repo
[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 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.dmaapbp.DmaapBlueprint;
39 import org.onap.blueprintgenerator.models.onapbp.OnapBlueprint;
40
41 import com.fasterxml.jackson.annotation.JsonInclude;
42 import com.fasterxml.jackson.core.JsonProcessingException;
43 //import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
46 import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
47
48 import lombok.AllArgsConstructor;
49 import lombok.Builder;
50 import lombok.Getter;
51 import lombok.NoArgsConstructor;
52 import lombok.Setter;
53
54
55
56 @Getter @Setter
57 @JsonInclude(JsonInclude.Include.NON_NULL)
58
59 public class Blueprint {
60
61
62         private String tosca_definitions_version;
63
64         private String description;
65
66         private ArrayList<String> imports;
67
68         private TreeMap<String, LinkedHashMap<String, Object>> inputs;
69
70         private TreeMap<String, Node> node_templates;
71
72         public Blueprint createBlueprint(ComponentSpec cs, String name, char bpType, String importPath, String override) {
73                 Blueprint bp = new Blueprint();
74                 if(bpType == 'o') {
75                         OnapBlueprint onap = new OnapBlueprint();
76                         bp = onap.createOnapBlueprint(cs, importPath, override);
77                         bp = bp.setQuotations(bp);
78                 }
79
80                 if(bpType == 'd') {
81                         DmaapBlueprint dmaap = new DmaapBlueprint();
82                         bp = dmaap.createDmaapBlueprint(cs, importPath, override);
83                         bp = bp.setQuotations(bp);
84                 }
85                 return bp;
86         }
87         public Blueprint setQuotations(Blueprint bp) {
88                 for(String s: bp.getInputs().keySet()) {
89                         LinkedHashMap<String, Object> temp = bp.getInputs().get(s);
90                         if(temp.get("type") == "string") {
91                                 String def = (String) temp.get("default");
92                                 def = '"' + def + '"';
93                                 temp.replace("default", def);
94                                 bp.getInputs().replace(s, temp);
95                         }
96                 }
97                 
98                 return bp;
99         }
100
101         public void blueprintToYaml(String outputPath, String bluePrintName, ComponentSpec cs) {
102                 File outputFile;
103
104                 if(bluePrintName.equals("")) {
105                         String name = cs.getSelf().getName();
106                         if(name.contains(".")) {
107                                 name = name.replaceAll(Pattern.quote("."), "_");
108                         }
109                         if(name.contains(" ")) {
110                                 name = name.replaceAll(" ", "");
111                         }
112                         String file = name + ".yaml";
113
114
115                         outputFile = new File(outputPath, file);
116                         outputFile.getParentFile().mkdirs();
117                         try {
118                                 outputFile.createNewFile();
119                         } catch (IOException e) {
120                                 
121                                 throw new RuntimeException(e);
122                         }
123                 } else {
124                         if(bluePrintName.contains(" ") || bluePrintName.contains(".")) {
125                                 bluePrintName = bluePrintName.replaceAll(Pattern.quote("."), "_");
126                                 bluePrintName = bluePrintName.replaceAll(" ", "");
127                         }
128                         String file = bluePrintName + ".yaml";
129                         outputFile = new File(outputPath, file);
130                         outputFile.getParentFile().mkdirs();
131                         try {
132                                 outputFile.createNewFile();
133                         } catch (IOException e) {
134                                 throw new RuntimeException(e);
135                         }
136                 }
137
138                 String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
139                 String description = "#description: " + cs.getSelf().getDescription() + '\n';
140
141                 BufferedWriter writer = null;
142                 try {
143                         writer = new BufferedWriter(new FileWriter(outputFile, false));
144                 } catch (IOException e1) {
145                         throw new RuntimeException(e1);
146                 }
147                 if(writer != null) {
148                         try {
149                                 writer.write(description);
150                         } catch (IOException e) {
151                                 throw new RuntimeException(e);
152                         }
153                         try {
154                                 writer.write(version);
155                         } catch (IOException e) {
156                                 throw new RuntimeException(e);
157                         }
158                         try {
159                                 writer.close();
160                         } catch (IOException e) {
161                                 throw new RuntimeException(e);
162                         }
163                 }
164
165
166                 //read the translated blueprint into the file
167                 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
168
169                 PrintWriter out = null;
170                 try {
171                         out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
172                 } catch (IOException e) {
173                         throw new RuntimeException(e);
174                 }
175
176                 try {
177                         if(out != null) {
178                                 blueprintMapper.writeValue(out, this);
179                                 out.close();
180                         }
181                 } catch (IOException e) {
182                         
183                         throw new RuntimeException(e);
184                 }
185
186
187                 Fixes fix = new Fixes();
188                 try {
189                         fix.fixSingleQuotes(outputFile);
190                 } catch (IOException e) {
191                         throw new RuntimeException(e);
192                 }
193
194                 System.out.println("Blueprint created");
195         }
196
197
198         public String blueprintToString() {
199                 String ret = "";
200
201                 ObjectMapper blueprintMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
202                 try {
203                         ret = blueprintMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
204                 } catch (JsonProcessingException e) {
205                         throw new RuntimeException(e);
206                 }
207
208
209                 return ret;
210         }
211 }