c45173d10acae6443a0f38cad4d2c5e7da3c35a2
[dcaegen2/platform.git] /
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     private boolean useDmaapPlugin;
35
36     public void setTopicUrl(String topicUrl) {
37         this.topicUrl = topicUrl;
38     }
39
40     public void setImportFilePath(String importFilePath) {
41         this.importFilePath = importFilePath;
42     }
43
44     public void setUseDmaapPlugin(boolean useDmaapPlugin) {
45         this.useDmaapPlugin = useDmaapPlugin;
46     }
47
48     @Override
49     public String createBlueprint(String componentSpecString) {
50         ComponentSpec componentSpec = new ComponentSpec();
51         componentSpec.createComponentSpecFromString(componentSpecString);
52         Blueprint blueprint = new Blueprint().createBlueprint(componentSpec,"",useDmaapPlugin?'d':'o',importFilePath,"");
53         return blueprint.blueprintToString();
54     }
55
56     @Override
57     public void resolveDmaapConnection(Node node, String locationPort, String dmaapEntityName) {
58         if(node == null || locationPort == null){
59             return;
60         }
61         String blueprintContent = node.getBlueprintData().getBlueprint_content();
62         locationPort = locationPort.replaceAll("-","_");
63         Yaml yaml = getYamlInstance();
64         Map<String,Object> obj = yaml.load(blueprintContent);
65         Map<String,Object> inputsObj = (Map<String, Object>) obj.get("inputs");
66         for(Map.Entry<String,Object> entry: inputsObj.entrySet()){
67             LinkedHashMap<String, Object> modified = retainQuotesForDefault(entry.getValue());
68             entry.setValue(modified);
69             if(entry.getKey().matches(locationPort+".*url")) {
70                 Map<String,String> inputValue = (Map<String, String>) entry.getValue();
71                 inputValue.put("default",topicUrl + "/" + dmaapEntityName);
72             }
73         }
74         node.getBlueprintData().setBlueprint_content(Fixes.applyFixes(yaml.dump(obj)));
75     }
76
77     private LinkedHashMap<String, Object> retainQuotesForDefault(Object valueOfInputObject) {
78         LinkedHashMap<String, Object> temp = (LinkedHashMap<String, Object>) valueOfInputObject;
79         if(temp.containsKey("type") && temp.get("type").equals("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         }
87         return temp;
88     }
89
90     private Yaml getYamlInstance() {
91         DumperOptions options = new DumperOptions();
92         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
93         options.setPrettyFlow(true);
94         return new Yaml(options);
95     }
96
97 //    private String attachSingleQoutes(String str) {
98 //        return "'" + str + "'";
99 //    }
100 }