Update runtime-api with refactored bp-gen
[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  * Copyright (C) 2020 Nokia. 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 package org.onap.dcae.runtime.core.blueprint_creator;
20
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23 import org.onap.blueprintgenerator.model.base.Blueprint;
24 import org.onap.blueprintgenerator.model.common.Input;
25 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
26 import org.onap.blueprintgenerator.service.BlueprintCreatorService;
27 import org.onap.blueprintgenerator.service.base.BlueprintService;
28 import org.onap.blueprintgenerator.service.base.FixesService;
29 import org.onap.blueprintgenerator.service.common.ComponentSpecService;
30 import org.onap.dcae.runtime.core.Node;
31 import org.yaml.snakeyaml.DumperOptions;
32 import org.yaml.snakeyaml.Yaml;
33
34 public class BlueprintCreatorOnap implements BlueprintCreator {
35
36     private String topicUrl;
37     private String importFilePath;
38     private boolean useDmaapPlugin;
39     private final ComponentSpecService componentSpecService;
40     private final BlueprintCreatorService blueprintCreatorService;
41     private final BlueprintService blueprintService;
42     private final FixesService fixesService;
43
44     public BlueprintCreatorOnap(ComponentSpecService componentSpecService,
45         BlueprintCreatorService blueprintCreatorService, BlueprintService blueprintService,
46         FixesService fixesService) {
47         this.componentSpecService = componentSpecService;
48         this.blueprintCreatorService = blueprintCreatorService;
49         this.blueprintService = blueprintService;
50         this.fixesService = fixesService;
51     }
52
53     public void setTopicUrl(String topicUrl) {
54         this.topicUrl = topicUrl;
55     }
56
57     public void setImportFilePath(String importFilePath) {
58         this.importFilePath = importFilePath;
59     }
60
61     public void setUseDmaapPlugin(boolean useDmaapPlugin) {
62         this.useDmaapPlugin = useDmaapPlugin;
63     }
64
65     @Override
66     public String createBlueprint(String componentSpecString) {
67         OnapComponentSpec componentSpec = componentSpecService.createComponentSpecFromString(componentSpecString);
68         Input input = new Input();
69         input.setBpType(useDmaapPlugin ? "d" : "o");
70         input.setImportPath(importFilePath);
71         Blueprint blueprint = blueprintCreatorService.createBlueprint(componentSpec, input);
72         return blueprintService.blueprintToString(componentSpec, blueprint, input);
73     }
74
75     @Override
76     public void resolveDmaapConnection(Node node, String locationPort, String dmaapEntityName) {
77         if (node == null || locationPort == null) {
78             return;
79         }
80         String blueprintContent = node.getBlueprintData().getBlueprint_content();
81         locationPort = locationPort.replaceAll("-", "_");
82         Yaml yaml = getYamlInstance();
83         Map<String, Object> obj = yaml.load(blueprintContent);
84         Map<String, Object> inputsObj = (Map<String, Object>) obj.get("inputs");
85         for (Map.Entry<String, Object> entry : inputsObj.entrySet()) {
86             LinkedHashMap<String, Object> modified = retainQuotesForDefault(entry.getValue());
87             entry.setValue(modified);
88             if (entry.getKey().matches(locationPort + ".*url")) {
89                 Map<String, String> inputValue = (Map<String, String>) entry.getValue();
90                 inputValue.put("default", topicUrl + "/" + dmaapEntityName);
91             }
92         }
93         node.getBlueprintData().setBlueprint_content(fixesService.applyFixes(yaml.dump(obj)));
94     }
95
96     private LinkedHashMap<String, Object> retainQuotesForDefault(Object valueOfInputObject) {
97         LinkedHashMap<String, Object> temp = (LinkedHashMap<String, Object>) valueOfInputObject;
98         if (temp.containsKey("type") && temp.get("type").equals("string")) {
99             String def = (String) temp.get("default");
100             if (def != null) {
101                 def = def.replaceAll("\"$", "").replaceAll("^\"", "");
102             }
103             def = '"' + def + '"';
104             temp.replace("default", def);
105         }
106         return temp;
107     }
108
109     private Yaml getYamlInstance() {
110         DumperOptions options = new DumperOptions();
111         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
112         options.setPrettyFlow(true);
113         return new Yaml(options);
114     }
115
116 //    private String attachSingleQoutes(String str) {
117 //        return "'" + str + "'";
118 //    }
119 }