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