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