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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
18 package org.onap.dcae.runtime.core.blueprint_creator;
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;
27 import java.util.LinkedHashMap;
30 public class BlueprintCreatorOnap implements BlueprintCreator{
32 private String topicUrl;
33 private String importFilePath;
34 private boolean useDmaapPlugin;
36 public void setTopicUrl(String topicUrl) {
37 this.topicUrl = topicUrl;
40 public void setImportFilePath(String importFilePath) {
41 this.importFilePath = importFilePath;
44 public void setUseDmaapPlugin(boolean useDmaapPlugin) {
45 this.useDmaapPlugin = useDmaapPlugin;
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();
57 public void resolveDmaapConnection(Node node, String locationPort, String dmaapEntityName) {
58 if(node == null || locationPort == null){
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);
74 node.getBlueprintData().setBlueprint_content(Fixes.applyFixes(yaml.dump(obj)));
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");
82 def = def.replaceAll("\"$", "").replaceAll("^\"", "");
84 def = '"' + def + '"';
85 temp.replace("default", def);
90 private Yaml getYamlInstance() {
91 DumperOptions options = new DumperOptions();
92 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
93 options.setPrettyFlow(true);
94 return new Yaml(options);
97 // private String attachSingleQoutes(String str) {
98 // return "'" + str + "'";