Added oparent to sdc main
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / sanity / ToscaValidationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.sanity;
22
23 import com.aventstack.extentreports.Status;
24 import fj.data.Either;
25 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
26 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
27 import org.onap.sdc.toscaparser.api.Group;
28 import org.onap.sdc.toscaparser.api.NodeTemplate;
29 import org.onap.sdc.toscaparser.api.elements.Metadata;
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.Service;
34 import org.openecomp.sdc.ci.tests.dataProvider.OnbordingDataProviders;
35 import org.openecomp.sdc.ci.tests.datatypes.*;
36 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
38 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
39 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
40 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
41 import org.openecomp.sdc.ci.tests.tosca.datatypes.*;
42 import org.openecomp.sdc.ci.tests.tosca.model.ToscaMetadataFieldsPresentationEnum;
43 import org.openecomp.sdc.ci.tests.utilities.DownloadManager;
44 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
45 import org.openecomp.sdc.ci.tests.utils.CsarParserUtils;
46 import org.openecomp.sdc.ci.tests.utils.ToscaParserUtils;
47 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
48 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
49 import org.openecomp.sdc.ci.tests.utils.general.OnboardingUtillViaApis;
50 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
51 import org.openecomp.sdc.ci.tests.utils.rest.PropertyRestUtils;
52 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
53 import org.openecomp.sdc.ci.tests.verificator.ToscaValidation;
54 import org.testng.annotations.DataProvider;
55 import org.testng.annotations.Test;
56
57 import java.io.File;
58 import java.util.*;
59
60 import static org.testng.Assert.assertFalse;
61
62
63 public class ToscaValidationTest extends SetupCDTest {
64
65     private static final String GENERIC_VF = "Generic_VF";
66     private static final String GENERIC_PNF = "Generic_PNF";
67     private static final String GENERIC_CR = "Generic_CR";
68
69     protected SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
70     User user = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
71
72     @Test(dataProviderClass = OnbordingDataProviders.class, dataProvider = "VNF_List")
73     public void toscaFileValidator(String filePath, String vnfFile) throws Exception {
74 //--------------------------GENERAL--------------------------------
75 /*//            for debugging only
76         setLog("Test");
77                 File amdocsCsarFileName = (new File("C:\\Users\\al714h\\Downloads\\d218be69637647b0b693647d84a8c03f.csar"));
78                 toscaMainAmdocsDefinition = ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(amdocsCsarFileName);
79                 toscaMainVfDefinition = ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(new File("C:\\Users\\al714h\\Downloads\\resource-Civfonboarded2016073VmxBv301072E2eE60f5c15-csar.csar"));
80         */
81   //      vnfFile = "vRouter for DHV Test_Version_4.zip";
82         setLog(vnfFile);
83         List<Boolean> status = new ArrayList<>();
84         ISdcCsarHelper fdntCsarHelper;
85         File filesFolder = new File(SetupCDTest.getWindowTest().getDownloadDirectory());
86 //--------------------------AMDOCS--------------------------------
87         ResourceReqDetails resourceReqDetails = ElementFactory.getDefaultResourceByType(ResourceTypeEnum.VF, user);//getResourceReqDetails(ComponentConfigurationTypeEnum.DEFAULT);
88         resourceReqDetails = createCustomizedVsp(resourceReqDetails, filePath, vnfFile);
89         ToscaDefinition toscaMainAmdocsDefinition = downloadAndGetToscaMainYamlObjectUI(resourceReqDetails, filesFolder);
90 //------adding generic inputs to expected object
91         toscaMainAmdocsDefinition = addGenericPropertiesToToscaDefinitionObject(toscaMainAmdocsDefinition, GENERIC_VF);
92 //      copy object
93         ToscaDefinition toscaExpectedMainServiceDefinition = new ToscaDefinition(toscaMainAmdocsDefinition);
94 //              create list of modules from HEAT.meta file
95         File latestFileFromDir = FileHandling.getLastModifiedFileNameFromDir();
96         List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition = CsarParserUtils.getListTypeHeatMetaDefinition(latestFileFromDir);
97 //TODO  VfModuleVerificator.verifyGroupMetadata();
98 //TODO--------------------------AMDOCS DOWNLOAD VIA APIS--------------------------------
99 //--------------------------VF--------------------------------
100 //              create VF base on VNF imported from previous step - have, resourceReqDetails object include part of resource metadata
101         Resource resource = OnboardingUtillViaApis.createResourceFromVSP(resourceReqDetails);
102         resource = (Resource) AtomicOperationUtils.changeComponentState(resource, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
103         ToscaDefinition toscaMainVfDefinition = downloadAndGetToscaMainYamlObjectApi(resource, filesFolder);
104 //--------------------------SERVICE--------------------------------
105         ServiceReqDetails serviceReqDetails = ElementFactory.getDefaultService();//getServiceReqDetails(ComponentConfigurationTypeEnum.DEFAULT);
106         Service service = AtomicOperationUtils.createCustomService(serviceReqDetails, UserRoleEnum.DESIGNER, true).left().value();
107         Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainer = AtomicOperationUtils.addComponentInstanceToComponentContainer(resource, service, UserRoleEnum.DESIGNER, true);
108         ComponentInstance componentInstanceDefinition = addComponentInstanceToComponentContainer.left().value();
109 //--------------------------getProperties set values and declare--------------------
110         Component componentObject = AtomicOperationUtils.getComponentObject(service, UserRoleEnum.DESIGNER);
111         Map<String, List<ComponentInstanceInput>> componentInstancesInputs = componentObject.getComponentInstancesInputs();
112         setValuesToPropertiesList(componentInstancesInputs, toscaExpectedMainServiceDefinition);
113         PropertyRestUtils.declareProporties(componentObject, componentInstancesInputs, user);
114
115         service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
116         Map<String, VfModuleDefinition> expectedVfModulesDefinitionObject = createExpectedVfModuleDefinitionObject(resource, service, listTypeHeatMetaDefinition);
117
118         File serviceCsarFileName = new File(File.separator + "ServiceCsar_" + ElementFactory.generateUUIDforSufix() + ".csar");
119         OnboardingUtillViaApis.downloadToscaCsarToDirectory(service, new File(filesFolder.getPath() + serviceCsarFileName));
120         ToscaDefinition toscaMainServiceDefinition = ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(new File(filesFolder.getPath() + serviceCsarFileName));
121 //--------------------------initialization of Tosca Parser--------------------------------
122         fdntCsarHelper = initSdcCsarHelper(serviceCsarFileName, filesFolder);
123 ////---------------------------TESTS--------------------------------------------------
124         validateVfModuleJsonFile(expectedVfModulesDefinitionObject, service, componentInstanceDefinition.getUniqueId(), vnfFile, status);
125         validateVfMetadata(toscaMainAmdocsDefinition, toscaMainVfDefinition, resourceReqDetails, resource, vnfFile, status);
126         validateResourceNodeTemplateMetadata(toscaMainVfDefinition, resource, vnfFile, status);
127         validateServiceMetadata(toscaMainServiceDefinition, serviceReqDetails, service, vnfFile, status);
128         validateServiceNodeTemplateMetadata(toscaMainServiceDefinition, componentInstanceDefinition, resourceReqDetails, resource, vnfFile, status);
129         validateServiceMetadataUsingParser(fdntCsarHelper, serviceReqDetails, service, vnfFile, status);
130         validateServiceNodeTemplateMetadataUsingParser(fdntCsarHelper, resourceReqDetails, resource, componentInstanceDefinition, vnfFile, status);
131         validateResourceInputs(toscaMainAmdocsDefinition, toscaMainVfDefinition, vnfFile, status);
132         validateServiceInputs(toscaExpectedMainServiceDefinition, toscaMainServiceDefinition, vnfFile, status);
133         validateServiceInputsUsingParser(fdntCsarHelper, toscaExpectedMainServiceDefinition, vnfFile, status);
134
135         Map<String, ToscaGroupsTopologyTemplateDefinition> expectedToscaServiceGroupsDefinitionObject = createExpectedToscaServiceGroupsDefinitionObject(resource, service, listTypeHeatMetaDefinition);
136         validateServiceModuleMetadata(expectedToscaServiceGroupsDefinitionObject, toscaMainServiceDefinition, vnfFile, status);
137         validateServiceModuleProperty(expectedToscaServiceGroupsDefinitionObject, toscaMainServiceDefinition, vnfFile, status);
138         validateServiceModuleMetadataUsingParser(fdntCsarHelper, expectedToscaServiceGroupsDefinitionObject, vnfFile, status);
139         validateServiceModulePropertyUsingParser(fdntCsarHelper, expectedToscaServiceGroupsDefinitionObject, vnfFile, status);
140
141         if (status.contains(false)) {
142             SetupCDTest.getExtendTest().log(Status.FAIL, "Summary: tosca validation test failed with zip file " + vnfFile);
143             assertFalse(true);
144         }
145     }
146
147
148     @DataProvider(name = "toscaValidationTest", parallel = true)
149     public static Object[][] dataProviderForSpecificResourceType() {
150         return new Object[][]{
151                 {"networkModel", ResourceTypeEnum.PNF, GENERIC_PNF},
152                 {"collectorResourceModel", ResourceTypeEnum.CR, GENERIC_CR},
153         };
154     }
155
156     @Test(dataProvider = "toscaValidationTest")
157     public void validateSpecificResourceType(String type, ResourceTypeEnum resourceType, String genericPropName) throws Exception {
158 //--------------------------GENERAL--------------------------------
159         String vnfFile = type;
160         setLog(vnfFile);
161         List<Boolean> status = new ArrayList<>();
162         ISdcCsarHelper fdntCsarHelper;
163         ToscaDefinition expectedToscaMainDefinition = new ToscaDefinition();
164         File filesFolder = new File(SetupCDTest.getWindowTest().getDownloadDirectory());
165 //              filesFolder = new File(SetupCDTest.getWindowTest().getDownloadDirectory());
166
167         ResourceReqDetails resourceReqDetails = ElementFactory.getDefaultResourceByType(resourceType, user);
168         expectedToscaMainDefinition = addGenericPropertiesToToscaDefinitionObject(expectedToscaMainDefinition, genericPropName);
169         ToscaDefinition toscaExpectedMainServiceDefinition = new ToscaDefinition(expectedToscaMainDefinition);
170 //--------------------------VF--------------------------------
171         Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceReqDetails, UserRoleEnum.DESIGNER, true).left().value();
172         resource = (Resource) AtomicOperationUtils.changeComponentState(resource, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
173
174         ToscaDefinition toscaMainVfDefinition = downloadAndGetToscaMainYamlObjectApi(resource, filesFolder);
175
176 //--------------------------SERVICE--------------------------------
177         ServiceReqDetails serviceReqDetails = ElementFactory.getDefaultService();
178         Service service = AtomicOperationUtils.createCustomService(serviceReqDetails, UserRoleEnum.DESIGNER, true).left().value();
179
180         Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainer = AtomicOperationUtils.addComponentInstanceToComponentContainer(resource, service, UserRoleEnum.DESIGNER, true);
181         ComponentInstance componentInstanceDefinition = addComponentInstanceToComponentContainer.left().value();
182
183 //--------------------------getProperties set values and declare--------------------
184
185         Component componentObject = AtomicOperationUtils.getComponentObject(service, UserRoleEnum.DESIGNER);
186         Map<String, List<ComponentInstanceInput>> componentInstancesInputs = componentObject.getComponentInstancesInputs();
187         setValuesToPropertiesList(componentInstancesInputs, toscaExpectedMainServiceDefinition);
188         PropertyRestUtils.declareProporties(componentObject, componentInstancesInputs, user);
189
190         service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
191         File ServiceCsarFileName = new File(File.separator + "ServiceCsar_" + ElementFactory.generateUUIDforSufix() + ".csar");
192         OnboardingUtillViaApis.downloadToscaCsarToDirectory(service, new File(filesFolder.getPath() + ServiceCsarFileName));
193         ToscaDefinition toscaMainServiceDefinition = ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(new File(filesFolder.getPath() + ServiceCsarFileName));
194
195 //--------------------------initialization of Tosca Parser--------------------------------
196
197         fdntCsarHelper = initSdcCsarHelper(ServiceCsarFileName, filesFolder);
198
199
200 //---------------------------TESTS--------------------------------------------------
201         validateVfMetadata(expectedToscaMainDefinition, toscaMainVfDefinition, resourceReqDetails, resource, vnfFile, status);
202         validateResourceNodeTemplateMetadata(toscaMainVfDefinition, resource, vnfFile, status);
203         validateServiceMetadata(toscaMainServiceDefinition, serviceReqDetails, service, vnfFile, status);
204         validateServiceNodeTemplateMetadata(toscaMainServiceDefinition, componentInstanceDefinition, resourceReqDetails, resource, vnfFile, status);
205         validateServiceMetadataUsingParser(fdntCsarHelper, serviceReqDetails, service, vnfFile, status);
206         validateServiceNodeTemplateMetadataUsingParser(fdntCsarHelper, resourceReqDetails, resource, componentInstanceDefinition, vnfFile, status);
207         validateResourceInputs(expectedToscaMainDefinition, toscaMainVfDefinition, vnfFile, status);
208         validateServiceInputs(toscaExpectedMainServiceDefinition, toscaMainServiceDefinition, vnfFile, status);
209         validateServiceInputsUsingParser(fdntCsarHelper, toscaExpectedMainServiceDefinition, vnfFile, status);
210
211         if (status.contains(false)) {
212             SetupCDTest.getExtendTest().log(Status.FAIL, "Summary: tosca validation test failed with zip file " + vnfFile);
213             assertFalse(true);
214         }
215     }
216
217     /**
218      * The method set values to toscaDefinition object service level only, to resource level should put instead of setDefault --> setValue
219      * inputs.get(componentInstanceInput.getName()).setValue(randomString);
220      *
221      * @param componentInstancesInputs list of componentInstancesInputs
222      * @param toscaDefinition
223      */
224     private void setValuesToPropertiesList(Map<String, List<ComponentInstanceInput>> componentInstancesInputs, ToscaDefinition toscaDefinition) {
225         for (Map.Entry<String, List<ComponentInstanceInput>> entry : componentInstancesInputs.entrySet()) {
226             List<ComponentInstanceInput> value = entry.getValue();
227             String[] names = entry.getKey().split("\\.");
228             String expectedServiceInputPrefix = null;
229             Map<String, ToscaInputsTopologyTemplateDefinition> inputs = toscaDefinition.getTopology_template().getInputs();
230             if (names.length > 0) {
231                 expectedServiceInputPrefix = names[names.length - 1] + "_";
232             }
233             for (ComponentInstanceInput componentInstanceInput : value) {
234
235
236                 String type = componentInstanceInput.getType();
237                 List<String> myList = new ArrayList<>();
238                 myList.add("cbf8049e-69e8-48c3-a06f-255634391403");
239                 switch (type) {
240                     case "string":
241                         String randomString = getRandomString();
242                         componentInstanceInput.setValue(randomString);
243                         inputs.get(componentInstanceInput.getName()).setDefault(randomString);
244
245                         break;
246                     case "integer":
247                         int randomInteger = getRandomInteger();
248                         componentInstanceInput.setValue(Integer.toString(randomInteger));
249                         inputs.get(componentInstanceInput.getName()).setDefault(randomInteger);
250                         break;
251                     case "float":
252                         componentInstanceInput.setValue("5.5");
253                         inputs.get(componentInstanceInput.getName()).setDefault("5.5");
254
255                         break;
256                     case "boolean":
257                         componentInstanceInput.setValue("true");
258                         inputs.get(componentInstanceInput.getName()).setDefault("true");
259                         break;
260                     case "list":
261                         String myListofStrings = myList.toString();
262                         componentInstanceInput.setValue(myListofStrings);
263                         inputs.get(componentInstanceInput.getName()).setDefault(myListofStrings);
264                         break;
265                     case "json":
266                         String myJson = "{\"firstParam\":\"my First Param Value\",\"secondParam\":\"my Second Param Value\",\"numberParam\":666}";
267                         componentInstanceInput.setValue(myJson);
268                         inputs.get(componentInstanceInput.getName()).setDefault(myJson);
269                         break;
270                     case "comma_delimited_list":
271                         String commaDelimitedList = "[\"one\", \"two\"]";
272                         componentInstanceInput.setValue(commaDelimitedList);
273                         inputs.get(componentInstanceInput.getName()).setDefault(commaDelimitedList);
274                         break;
275                     default:
276                         break;
277                 }
278
279                 String expectedServiceInputName = expectedServiceInputPrefix + componentInstanceInput.getName();
280                 ToscaInputsTopologyTemplateDefinition oldInput = inputs.get(componentInstanceInput.getName());
281                 inputs.put(expectedServiceInputName, oldInput);
282                 inputs.remove(componentInstanceInput.getName());
283
284             }
285
286         }
287     }
288
289     protected String getRandomString() {
290         String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
291         StringBuilder salt = new StringBuilder();
292         Random rnd = new Random();
293         while (salt.length() < 18) { // length of the random string.
294             int index = (int) (rnd.nextFloat() * SALTCHARS.length());
295             salt.append(SALTCHARS.charAt(index));
296         }
297         String strValue = salt.toString();
298         return strValue;
299
300     }
301
302     protected int getRandomInteger() {
303         Random r = new Random();
304         int low = 10;
305         int high = 100;
306         int integerValue = r.nextInt(high - low) + low;
307         return integerValue;
308     }
309
310
311     //--------------------------Metadata verification--------------------------------
312 //--------------------------Resource--------------------------------    
313
314     public List<Boolean> validateVfMetadata(ToscaDefinition toscaMainAmdocsDefinition, ToscaDefinition toscaMainVfDefinition, ResourceReqDetails resourceReqDetails, Resource resource, String vnfFile, List<Boolean> status) throws Exception {
315         reportStartTestPrint("validateVfMetadata", vnfFile);
316         //add resource metadata to expected object
317         toscaMainAmdocsDefinition = addAndGenerateResourceMetadataToExpectedObject(toscaMainAmdocsDefinition, resourceReqDetails, resource);
318         Either<Boolean, Map<String, Object>> resourceToscaMetadataValidator = ToscaValidation.resourceToscaMetadataValidator(toscaMainAmdocsDefinition, toscaMainVfDefinition);
319         if (resourceToscaMetadataValidator.isRight())
320             status.add(false);
321         return status;
322     }
323
324     public List<Boolean> validateResourceNodeTemplateMetadata(ToscaDefinition toscaMainVfDefinition, Resource resource, String vnfFile, List<Boolean> status) throws Exception {
325         reportStartTestPrint("validateResourceNodeTemplateMetadata", vnfFile);
326         Map<String, Map<String, String>> generateReosurceNodeTemplateMetadataToExpectedObject = generateResourceNodeTemplateMetadataToExpectedObject(resource);
327         Boolean resourceToscaMetadataValidator = ToscaValidation.resourceToscaNodeTemplateMetadataValidator(generateReosurceNodeTemplateMetadataToExpectedObject, toscaMainVfDefinition);
328         if (!resourceToscaMetadataValidator)
329             status.add(false);
330         return status;
331     }
332
333     //--------------------------Service--------------------------------
334     public List<Boolean> validateServiceMetadata(ToscaDefinition toscaMainServiceDefinition, ServiceReqDetails serviceReqDetails, Service service, String vnfFile, List<Boolean> status) throws Exception {
335         reportStartTestPrint("validateServiceMetadata", vnfFile);
336         Map<String, String> generateServiceMetadataToExpectedObject = generateServiceMetadataToExpectedObject(serviceReqDetails, service);
337         Either<Boolean, Map<String, Object>> serviceToscaMetadataValidator = ToscaValidation.serviceToscaMetadataValidator(generateServiceMetadataToExpectedObject, toscaMainServiceDefinition);
338         if (serviceToscaMetadataValidator.isRight())
339             status.add(false);
340         return status;
341     }
342
343
344     public List<Boolean> validateServiceNodeTemplateMetadata(ToscaDefinition toscaMainServiceDefinition, ComponentInstance componentInstanceDefinition, ResourceReqDetails resourceReqDetails, Resource resource, String vnfFile, List<Boolean> status) throws Exception {
345         reportStartTestPrint("validateServiceNodeTemplateMetadata", vnfFile);
346         Map<String, String> generateServiceNodeTemplateMetadataToExpectedObject = generateServiceNodeTemplateMetadataToExpectedObject(resourceReqDetails, resource, componentInstanceDefinition);
347         Either<Boolean, Map<String, Object>> serviceToscaMetadataValidator = ToscaValidation.componentToscaNodeTemplateMetadataValidator(generateServiceNodeTemplateMetadataToExpectedObject, toscaMainServiceDefinition, componentInstanceDefinition.getName(), ComponentTypeEnum.SERVICE, componentInstanceDefinition.getName());
348         if (serviceToscaMetadataValidator.isRight())
349             status.add(false);
350         return status;
351     }
352
353     private List<Boolean> validateVfModuleJsonFile(Map<String, VfModuleDefinition> expectedVfModulesDefinitionObject, Service service, String resInstUniqueId, String vnfFile, List<Boolean> status) throws Exception {
354         reportStartTestPrint("validateVfModuleJsonFile", vnfFile);
355         String artifactUniqueId = null;
356         for (ComponentInstance inst : service.getComponentInstances()) {
357             if (inst.getUniqueId().equals(resInstUniqueId)) {
358                 artifactUniqueId = inst.getDeploymentArtifacts().get("vfModulesMetadata").getUniqueId();
359                 break;
360             }
361         }
362         if (artifactUniqueId == null) {
363             SetupCDTest.getExtendTest().log(Status.ERROR, "validateVfModuleJsonFile verification failed, artifact vfModulesMetadata not found");
364             status.add(false);
365             return status;
366         }
367         RestResponse restResponse = ArtifactRestUtils.downloadResourceInstanceArtifact(service.getUniqueId(), resInstUniqueId, user, artifactUniqueId);
368         String artifactPayload = ArtifactRestUtils.getDecodedArtifactPayloadFromResponse(restResponse);
369         Map<String, VfModuleDefinition> actualVfModulesDefinitionObject = ResponseParser.convertVfModuleJsonResponseToJavaObject(artifactPayload);
370         Either<Boolean, Map<String, Object>> vfModuleJsonFileValidator = ToscaValidation.vfModuleJsonFileValidator(expectedVfModulesDefinitionObject, actualVfModulesDefinitionObject);
371         if (vfModuleJsonFileValidator.isRight())
372             status.add(false);
373         return status;
374     }
375
376     //--------------------------Service verification against Pavel Parser--------------------------------
377     public List<Boolean> validateServiceMetadataUsingParser(ISdcCsarHelper fdntCsarHelper, ServiceReqDetails serviceReqDetails, Service service, String vnfFile, List<Boolean> status) throws Exception {
378         if (fdntCsarHelper == null) {
379             reportSkipTestPrint("validateServiceMetadataUsingParser", status);
380         } else {
381             reportStartTestPrint("validateServiceMetadataUsingParser", vnfFile);
382             Map<String, String> generateServiceMetadataToExpectedObject = generateServiceMetadataToExpectedObject(serviceReqDetails, service);
383             Metadata serviceMetadata = fdntCsarHelper.getServiceMetadata();
384             Either<Boolean, Map<String, Object>> serviceToscaMetadataValidatorAgainstParser = ToscaValidation.serviceToscaMetadataValidatorAgainstParser(generateServiceMetadataToExpectedObject, serviceMetadata);
385             if (serviceToscaMetadataValidatorAgainstParser.isRight())
386                 status.add(false);
387         }
388         return status;
389     }
390
391     public List<Boolean> validateServiceNodeTemplateMetadataUsingParser(ISdcCsarHelper fdntCsarHelper, ResourceReqDetails resourceReqDetails, Resource resource, ComponentInstance componentInstanceDefinition, String vnfFile, List<Boolean> status) throws Exception {
392         if (fdntCsarHelper == null) {
393             reportSkipTestPrint("validateServiceNodeTemplateMetadataUsingParser", status);
394         } else {
395             reportStartTestPrint("validateServiceNodeTemplateMetadataUsingParser", vnfFile);
396             Map<String, String> generateServiceNodeTemplateMetadataToExpectedObject = generateServiceNodeTemplateMetadataToExpectedObject(resourceReqDetails, resource, componentInstanceDefinition);
397             List<NodeTemplate> serviceNodeTemplates = fdntCsarHelper.getServiceNodeTemplates();
398             Metadata serviceNodeTemplateMetadata = serviceNodeTemplates.get(0).getMetaData();
399             Either<Boolean, Map<String, Object>> serviceNodeTemplateToscaMetadataValidatorAgainstParser = ToscaValidation.serviceToscaMetadataValidatorAgainstParser(generateServiceNodeTemplateMetadataToExpectedObject, serviceNodeTemplateMetadata);
400             if (serviceNodeTemplateToscaMetadataValidatorAgainstParser.isRight())
401                 status.add(false);
402         }
403         return status;
404     }
405
406     //--------------------------Input verification--------------------------------
407
408     //--------------------------Resource--------------------------------
409     public List<Boolean> validateResourceInputs(ToscaDefinition toscaMainAmdocsDefinition, ToscaDefinition toscaMainVfDefinition, String vnfFile, List<Boolean> status) throws Exception {
410         reportStartTestPrint("validateResourceInputs", vnfFile);
411         Map<String, ToscaInputsTopologyTemplateDefinition> expectedInputsMap = toscaMainAmdocsDefinition.getTopology_template().getInputs();
412         Map<String, ToscaInputsTopologyTemplateDefinition> actualInputsMap = toscaMainVfDefinition.getTopology_template().getInputs();
413         Either<Boolean, Map<String, Object>> toscaInputsValidator = ToscaValidation.toscaInputsValidator(expectedInputsMap, actualInputsMap);
414         if (toscaInputsValidator.isRight())
415             status.add(false);
416         return status;
417     }
418
419     //--------------------------Service--------------------------------
420
421     public List<Boolean> validateServiceInputs(ToscaDefinition toscaExpectedMainServiceDefinition, ToscaDefinition toscaMainServiceDefinition, String vnfFile, List<Boolean> status) throws Exception {
422         reportStartTestPrint("validateServiceInputs", vnfFile);
423         Map<String, ToscaInputsTopologyTemplateDefinition> expectedInputsMap = toscaExpectedMainServiceDefinition.getTopology_template().getInputs();
424         Map<String, ToscaInputsTopologyTemplateDefinition> actualInputsMap = toscaMainServiceDefinition.getTopology_template().getInputs();
425         Either<Boolean, Map<String, Object>> toscaInputsValidator = ToscaValidation.toscaInputsValidator(expectedInputsMap, actualInputsMap);
426         if (toscaInputsValidator.isRight())
427             status.add(false);
428         return status;
429     }
430
431     public List<Boolean> validateServiceModuleMetadata(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedToscaServiceGroupsDefinitionObject, ToscaDefinition toscaMainServiceDefinition, String vnfFile, List<Boolean> status) {
432         reportStartTestPrint("validateServiceModuleMetadata", vnfFile);
433
434         Either<Boolean, Map<String, Object>> toscaServiceModuleMetadataValidator = ToscaValidation.serviceToscaGroupMetadataValidator(expectedToscaServiceGroupsDefinitionObject, toscaMainServiceDefinition);
435         if (toscaServiceModuleMetadataValidator.isRight())
436             status.add(false);
437         return status;
438     }
439
440     public List<Boolean> validateServiceModuleProperty(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedToscaServiceGroupsDefinitionObject, ToscaDefinition toscaMainServiceDefinition, String vnfFile, List<Boolean> status) {
441         reportStartTestPrint("validateServiceModuleProperty", vnfFile);
442
443         Either<Boolean, Map<String, Object>> toscaServiceModulePropertyValidator = ToscaValidation.serviceToscaGroupPropertyValidator(expectedToscaServiceGroupsDefinitionObject, toscaMainServiceDefinition);
444         if (toscaServiceModulePropertyValidator.isRight())
445             status.add(false);
446         return status;
447     }
448
449     //--------------------------Service verification against Pavel Parser--------------------------------
450     public List<Boolean> validateServiceInputsUsingParser(ISdcCsarHelper fdntCsarHelper, ToscaDefinition toscaExpectedMainServiceDefinition, String vnfFile, List<Boolean> status) throws Exception {
451         if (fdntCsarHelper == null) {
452             reportSkipTestPrint("validateServiceInputsUsingParser", status);
453         } else {
454             reportStartTestPrint("validateServiceInputsUsingParser", vnfFile);
455             Map<String, ToscaInputsTopologyTemplateDefinition> expectedInputsMap = toscaExpectedMainServiceDefinition.getTopology_template().getInputs();
456             Either<Boolean, Map<String, Object>> toscaInputsValidator = ToscaValidation.toscaInputsValidatorAgainstParser(expectedInputsMap, fdntCsarHelper);
457             if (toscaInputsValidator.isRight())
458                 status.add(false);
459         }
460         return status;
461     }
462
463     public List<Boolean> validateServiceModuleMetadataUsingParser(ISdcCsarHelper fdntCsarHelper, Map<String, ToscaGroupsTopologyTemplateDefinition> expectedToscaServiceGroupsDefinitionObject, String vnfFile, List<Boolean> status) {
464         reportStartTestPrint("validateServiceModuleMetadataUsingParser", vnfFile);
465         String customizationUUID = fdntCsarHelper.getServiceNodeTemplates().get(0).getMetaData().getValue("customizationUUID");
466         List<Group> actualGroups = fdntCsarHelper.getVfModulesByVf(customizationUUID);
467         Either<Boolean, Map<String, Object>> toscaServiceModuleMetadataValidator = ToscaValidation.serviceToscaGroupMetadataValidatorUsingParser(expectedToscaServiceGroupsDefinitionObject, actualGroups);
468         if (toscaServiceModuleMetadataValidator.isRight())
469             status.add(false);
470         return status;
471     }
472
473     public List<Boolean> validateServiceModulePropertyUsingParser(ISdcCsarHelper fdntCsarHelper, Map<String, ToscaGroupsTopologyTemplateDefinition> expectedToscaServiceGroupsDefinitionObject, String vnfFile, List<Boolean> status) {
474         reportStartTestPrint("validateServiceModuleMetadataUsingParser", vnfFile);
475         String customizationUUID = fdntCsarHelper.getServiceNodeTemplates().get(0).getMetaData().getValue("customizationUUID");
476         List<Group> actualGroups = fdntCsarHelper.getVfModulesByVf(customizationUUID);
477         Either<Boolean, Map<String, Object>> toscaServiceModuleMetadataValidator = ToscaValidation.serviceToscaGroupPropertyValidatorUsingParser(expectedToscaServiceGroupsDefinitionObject, actualGroups);
478         if (toscaServiceModuleMetadataValidator.isRight())
479             status.add(false);
480         return status;
481     }
482
483     private Map<String, ToscaGroupsTopologyTemplateDefinition> createExpectedToscaServiceGroupsDefinitionObject(Resource resource, Service service, List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition) {
484         Map<String, ToscaGroupsTopologyTemplateDefinition> toscaGroupsTopologyTemplateDefinitionMap = new HashMap<>();
485
486         for (TypeHeatMetaDefinition moduleType : listTypeHeatMetaDefinition) {
487             if (!moduleType.getTypeName().equals("artifacts")) {
488                 for (GroupHeatMetaDefinition module : moduleType.getGroupHeatMetaDefinition()) {
489                     ToscaGroupsTopologyTemplateDefinition toscaGroupsTopologyTemplateDefinition = new ToscaGroupsTopologyTemplateDefinition();
490                     String resourceModuleName = buildResourceModuleName(resource, module.getGroupName());
491                     ToscaServiceGroupsMetadataDefinition toscaServiceGroupsMetadataDefinition = setGroupMetadataFromResourceObject(resourceModuleName, resource);
492                     if (!toscaServiceGroupsMetadataDefinition.equals("")) {
493                         String serviceModuleName = buildServiceModuleName(service.getComponentInstances().get(0).getNormalizedName(), toscaServiceGroupsMetadataDefinition.getVfModuleModelName());
494                         toscaServiceGroupsMetadataDefinition = setGroupMetadataFromServiceObject(toscaServiceGroupsMetadataDefinition, serviceModuleName, service);
495                         toscaGroupsTopologyTemplateDefinition.setMetadata(toscaServiceGroupsMetadataDefinition);
496                         ToscaGroupPropertyDefinition toscaGroupPropertyDefinition = setGroupProperty(module);
497                         toscaGroupsTopologyTemplateDefinition.setProperties(toscaGroupPropertyDefinition);
498                         toscaGroupsTopologyTemplateDefinitionMap.put(serviceModuleName, toscaGroupsTopologyTemplateDefinition);
499                     } else {
500                         getExtendTest().log(Status.FAIL, "module name [" + module.getGroupName() + "] didn't represent in resource");
501                     }
502                 }
503             }
504         }
505         return toscaGroupsTopologyTemplateDefinitionMap;
506
507     }
508
509
510     private Map<String, VfModuleDefinition> createExpectedVfModuleDefinitionObject(Resource resource, Service service, List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition) {
511         Map<String, VfModuleDefinition> toscaGroupsTopologyTemplateDefinitionMap = new HashMap<>();
512
513         for (TypeHeatMetaDefinition moduleType : listTypeHeatMetaDefinition) {
514             if (!moduleType.getTypeName().equals("artifacts")) {
515                 for (GroupHeatMetaDefinition module : moduleType.getGroupHeatMetaDefinition()) {
516                     VfModuleDefinition toscaGroupsTopologyTemplateDefinition = new VfModuleDefinition();
517                     String resourceModuleName = buildResourceModuleName(resource, module.getGroupName());
518                     ToscaServiceGroupsMetadataDefinition toscaServiceGroupsMetadataDefinition = setGroupMetadataFromResourceObject(resourceModuleName, resource);
519                     if (!toscaServiceGroupsMetadataDefinition.equals("")) {
520                         toscaServiceGroupsMetadataDefinition = setGroupMetadataFromServiceObject(toscaServiceGroupsMetadataDefinition, resourceModuleName, service);
521                         toscaGroupsTopologyTemplateDefinition.setMetadata(toscaServiceGroupsMetadataDefinition);
522                         ToscaGroupPropertyDefinition toscaGroupPropertyDefinition = setGroupProperty(module);
523                         toscaGroupsTopologyTemplateDefinition.setProperties(toscaGroupPropertyDefinition);
524                         toscaGroupsTopologyTemplateDefinition.setArtifacts(getArtifactsUuidListForRI(module, resource, service));
525                         toscaGroupsTopologyTemplateDefinitionMap.put(toscaServiceGroupsMetadataDefinition.vfModuleModelName, toscaGroupsTopologyTemplateDefinition);
526
527                     } else {
528                         getExtendTest().log(Status.FAIL, "module name [" + module.getGroupName() + "] didn't represent in resource");
529                     }
530                 }
531             }
532         }
533         return toscaGroupsTopologyTemplateDefinitionMap;
534
535     }
536
537     private List<String> getArtifactsUuidListForRI(GroupHeatMetaDefinition module, Resource resource, Service service) {
538 //TODO check if each heat include its env file
539         List<String> artifactsUuidList = new ArrayList<>();
540         ComponentInstance resourceInstance = null;
541         for (ComponentInstance componentInstance : service.getComponentInstances()) {
542             if (componentInstance.getUniqueId().contains(resource.getUniqueId())) {
543                 resourceInstance = componentInstance;
544                 break;
545             }
546         }
547
548         Collection<ArtifactDefinition> artifactsDefinitionValues = resourceInstance.getDeploymentArtifacts().values();
549
550         for (HeatMetaFirstLevelDefinition moduleArtifactDefinition : module.getArtifactList()) {
551             for (ArtifactDefinition riArtifactDefinition : artifactsDefinitionValues) {
552                 String heatYamlName = moduleArtifactDefinition.getFileName();
553                 String heatEnvName = heatYamlName.replaceAll("yaml|yml", "env");
554                 String riArtifactName = riArtifactDefinition.getArtifactName();
555                 if (riArtifactName.equals(heatYamlName) || riArtifactName.equals(heatEnvName)) {
556                     artifactsUuidList.add(riArtifactDefinition.getArtifactUUID());
557                 }
558             }
559         }
560
561         return artifactsUuidList;
562     }
563
564     private ToscaGroupPropertyDefinition setGroupProperty(GroupHeatMetaDefinition module) {
565         ToscaGroupPropertyDefinition toscaGroupPropertyDefinition = new ToscaGroupPropertyDefinition();
566         toscaGroupPropertyDefinition.setVf_module_label(module.getGroupName());
567         Boolean isBase = module.getPropertyHeatMetaDefinition().getValue();
568         if (isBase) {
569             toscaGroupPropertyDefinition.setInitial_count("1");
570             toscaGroupPropertyDefinition.setMin_vf_module_instances("1");
571             toscaGroupPropertyDefinition.setMax_vf_module_instances("1");
572             toscaGroupPropertyDefinition.setVf_module_type("Base");
573         } else {
574             toscaGroupPropertyDefinition.setInitial_count("0");
575             toscaGroupPropertyDefinition.setMin_vf_module_instances("0");
576             toscaGroupPropertyDefinition.setMax_vf_module_instances("");
577             toscaGroupPropertyDefinition.setVf_module_type("Expansion");
578         }
579         toscaGroupPropertyDefinition.setAvailability_zone_count("");
580         toscaGroupPropertyDefinition.setVfc_list("");
581         toscaGroupPropertyDefinition.setVf_module_description("");
582         toscaGroupPropertyDefinition.setVolume_group(isVolumeGroup(module));
583
584         return toscaGroupPropertyDefinition;
585     }
586
587     private String isVolumeGroup(GroupHeatMetaDefinition module) {
588         String isVolumeGroup = "false";
589         for (HeatMetaFirstLevelDefinition artifactList : module.getArtifactList()) {
590             if (artifactList.getType().equals(ArtifactTypeEnum.HEAT_VOL.getType())) {
591                 isVolumeGroup = "true";
592                 return isVolumeGroup;
593             }
594         }
595         return isVolumeGroup;
596     }
597
598     private Map<String, ToscaServiceGroupsMetadataDefinition> createExpectedToscaServiceGroupsPropertyDefinitionObject(Resource resource, Service service, List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition) {
599
600         Map<String, ToscaServiceGroupsMetadataDefinition> toscaServiceGroupsMetadataDefinitionMap = new HashMap<>();
601         for (TypeHeatMetaDefinition moduleType : listTypeHeatMetaDefinition) {
602             Map<String, String> groupProperty = new HashMap<>();
603
604             ToscaServiceGroupsMetadataDefinition toscaServiceGroupsMetadataDefinition = new ToscaServiceGroupsMetadataDefinition();
605             for (GroupHeatMetaDefinition module : moduleType.getGroupHeatMetaDefinition()) {
606                 String resourceModuleName = buildResourceModuleName(resource, module.getGroupName());
607                 toscaServiceGroupsMetadataDefinition = setGroupMetadataFromResourceObject(resourceModuleName, resource);
608                 if (!toscaServiceGroupsMetadataDefinition.equals("")) {
609                     String serviceModuleName = buildServiceModuleName(service.getComponentInstances().get(0).getNormalizedName(), toscaServiceGroupsMetadataDefinition.getVfModuleModelName());
610                     toscaServiceGroupsMetadataDefinition = setGroupMetadataFromServiceObject(toscaServiceGroupsMetadataDefinition, serviceModuleName, service);
611                     toscaServiceGroupsMetadataDefinitionMap.put(serviceModuleName, toscaServiceGroupsMetadataDefinition);
612                 } else {
613                     getExtendTest().log(Status.FAIL, "module name [" + module.getGroupName() + "] didn't represent in resource");
614                 }
615             }
616         }
617         return toscaServiceGroupsMetadataDefinitionMap;
618
619     }
620
621     private ToscaServiceGroupsMetadataDefinition setGroupMetadataFromServiceObject(ToscaServiceGroupsMetadataDefinition toscaServiceGroupsMetadataDefinition, String serviceModuleName, Service service) {
622         for (GroupInstance groupInstance : service.getComponentInstances().get(0).getGroupInstances()) {
623             if (groupInstance.getName().contains(serviceModuleName)) {
624                 toscaServiceGroupsMetadataDefinition.setVfModuleModelCustomizationUUID(groupInstance.getCustomizationUUID());
625                 return toscaServiceGroupsMetadataDefinition;
626             }
627         }
628         return toscaServiceGroupsMetadataDefinition;
629     }
630
631     private ToscaServiceGroupsMetadataDefinition setGroupMetadataFromResourceObject(String resourceModuleName, Resource resource) {
632         ToscaServiceGroupsMetadataDefinition toscaServiceGroupsMetadataDefinition = new ToscaServiceGroupsMetadataDefinition();
633         for (GroupDefinition group : resource.getGroups()) {
634             if (group.getName().contains(resourceModuleName)) {
635                 toscaServiceGroupsMetadataDefinition.setVfModuleModelName(group.getName());
636                 toscaServiceGroupsMetadataDefinition.setVfModuleModelInvariantUUID(group.getInvariantUUID());
637                 toscaServiceGroupsMetadataDefinition.setVfModuleModelUUID(group.getGroupUUID());
638                 toscaServiceGroupsMetadataDefinition.setVfModuleModelVersion(group.getVersion());
639                 return toscaServiceGroupsMetadataDefinition;
640             }
641         }
642         return toscaServiceGroupsMetadataDefinition;
643     }
644
645     public static String buildResourceModuleName(Resource resource, String groupName) {
646         return resource.getSystemName() + ".." + groupName + ".." + "module-";
647     }
648
649     public static String buildServiceModuleName(String resourceInstanceNormalizedName, String resourceGroupName) {
650         return resourceInstanceNormalizedName + ".." + resourceGroupName;
651     }
652
653
654     @Override
655     protected UserRoleEnum getRole() {
656         return UserRoleEnum.DESIGNER;
657     }
658
659
660     public static ToscaDefinition addGenericInputsToToscaObject(ToscaDefinition toscaDefinition, String genericName) throws Exception {
661         Resource genericResource = AtomicOperationUtils.getResourceObjectByNameAndVersion(UserRoleEnum.DESIGNER, genericName, "1.0");
662         ToscaTopologyTemplateDefinition topologyTemplate = toscaDefinition.getTopology_template();
663         Map<String, ToscaInputsTopologyTemplateDefinition> newInput = new HashMap<>();
664         for (PropertyDefinition property : genericResource.getProperties()) {
665             ToscaInputsTopologyTemplateDefinition input = new ToscaInputsTopologyTemplateDefinition();
666 //                              input.setConstraints(property.getConstraints());
667             input.setDefault(property.getDefaultValue());
668             input.setDescription(property.getDescription());
669 //                              input.setEntry_schema(property.getSchema());
670             input.setName(property.getName());
671 //                              input.setRequired(property.get);
672             input.setStatus(property.getStatus());
673             input.setType(property.getType());
674             input.setValue(property.getValue());
675             newInput.put(property.getName(), input);
676         }
677
678         topologyTemplate.addInputs(newInput);
679         toscaDefinition.setTopology_template(topologyTemplate);
680         return toscaDefinition;
681     }
682
683     public static ToscaDefinition setNameToToscaInput(ToscaDefinition toscaDefinition) {
684         Map<String, ToscaInputsTopologyTemplateDefinition> inputs = toscaDefinition.getTopology_template().getInputs();
685         for (String name : inputs.keySet()) {
686             inputs.get(name).setName(name);
687         }
688         toscaDefinition.getTopology_template().setInputs(inputs);
689         return toscaDefinition;
690     }
691
692     public static ToscaDefinition addAndGenerateResourceMetadataToExpectedObject(ToscaDefinition toscaDefinition, ResourceReqDetails resourceReqDetails, Component component) {
693
694         Map<String, String> metadata = convertResourceMetadataToMap(resourceReqDetails, component);
695         toscaDefinition.setMetadata(metadata);
696         return toscaDefinition;
697     }
698
699     public static Map<String, String> convertResourceMetadataToMap(ResourceReqDetails resourceReqDetails, Component component) {
700         Map<String, String> metadata = new HashMap<>();
701
702         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.CATEGORY.value, resourceReqDetails.getCategories().get(0).getName());
703         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.DESCRIPTION.value, resourceReqDetails.getDescription());
704         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.INVARIANT_UUID.value, component.getInvariantUUID());
705         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.TYPE.value, resourceReqDetails.getResourceType());
706         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.UUID.value, component.getUUID());
707         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.NAME.value, component.getName());
708
709         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.RESOURCE_VENDOR_NAME.value, resourceReqDetails.getVendorName());
710         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.RESOURCE_VENDOR_MODEL_NUMBER.value, resourceReqDetails.getResourceVendorModelNumber());
711         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.RESOURCE_VENDOR_RELEASE.value, resourceReqDetails.getVendorRelease());
712         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.SUBCATEGORY.value, resourceReqDetails.getCategories().get(0).getSubcategories().get(0).getName());
713         return metadata;
714     }
715
716     public static Map<String, String> convertResourceNodeTemplateMetadataToMap(ComponentInstance componentInstance) throws Exception {
717
718         Resource resource = AtomicOperationUtils.getResourceObject(componentInstance.getComponentUid());
719         Map<String, String> metadata = new HashMap<>();
720
721         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.CATEGORY.value, resource.getCategories().get(0).getName());
722         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.DESCRIPTION.value, resource.getDescription());
723         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.INVARIANT_UUID.value, resource.getInvariantUUID());
724         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.TYPE.value, resource.getResourceType().toString());
725         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.UUID.value, resource.getUUID());
726         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.NAME.value, resource.getName());
727
728         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.RESOURCE_VENDOR_NAME.value, resource.getVendorName());
729         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.RESOURCE_VENDOR_MODEL_NUMBER.value, resource.getResourceVendorModelNumber());
730         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.RESOURCE_VENDOR_RELEASE.value, resource.getVendorRelease());
731         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.SUBCATEGORY.value, resource.getCategories().get(0).getSubcategories().get(0).getName());
732         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.CUSTOMIZATION_UUID.value, componentInstance.getCustomizationUUID());
733         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.VERSION.value, componentInstance.getComponentVersion());
734
735         return metadata;
736     }
737
738     public static Map<String, String> generateServiceNodeTemplateMetadataToExpectedObject(ResourceReqDetails resourceReqDetails, Component component, ComponentInstance componentInstanceDefinition) {
739
740         Map<String, String> metadata = convertResourceMetadataToMap(resourceReqDetails, component);
741         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.CUSTOMIZATION_UUID.value, componentInstanceDefinition.getCustomizationUUID());
742         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.VERSION.value, componentInstanceDefinition.getComponentVersion());
743
744         return metadata;
745     }
746
747     public static Map<String, Map<String, String>> generateResourceNodeTemplateMetadataToExpectedObject(Component component) throws Exception {
748
749         Map<String, Map<String, String>> resourcesNodeTemplateMetadataMap = new HashMap<>();
750         if (component.getComponentInstances() != null && !component.getComponentInstances().isEmpty()) {
751             for (ComponentInstance componentInstance : component.getComponentInstances()) {
752                 Map<String, String> metadata = convertResourceNodeTemplateMetadataToMap(componentInstance);
753                 resourcesNodeTemplateMetadataMap.put(componentInstance.getName(), metadata);
754             }
755         }
756         return resourcesNodeTemplateMetadataMap;
757     }
758
759     public static Map<String, String> generateServiceMetadataToExpectedObject(ServiceReqDetails serviceReqDetails, Component component) {
760
761         Map<String, String> metadata = new HashMap<>();
762
763         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.CATEGORY.value, serviceReqDetails.getCategories().get(0).getName());
764         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.DESCRIPTION.value, serviceReqDetails.getDescription());
765         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.INVARIANT_UUID.value, component.getInvariantUUID());
766         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.TYPE.value, "Service");
767         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.UUID.value, component.getUUID());
768         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.NAME.value, component.getName());
769
770         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.SERVICE_TYPE.value, serviceReqDetails.getServiceType());
771         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.SERVICE_ROLE.value, serviceReqDetails.getServiceRole());
772         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.NAMING_POLICY.value, serviceReqDetails.getNamingPolicy());
773         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.ECOMP_GENERATED_NAMING.value, serviceReqDetails.getEcompGeneratedNaming().toString());
774         metadata.put(ToscaMetadataFieldsPresentationEnum.ToscaMetadataFieldsEnum.SERVICE_ECOMP_NAMING.value, serviceReqDetails.getEcompGeneratedNaming().toString());//equals to ECOMP_GENERATED_NAMING
775
776         return metadata;
777     }
778
779     public static void reportStartTestPrint(String testName, String vnfFile) {
780 //              reportMessageInColor("info", "blue", "Running test \" + testName + \" with zip file - \" + vnfFile");
781         SetupCDTest.getExtendTest().log(Status.INFO, "<html><font color=\"blue\"> Running test " + testName + " with zip file - " + vnfFile + "</font></html>");
782     }
783
784     public static void reportSkipTestPrint(String testName, List<Boolean> status) {
785 //              reportMessageInColor("error", "orange", "Skip test \" + testName + \" due to previous tosca parser error");
786         SetupCDTest.getExtendTest().log(Status.ERROR, "<html><font color=\"orange\"> Skip test " + testName + " due to previous tosca parser error" + "</font></html>");
787         status.add(false);
788     }
789
790     public static void reportMessageInColor(String status, String color, String message) {
791         String printLine = getReportMessageInColor(color, message);
792         SetupCDTest.getExtendTest().log(Status.valueOf(status), printLine);
793 //              SetupCDTest.getExtendTest().log(Status.valueOf(status), getReportMessageInColor(color, message));
794     }
795
796     /**
797      * @param color   = red, green, orange, blue ...
798      * @param message - message string
799      * @return string in desired color
800      */
801     public static String getReportMessageInColor(String color, String message) {
802         String returnValue = ("<html><font color=\\\"+color+\"\">" + message + "</font></html>").toString();
803         return returnValue;
804     }
805
806 /*      @Test()
807     public void printTest(){
808                 System.out.println("print");
809                 reportMessageInColor("ERROR", "green", "green");
810                 reportMessageInColor("INFO", "orange", "orange");
811                 reportMessageInColor("INFO", "red", "red");
812         }*/
813
814 /*      @Test
815         public void allottedResourceModelTest() throws Exception{
816                 List<Boolean> status = new ArrayList<>();
817
818                 List<String> fileNamesFromFolder = OnboardingUtils.getVnfNamesFileListExcludeToscaParserFailure();
819                 List<String> newRandomFileNamesFromFolder = OnbordingDataProviders.getRandomElements(1, fileNamesFromFolder);
820                 String vnfFile = newRandomFileNamesFromFolder.get(0);
821                 setLog(vnfFile);
822                 String filePath = FileHandling.getVnfRepositoryPath();
823                 File filesFolder = new File(SetupCDTest.getWindowTest().getDownloadDirectory());
824
825                 ResourceReqDetails resourceReqDetails = ElementFactory.getDefaultResource(ResourceCategoryEnum.ALLOTTED_RESOURCE_SERVICE_ADMIN);
826                 resourceReqDetails = createCustomizedVsp(resourceReqDetails, filePath, vnfFile);
827
828                 ToscaDefinition toscaMainAmdocsDefinition = downloadAndGetToscaMainYamlObjectUI(resourceReqDetails, filesFolder);
829                 toscaMainAmdocsDefinition = addGenericPropertiesToToscaDefinitionObject(toscaMainAmdocsDefinition, GENERIC_VF);
830
831                 Resource resource = OnboardingUtillViaApis.createResourceFromVSP(resourceReqDetails);
832                 resource = (Resource) AtomicOperationUtils.changeComponentState(resource, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
833
834                 ToscaDefinition toscaMainVfDefinition = downloadAndGetToscaMainYamlObjectApi(resource, filesFolder);
835
836                 status = validateVfMetadata(toscaMainAmdocsDefinition, toscaMainVfDefinition, resourceReqDetails, resource, vnfFile, status);
837                 status = validateResourceNodeTemplateMetadata(toscaMainVfDefinition, resource, vnfFile, status);
838                 status = validateResourceInputs(toscaMainAmdocsDefinition, toscaMainVfDefinition, vnfFile, status);
839
840                 if(status.contains(false)){
841                         SetupCDTest.getExtendTest().log(Status.FAIL, "Summary: allottedResourceModelTest tosca validation test failed with zip file " + vnfFile);
842                         Assert.assertFalse(true);
843                 }
844         }*/
845
846
847     //  help method to toscaValidation tests
848     private ISdcCsarHelper initSdcCsarHelper(File serviceCsarFileName, File filesFolder) {
849
850         ISdcCsarHelper fdntCsarHelper;
851         try {
852             SetupCDTest.getExtendTest().log(Status.INFO, "Tosca parser is going to convert service csar file to ISdcCsarHelper object...");
853             fdntCsarHelper = factory.getSdcCsarHelper(filesFolder.getPath() + serviceCsarFileName);
854         } catch (Exception e) {
855             SetupCDTest.getExtendTest().log(Status.ERROR, "Tosca parser FAILED to convert service csar file to ISdcCsarHelper object...");
856             SetupCDTest.getExtendTest().log(Status.FAIL, e);
857             fdntCsarHelper = null;
858         }
859         return fdntCsarHelper;
860     }
861
862
863     /**
864      * @param resourceReqDetails to create Vsp
865      * @return updated resourceReqDetails after Vsp was created
866      */
867     private ResourceReqDetails createCustomizedVsp(ResourceReqDetails resourceReqDetails, String filePath, String vnfFile) throws Exception {
868         VendorSoftwareProductObject vendorSoftwareProductObject = OnboardingUtillViaApis.createVspViaApis(resourceReqDetails, filePath, vnfFile, user);
869         resourceReqDetails = OnboardingUtillViaApis.prepareOnboardedResourceDetailsBeforeCreate(resourceReqDetails, vendorSoftwareProductObject);
870         return resourceReqDetails;
871     }
872
873     /**
874      * @param resourceReqDetails to download csar file via UI
875      * @return Tosca definition object from main yaml file
876      */
877     private ToscaDefinition downloadAndGetToscaMainYamlObjectUI(ResourceReqDetails resourceReqDetails, File filesFolder) throws Exception {
878         DownloadManager.downloadCsarByNameFromVSPRepository(resourceReqDetails.getName(), false);
879         File amdocsCsarFileName = FileHandling.getLastModifiedFileNameFromDir(filesFolder.getAbsolutePath());
880         return ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(amdocsCsarFileName);
881     }
882
883     /**
884      * @param toscaMainAmdocsDefinition object to add generic properties
885      * @param genericName               resource name
886      * @return updated toscaMainAmdocsDefinition object
887      */
888     private ToscaDefinition addGenericPropertiesToToscaDefinitionObject(ToscaDefinition toscaMainAmdocsDefinition, String genericName) throws Exception {
889         toscaMainAmdocsDefinition = setNameToToscaInput(toscaMainAmdocsDefinition);
890         toscaMainAmdocsDefinition = addGenericInputsToToscaObject(toscaMainAmdocsDefinition, genericName);
891         return toscaMainAmdocsDefinition;
892     }
893
894     /**
895      * @param resource to download csar file via API
896      * @return Tosca definition object from main yaml file
897      */
898     private ToscaDefinition downloadAndGetToscaMainYamlObjectApi(Resource resource, File filesFolder) throws Exception {
899         File vfCsarFileName = new File(File.separator + "VfCsar_" + ElementFactory.generateUUIDforSufix() + ".csar");
900         OnboardingUtillViaApis.downloadToscaCsarToDirectory(resource, new File(filesFolder.getPath() + vfCsarFileName));
901         return ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(new File(filesFolder.getPath() + vfCsarFileName));
902     }
903
904
905 }
906
907