1 package org.onap.sdc.dcae.catalog.asdc;
3 import java.util.AbstractMap;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
8 import java.util.stream.Stream;
10 import org.apache.commons.jxpath.JXPathContext;
11 import org.apache.commons.jxpath.Pointer;
12 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
13 import org.onap.sdc.common.onaplog.OnapLoggerError;
14 import org.onap.sdc.common.onaplog.Enums.LogLevel;
15 import org.onap.sdc.dcae.catalog.commons.ListBuilder;
16 import org.onap.sdc.dcae.catalog.commons.MapBuilder;
17 import org.onap.sdc.dcae.checker.Catalog;
18 import org.onap.sdc.dcae.checker.Construct;
19 import org.onap.sdc.dcae.checker.Target;
21 import com.google.common.collect.Lists;
22 import org.yaml.snakeyaml.DumperOptions;
23 import org.yaml.snakeyaml.Yaml;
26 public class Cloudify {
28 private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
29 private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
33 public Cloudify(Catalog c)
37 public class ModelTemplate {
38 public Map<String, Map> template;
39 public JXPathContext jx;
41 public ModelTemplate(Map<String, Map> t, JXPathContext j, String node_name)
48 public Object getPropValue(JXPathContext jx_src, String name)
51 Object ret = jx_src.getValue("properties/"+name+"/get_input");
53 return jx_src.getValue("properties/"+name);
54 return getDefaultPropValue((String)ret);
56 catch (RuntimeException e) {
60 return jx_src.getValue("properties/"+name+"");
62 catch (RuntimeException e) {
67 public Object getDefaultPropValue(String name) {
69 return jx.getValue("//"+name+"/default");
71 catch (RuntimeException e) {
78 public class ModelTranslate {
79 public Map<String, Map> template;
80 public JXPathContext jx;
83 public ModelTranslate(Map<String, Map> t, JXPathContext j, String node_name)
90 public String getTranslateName()
92 Map<String, Object> node_temp = (Map<String, Object>)jx.getValue("//node_templates");
93 Iterator it = node_temp.keySet().iterator();
95 return node + "_"+ it.next();
100 public Map<String, Object> translate(JXPathContext jx_src, Map<String, Map> model_lib, String node_name)
102 for (Iterator prop_iter = jx.iteratePointers("//*[@get_input]"); prop_iter.hasNext();) {
104 Pointer p = (Pointer)prop_iter.next();
105 JXPathContext prop_path = jx.getRelativeContext(p);
107 ModelTemplate src_model =(ModelTemplate) model_lib.get(node_name).get("model");
109 Object temp_o = src_model.getPropValue(jx_src, (String) prop_path.getValue("get_input"));
110 //prop_path.setValue(".", temp_o);
111 jx.setValue(p.asPath(), temp_o);
114 // JXPathContext jx_src = JXPathContext.newContext(src);
115 for (Iterator req_iter = jx_src.iteratePointers("//*/node"); req_iter.hasNext();) {
116 Pointer p = (Pointer)req_iter.next();
117 String req_node_name = (String)jx_src.getValue(p.asPath());
119 for (Iterator it = model_lib.keySet().iterator(); it.hasNext();) {
120 String key = (String) it.next();
121 if (key.indexOf(req_node_name) <0 )
123 ModelTranslate tt = (ModelTranslate) model_lib.get(key).get("translate");
125 req_node_name = null;
128 req_node_name = tt.getTranslateName();
135 String tn_name = getTranslateName();
138 return (Map<String, Object>)jx.getValue("//node_templates");
140 return (new MapBuilder<String, Object>().put(tn_name, jx.getValue("//node_templates/*")).build());
145 public ModelTranslate findTranslateTemplate(String ty, String node) {
146 for (Target t: catalog.targets()) {
148 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "findTranslateTemplate: target {}", t.getName());
149 if (t.getName().startsWith("translat") == false) {
153 Map<String, Map>temp = (Map<String, Map>)t.getTarget();
155 JXPathContext jxroot = JXPathContext.newContext(temp);
157 String sub_type = (String)jxroot.getValue("topology_template/substitution_mappings/node_type");
158 if (sub_type != null && sub_type.equals(ty)) {
159 return new ModelTranslate(temp, jxroot, node);
162 catch (RuntimeException e) {
163 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "translate template {} does not have substitution mapping section", t.getName());
169 public ModelTemplate findModelTemplate(String ty, String node) {
170 for (Target t: catalog.targets()) {
172 if (t.getName().startsWith("templat") == false)
174 Map<String, Map>temp = (Map<String, Map>)t.getTarget();
176 JXPathContext jxroot = JXPathContext.newContext(temp);
177 for (Iterator it = jxroot.iterate("topology_template/node_templates/*/type"); it.hasNext();) {
178 String node_type = (String)it.next();
179 if (node_type != null && node_type.equals(ty)) {
180 return new ModelTemplate(temp, jxroot, node);
187 public Map<String, Object> createBlueprint() {
189 Map<String, Map> target_temp = null;
190 for (Target t: catalog.targets()) {
192 if (t.getName().equals("cdump")) {
193 target_temp = catalog.getTargetTemplates(t, Construct.Node);
197 JXPathContext jxroot = JXPathContext.newContext(target_temp);
199 Map<String, Object> output_temp = new HashMap<String, Object>();
200 Map<String, Map> model_lib = new HashMap<String, Map>();
202 for (Iterator iter = target_temp.keySet().iterator(); iter.hasNext();)
204 String node_key = (String)iter.next();
205 //jxroot.getVariables().declareVariable("name", target_temp.get(node_key));
206 //String node_type = (String)jxroot.getValue("$name/type");
207 String node_type = (String)jxroot.getValue(node_key+"/type");
209 ModelTranslate t_temp = findTranslateTemplate(node_type, node_key);
210 ModelTemplate t_model = findModelTemplate(node_type, node_key);
212 model_lib.put(node_key, new MapBuilder()
213 .put("model", t_model)
214 .put("translate", t_temp)
218 for (Iterator iter = model_lib.keySet().iterator(); iter.hasNext();) {
219 String node_key = (String) iter.next();
220 ModelTranslate t = (ModelTranslate) model_lib.get(node_key).get("translate");
221 JXPathContext jxnode = jxroot.getRelativeContext(jxroot.getPointer(node_key));
223 Map<String, Object> t_output =t.translate(jxnode, model_lib, node_key);
224 if (t_output != null)
225 output_temp.putAll(t_output);
230 return new MapBuilder<String, Object>()
231 .put("tosca_definitions_version", new String("cloudify_dsl_1_3"))
232 .put("imports", new ListBuilder()
233 .add(new MapBuilder()
235 "http://www.getcloudify.org/spec/cloudify/3.4/types.yaml")
238 .put("node_templates", output_temp)
243 public String createBlueprintDocument() {
244 DumperOptions options = new DumperOptions();
245 options.setWidth(1000000);
246 Yaml yaml = new Yaml(options);
247 return yaml.dump(createBlueprint());