Merge "Changes to Regex for Name and version Issue-ID: DCAEGEN2-1867"
[dcaegen2/platform.git] / mod / bpgenerator / common / src / main / java / org / onap / blueprintgenerator / service / base / BlueprintService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
7  *  *  ================================================================================
8  *  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  *  you may not use this file except in compliance with the License.
10  *  *  You may obtain a copy of the License at
11  *  *
12  *  *   http://www.apache.org/licenses/LICENSE-2.0
13  *  *
14  *  *  Unless required by applicable law or agreed to in writing, software
15  *  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  *  See the License for the specific language governing permissions and
18  *  *  limitations under the License.
19  *  *  ============LICENSE_END=========================================================
20  *
21  *
22  */
23
24 package org.onap.blueprintgenerator.service.base;
25
26 import org.onap.blueprintgenerator.model.base.Blueprint;
27 import org.onap.blueprintgenerator.model.common.Input;
28 import org.onap.blueprintgenerator.model.componentspec.base.ComponentSpec;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.beans.factory.annotation.Qualifier;
33 import org.springframework.stereotype.Service;
34 import org.springframework.util.StringUtils;
35
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.io.FileWriter;
40 import java.io.BufferedWriter;
41 import java.io.PrintWriter;
42 import java.util.LinkedHashMap;
43 import java.util.Map;
44 import java.util.regex.Pattern;
45
46 /**
47  * @author : Ravi Mantena
48  * @date 10/16/2020
49  * Application: DCAE/ONAP - Blueprint Generator
50  * Common Module: Used by both ONAp and DCAE Blueprint Applications
51  * Service: For Adding Quotes and Converting Blueprint to Yaml/String
52  */
53
54 @Service
55 public abstract class BlueprintService {
56
57   @Autowired
58   protected FixesService fixesService;
59
60   @Qualifier("objectMapper")
61   @Autowired
62   protected ObjectMapper objectMapper;
63
64   @Qualifier("yamlObjectMapper")
65   @Autowired
66   protected ObjectMapper yamlObjectMapper;
67
68   public void blueprintToYaml(ComponentSpec cs, Blueprint blueprint, Input input) {
69     String bluePrintName = input.getBluePrintName();
70     String outputPath = input.getOutputPath();
71     String comment = "# " + input.getComment() + '\n';
72
73     try {
74     File outputFile;
75     String name = StringUtils.isEmpty(bluePrintName) ? cs.getSelf().getName() : bluePrintName;
76     if(name.contains(".")) {
77       name = name.replaceAll(Pattern.quote("."), "_");
78     }
79     if(name.contains(" ")) {
80       name = name.replaceAll(" ", "");
81     }
82     String file = name + ".yaml";
83     outputFile = new File(outputPath, file);
84     outputFile.getParentFile().mkdirs();
85     try {
86       outputFile.createNewFile();
87     } catch (IOException e) {
88       throw new RuntimeException(e);
89     }
90
91     String version = "#blueprint_version: " + cs.getSelf().getVersion() + '\n';
92     String description = "#description: " + cs.getSelf().getDescription() + '\n';
93
94     BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile, false));
95     writer.write(description);
96     writer.write(version);
97
98     if(input.getBpType().equals("dti") || input.getBpType().equals("m") || input.getBpType().equals("k"))
99       writer.write(comment);
100
101     writer.close();
102
103     PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
104     yamlObjectMapper.writeValue(out, blueprint);
105     out.close();
106
107     if(input.getBpType().equals("dti") || input.getBpType().equals("m") || input.getBpType().equals("k"))
108       fixesService.fixDcaeSingleQuotes(outputFile);
109     else
110       fixesService.fixOnapSingleQuotes(outputFile);
111
112     //new Yaml().load(new FileInputStream(outputFile));
113
114     System.out.println("Blueprint is created with valid YAML Format");
115     } catch (Exception ex) {
116     throw new RuntimeException("Unable to generate YAML file from Blueprint  or the generated YAML is not valid", ex);
117     }
118   }
119
120   // Used for DCAE
121   public Map<String, LinkedHashMap<String, Object>> addQuotes(Map<String, LinkedHashMap<String, Object>> inputs) {
122
123     inputs.forEach((key,value) -> {
124     if(value.get("type") != null){
125       if (value.get("type").equals("string") && value.get("default") != null && !key.contains("policies")){
126         value.replace("default",  "'" + value.get("default").toString() + "'");
127       } else if (value.get("type").equals("map") || value.get("type").equals("list")) {
128         // Commented the Code as we need to read the object as is for Map and List. If the List object is to be converted to string uncomment the below code.
129         /* if(inputs.get(s).get("type").equals("list")) {
130         String temp = inputs.get(s).get("default").toString();
131         inputs.get(s).replace("default", temp);
132         }*/
133         inputs.get(key).remove("type");
134       }
135     }
136     });
137
138    return inputs;
139   }
140
141   public String blueprintToString(ComponentSpec componentSpec, Blueprint blueprint, Input input) {
142     String ret = "";
143     try {
144     ret = yamlObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(blueprint);
145     } catch (JsonProcessingException e) {
146     e.printStackTrace();
147     }
148     if(input.getBpType().equals("dti") || input.getBpType().equals("m") || input.getBpType().equals("k")){
149     ret = "# " + componentSpec.getSelf().getDescription() + "\n" + "# " + componentSpec.getSelf().getVersion() + "\n" + "# " + input.getComment() + "\n" + ret;
150     ret = fixesService.fixStringQuotes(ret);
151     }else
152     ret = fixesService.applyFixes(ret);
153     return ret;
154   }
155
156
157 }