re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / imports / ExportToscaTest.java
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.ci.tests.execute.imports;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonParser;
25 import com.google.gson.reflect.TypeToken;
26 import org.apache.commons.codec.binary.Base64;
27 import org.junit.Rule;
28 import org.junit.rules.TestName;
29 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
32 import org.openecomp.sdc.be.model.*;
33 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
34 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
35 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
36 import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
37 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
38 import org.openecomp.sdc.ci.tests.datatypes.enums.*;
39 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
40 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
41 import org.openecomp.sdc.ci.tests.utils.rest.*;
42 import org.openecomp.sdc.ci.tests.utils.validation.BaseValidationUtils;
43 import org.openecomp.sdc.common.api.Constants;
44 import org.testng.annotations.Test;
45 import org.yaml.snakeyaml.Yaml;
46
47 import java.io.ByteArrayInputStream;
48 import java.io.InputStream;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.*;
53 import java.util.Map.Entry;
54 import java.util.stream.Collectors;
55
56 import static org.junit.Assert.*;
57
58 public class ExportToscaTest extends ComponentBaseTest {
59         @Rule
60         public static TestName name = new TestName();
61
62         public ExportToscaTest() {
63                 super(name, ExportToscaTest.class.getName());
64         }
65
66         @Test(enabled = true)
67         public void exportVfModuleTest() throws Exception {
68                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
69
70                 Resource createdResource = createVfFromCSAR(sdncModifierDetails, "VSPPackage");
71
72                 Map<String, Object> load = downloadAndParseToscaTemplate(sdncModifierDetails, createdResource);
73                 assertNotNull(load);
74                 Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template");
75                 assertNotNull(topology_template);
76                 Map<String, Object> groups = (Map<String, Object>) topology_template.get("groups");
77                 assertNotNull(groups);
78                 List<GroupDefinition> groupsOrigin = createdResource.getGroups();
79
80                 assertEquals("Validate groups size", groupsOrigin.size(), groups.size());
81                 for (GroupDefinition group : groupsOrigin) {
82                         Map<String, Object> groupTosca = (Map<String, Object>) groups.get(group.getName());
83                         assertNotNull(groupTosca);
84
85                         Map<String, Object> metadata = (Map<String, Object>) groupTosca.get("metadata");
86                         assertNotNull(metadata);
87
88                         String invariantUUID;
89                         String name;
90                         String UUID;
91                         String version;
92                         Map<String, Object> properties = (Map<String, Object>) groupTosca.get("properties");
93
94                         if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
95                                 invariantUUID = (String) metadata.get("vfModuleModelInvariantUUID");
96                                 name = (String) metadata.get("vfModuleModelName");
97                                 UUID = (String) metadata.get("vfModuleModelUUID");
98                                 version = (String) metadata.get("vfModuleModelVersion");
99                                 assertNotNull(properties);
100
101                                 String vf_module_type = (String) properties.get("vf_module_type");
102                                 List<PropertyDataDefinition> props = group.getProperties();
103                                 for (PropertyDataDefinition prop : props) {
104                                         if (prop.getName().equals(Constants.IS_BASE)) {
105                                                 String value = prop.getValue() == null ? prop.getDefaultValue() : prop.getValue();
106                                                 boolean bvalue = Boolean.parseBoolean(value);
107                                                 if (bvalue) {
108                                                         assertEquals("Validate vf_module_type", "Base", vf_module_type);
109                                                 } else {
110                                                         assertEquals("Validate vf_module_type", "Expansion", vf_module_type);
111                                                 }
112                                                 break;
113                                         }
114                                 }
115                                 String vf_module_description = (String) properties.get("vf_module_description");
116                                 assertEquals("Validate vf_module_description", group.getDescription(), vf_module_description);
117
118                                 Boolean volume_group = (Boolean) properties.get("volume_group");
119                                 boolean isVolume = false;
120                                 List<String> artifactsList = group.getArtifacts();
121                                 List<ArtifactDefinition> artifacts = new ArrayList<>();
122                                 if (artifactsList != null && !artifactsList.isEmpty()) {
123                                         ArtifactDefinition masterArtifact = findMasterArtifact(createdResource.getDeploymentArtifacts(),
124                                                         artifacts, artifactsList);
125                                         if (masterArtifact.getArtifactType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType())) {
126                                                 isVolume = true;
127                                         }
128                                 }
129                                 assertEquals("Validate volume_group", isVolume, volume_group);
130
131                         } else {
132                                 invariantUUID = (String) metadata.get("invariantUUID");
133                                 name = (String) metadata.get("name");
134                                 UUID = (String) metadata.get("UUID");
135                                 version = (String) metadata.get("version");
136                                 assertNull(properties);
137
138                         }
139                         assertEquals("Validate InvariantUUID", group.getInvariantUUID(), invariantUUID);
140                         assertEquals("Validate name", group.getName(), name);
141                         assertEquals("Validate UUID", group.getGroupUUID(), UUID);
142                         assertEquals("Validate version", group.getVersion(), version);
143
144                 }
145         }
146
147         @Test(enabled = true)
148         public void exportCsarInputsTest() throws Exception {
149                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
150
151                 Resource createdResource = createVfFromCSAR(sdncModifierDetails, "csar_1");
152                 Map<String, Object> load = downloadAndParseToscaTemplate(sdncModifierDetails, createdResource);
153                 assertNotNull(load);
154
155                 Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template");
156                 assertNotNull(topology_template);
157
158                 Map<String, Object> inputs = (Map<String, Object>) topology_template.get("inputs");
159                 assertNotNull(inputs);
160
161                 List<InputDefinition> inputsFromResource = createdResource.getInputs();
162                 assertEquals("validate inputs size", inputsFromResource.size(), inputs.size());
163                 for (InputDefinition inputDef : inputsFromResource) {
164                         Map<String, Object> inputInFile = (Map<String, Object>) inputs.get(inputDef.getName());
165                         assertNotNull(inputInFile);
166                         validateInput(inputDef, inputInFile);
167                 }
168                 List<ComponentInstance> componentInstances = createdResource.getComponentInstances();
169                 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = createdResource
170                                 .getComponentInstancesProperties();
171                 Map<String, Object> node_templates = (Map<String, Object>) topology_template.get("node_templates");
172                 assertNotNull(node_templates);
173
174                 JsonParser jsonParser = new JsonParser();
175
176                 for (Map.Entry<String, List<ComponentInstanceProperty>> entry : componentInstancesProperties.entrySet()) {
177
178                         Optional<ComponentInstance> findFirst = componentInstances.stream()
179                                         .filter(ci -> ci.getUniqueId().equals(entry.getKey())).findFirst();
180                         assertTrue(findFirst.isPresent());
181                         String resourceName = findFirst.get().getName();
182                         Map<String, Object> instance = (Map<String, Object>) node_templates.get(resourceName);
183                         assertNotNull(instance);
184                         Map<String, Object> properties = (Map<String, Object>) instance.get("properties");
185
186                         for (ComponentInstanceProperty cip : entry.getValue()) {
187                                 if (cip.getValueUniqueUid() != null && !cip.getValueUniqueUid().isEmpty()) {
188                                         assertNotNull(properties);
189                                         if (cip.getValue().contains("get_input")) {
190                                                 Object prop = properties.get(cip.getName());
191                                                 assertNotNull(prop);
192
193                                                 Gson gson = new Gson();
194                                                 String json = gson.toJson(prop);
195                                                 assertEquals("validate json property", cip.getValue(), json);
196                                         }
197
198                                 }
199                         }
200
201                 }
202
203         }
204
205         @Test
206         public void importExportCsarWithJsonPropertyType() throws Exception {
207                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
208                 String payloadName = "jsonPropertyTypeTest.csar";
209                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
210                 String rootPath = System.getProperty("user.dir");
211                 Path path = null;
212                 byte[] data = null;
213                 String payloadData = null;
214                 path = Paths.get(rootPath + "/src/test/resources/CI/csars/jsonPropertyTypeTest.csar");
215                 data = Files.readAllBytes(path);
216                 payloadData = Base64.encodeBase64String(data);
217                 resourceDetails.setPayloadData(payloadData);
218                 resourceDetails.setCsarUUID(payloadName);
219                 resourceDetails.setPayloadName(payloadName);
220                 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
221                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
222                 BaseRestUtils.checkCreateResponse(createResource);
223                 Resource resource = ResponseParser.parseToObjectUsingMapper(createResource.getResponse(), Resource.class);
224                 ComponentInstance pmaaServer = resource.getComponentInstances().stream()
225                                 .filter(p -> p.getName().equals("pmaa_server_0")).findAny().get();
226                 ComponentInstanceProperty jsonProp = resource.getComponentInstancesProperties().get(pmaaServer.getUniqueId())
227                                 .stream().filter(p -> p.getType().equals(ToscaPropertyType.JSON.getType())).findAny().get();
228                 String jsonValue = "{\"pmaa.sb_nic\":{\"address\":{\"get_input\":\"pmaa_dpu_fixed_ip\"},\"cidr\":{\"get_input\":\"pmaa_dpu_cidr\"},\"gateway\":{\"get_input\":\"pmaa_dpu_gateway\"}}}";
229                 assertEquals(jsonProp.getValue(), jsonValue);
230                 // download and compare
231                 Map<String, Object> load = downloadAndParseToscaTemplate(sdncModifierDetails, resource);
232                 assertNotNull(load);
233                 Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template");
234                 assertNotNull(topology_template);
235                 Map<String, Object> nodes = (Map<String, Object>) topology_template.get("node_templates");
236                 assertNotNull(nodes);
237                 Map<String, Object> pmaaServerObj = (Map<String, Object>) nodes.get("pmaa_server_0");
238                 assertNotNull(pmaaServerObj);
239                 Map<String, Object> props = (Map<String, Object>) pmaaServerObj.get("properties");
240                 assertNotNull(props);
241                 Map<String, Object> jsonPropObj = (Map<String, Object>) props.get("metadata");
242                 assertNotNull(jsonPropObj);
243                 Gson gson = new Gson();
244                 String json = gson.toJson(jsonPropObj);
245                 assertEquals(json, jsonValue);
246         }
247         
248         @Test(enabled = true)
249         public void exportServiceInputValue() throws Exception {
250                 //1 create vf as certified
251                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
252
253                 Resource createdResource = createVfFromCSAR(sdncModifierDetails, "csar_1");
254                 RestResponse checkinState = LifecycleRestUtils.changeComponentState(createdResource, sdncModifierDetails, LifeCycleStatesEnum.CHECKIN);
255                 BaseRestUtils.checkSuccess(checkinState);
256                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService(
257                                 "ciNewtestservice1", ServiceCategoriesEnum.MOBILITY, sdncModifierDetails.getUserId(), 
258                                 ServiceInstantiationType.A_LA_CARTE.getValue());
259                 
260                 //2 create service
261                 RestResponse createServiceResponse = ServiceRestUtils.createService(serviceDetails, sdncModifierDetails);
262                 ResourceRestUtils.checkCreateResponse(createServiceResponse);
263                 Service service = ResponseParser.parseToObjectUsingMapper(createServiceResponse.getResponse(), Service.class);
264
265                 //3 create vf instance in service
266                 ComponentInstanceReqDetails componentInstanceDetails = ElementFactory.getComponentInstance(createdResource);
267                 RestResponse createComponentInstance = ComponentInstanceRestUtils.createComponentInstance(componentInstanceDetails, sdncModifierDetails, service);
268                 ResourceRestUtils.checkCreateResponse(createComponentInstance);
269                 
270                 RestResponse getService = ServiceRestUtils.getService(service.getUniqueId());
271                 BaseRestUtils.checkSuccess(getService);
272                 service = ResponseParser.parseToObjectUsingMapper(getService.getResponse(), Service.class);
273                 
274                 //4 download tosca template 
275                 Map<String, Object> tosca = downloadAndParseToscaTemplate(sdncModifierDetails, service);
276                 assertNotNull(tosca);
277                 Map<String, Object> topology_template = (Map<String, Object>) tosca.get("topology_template");
278                 assertNotNull(topology_template);
279         
280                 //5 validate no inputs in service
281                 Map<String, Object> inputs = (Map<String, Object>) tosca.get("inputs");
282                 assertNull(inputs);
283         
284                 List<ComponentInstance> componentInstances = service.getComponentInstances();
285                 assertNotNull(componentInstances);
286                 assertEquals(1, componentInstances.size());
287                 ComponentInstance vfi = componentInstances.get(0);
288                 
289                 //6 add instance inputs in service
290                 RestResponse getComponentInstanceInputsResponse = InputsRestUtils.getComponentInstanceInputs(service, vfi);
291                 BaseValidationUtils.checkSuccess(getComponentInstanceInputsResponse);
292                 List<ComponentInstancePropInput> instanceInputs = new Gson().fromJson(getComponentInstanceInputsResponse.getResponse(), new TypeToken<ArrayList<ComponentInstancePropInput>>(){}.getType());
293                 // Take only the 2 first inputs
294                 List<ComponentInstancePropInput> inputsToAdd = instanceInputs.stream().limit(2).collect(Collectors.toList());
295
296                 //7 Build component instances input map to add to server
297                 ComponentInstInputsMap buildComponentInstInputsMap = buildComponentInstInputsMap(vfi.getUniqueId(), inputsToAdd);
298                 RestResponse addInputResponse = InputsRestUtils.addInput(service, buildComponentInstInputsMap, UserRoleEnum.DESIGNER);
299                 BaseValidationUtils.checkSuccess(addInputResponse);
300
301                 //8 validate inputs in service 
302                 //8.1 download tosca template 
303                 getService = ServiceRestUtils.getService(service.getUniqueId());
304                 BaseRestUtils.checkSuccess(getService);
305                 service = ResponseParser.parseToObjectUsingMapper(getService.getResponse(), Service.class);
306                 
307                 tosca = downloadAndParseToscaTemplate(sdncModifierDetails, service);
308                 assertNotNull(tosca);
309                 topology_template = (Map<String, Object>) tosca.get("topology_template");
310                 assertNotNull(topology_template);
311         
312                 //8.2 validate inputs in service
313                 inputs = (Map<String, Object>) topology_template.get("inputs");
314                 assertNotNull(inputs);
315                 assertEquals(2, inputs.size());
316         
317                 //validate created inputs vs inputs in Tosca inputs section             
318                 final Map<String, Object> inputsFinal = inputs;
319                 buildComponentInstInputsMap.getComponentInstanceInputsMap().values().forEach(listPerInstance ->{
320                         listPerInstance.forEach(input ->{
321                                 Map<String, Object> inputInMap = (Map<String, Object>)inputsFinal.get(input.getName());
322                                 assertNotNull(inputInMap);
323                         });
324                 });
325                 Map<String, List<ComponentInstanceInput>> componentInstancesInputs = service.getComponentInstancesInputs();
326                 
327                 //validate created inputs vs inputs in Tosca instance input value
328                 List<ComponentInstanceInput> vfiInputs = componentInstancesInputs.get(vfi.getUniqueId());
329                 assertNotNull(vfiInputs);
330                 assertEquals(2, vfiInputs.size());
331         
332                 Map<String, Object> node_templates = (Map<String, Object>) topology_template.get("node_templates");
333                 assertNotNull(node_templates);
334
335                 Map<String, Object> instance = (Map<String, Object>) node_templates.get(vfi.getName());
336                 assertNotNull(instance);
337                 Map<String, Object> properties = (Map<String, Object>)instance.get("properties");
338                 assertNotNull(properties);
339         
340                 vfiInputs.forEach(vfiInput ->{
341                         Map<String, Object> inputPropValueInTosca = (Map<String, Object>)properties.get(vfiInput.getName() );
342                         assertNotNull(inputPropValueInTosca);
343                         String instaneInputName = (String)inputPropValueInTosca.get("get_input");
344                         assertNotNull(instaneInputName);
345                         Map<String, Object> inputInMap = (Map<String, Object>)inputsFinal.get(instaneInputName);
346                         assertNotNull(inputInMap);
347                 });
348                 
349
350         }
351
352         // ----------------------------------------
353         private void validateInput(InputDefinition inputDef, Map<String, Object> inputInFile) {
354                 assertEquals("validate input type", inputDef.getType(), (String) inputInFile.get("type"));
355
356                 if (inputDef.getDefaultValue() == null) {
357                         assertNull(inputInFile.get("default"));
358                 } else {
359                         assertNotNull(inputInFile.get("default"));
360                         String value = inputDef.getDefaultValue().replace("\"", "");
361                         value = value.replace(" ", "");
362                         String expValue = inputInFile.get("default").toString().replace(" ", "");
363                         assertEquals("validate input default", value, expValue);
364                 }
365                 assertEquals("validate input description", inputDef.getDescription(), (String) inputInFile.get("description"));
366         }
367
368         private Map<String, Object> downloadAndParseToscaTemplate(User sdncModifierDetails, Component createdComponent)
369                         throws Exception {
370                 String artifactUniqeId = createdComponent.getToscaArtifacts().get("assettoscatemplate").getUniqueId();
371                 RestResponse toscaTemplate;
372
373                 if ( createdComponent.getComponentType() == ComponentTypeEnum.RESOURCE ){
374                         toscaTemplate = ArtifactRestUtils.downloadResourceArtifactInternalApi(
375                                         createdComponent.getUniqueId(), sdncModifierDetails, artifactUniqeId);
376                                 
377                 }else{
378                         toscaTemplate = ArtifactRestUtils.downloadServiceArtifactInternalApi(
379                                         createdComponent.getUniqueId(), sdncModifierDetails, artifactUniqeId);
380                 }
381                 BaseRestUtils.checkSuccess(toscaTemplate);
382
383                 ArtifactUiDownloadData artifactUiDownloadData = ResponseParser.parseToObject(toscaTemplate.getResponse(),
384                                 ArtifactUiDownloadData.class);
385                 byte[] fromUiDownload = artifactUiDownloadData.getBase64Contents().getBytes();
386                 byte[] decodeBase64 = Base64.decodeBase64(fromUiDownload);
387                 Yaml yaml = new Yaml();
388
389                 InputStream inputStream = new ByteArrayInputStream(decodeBase64);
390
391                 Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
392                 return load;
393         }
394
395
396         public ArtifactDefinition findMasterArtifact(Map<String, ArtifactDefinition> deplymentArtifact,
397                         List<ArtifactDefinition> artifacts, List<String> artifactsList) {
398                 for (String artifactUid : artifactsList) {
399                         for (Entry<String, ArtifactDefinition> entry : deplymentArtifact.entrySet()) {
400                                 ArtifactDefinition artifact = entry.getValue();
401                                 if (artifactUid.equalsIgnoreCase(artifact.getUniqueId())) {
402                                         artifacts.add(artifact);
403                                 }
404
405                         }
406                 }
407                 ArtifactDefinition masterArtifact = null;
408                 for (ArtifactDefinition artifactInfo : artifacts) {
409                         String atrifactType = artifactInfo.getArtifactType();
410                         if (atrifactType.equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType())
411                                         || atrifactType.equalsIgnoreCase(ArtifactTypeEnum.HEAT_NET.getType())) {
412                                 masterArtifact = artifactInfo;
413                                 continue;
414                         }
415                         if (atrifactType.equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType())) {
416                                 masterArtifact = artifactInfo;
417                                 break;
418                         }
419                 }
420                 return masterArtifact;
421         }
422         private ComponentInstInputsMap buildComponentInstInputsMap (String addToInput, List<ComponentInstancePropInput> inputs) {
423                 Map<String, List<ComponentInstancePropInput>> map = new HashMap<>();
424                 map.put(addToInput, inputs);
425                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
426                 componentInstInputsMap.setComponentInstanceInputsMap(map);              
427                 return componentInstInputsMap;
428         }
429
430 }