ced108380e2ba3b5ee78b416b6e67bd5199fa3f4
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.tosca.datatypes;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.openecomp.sdc.tosca.datatypes.model.ArtifactType;
26 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
27 import org.openecomp.sdc.tosca.datatypes.model.CapabilityAssignment;
28 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
29 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
30 import org.openecomp.sdc.tosca.datatypes.model.Directive;
31 import org.openecomp.sdc.tosca.datatypes.model.Import;
32 import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinition;
33 import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinitionType;
34 import org.openecomp.sdc.tosca.datatypes.model.InterfaceType;
35 import org.openecomp.sdc.tosca.datatypes.model.NodeFilter;
36 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
37 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
38 import org.openecomp.sdc.tosca.datatypes.model.OperationDefinition;
39 import org.openecomp.sdc.tosca.datatypes.model.OperationDefinitionType;
40 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
41 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
42 import org.openecomp.sdc.tosca.datatypes.model.PropertyType;
43 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
44 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
45 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
46 import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping;
47 import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate;
48 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
49 import org.openecomp.sdc.tosca.services.DataModelUtil;
50 import org.openecomp.sdc.tosca.services.ToscaConstants;
51 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
52 import org.openecomp.sdc.tosca.services.YamlUtil;
53
54 import java.io.IOException;
55 import java.io.InputStream;
56 import java.util.ArrayList;
57 import java.util.HashMap;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.Optional;
61
62
63 public class ToscaModelTest {
64   private YamlUtil yamlUtil = new YamlUtil();
65   private static final String INTERFACE_ID = "inter_1";
66   private static final String NODE_TEMPLATE_ID = "firstNodeTemplate";
67   private static final String BASE_DIR = "/mock/model";
68   private static final String ST = "/serviceTemplate.yaml";
69   private static final String ST_WITH_INTERFACE = "/serviceTemplateWithInterface.yaml";
70   private static final String ST_WITH_OPERATIONS = "/serviceTemplateWithInterfaceAndOperation.yaml";
71   private static final String ST_WITH_INTERFACE_DEF =
72       "/serviceTemplateWithNodeTemplateInterface.yaml";
73
74
75   @Test
76   public void testServiceTemplateJavaToYaml() {
77     ServiceTemplate serviceTemplate = new ServiceTemplate();
78     Map<String, String> metadata = new HashMap<>();
79     metadata.put("Template_author", "OPENECOMP");
80     metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test");
81     metadata.put("Template_version", "1.0.0");
82     serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
83     serviceTemplate.setDescription("testing desc tosca service template");
84
85     Import fileImport1 = new Import();
86     fileImport1.setFile("path1/path2/file1.yaml");
87     Import fileImport2 = new Import();
88     fileImport2.setFile("path1/path2/file2.yaml");
89     List<Map<String, Import>> imports = new ArrayList<>();
90     Map<String, Import> importsMap = new HashMap<>();
91     importsMap.put("myfile1", fileImport1);
92     imports.add(importsMap);
93     importsMap = new HashMap<>();
94     importsMap.put("myfile2", fileImport2);
95     imports.add(importsMap);
96     serviceTemplate.setImports(imports);
97
98     OperationDefinition operationDefinition = new OperationDefinition();
99     operationDefinition.setDescription("test operation");
100     InterfaceType interfaceType = new InterfaceType();
101     interfaceType.setDerived_from("derived_from");
102     interfaceType.setDescription("desc");
103     interfaceType.addOperation("test", operationDefinition);
104     serviceTemplate.addInterfaceType("test_interface", interfaceType);
105
106     ArtifactType artifact = new ArtifactType();
107     artifact.setMime_type("application/java-archive");
108     ArrayList<String> ext = new ArrayList<>();
109     ext.add("yaml");
110     ext.add("xml");
111     artifact.setFile_ext(ext);
112     Map<String, ArtifactType> artifactTypes = new HashMap<>();
113     artifactTypes.put("one_artifact", artifact);
114     serviceTemplate.setArtifact_types(artifactTypes);
115
116     NodeType nodeType = new NodeType();
117     nodeType.setDerived_from("tosca.nodes.Root");
118     nodeType.setVersion("1.0.0");
119     nodeType.setDescription("tosca compute test");
120
121     PropertyDefinition propDef1 = new PropertyDefinition();
122     propDef1.setType("integer");
123     propDef1.setDescription("Number of CPUs requested for a software node instance");
124     propDef1.setRequired(true);
125     propDef1.set_default(1);
126
127     Constraint graterOrEqual = new Constraint();
128     graterOrEqual.setGreater_or_equal((float) 5.0);
129     Constraint constraintEqual = new Constraint();
130     constraintEqual.setEqual(5);
131     Constraint greater_than = new Constraint();
132     greater_than.setGreater_than(6.02);
133     Constraint inRange = new Constraint();
134     inRange.setIn_range(new Object[2]);
135     inRange.getIn_range()[0] = 0;
136     inRange.getIn_range()[1] = ToscaConstants.UNBOUNDED;
137
138     List<Constraint> constraints = new ArrayList<>();
139     constraints.add(graterOrEqual);
140     constraints.add(constraintEqual);
141     constraints.add(greater_than);
142     constraints.add(inRange);
143     propDef1.setConstraints(constraints);
144
145     Map<String, PropertyDefinition> properties = new HashMap<>();
146     properties.put("cpu_num", propDef1);
147     nodeType.setProperties(properties);
148
149     Map<String, AttributeDefinition> attributesDef = new HashMap<>();
150     AttributeDefinition attDef = new AttributeDefinition();
151     attDef.setType(PropertyType.STRING.getDisplayName());
152     attDef.set_default("hi");
153     attributesDef.put("attDef1", attDef);
154     nodeType.setAttributes(attributesDef);
155
156     Map<String, RequirementDefinition> reqsDef = new HashMap<>();
157     RequirementDefinition reqDef = new RequirementDefinition();
158     reqDef.setCapability("tosca.cap1");
159     reqDef.getOccurrences()[0] = 5;
160     reqsDef.put("re1", reqDef);
161     List<Map<String, RequirementDefinition>> reqList = new ArrayList<>();
162     reqList.add(reqsDef);
163     nodeType.setRequirements(reqList);
164
165
166     Map<String, CapabilityDefinition> capsDef = new HashMap<>();
167     CapabilityDefinition capdef = new CapabilityDefinition();
168     capdef.setType("tosca.cap");
169     List<String> vvSource = new ArrayList<>();
170     vvSource.add("node1");
171     vvSource.add("node2");
172     capdef.setValid_source_types(vvSource);
173     capsDef.put("cap1", capdef);
174     nodeType.setCapabilities(capsDef);
175
176     Map<String, NodeType> nodeTypes = new HashMap<>();
177     nodeTypes.put("compute_node_type", nodeType);
178     serviceTemplate.setNode_types(nodeTypes);
179
180     TopologyTemplate topologyTemplate = new TopologyTemplate();
181     topologyTemplate.setDescription("topologi template descroption");
182     Map<String, ParameterDefinition> inputs = new HashMap<>();
183     ParameterDefinition paramDef = new ParameterDefinition();
184     paramDef.setType(PropertyType.STRING.getDisplayName());
185     paramDef.setDescription("desc");
186     paramDef.set_default("my default val");
187     paramDef.setRequired(false);
188     paramDef.setEntry_schema(DataModelUtil.createEntrySchema("tosca.myType", null, null));
189     List<Constraint> paramConstraint = new ArrayList<>();
190     Constraint paramConst1 = new Constraint();
191     paramConst1.setGreater_than(6);
192     Constraint paramConst2 = new Constraint();
193     paramConst2.setGreater_or_equal(9);
194     paramConstraint.add(paramConst1);
195     paramConstraint.add(paramConst2);
196     paramDef.setConstraints(paramConstraint);
197     inputs.put("inParam1", paramDef);
198     topologyTemplate.setInputs(inputs);
199
200     Map<String, NodeTemplate> nodeTemplates = new HashMap<>();
201     NodeTemplate nodeTemplate = new NodeTemplate();
202     nodeTemplate.setType("nodeTypeRef");
203     List<String> directives = new ArrayList<>();
204     directives.add(Directive.SELECTABLE.getDisplayName());
205     directives.add(Directive.SUBSTITUTABLE.getDisplayName());
206     nodeTemplate.setDirectives(directives);
207     Map<String, Object> nodeTemplateProperties = new HashMap<>();
208     nodeTemplateProperties.put("prop1", "abcd");
209     nodeTemplateProperties.put("prop2", "{ get_input: my_mysql_rootpw }");
210     nodeTemplate.setProperties(nodeTemplateProperties);
211     Map<String, Object> nodeTemplateAtts = new HashMap<>();
212     nodeTemplateAtts.put("att1", "att1Val");
213     nodeTemplateAtts.put("att2", "{ get_input: my_mysql_rootpw }");
214     nodeTemplate.setAttributes(nodeTemplateAtts);
215
216
217     RequirementAssignment reqAssignment1 = new RequirementAssignment();
218     reqAssignment1.setNode("nodeA");
219     reqAssignment1.setCapability("capA");
220     reqAssignment1.setRelationship("relationB");
221     Object[] reqAssOccurrences = new Object[2];
222     reqAssOccurrences[0] = 1;
223     reqAssOccurrences[1] = 2;
224     reqAssignment1.setOccurrences(reqAssOccurrences);
225     NodeFilter reqNodeFilter = new NodeFilter();
226     List<Constraint> propConstrain1 = new ArrayList<>();
227     Constraint propConst1 = new Constraint();
228     propConst1.setGreater_or_equal(9);
229     propConstrain1.add(propConst1);
230     List<Constraint> propConstrain2 = new ArrayList<>();
231     Constraint propConst2 = new Constraint();
232     propConst2.setMin_length(1);
233     propConstrain2.add(propConst2);
234     Constraint propConst3 = new Constraint();
235     propConst3.setMax_length(2);
236     propConstrain2.add(propConst3);
237     Map<String, List<Constraint>> nodeFilterProp = new HashMap<>();
238     nodeFilterProp.put("propName1", propConstrain1);
239     nodeFilterProp.put("propName2", propConstrain2);
240     reqNodeFilter.setProperties(nodeFilterProp);
241     reqAssignment1.setNode_filter(reqNodeFilter);
242
243     RequirementAssignment reqAssignment2 = new RequirementAssignment();
244     reqAssignment2.setNode("nodeA");
245     reqAssignment2.setCapability("capA");
246     reqAssignment2.setRelationship("relationB");
247     Map<String, RequirementAssignment> nodeTemplateRequirement1 = new HashMap<>();
248     Map<String, RequirementAssignment> nodeTemplateRequirement2 = new HashMap<>();
249     nodeTemplateRequirement1.put("req1", reqAssignment1);
250     nodeTemplateRequirement2.put("req2", reqAssignment2);
251     nodeTemplate.setRequirements(new ArrayList<>());
252     nodeTemplate.getRequirements().add(nodeTemplateRequirement1);
253     nodeTemplate.getRequirements().add(nodeTemplateRequirement2);
254
255     Map<String, CapabilityAssignment> nodeTemplateCapability = new HashMap<>();
256     CapabilityAssignment capAss = new CapabilityAssignment();
257     Map<String, Object> capProps = new HashMap<>();
258     capProps.put("num_cpus", "{ get_input: cpus }");
259     capAss.setProperties(capProps);
260     Map<String, Object> capAtts = new HashMap<>();
261     capAtts.put("num_cpus", "66");
262     capAss.setAttributes(capAtts);
263     nodeTemplateCapability.put("cap1", capAss);
264     nodeTemplate.setCapabilities(nodeTemplateCapability);
265
266     NodeFilter nodeTemplateNodeFilter = new NodeFilter();
267     Map<String, List<Constraint>> ntProp = new HashMap<>();
268     Constraint c1 = new Constraint();
269     c1.setEqual("1 MB");
270     List<Constraint> consList = new ArrayList<>();
271     consList.add(c1);
272     ntProp.put("test1", consList);
273     nodeTemplateNodeFilter.setProperties(ntProp);
274     nodeTemplate.setNode_filter(nodeTemplateNodeFilter);
275     nodeTemplates.put("firatNodeTemplate", nodeTemplate);
276     topologyTemplate.setNode_templates(nodeTemplates);
277
278     SubstitutionMapping subMap = new SubstitutionMapping();
279     subMap.setNode_type("myNodeType.node");
280     Map<String, List<String>> mapCapabilities = new HashMap<>();
281     List<String> NodeCap = new ArrayList<>();
282     NodeCap.add("database");
283     NodeCap.add("database_endpoint");
284     mapCapabilities.put("database_endpoint", NodeCap);
285     subMap.setCapabilities(mapCapabilities);
286     topologyTemplate.setSubstitution_mappings(subMap);
287     serviceTemplate.setTopology_template(topologyTemplate);
288
289     String yaml = new YamlUtil().objectToYaml(serviceTemplate);
290     ServiceTemplate serviceTemplateFromYaml =
291         new YamlUtil().yamlToObject(yaml, ServiceTemplate.class);
292     Assert.assertNotNull(serviceTemplateFromYaml);
293   }
294
295
296   @Test
297   public void testYamlToServiceTemplateObj() throws IOException {
298     ServiceTemplate serviceTemplateFromYaml =
299         getServiceTemplate(BASE_DIR + ST);
300     Assert.assertNotNull(serviceTemplateFromYaml);
301   }
302
303   @Test
304   public void testYamlWithInterfaceToServiceTemplateObj() throws IOException {
305     ServiceTemplate serviceTemplateWithOperation =
306         getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS);
307     Assert.assertNotNull(serviceTemplateWithOperation);
308
309     InterfaceType expectedInterfaceType = getInterfaceType();
310
311     Map<String, InterfaceType> interfaceTypes =
312         DataModelUtil.getInterfaceTypes(serviceTemplateWithOperation);
313     Assert.assertEquals(1, interfaceTypes.size());
314     InterfaceType actualInterfaceType = interfaceTypes.get(INTERFACE_ID);
315     Assert.assertEquals(expectedInterfaceType, actualInterfaceType);
316   }
317
318   @Test
319   public void testAddOperationToInterface() throws IOException {
320     YamlUtil yamlUtil = new YamlUtil();
321     ServiceTemplate serviceTemplateWithInterface =
322         getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE);
323     ServiceTemplate serviceTemplateWithOperation =
324         getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS);
325
326     OperationDefinition operationDefinition = getOperationDefinition();
327
328     DataModelUtil
329         .addInterfaceOperation(serviceTemplateWithInterface, INTERFACE_ID, "start",
330             operationDefinition);
331     String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation);
332     String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface);
333     Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate);
334   }
335
336   @Test
337   public void testInterfaceTypeToObjConversion() throws IOException {
338     ServiceTemplate serviceTemplateWithInterface =
339         getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE);
340     ServiceTemplate serviceTemplateWithOperation =
341         getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS);
342     InterfaceType interfaceType = getInterfaceType();
343
344     Optional<Object> interfaceAsObj = DataModelUtil.convertInterfaceTypeToObj(interfaceType);
345     Assert.assertTrue(interfaceAsObj.isPresent());
346
347     Map<String, Object> interfaceTypes = new HashMap<>();
348     interfaceTypes.put(INTERFACE_ID, interfaceAsObj.get());
349     serviceTemplateWithInterface.setInterface_types(interfaceTypes);
350
351     String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation);
352     String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface);
353     Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate);
354   }
355
356   @Test
357   public void testObjToInterfaceTypeConversion() throws IOException, ReflectiveOperationException {
358     ServiceTemplate serviceTemplateWithOperation =
359         getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS);
360     Map<String, Object> interfaceTypes = serviceTemplateWithOperation.getInterface_types();
361     Object interfaceObj = interfaceTypes.get(INTERFACE_ID);
362     Optional<InterfaceType> actualInterfaceType =
363         DataModelUtil.convertObjToInterfaceType(INTERFACE_ID, interfaceObj);
364
365     Assert.assertTrue(actualInterfaceType.isPresent());
366     InterfaceType expectedInterfaceType = getInterfaceType();
367     Assert.assertEquals(expectedInterfaceType, actualInterfaceType.get());
368   }
369
370   @Test
371   public void testInterfaceDefinitionToObjConversion() throws IOException {
372     ServiceTemplate serviceTemplate =
373         getServiceTemplate(BASE_DIR + ST);
374     ServiceTemplate serviceTemplateWithInterfaceDef =
375         getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE_DEF);
376
377     NodeTemplate nodeTemplate =
378         DataModelUtil.getNodeTemplate(serviceTemplate, NODE_TEMPLATE_ID);
379     InterfaceDefinitionType interfaceDefinitionType = getInterfaceDefinition();
380     Optional<Object> interfaceObj = DataModelUtil.convertInterfaceDefinitionToObj(
381         interfaceDefinitionType);
382
383     Assert.assertTrue(interfaceObj.isPresent());
384     Map<String, Object> interfaces = new HashMap<>();
385     interfaces.put(INTERFACE_ID, interfaceObj.get());
386     nodeTemplate.setInterfaces(interfaces);
387
388     String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterfaceDef);
389     String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplate);
390     Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate);
391   }
392
393   @Test
394   public void testObjToInterfaceDefinitionConversion()
395       throws IOException, ReflectiveOperationException {
396     ServiceTemplate serviceTemplateWithInterfaceDef =
397         getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE_DEF);
398     NodeTemplate nodeTemplateWithInterface =
399         DataModelUtil.getNodeTemplate(serviceTemplateWithInterfaceDef, NODE_TEMPLATE_ID);
400     Map<String, Object> interfaces = nodeTemplateWithInterface.getInterfaces();
401     Object interfaceObj = interfaces.get(INTERFACE_ID);
402
403     Optional<? extends InterfaceDefinition> actualInterfaceDefinition =
404         DataModelUtil.convertObjToInterfaceDefinition(INTERFACE_ID, interfaceObj,
405             InterfaceDefinitionType.class);
406
407     Assert.assertTrue(actualInterfaceDefinition.isPresent());
408
409     InterfaceDefinitionType expectedInterfaceDefinitionType = getInterfaceDefinition();
410     Assert.assertEquals(expectedInterfaceDefinitionType, actualInterfaceDefinition.get());
411   }
412
413   @Test
414   public void testYamlToServiceTemplateIncludingHeatExtend() throws IOException {
415     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
416     try (InputStream yamlFile =
417              toscaExtensionYamlUtil.loadYamlFileIs(BASE_DIR + "/serviceTemplateHeatExtend.yaml")) {
418       ServiceTemplate serviceTemplateFromYaml =
419           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
420       ParameterDefinitionExt parameterDefinitionExt =
421           (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs()
422               .get("inParam1");
423       Assert.assertNotNull(parameterDefinitionExt.getLabel());
424       String backToYamlString = toscaExtensionYamlUtil.objectToYaml(serviceTemplateFromYaml);
425       Assert.assertNotNull(backToYamlString);
426     }
427   }
428
429   private ServiceTemplate getServiceTemplate(String inputPath) throws IOException {
430     try (InputStream yamlFile = yamlUtil.loadYamlFileIs(inputPath)) {
431       return yamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
432     }
433   }
434
435   private InterfaceType getInterfaceType() {
436     OperationDefinition operationDefinition = getOperationDefinition();
437     InterfaceType interfaceType = new InterfaceType();
438     interfaceType.setDescription("test interface");
439     interfaceType.addOperation("start", operationDefinition);
440     return interfaceType;
441   }
442
443   private OperationDefinition getOperationDefinition() {
444     OperationDefinition operationDefinition = new OperationDefinition();
445     operationDefinition.setDescription("start operation");
446     return operationDefinition;
447   }
448
449   private InterfaceDefinitionType getInterfaceDefinition() {
450     OperationDefinition operationDefinition = getOperationDefinition();
451     InterfaceDefinitionType interfaceDefinitionType = new InterfaceDefinitionType();
452     interfaceDefinitionType.setType("test interface");
453     interfaceDefinitionType.addOperation("start", operationDefinition);
454     return interfaceDefinitionType;
455   }
456
457 }
458
459
460
461
462
463