DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / asdc / src / main / java / org / onap / sdc / dcae / catalog / asdc / Cloudify.java
1 package org.onap.sdc.dcae.catalog.asdc;
2
3 import java.util.AbstractMap;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.Map;
8 import java.util.stream.Stream;
9
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;
20
21 import com.google.common.collect.Lists;
22 import org.yaml.snakeyaml.DumperOptions;
23 import org.yaml.snakeyaml.Yaml;
24
25
26 public class Cloudify {
27
28         private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
29         private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
30
31         Catalog catalog;
32
33         public Cloudify(Catalog c)
34         {
35                 catalog = c;
36         }
37         public class ModelTemplate {
38                 public Map<String, Map> template;
39                 public JXPathContext jx;
40                 public String node;
41                 public ModelTemplate(Map<String, Map> t, JXPathContext j, String node_name)
42                 {
43                         template = t;
44                         jx = j;
45                         node = node_name;
46                 }               
47                 
48                 public Object getPropValue(JXPathContext jx_src, String name)
49                 {
50                         try{
51                                 Object ret = jx_src.getValue("properties/"+name+"/get_input");
52                                 if (ret==null)
53                                         return jx_src.getValue("properties/"+name);
54                                 return getDefaultPropValue((String)ret);
55                         }
56                         catch (RuntimeException e) {
57
58                         }
59                         try{
60                                 return jx_src.getValue("properties/"+name+"");
61                         }
62                         catch (RuntimeException e) {
63                                 return null;
64                         }                                               
65                 }
66                 
67                 public Object getDefaultPropValue(String name) {
68                         try {
69                                 return jx.getValue("//"+name+"/default");
70                         }
71                         catch (RuntimeException e) {
72                                 return null;
73                         }
74
75                 }
76         }
77         
78         public class ModelTranslate {
79                 public Map<String, Map> template;
80                 public JXPathContext jx;
81                 public String node;
82
83                 public ModelTranslate(Map<String, Map> t, JXPathContext j, String node_name)
84                 {
85                         template = t;
86                         jx = j;
87                         node = node_name;
88                 }
89                 
90                 public String getTranslateName()
91                 {
92                         Map<String, Object> node_temp = (Map<String, Object>)jx.getValue("//node_templates");
93                         Iterator it = node_temp.keySet().iterator();
94                         if (it.hasNext())
95                                 return node + "_"+ it.next();
96                         else
97                                 return null;
98                 }
99                 
100                 public Map<String, Object> translate(JXPathContext jx_src, Map<String, Map> model_lib, String node_name)
101                 {
102                         for (Iterator prop_iter = jx.iteratePointers("//*[@get_input]"); prop_iter.hasNext();) {
103
104                                 Pointer p = (Pointer)prop_iter.next();
105                                 JXPathContext prop_path = jx.getRelativeContext(p);
106
107                                 ModelTemplate src_model =(ModelTemplate) model_lib.get(node_name).get("model");
108
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);
112                         }
113                         
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());
118
119                                 for (Iterator it = model_lib.keySet().iterator(); it.hasNext();) {
120                                         String key = (String) it.next();
121                                         if (key.indexOf(req_node_name) <0 )
122                                                 continue;
123                                         ModelTranslate tt = (ModelTranslate) model_lib.get(key).get("translate");
124                                         if (tt == null)
125                                                 req_node_name = null;
126                                         else
127                                         {
128                                                 req_node_name = tt.getTranslateName();
129                                         }
130                                         break;                                  
131                                 }       
132                                 
133                         }
134                         
135                         String tn_name = getTranslateName();
136                         
137                         if (tn_name == null)            
138                                 return (Map<String, Object>)jx.getValue("//node_templates");
139                         else
140                                 return (new MapBuilder<String, Object>().put(tn_name, jx.getValue("//node_templates/*")).build());
141                 }
142                 
143         }
144         
145         public ModelTranslate findTranslateTemplate(String ty, String node) {
146                 for (Target t: catalog.targets()) {
147
148                         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "findTranslateTemplate: target {}", t.getName());
149                         if (t.getName().startsWith("translat") == false) {
150                                 continue;
151                         }
152
153                         Map<String, Map>temp = (Map<String, Map>)t.getTarget();
154                         
155                         JXPathContext jxroot = JXPathContext.newContext(temp);
156                         try{
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);
160                                 }
161                         }
162                         catch (RuntimeException e) {
163                                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "translate template {} does not have substitution mapping section", t.getName());
164                         }
165                 }
166                 return null;
167         }
168         
169         public ModelTemplate findModelTemplate(String ty, String node) {
170                 for (Target t: catalog.targets()) {
171
172                         if (t.getName().startsWith("templat") == false)
173                                 continue;
174                         Map<String, Map>temp = (Map<String, Map>)t.getTarget();
175                         
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);
181                                 }
182                         }
183                 }
184                 return null;
185         }
186         
187         public Map<String, Object> createBlueprint()    {
188                 
189                 Map<String, Map> target_temp = null; 
190                 for (Target t: catalog.targets()) {
191
192                         if (t.getName().equals("cdump")) {
193                                 target_temp = catalog.getTargetTemplates(t, Construct.Node);
194                         }
195                 }
196
197                 JXPathContext jxroot = JXPathContext.newContext(target_temp);
198                 
199                 Map<String, Object> output_temp = new HashMap<String, Object>();
200                 Map<String, Map> model_lib = new HashMap<String, Map>();
201
202                 for (Iterator iter = target_temp.keySet().iterator(); iter.hasNext();)
203                 {
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");
208
209                         ModelTranslate t_temp = findTranslateTemplate(node_type, node_key);
210                         ModelTemplate t_model = findModelTemplate(node_type, node_key);
211                         
212                         model_lib.put(node_key, new MapBuilder()
213                                                                                                                                                         .put("model", t_model)
214                                                                                                                                                         .put("translate", t_temp)
215                                                                                                                                                         .build());
216                 }
217                 
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));
222                         if (t != null) {
223                                 Map<String, Object> t_output =t.translate(jxnode, model_lib, node_key);
224                                 if (t_output != null)
225                                         output_temp.putAll(t_output);
226                         }
227
228                 }
229                 
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()
234                                                                                                                                                                                                 .put("cloudify",
235                                                                                                                                                                                                                  "http://www.getcloudify.org/spec/cloudify/3.4/types.yaml")
236                                                                                                                                                                                                 .build())
237                                                                                                                                                                 .build())
238                                                                                         .put("node_templates", output_temp)
239                                                                                         .build();
240
241         }
242
243         public String createBlueprintDocument() {
244                 DumperOptions options = new DumperOptions();
245                 options.setWidth(1000000);
246                 Yaml yaml = new Yaml(options);
247                 return yaml.dump(createBlueprint());
248         }
249 }