Add mod/runtimeapi
[dcaegen2/platform.git] / mod / runtimeapi / runtime-core / src / main / java / org / onap / dcae / runtime / core / blueprint_creator / BlueprintCreatorOnapDublin.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.dcae.runtime.core.Node;
21 import org.onap.blueprintgenerator.models.blueprint.Blueprint;
22 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
23 import org.yaml.snakeyaml.DumperOptions;
24 import org.yaml.snakeyaml.Yaml;
25
26 import java.util.Map;
27
28 public class BlueprintCreatorOnapDublin implements BlueprintCreator{
29
30     private String topicUrl;
31     private String importFilePath;
32
33     public void setTopicUrl(String topicUrl) {
34         this.topicUrl = topicUrl;
35     }
36
37     public void setImportFilePath(String importFilePath) {
38         this.importFilePath = importFilePath;
39     }
40
41     @Override
42     public String createBlueprint(String componentSpecString) {
43         ComponentSpec componentSpec = new ComponentSpec();
44         componentSpec.createComponentSpecFromString(componentSpecString);
45         Blueprint blueprint = new Blueprint().createBlueprint(componentSpec,"",'o',importFilePath);
46         return blueprint.blueprintToString();
47     }
48
49     @Override
50     public void resolveDmaapConnection(Node node, String locationPort, String dmaapEntityName) {
51         if(node == null || locationPort == null){
52             return;
53         }
54         String blueprintContent = node.getBlueprintData().getBlueprint_content();
55         locationPort = locationPort.replaceAll("-","_");
56         Yaml yaml = getYamlInstance();
57         Map<String,Object> obj = yaml.load(blueprintContent);
58         Map<String,Object> inputsObj = (Map<String, Object>) obj.get("inputs");
59         for(Map.Entry<String,Object> entry: inputsObj.entrySet()){
60             if(entry.getKey().matches(locationPort+".*url")) {
61                 Map<String,String> inputValue = (Map<String, String>) entry.getValue();
62                 inputValue.put("default",topicUrl + "/" + dmaapEntityName);
63             }
64         }
65         node.getBlueprintData().setBlueprint_content(yaml.dump(obj));
66     }
67
68     private Yaml getYamlInstance() {
69         DumperOptions options = new DumperOptions();
70         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
71         options.setPrettyFlow(true);
72         return new Yaml(options);
73     }
74
75 //    private String attachSingleQoutes(String str) {
76 //        return "'" + str + "'";
77 //    }
78 }