7e11b100805a38ab789fbddb8fd08f54951944fb
[dcaegen2/platform.git] / mod / runtimeapi / runtime-core / src / main / java / org / onap / dcae / runtime / core / blueprint_creator / BlueprintCreatorOnap.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.dcae.runtime.core.blueprint_creator;
19
20 import org.onap.blueprintgenerator.core.Fixes;
21 import org.onap.dcae.runtime.core.Node;
22 import org.onap.blueprintgenerator.models.blueprint.Blueprint;
23 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
24 import org.yaml.snakeyaml.DumperOptions;
25 import org.yaml.snakeyaml.Yaml;
26
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29
30 public class BlueprintCreatorOnap implements BlueprintCreator{
31
32     private String topicUrl;
33     private String importFilePath;
34
35     public void setTopicUrl(String topicUrl) {
36         this.topicUrl = topicUrl;
37     }
38
39     public void setImportFilePath(String importFilePath) {
40         this.importFilePath = importFilePath;
41     }
42
43     @Override
44     public String createBlueprint(String componentSpecString) {
45         ComponentSpec componentSpec = new ComponentSpec();
46         componentSpec.createComponentSpecFromString(componentSpecString);
47         Blueprint blueprint = new Blueprint().createBlueprint(componentSpec,"",'o',importFilePath,"");
48         return blueprint.blueprintToString();
49     }
50
51     @Override
52     public void resolveDmaapConnection(Node node, String locationPort, String dmaapEntityName) {
53         if(node == null || locationPort == null){
54             return;
55         }
56         String blueprintContent = node.getBlueprintData().getBlueprint_content();
57         locationPort = locationPort.replaceAll("-","_");
58         Yaml yaml = getYamlInstance();
59         Map<String,Object> obj = yaml.load(blueprintContent);
60         Map<String,Object> inputsObj = (Map<String, Object>) obj.get("inputs");
61         for(Map.Entry<String,Object> entry: inputsObj.entrySet()){
62             LinkedHashMap<String, Object> modified = retainQuotesForDefault(entry.getValue());
63             entry.setValue(modified);
64             if(entry.getKey().matches(locationPort+".*url")) {
65                 Map<String,String> inputValue = (Map<String, String>) entry.getValue();
66                 inputValue.put("default",topicUrl + "/" + dmaapEntityName);
67             }
68         }
69         node.getBlueprintData().setBlueprint_content(Fixes.applyFixes(yaml.dump(obj)));
70     }
71
72     private LinkedHashMap<String, Object> retainQuotesForDefault(Object valueOfInputObject) {
73         LinkedHashMap<String, Object> temp = (LinkedHashMap<String, Object>) valueOfInputObject;
74         if(temp.containsKey("type") && temp.get("type").equals("string")) {
75             String def = (String) temp.get("default");
76             if(def != null){
77                 def = def.replaceAll("\"$", "").replaceAll("^\"", "");
78             }
79             def = '"' + def + '"';
80             temp.replace("default", def);
81         }
82         return temp;
83     }
84
85     private Yaml getYamlInstance() {
86         DumperOptions options = new DumperOptions();
87         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
88         options.setPrettyFlow(true);
89         return new Yaml(options);
90     }
91
92 //    private String attachSingleQoutes(String str) {
93 //        return "'" + str + "'";
94 //    }
95 }