c69d70a66d9b92a5ab9af9e9603db21898ad5e62
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.backend.ci.tests.utils.general;
22
23 import com.aventstack.extentreports.Status;
24 import com.google.gson.Gson;
25 import fj.data.Either;
26 import org.apache.commons.codec.binary.Base64;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.json.JSONException;
29 import org.onap.sdc.backend.ci.tests.datatypes.*;
30 import org.onap.sdc.backend.ci.tests.datatypes.enums.*;
31 import org.onap.sdc.backend.ci.tests.datatypes.http.HttpHeaderEnum;
32 import org.onap.sdc.backend.ci.tests.datatypes.http.HttpRequest;
33 import org.onap.sdc.backend.ci.tests.datatypes.http.RestResponse;
34 import org.onap.sdc.backend.ci.tests.execute.lifecycle.LCSbaseTest;
35 import org.onap.sdc.backend.ci.tests.utils.rest.*;
36 import org.onap.sdc.backend.ci.tests.datatypes.enums.*;
37 import org.onap.sdc.backend.ci.tests.utils.rest.*;
38 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
39 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
40 import org.openecomp.sdc.be.datatypes.elements.ConsumerDataDefinition;
41 import org.openecomp.sdc.be.datatypes.enums.AssetTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
43 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
44 import org.openecomp.sdc.be.model.*;
45 import org.onap.sdc.backend.ci.tests.api.ComponentBaseTest;
46 import org.onap.sdc.backend.ci.tests.api.ExtentTestActions;
47 import org.onap.sdc.backend.ci.tests.api.Urls;
48 import org.onap.sdc.backend.ci.tests.config.Config;
49 import org.onap.sdc.backend.ci.tests.tosca.datatypes.ToscaDefinition;
50 import org.onap.sdc.backend.ci.tests.utils.CsarToscaTester;
51 import org.onap.sdc.backend.ci.tests.utils.DistributionUtils;
52 import org.onap.sdc.backend.ci.tests.utils.ToscaParserUtils;
53 import org.onap.sdc.backend.ci.tests.utils.Utils;
54 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
55 import org.openecomp.sdc.common.util.GeneralUtility;
56 import org.testng.SkipException;
57
58 import java.io.File;
59 import java.io.IOException;
60 import java.nio.file.Files;
61 import java.nio.file.Path;
62 import java.nio.file.Paths;
63 import java.util.ArrayList;
64 import java.util.HashMap;
65 import java.util.List;
66 import java.util.Map;
67 import java.util.Map.Entry;
68 import java.util.concurrent.TimeUnit;
69 import java.util.stream.Collectors;
70
71 import static org.junit.Assert.assertFalse;
72 import static org.testng.AssertJUnit.assertEquals;
73 import static org.testng.AssertJUnit.assertTrue;
74
75 public final class AtomicOperationUtils {
76
77         static final String basicAuthentication = "Basic Y2k6MTIzNDU2";
78
79         private AtomicOperationUtils() {
80                 throw new UnsupportedOperationException();
81         }
82
83         // *********** RESOURCE ****************
84         /**
85          * Import a vfc From tosca file
86          *
87          * @param filePath
88          * @param fileName
89          * @return
90          * @throws IOException
91          * @throws JSONException
92          */
93         public static Either<Resource, RestResponse> importResource(String filePath, String fileName) {
94                 try {
95                         User designer = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
96                         ImportReqDetails importReqDetails = ElementFactory.getDefaultImportResource(ElementFactory.getResourcePrefix());
97                         importReqDetails = ImportUtils.getImportResourceDetailsByPathAndName(importReqDetails, filePath, fileName);
98                         RestResponse importResourceResponse = ResourceRestUtils.createImportResource(importReqDetails, designer, null);
99                         return buildResourceFromResponse(importResourceResponse);
100                 } catch (Exception e) {
101                         throw new AtomicOperationException(e);
102                 }
103         }
104
105         public static Either<Resource, RestResponse> importResource(ImportReqDetails importReqDetails, String filePath, String fileName, User userRole, Boolean validateState) {
106                 try {
107                         importReqDetails = ImportUtils.getImportResourceDetailsByPathAndName(importReqDetails, filePath, fileName);
108                         RestResponse importResourceResponse = ResourceRestUtils.createImportResource(importReqDetails, userRole, null);
109
110                         if (validateState) {
111                                 assertTrue("Import resource failed with error: " + importResourceResponse.getResponse(),importResourceResponse.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
112                         }
113
114                         if (importResourceResponse.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
115                                 Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(importResourceResponse.getResponse());
116                                 return Either.left(resourceResponseObject);
117                         }
118                         return Either.right(importResourceResponse);
119                 } catch (Exception e) {
120                         throw new AtomicOperationException(e);
121                 }
122         }
123
124
125         public static Either<Resource, RestResponse> createResourceByType(ResourceTypeEnum resourceType, UserRoleEnum userRole, Boolean validateState) {
126                 try {
127                         User defaultUser = ElementFactory.getDefaultUser(userRole);
128                         ResourceReqDetails defaultResource = ElementFactory.getDefaultResourceByType(resourceType, defaultUser);
129                         RestResponse resourceResp = ResourceRestUtils.createResource(defaultResource, defaultUser);
130
131                         if (validateState) {
132                                 assertTrue("Create resource failed with error: " + resourceResp.getResponse(),resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
133                         }
134
135                         if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
136                                 Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
137                                 return Either.left(resourceResponseObject);
138                         }
139                         return Either.right(resourceResp);
140                 } catch (Exception e) {
141                         throw new AtomicOperationException(e);
142                 }
143         }
144
145         public static Either<Resource, RestResponse> createResourceByResourceDetails(ResourceReqDetails resourceDetails, UserRoleEnum userRole, Boolean validateState) {
146                 try {
147                         User defaultUser = ElementFactory.getDefaultUser(userRole);
148                         RestResponse resourceResp = ResourceRestUtils.createResource(resourceDetails, defaultUser);
149
150                         if (validateState) {
151                                 assertTrue("Create resource failed with error: " + resourceResp.getResponse(),resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
152                         }
153
154                         if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
155                                 Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
156                                 return Either.left(resourceResponseObject);
157                         }
158                         return Either.right(resourceResp);
159                 } catch (Exception e) {
160                         throw new AtomicOperationException(e);
161                 }
162         }
163
164         public static Either<Resource, RestResponse> createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum resourceType, NormativeTypesEnum normativeTypes, ResourceCategoryEnum resourceCategory, UserRoleEnum userRole, Boolean validateState)
165                         throws Exception {
166                 User defaultUser = ElementFactory.getDefaultUser(userRole);
167                 ResourceReqDetails defaultResource = ElementFactory.getDefaultResourceByTypeNormTypeAndCatregory(resourceType, normativeTypes, resourceCategory, defaultUser);
168                 RestResponse resourceResp = ResourceRestUtils.createResource(defaultResource, defaultUser);
169
170                 if (validateState) {
171                         assertTrue("Actual Response Code is: " + resourceResp.getErrorCode(), resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
172                 }
173
174                 if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
175                         // Resource resourceResponseObject = ResponseParser
176                         // .convertResourceResponseToJavaObject(resourceResp.getResponse());
177                         Resource resourceResponseObject = ResponseParser.parseToObjectUsingMapper(resourceResp.getResponse(), Resource.class);
178                         return Either.left(resourceResponseObject);
179                 }
180                 return Either.right(resourceResp);
181         }
182
183         public static Either<Resource, RestResponse> createResourcesByCustomNormativeTypeAndCatregory(ResourceTypeEnum resourceType, Resource resourceNormativeType, ResourceCategoryEnum resourceCategory, UserRoleEnum userRole, Boolean validateState)
184                         throws Exception {
185                 User defaultUser = ElementFactory.getDefaultUser(userRole);
186                 ResourceReqDetails defaultResource = ElementFactory.getDefaultResourceByTypeNormTypeAndCatregory(resourceType, resourceNormativeType, resourceCategory, defaultUser);
187                 RestResponse resourceResp = ResourceRestUtils.createResource(defaultResource, defaultUser);
188
189                 if (validateState) {
190                         assertTrue("Create resource failed with error: " + resourceResp.getResponse(), resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
191                 }
192
193                 if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
194                         // Resource resourceResponseObject = ResponseParser
195                         // .convertResourceResponseToJavaObject(resourceResp.getResponse());
196                         Resource resourceResponseObject = ResponseParser.parseToObjectUsingMapper(resourceResp.getResponse(), Resource.class);
197                         return Either.left(resourceResponseObject);
198                 }
199                 return Either.right(resourceResp);
200         }
201
202         public static Either<Resource, RestResponse> updateResource(ResourceReqDetails resourceReqDetails, User defaultUser, Boolean validateState) {
203                 try {
204
205                         RestResponse resourceResp = ResourceRestUtils.updateResource(resourceReqDetails, defaultUser, resourceReqDetails.getUniqueId());
206
207                         if (validateState) {
208                                 assertTrue("Update resource failed with error: " + resourceResp.getResponse(),resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_SUCCESS);
209                         }
210
211                         if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_SUCCESS) {
212                                 Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
213                                 return Either.left(resourceResponseObject);
214                         }
215                         return Either.right(resourceResp);
216                 } catch (Exception e) {
217                         throw new AtomicOperationException(e);
218                 }
219         }
220
221         // *********** SERVICE ****************
222
223         public static Either<Service, RestResponse> createDefaultService(UserRoleEnum userRole, Boolean validateState) throws Exception {
224                 User defaultUser = ElementFactory.getDefaultUser(userRole);
225                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService(defaultUser);
226                 RestResponse createServiceResp = ServiceRestUtils.createService(serviceDetails, defaultUser);
227
228                 if (validateState) {
229                         assertTrue("Create service failed with error: " + createServiceResp.getResponse(),createServiceResp.getErrorCode() == ServiceRestUtils.STATUS_CODE_CREATED);
230                 }
231
232                 if (createServiceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
233                         Service serviceResponseObject = ResponseParser.convertServiceResponseToJavaObject(createServiceResp.getResponse());
234                         return Either.left(serviceResponseObject);
235                 }
236                 return Either.right(createServiceResp);
237         }
238
239         public static Either<Service, RestResponse> createServiceByCategory(ServiceCategoriesEnum category, UserRoleEnum userRole, Boolean validateState) throws Exception {
240                 User defaultUser = ElementFactory.getDefaultUser(userRole);
241                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService(category, defaultUser);
242                 RestResponse createServiceResp = ServiceRestUtils.createService(serviceDetails, defaultUser);
243
244                 if (validateState) {
245                         assertTrue("Create service failed with error: " + createServiceResp.getResponse(),createServiceResp.getErrorCode() == ServiceRestUtils.STATUS_CODE_CREATED);
246                 }
247
248                 if (createServiceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
249                         Service serviceResponseObject = ResponseParser.convertServiceResponseToJavaObject(createServiceResp.getResponse());
250                         return Either.left(serviceResponseObject);
251                 }
252                 return Either.right(createServiceResp);
253         }
254
255         public static Either<Service, RestResponse> createCustomService(ServiceReqDetails serviceDetails, UserRoleEnum userRole, Boolean validateState) throws Exception {
256                 User defaultUser = ElementFactory.getDefaultUser(userRole);
257                 RestResponse createServiceResp = ServiceRestUtils.createService(serviceDetails, defaultUser);
258
259                 if (validateState) {
260                         assertTrue("Create service failed with error: " + createServiceResp.getResponse(),createServiceResp.getErrorCode() == ServiceRestUtils.STATUS_CODE_CREATED);
261                 }
262
263                 if (createServiceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
264                         Service serviceResponseObject = ResponseParser.convertServiceResponseToJavaObject(createServiceResp.getResponse());
265                         return Either.left(serviceResponseObject);
266                 }
267                 return Either.right(createServiceResp);
268         }
269         // *********** PRODUCT ****************
270
271         public static Either<Product, RestResponse> createDefaultProduct(UserRoleEnum userRole, Boolean validateState) throws Exception {
272                 User defaultUser = ElementFactory.getDefaultUser(userRole);
273                 ProductReqDetails defaultProduct = ElementFactory.getDefaultProduct();
274                 RestResponse createProductResp = ProductRestUtils.createProduct(defaultProduct, defaultUser);
275
276                 if (validateState) {
277                         assertTrue(createProductResp.getErrorCode() == ProductRestUtils.STATUS_CODE_CREATED);
278                 }
279
280                 if (createProductResp.getErrorCode() == ProductRestUtils.STATUS_CODE_CREATED) {
281                         Product productResponseJavaObject = ResponseParser.convertProductResponseToJavaObject(createProductResp.getResponse());
282                         return Either.left(productResponseJavaObject);
283                 }
284                 return Either.right(createProductResp);
285         }
286
287         // public static ComponentReqDetails
288         // convertCompoentToComponentReqDetails(Component component){
289         //
290         // ComponentReqDetails componentReqDetails =
291         // ElementFactory.getDefaultService();
292         // componentReqDetails.setName(component.getName());
293         // componentReqDetails.setDescription(component.getDescription());
294         // componentReqDetails.setTags(component.getTags());
295         // componentReqDetails.setContactId(component.getContactId());
296         // componentReqDetails.setIcon(component.getIcon());
297         // componentReqDetails.setUniqueId(component.getUniqueId());
298         // componentReqDetails.setCreatorUserId(component.getCreatorUserId());
299         // componentReqDetails.setCreatorFullName(component.getCreatorFullName());
300         // componentReqDetails.setLastUpdaterUserId(component.getLastUpdaterUserId());
301         // componentReqDetails.setLastUpdaterFullName(component.getLastUpdaterFullName());
302         // componentReqDetails.setCreationDate(component.getCreationDate());
303         // componentReqDetails.setLastUpdateDate(component.getLastUpdateDate());
304         // componentReqDetails.setLifecycleState(component.getLifecycleState());
305         // componentReqDetails.setVersion(component.getVersion());
306         // componentReqDetails.setUuid(component.getUUID());
307         // componentReqDetails.setCategories(component.getCategories());
308         // componentReqDetails.setProjectCode(component.getProjectCode());
309         //
310         // return componentReqDetails;
311         // }
312
313         // *********** LIFECYCLE ***************
314
315     public static Pair<Component, RestResponse> changeComponentState(Component component, UserRoleEnum userRole, LifeCycleStatesEnum targetState, Boolean validateState) throws Exception {
316
317         Boolean isValidationFailed = false;
318         RestResponse lifeCycleStatesResponse = null;
319         User defaultUser;
320
321         LifeCycleStatesEnum currentCompState = LifeCycleStatesEnum.findByCompState(component.getLifecycleState().toString());
322
323                 if (currentCompState == targetState) {
324                         component = getComponentObject(component, userRole);
325                         return Pair.of(component, null);
326                 }
327                 String componentType = component.getComponentType().getValue();
328                 ArrayList<String> lifeCycleStatesEnumList = new ArrayList<>();
329                 if (currentCompState.equals(LifeCycleStatesEnum.CHECKIN) && targetState.equals(LifeCycleStatesEnum.CHECKOUT)) {
330                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CHECKIN.toString());
331                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CHECKOUT.toString());
332                 } else {
333                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CHECKOUT.toString());
334                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CHECKIN.toString());
335                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CERTIFY.toString());
336                 }
337                 for (int i = 0; i < lifeCycleStatesEnumList.size(); i++) {
338                         if (lifeCycleStatesEnumList.get(i).equals(currentCompState.name())) {
339                                 int a;
340                                 a = (i == lifeCycleStatesEnumList.size() - 1) ? 0 : i + 1;
341                                 for (int n = a; n < lifeCycleStatesEnumList.size(); n++) {
342                                         defaultUser = ElementFactory.getDefaultUser(userRole);
343                                         lifeCycleStatesResponse = LifecycleRestUtils.changeComponentState(component, defaultUser, LifeCycleStatesEnum.findByState(lifeCycleStatesEnumList.get(n)));
344                                         if (lifeCycleStatesResponse.getErrorCode() != LifecycleRestUtils.STATUS_CODE_SUCCESS)
345                                                 isValidationFailed = true;
346                                         if (lifeCycleStatesEnumList.get(n).equals(targetState.toString()) || isValidationFailed) {
347                                                 break;
348                                         }
349                                 }
350                         }
351                 }
352                 Component componentJavaObject = getComponentObject(component, userRole);
353
354                 if (validateState && isValidationFailed) {
355                         assertTrue("change state to [" + targetState.getState() + "] failed" + lifeCycleStatesResponse.getResponse(), false);
356                         return Pair.of(componentJavaObject, lifeCycleStatesResponse);
357                 }
358
359                 if (isValidationFailed) {
360                         return Pair.of(componentJavaObject, lifeCycleStatesResponse);
361                 }
362
363                 return Pair.of(componentJavaObject, lifeCycleStatesResponse);
364         }
365
366         public static RestResponse distributeService(Component component, Boolean validateState) throws Exception {
367
368                 Service service = (Service) component;
369
370                 User opsUser = ElementFactory.getDefaultUser(UserRoleEnum.OPS);
371                 User governotUser = ElementFactory.getDefaultUser(UserRoleEnum.GOVERNOR);
372
373                 ServiceReqDetails serviceDetails = new ServiceReqDetails(service);
374                 RestResponse distributionService = null;
375
376                 RestResponse approveDistribution = LifecycleRestUtils.changeDistributionStatus(serviceDetails, null, governotUser, "approveService", DistributionStatusEnum.DISTRIBUTED);
377                 if (approveDistribution.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
378                         distributionService = LifecycleRestUtils.changeDistributionStatus(serviceDetails, null, opsUser, "approveService", DistributionStatusEnum.DISTRIBUTED);
379                 }
380
381                 if (validateState) {
382                         assertTrue("Distribution approve failed with error: " + approveDistribution.getResponse(),approveDistribution.getErrorCode() == ProductRestUtils.STATUS_CODE_SUCCESS);
383                         assertTrue("Distribute service failed with error: " + distributionService.getResponse(),distributionService.getErrorCode() == ProductRestUtils.STATUS_CODE_SUCCESS);
384                         return distributionService;
385                 }
386
387                 return distributionService;
388         }
389
390         public static void toscaValidation(Component component, String vnfFile) throws Exception {
391
392                 ISdcCsarHelper fdntCsarHelper;
393                 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
394                 File csarFile = AssetRestUtils.getToscaModelCsarFile(AssetTypeEnum.SERVICES, component.getUUID() , vnfFile);
395                 ExtentTestActions.log(Status.INFO, "Tosca parser is going to convert service csar file to ISdcCsarHelper object...");
396                 fdntCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
397                 CsarToscaTester.processCsar(fdntCsarHelper);
398                 ExtentTestActions.log(Status.INFO, String.format("Tosca parser successfully parsed service CSAR"));
399
400         }
401
402         // *********** ARTIFACTS *****************
403
404         public static Either<ArtifactDefinition, RestResponse> uploadArtifactByType(ArtifactTypeEnum artifactType, Component component, UserRoleEnum userRole, Boolean deploymentTrue, Boolean validateState) throws Exception {
405
406                 User defaultUser = ElementFactory.getDefaultUser(userRole);
407                 ArtifactReqDetails artifactDetails = ElementFactory.getArtifactByType(null, artifactType, deploymentTrue);
408                 if (!deploymentTrue)
409                         artifactDetails.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL.getType());
410                 RestResponse uploadArtifactResp = ArtifactRestUtils.uploadArtifact(artifactDetails, component, defaultUser);
411
412                 if (validateState) {
413                         assertTrue("artifact upload failed: " + artifactDetails.getArtifactName(), uploadArtifactResp.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
414                 }
415
416                 if (uploadArtifactResp.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
417                         ArtifactDefinition artifactJavaObject = ResponseParser.convertArtifactDefinitionResponseToJavaObject(uploadArtifactResp.getResponse());
418                         return Either.left(artifactJavaObject);
419                 }
420                 return Either.right(uploadArtifactResp);
421         }
422
423         // *********** CONTAINERS *****************
424         /**
425          * Adds Component instance to Component
426          *
427          * @param compInstParent
428          * @param compContainer
429          * @return
430          */
431         public static Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainer(Component compInstParent, Component compContainer) {
432                 return addComponentInstanceToComponentContainer(compInstParent, compContainer, UserRoleEnum.DESIGNER, false);
433         }
434
435     public static Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainer(Component compInstParent,
436                                                                                                    Component compContainer,
437                                                                                                    UserRoleEnum userRole,
438                                                                                                    Boolean validateState) {
439         try {
440             User defaultUser = ElementFactory.getDefaultUser(userRole);
441             ComponentInstanceReqDetails componentInstanceDetails = ElementFactory.getComponentInstance(compInstParent);
442             if (componentInstanceDetails.getOriginType() == null){
443                 componentInstanceDetails.setOriginType(((Resource) compInstParent).getResourceType().toString());
444             }
445             RestResponse createComponentInstance = ComponentInstanceRestUtils.createComponentInstance(componentInstanceDetails,
446                     defaultUser, compContainer);
447
448             if (validateState) {
449                 if (createComponentInstance.getErrorCode() == ServiceRestUtils.STATUS_CODE_NOT_FOUND) {
450                     throw new SkipException("Open bug DE262001");
451                 } else {
452                     assertTrue("error - " + createComponentInstance.getErrorCode() + "instead - " +
453                             ServiceRestUtils.STATUS_CODE_CREATED,
454                             createComponentInstance.getErrorCode() == ServiceRestUtils.STATUS_CODE_CREATED);
455                 }
456             }
457
458             if (createComponentInstance.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
459                 ComponentInstance componentInstance = ResponseParser
460                         .convertComponentInstanceResponseToJavaObject(createComponentInstance.getResponse());
461                 return Either.left(componentInstance);
462             }
463             return Either.right(createComponentInstance);
464         } catch (Exception e) {
465             throw new AtomicOperationException(e);
466         }
467     }
468
469         public static Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainer(Component compInstParent, Component compContainer, UserRoleEnum userRole, Boolean validateState, String positionX, String positionY) {
470                 try {
471                         User defaultUser = ElementFactory.getDefaultUser(userRole);
472                         ComponentInstanceReqDetails componentInstanceDetails = ElementFactory.getComponentInstance(compInstParent);
473                         componentInstanceDetails.setPosX(positionX);
474                         componentInstanceDetails.setPosY(positionY);
475                         RestResponse createComponentInstance = ComponentInstanceRestUtils.createComponentInstance(componentInstanceDetails, defaultUser, compContainer);
476
477                         if (validateState) {
478                                 if (createComponentInstance.getErrorCode() == ServiceRestUtils.STATUS_CODE_NOT_FOUND)
479                                 {
480                                         throw new SkipException("Open bug DE262001");
481                                 }
482                                 else{
483                                         assertTrue("error - " + createComponentInstance.getErrorCode() + "instead - " + ServiceRestUtils.STATUS_CODE_CREATED, createComponentInstance.getErrorCode() == ServiceRestUtils.STATUS_CODE_CREATED);
484                                 }
485                         }
486
487                         if (createComponentInstance.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
488                                 ComponentInstance componentInstance = ResponseParser.convertComponentInstanceResponseToJavaObject(createComponentInstance.getResponse());
489                                 return Either.left(componentInstance);
490                         }
491                         return Either.right(createComponentInstance);
492                 } catch (Exception e) {
493                         throw new AtomicOperationException(e);
494                 }
495         }
496
497         public static Resource getResourceObject(Component containerDetails, UserRoleEnum userRole) throws Exception {
498                 // User defaultUser = ElementFactory.getDefaultUser(userRole);
499                 RestResponse restResponse = ResourceRestUtils.getResource(containerDetails.getUniqueId());
500                 return ResponseParser.convertResourceResponseToJavaObject(restResponse.getResponse());
501         }
502
503         public static Resource getResourceObject(String uniqueId) throws Exception {
504                 RestResponse restResponse = ResourceRestUtils.getResource(uniqueId);
505                 return ResponseParser.convertResourceResponseToJavaObject(restResponse.getResponse());
506         }
507
508         public static Resource getResourceObjectByNameAndVersion(UserRoleEnum sdncModifierDetails, String resourceName, String resourceVersion) throws Exception {
509                 User defaultUser = ElementFactory.getDefaultUser(sdncModifierDetails);
510                 RestResponse resourceResponse = ResourceRestUtils.getResourceByNameAndVersion(defaultUser.getUserId(), resourceName, resourceVersion);
511                 return ResponseParser.convertResourceResponseToJavaObject(resourceResponse.getResponse());
512         }
513
514         public static Service getServiceObject(Component containerDetails, UserRoleEnum userRole) throws Exception {
515                 User defaultUser = ElementFactory.getDefaultUser(userRole);
516                 RestResponse serviceResponse = ServiceRestUtils.getService(containerDetails.getUniqueId(), defaultUser);
517                 return ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse());
518         }
519
520         public static Service getServiceObjectByNameAndVersion(UserRoleEnum sdncModifierDetails, String serviceName, String serviceVersion) throws Exception {
521                 User defaultUser = ElementFactory.getDefaultUser(sdncModifierDetails);
522                 RestResponse serviceResponse = ServiceRestUtils.getServiceByNameAndVersion(defaultUser, serviceName, serviceVersion);
523                 return ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse());
524         }
525
526         public static Service getServiceObject(String uniqueId) throws Exception {
527                 RestResponse serviceResponse = ServiceRestUtils.getService(uniqueId);
528                 return ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse());
529         }
530
531         public static Product getProductObject(Component containerDetails, UserRoleEnum userRole) throws Exception {
532                 User defaultUser = ElementFactory.getDefaultUser(userRole);
533                 RestResponse productRest = ProductRestUtils.getProduct(containerDetails.getUniqueId(), defaultUser.getUserId());
534                 return ResponseParser.convertProductResponseToJavaObject(productRest.getResponse());
535         }
536
537         public static Component getComponentObject(Component containerDetails, UserRoleEnum userRole) throws Exception {
538                 User defaultUser = ElementFactory.getDefaultUser(userRole);
539
540                 switch (containerDetails.getComponentType()) {
541                         case RESOURCE:
542                                 RestResponse restResponse = ResourceRestUtils.getResource(containerDetails.getUniqueId());
543                                 containerDetails = ResponseParser.convertResourceResponseToJavaObject(restResponse.getResponse());
544                                 break;
545                         case SERVICE:
546                                 RestResponse serviceResponse = ServiceRestUtils.getService(containerDetails.getUniqueId(), defaultUser);
547                                 containerDetails = ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse());
548                                 break;
549                         case PRODUCT:
550                                 RestResponse productRest = ProductRestUtils.getProduct(containerDetails.getUniqueId(), defaultUser.getUserId());
551                                 containerDetails = ResponseParser.convertProductResponseToJavaObject(productRest.getResponse());
552                                 break;
553                         default:
554                                 break;
555                 }
556                 return containerDetails;
557         }
558
559         public static Component convertReposnseToComponentObject(Component containerDetails, RestResponse restresponse) {
560
561                 switch (containerDetails.getComponentType()) {
562                         case RESOURCE:
563                                 containerDetails = ResponseParser.convertResourceResponseToJavaObject(restresponse.getResponse());
564                                 break;
565                         case SERVICE:
566                                 containerDetails = ResponseParser.convertServiceResponseToJavaObject(restresponse.getResponse());
567                                 break;
568                         case PRODUCT:
569                                 containerDetails = ResponseParser.convertProductResponseToJavaObject(restresponse.getResponse());
570                                 break;
571                         default:
572                                 break;
573                 }
574                 return containerDetails;
575         }
576
577         public static Either<Component, RestResponse> associate2ResourceInstances(Component containerDetails, ComponentInstance fromNode, ComponentInstance toNode, String assocType, UserRoleEnum userRole, Boolean validateState) throws Exception {
578
579                 User defaultUser = ElementFactory.getDefaultUser(userRole);
580                 RestResponse associate2ResourceInstancesResponse = ResourceRestUtils.associate2ResourceInstances(containerDetails, fromNode, toNode, assocType, defaultUser);
581
582                 if (validateState) {
583                         assertTrue(associate2ResourceInstancesResponse.getErrorCode() == ServiceRestUtils.STATUS_CODE_SUCCESS);
584                 }
585
586                 if (associate2ResourceInstancesResponse.getErrorCode() == ResourceRestUtils.STATUS_CODE_SUCCESS) {
587
588                         switch (containerDetails.getComponentType()) {
589                                 case RESOURCE:
590                                         containerDetails = ResponseParser.convertResourceResponseToJavaObject(associate2ResourceInstancesResponse.getResponse());
591                                         break;
592                                 case SERVICE:
593                                         containerDetails = ResponseParser.convertServiceResponseToJavaObject(associate2ResourceInstancesResponse.getResponse());
594                                         break;
595                                 case PRODUCT:
596                                         containerDetails = ResponseParser.convertProductResponseToJavaObject(associate2ResourceInstancesResponse.getResponse());
597                                         break;
598                                 default:
599                                         break;
600                         }
601
602                         return Either.left(containerDetails);
603                 }
604                 return Either.right(associate2ResourceInstancesResponse);
605
606         }
607
608         public static Either<Pair<Component, ComponentInstance>, RestResponse> updateComponentInstance(ComponentInstanceReqDetails componentInstanceReqDetails, User sdncModifierDetails, Component container, boolean validateState) throws Exception {
609
610                 RestResponse updateComponentInstance = ComponentInstanceRestUtils.updateComponentInstance(componentInstanceReqDetails, sdncModifierDetails, container.getUniqueId(), container.getComponentType());
611                 if (validateState) {
612                         assertTrue("Update ComponentInstance failed: " + updateComponentInstance.getResponseMessage(), updateComponentInstance.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
613                 }
614                 if (updateComponentInstance.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
615                         String componentType = container.getComponentType().getValue();
616                         Component componentObject;
617                         if(componentType.equals("Resource")){
618                                 componentObject = getResourceObject(container.getUniqueId());
619                         }else{
620                                 componentObject = getServiceObject(container.getUniqueId());
621                         }
622                         ComponentInstance componentInstanceJavaObject = ResponseParser.convertComponentInstanceResponseToJavaObject(updateComponentInstance.getResponse());
623                         return Either.left(Pair.of(componentObject, componentInstanceJavaObject));
624                 }
625                 return Either.right(updateComponentInstance);
626         }
627
628         public static Either<Pair<Component, ComponentInstance>, RestResponse> changeComponentInstanceVersion(Component containerDetails, ComponentInstance componentInstanceToReplace, Component newInstance, UserRoleEnum userRole, Boolean validateState)
629                         throws Exception {
630                 User defaultUser = ElementFactory.getDefaultUser(userRole);
631
632                 RestResponse changeComponentInstanceVersionResp = ComponentInstanceRestUtils.changeComponentInstanceVersion(containerDetails, componentInstanceToReplace, newInstance, defaultUser);
633                 if (validateState) {
634                         assertTrue("change ComponentInstance version failed: " + changeComponentInstanceVersionResp.getResponseMessage(), changeComponentInstanceVersionResp.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
635                 }
636
637                 if (changeComponentInstanceVersionResp.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
638
639                         Component compoenntObject = AtomicOperationUtils.getComponentObject(containerDetails, userRole);
640                         ComponentInstance componentInstanceJavaObject = ResponseParser.convertComponentInstanceResponseToJavaObject(changeComponentInstanceVersionResp.getResponse());
641
642                         return Either.left(Pair.of(compoenntObject, componentInstanceJavaObject));
643                 }
644
645                 return Either.right(changeComponentInstanceVersionResp);
646         }
647
648         public static ComponentInstance getComponentInstanceByName(Component component, String name) {
649                 ComponentInstance componentInstance = component.getComponentInstances()
650                                 .stream()
651                                 .filter(ci->ci.getName().equals(name))
652                                 .findFirst()
653                                 .orElse(null);
654                 if (componentInstance == null) {
655                         List<String> componentInstancesNameList = component.getComponentInstances().stream().map(ComponentInstance::getName).collect(Collectors.toList());
656                         assertFalse("Instance name " + name + " not found in container " + component.getComponentType() + " named [" + component.getName()
657                                         + "]. Component instances available are: " + componentInstancesNameList.toString(),  true);
658                 }
659                 return componentInstance;
660         }
661
662         // *********** PROPERTIES *****************
663
664         public static Either<ComponentInstanceProperty, RestResponse> addCustomPropertyToResource(PropertyReqDetails propDetails, Resource resourceDetails, UserRoleEnum userRole, Boolean validateState) throws Exception {
665
666                 User defaultUser = ElementFactory.getDefaultUser(userRole);
667                 Map<String, PropertyReqDetails> propertyToSend = new HashMap<>();
668                 propertyToSend.put(propDetails.getName(), propDetails);
669                 Gson gson = new Gson();
670                 RestResponse addPropertyResponse = PropertyRestUtils.createProperty(resourceDetails.getUniqueId(), gson.toJson(propertyToSend), defaultUser);
671
672                 if (validateState) {
673                         assertTrue("add property to resource failed: " + addPropertyResponse.getErrorCode(), addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_CREATED);
674                 }
675
676                 if (addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_CREATED) {
677                         ComponentInstanceProperty compInstProp = null;
678                         String property = ResponseParser.getJsonObjectValueByKey(addPropertyResponse.getResponse(), propDetails.getName());
679                         compInstProp = (ResponseParser.convertPropertyResponseToJavaObject(property));
680                         return Either.left(compInstProp);
681                 }
682                 return Either.right(addPropertyResponse);
683         }
684
685         // Benny
686         public static Either<ComponentInstanceProperty, RestResponse> updatePropertyOfResource(PropertyReqDetails propDetails, Resource resourceDetails, String propertyUniqueId, UserRoleEnum userRole, Boolean validateState) throws Exception {
687
688                 User defaultUser = ElementFactory.getDefaultUser(userRole);
689                 Map<String, PropertyReqDetails> propertyToSend = new HashMap<>();
690                 propertyToSend.put(propDetails.getName(), propDetails);
691                 Gson gson = new Gson();
692                 RestResponse addPropertyResponse = PropertyRestUtils.updateProperty(resourceDetails.getUniqueId(), propertyUniqueId, gson.toJson(propertyToSend), defaultUser);
693
694                 if (validateState) {
695                         assertTrue("add property to resource failed: " + addPropertyResponse.getResponseMessage(), addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
696                 }
697
698                 if (addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
699                         ComponentInstanceProperty compInstProp = null;
700                         String property = ResponseParser.getJsonObjectValueByKey(addPropertyResponse.getResponse(), propDetails.getName());
701                         compInstProp = (ResponseParser.convertPropertyResponseToJavaObject(property));
702                         return Either.left(compInstProp);
703                 }
704                 return Either.right(addPropertyResponse);
705         }
706
707         public static RestResponse deletePropertyOfResource(String resourceId, String propertyId, UserRoleEnum userRole) throws Exception {
708                 User defaultUser = ElementFactory.getDefaultUser(userRole);
709                 return PropertyRestUtils.deleteProperty(resourceId, propertyId, defaultUser);
710         }
711
712         public static Either<ComponentInstanceProperty, RestResponse> addDefaultPropertyToResource(PropertyTypeEnum propertyType, Resource resourceDetails, UserRoleEnum userRole, Boolean validateState) throws Exception {
713
714                 User defaultUser = ElementFactory.getDefaultUser(userRole);
715                 PropertyReqDetails propDetails = ElementFactory.getPropertyDetails(propertyType);
716                 Map<String, PropertyReqDetails> propertyToSend = new HashMap<>();
717                 propertyToSend.put(propDetails.getName(), propDetails);
718                 Gson gson = new Gson();
719                 RestResponse addPropertyResponse = PropertyRestUtils.createProperty(resourceDetails.getUniqueId(), gson.toJson(propertyToSend), defaultUser);
720
721                 if (validateState) {
722                         assertTrue("add property to resource failed: " + addPropertyResponse.getResponseMessage(), addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_CREATED);
723                 }
724
725                 if (addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_CREATED) {
726                         ComponentInstanceProperty compInstProp = null;
727                         String property = ResponseParser.getJsonObjectValueByKey(addPropertyResponse.getResponse(), propDetails.getName());
728                         compInstProp = (ResponseParser.convertPropertyResponseToJavaObject(property));
729
730                         return Either.left(compInstProp);
731                 }
732                 return Either.right(addPropertyResponse);
733         }
734
735         public static Either<GroupDefinition, RestResponse> updateGroupPropertyOnResource(String maxVFModuleInstacesValue, Resource resource, String groupId, User user, Boolean validateState) throws Exception {
736
737 //              Gson gson = new Gson();
738         // Json group property object
739         String propertyObjectJson = "[{\"defaultValue\":null,\"description\":\"The maximum instances of this VF-Module\",\"name\":\"max_vf_module_instances\",\"parentUniqueId\":\"org.openecomp.groups.VfModule.1.0.grouptype.max_vf_module_instances\",\"password\":false,\"required\":false,\"schema\":{\"property\":{}},\"type\":\"integer\",\"uniqueId\":\"org.openecomp.groups.VfModule.1.0.grouptype.max_vf_module_instances.property.3\",\"value\":\"" + maxVFModuleInstacesValue + "\",\"definition\":false,\"getInputValues\":null,\"constraints\":null,\"valueUniqueUid\":null,\"ownerId\":\"org.openecomp.groups.VfModule.1.0.grouptype.max_vf_module_instances\"}]";
740 //        GroupProperty property = gson.fromJson(propertyObjectJson, GroupProperty.class);
741                 RestResponse updateGroupPropertyResponse = PropertyRestUtils.updateGroupProperty(resource, groupId, propertyObjectJson, user);
742
743                 if (validateState) {
744                         assertTrue("update group property to resource failed: " + updateGroupPropertyResponse.getResponseMessage(), updateGroupPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
745                 }
746
747                 if (updateGroupPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
748                         GroupDefinition responseGroupDefinition = ResponseParser.convertPropertyResponseToObject(updateGroupPropertyResponse.getResponse());
749                         return Either.left(responseGroupDefinition);
750                 }
751                 return Either.right(updateGroupPropertyResponse);
752         }
753
754
755         public static RestResponse createDefaultConsumer(Boolean validateState) {
756                 try {
757                         ConsumerDataDefinition defaultConsumerDefinition = ElementFactory.getDefaultConsumerDetails();
758                         RestResponse createResponse = ConsumerRestUtils.createConsumer(defaultConsumerDefinition, ElementFactory.getDefaultUser(UserRoleEnum.ADMIN));
759                         BaseRestUtils.checkCreateResponse(createResponse);
760
761                         if (validateState) {
762                                 assertTrue(createResponse.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
763                         }
764                         return createResponse;
765                 } catch (Exception e) {
766                         throw new AtomicOperationException(e);
767                 }
768         }
769
770         /**
771          * Builds Resource From rest response
772          *
773          * @param resourceResp
774          * @return
775          */
776         public static Either<Resource, RestResponse> buildResourceFromResponse(RestResponse resourceResp) {
777                 Either<Resource, RestResponse> result;
778                 if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
779                         Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
780                         result = Either.left(resourceResponseObject);
781                 } else {
782                         result = Either.right(resourceResp);
783                 }
784                 return result;
785         }
786
787         private static class AtomicOperationException extends RuntimeException {
788                 private AtomicOperationException(Exception e) {
789                         super(e);
790                 }
791
792                 private static final long serialVersionUID = 1L;
793         }
794
795         /**
796          * Import resource from CSAR
797          *
798          * @param resourceType
799          * @param userRole
800          * @param fileName
801          * @param filePath
802          * @return Resource
803          * @throws Exception
804          */
805         public static Resource importResourceFromCsar(ResourceTypeEnum resourceType, UserRoleEnum userRole, String fileName, String... filePath) throws Exception {
806                 // Get the CSARs path
807                 String realFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "CI" + File.separator + "csars" ;
808                 if (filePath != null && filePath.length > 0) {
809                         StringBuilder result = new StringBuilder();
810                         for(String currStr: filePath){
811                                 result.append(currStr);
812                         }
813 //                      realFilePath = Arrays.toString(filePath);
814                         realFilePath = result.toString();
815                 }
816
817                 // Create default import resource & user
818                 return importResourceFromCsarFile(resourceType, userRole, fileName, realFilePath);
819         }
820
821         public static Resource importResourceFromCsarFile(ResourceTypeEnum resourceType, UserRoleEnum userRole, String csarFileName, String csarFilePath) throws Exception{
822                 RestResponse createResource = getCreateResourceRestResponse(resourceType, userRole, csarFileName, csarFilePath);
823                 BaseRestUtils.checkCreateResponse(createResource);
824                 return ResponseParser.parseToObjectUsingMapper(createResource.getResponse(), Resource.class);
825         }
826
827         public static Resource importCertifiedResourceFromCsar(ResourceTypeEnum resourceType, UserRoleEnum userRole,    String csarFileName, String csarFilePath) throws Exception{
828                 RestResponse createResource = getCreateCertifiedResourceRestResponse(resourceType, userRole, csarFileName, csarFilePath);
829                 BaseRestUtils.checkSuccess(createResource);
830                 return ResponseParser.parseToObjectUsingMapper(createResource.getResponse(), Resource.class);
831         }
832         public static RestResponse getCreateResourceRestResponse(ResourceTypeEnum resourceType, UserRoleEnum userRole,
833                                                                                                                          String csarFileName, String csarFilePath) throws IOException, Exception {
834
835                 ImportReqDetails resourceDetails = buildImportReqDetails(resourceType, csarFileName, csarFilePath);
836                 User sdncModifierDetails = ElementFactory.getDefaultUser(userRole);
837                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
838                 return createResource;
839         }
840
841         public static RestResponse getCreateCertifiedResourceRestResponse(ResourceTypeEnum resourceType, UserRoleEnum userRole,
842                                                                                                                                                    String csarFileName, String csarFilePath) throws IOException, Exception {
843
844                 ImportReqDetails resourceDetails = buildImportReqDetails(resourceType, csarFileName, csarFilePath);
845                 User sdncModifierDetails = ElementFactory.getDefaultUser(userRole);
846                 RestResponse response = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
847                 BaseRestUtils.checkCreateResponse(response);
848                 return LCSbaseTest.certifyResource(resourceDetails, sdncModifierDetails);
849         }
850
851         private static ImportReqDetails buildImportReqDetails(ResourceTypeEnum resourceType, String csarFileName, String csarFilePath) throws IOException {
852                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
853                 Path path = Paths.get(csarFilePath + File.separator + csarFileName);
854                 byte[] data = Files.readAllBytes(path);
855                 String payloadName = csarFileName;
856                 String payloadData = Base64.encodeBase64String(data);
857                 resourceDetails.setPayloadData(payloadData);
858                 resourceDetails.setCsarUUID(payloadName);
859                 resourceDetails.setPayloadName(payloadName);
860                 resourceDetails.setResourceType(resourceType.name());
861                 return resourceDetails;
862         }
863
864         public static Resource updateResourceFromCsar(Resource resource, UserRoleEnum userRole, String csarFileName, String csarFilePath) throws Exception{
865                 User sdncModifierDetails = ElementFactory.getDefaultUser(userRole);
866
867                 byte[] data = null;
868                 Path path = Paths.get(csarFilePath + File.separator + csarFileName);
869                 data = Files.readAllBytes(path);
870                 String payloadName = csarFileName;
871                 String payloadData = Base64.encodeBase64String(data);
872                 ImportReqDetails resourceDetails = new ImportReqDetails(resource, payloadName, payloadData);
873                 resourceDetails.setPayloadData(payloadData);
874                 resourceDetails.setCsarUUID(payloadName);
875                 resourceDetails.setPayloadName(payloadName);
876
877                 String userId = sdncModifierDetails.getUserId();
878                 Config config = Utils.getConfig();
879                 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resource.getUniqueId());
880
881                 Map<String, String> headersMap = ResourceRestUtils.prepareHeadersMap(userId);
882
883                 Gson gson = new Gson();
884                 String userBodyJson = gson.toJson(resourceDetails);
885                 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
886                 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
887                 HttpRequest http = new HttpRequest();
888                 RestResponse updateResourceResponse = http.httpSendPut(url, userBodyJson, headersMap);
889                 BaseRestUtils.checkSuccess(updateResourceResponse);
890                 return ResponseParser.parseToObjectUsingMapper(updateResourceResponse.getResponse(), Resource.class);
891         }
892
893         public static Either<Resource, RestResponse> importResourceByFileName(ResourceTypeEnum resourceType, UserRoleEnum userRole, String fileName, Boolean validateState, String... filePath) throws IOException {
894
895                 String realFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "CI" + File.separator + "csars" ;
896                 if (filePath != null && filePath.length > 0) {
897                         realFilePath = filePath.toString();
898                 }
899
900                 try {
901                         User defaultUser = ElementFactory.getDefaultUser(userRole);
902                         ResourceReqDetails defaultResource = ElementFactory.getDefaultResource(defaultUser);
903                         ImportReqDetails defaultImportResource = ElementFactory.getDefaultImportResource(defaultResource);
904                         ImportUtils.getImportResourceDetailsByPathAndName(defaultImportResource, realFilePath, fileName);
905                         RestResponse resourceResp = ResourceRestUtils.createResource(defaultImportResource, defaultUser);
906
907                         if (validateState) {
908                                 assertTrue(resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
909                         }
910
911                         if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
912                                 Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
913                                 return Either.left(resourceResponseObject);
914                         }
915                         return Either.right(resourceResp);
916                 } catch (Exception e) {
917                         throw new AtomicOperationException(e);
918                 }
919         }
920
921         public static Either<String, RestResponse> getComponenetArtifactPayload(Component component, String artifactType) throws Exception {
922
923                 String url;
924                 Config config = Utils.getConfig();
925                 if(component.getComponentType().toString().toUpperCase().equals(ComponentTypeEnum.SERVICE.getValue().toUpperCase())){
926                         url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), component.getUniqueId(), component.getToscaArtifacts().get(artifactType).getUniqueId());
927                 }else{
928                         url = String.format(Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), component.getUniqueId(), component.getToscaArtifacts().get(artifactType).getUniqueId());
929                 }
930                 String userId = component.getLastUpdaterUserId();
931                 Map<String, String> headersMap = new HashMap<>();
932                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
933                 headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), BaseRestUtils.cacheControlHeader);
934                 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), basicAuthentication);
935                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
936                 if (userId != null) {
937                         headersMap.put(HttpHeaderEnum.USER_ID.getValue(), userId);
938                 }
939                 HttpRequest http = new HttpRequest();
940                 RestResponse response = http.httpSendGet(url, headersMap);
941                 if (response.getErrorCode() != BaseRestUtils.STATUS_CODE_SUCCESS && response.getResponse().getBytes() == null && response.getResponse().getBytes().length == 0) {
942                         return Either.right(response);
943                 }
944                 return Either.left(response.getResponse());
945
946         }
947
948         public static RestResponse getDistributionStatusByDistributionId(String distributionId, Boolean validateState) {
949
950                 try {
951                         User defaultUser = ElementFactory.getDefaultUser(UserRoleEnum.OPS);
952                         RestResponse response = DistributionUtils.getDistributionStatus(defaultUser, distributionId);
953
954                         if (validateState) {
955                                 assertTrue(response.getErrorCode() == ResourceRestUtils.STATUS_CODE_SUCCESS);
956                         }
957                         return response;
958
959                 } catch (Exception e) {
960                         throw new AtomicOperationException(e);
961                 }
962         }
963
964         public static Either <RestResponse, Map<String, List<DistributionMonitorObject>>> getSortedDistributionStatusMap(Service service, Boolean validateState) {
965
966                 try {
967                         ServiceDistributionStatus serviceDistributionObject = DistributionUtils.getLatestServiceDistributionObject(service);
968                         RestResponse response = getDistributionStatusByDistributionId(serviceDistributionObject.getDistributionID(), true);
969                         if(validateState) {
970                                 assertTrue(response.getErrorCode() == ResourceRestUtils.STATUS_CODE_SUCCESS);
971                         }
972                         if(response.getErrorCode() == ResourceRestUtils.STATUS_CODE_SUCCESS){
973                                 Map<String, List<DistributionMonitorObject>> parsedDistributionStatus = DistributionUtils.getSortedDistributionStatus(response);
974                                 return Either.right(parsedDistributionStatus);
975                         }
976                         return Either.left(response);
977                 } catch (Exception e) {
978                         throw new AtomicOperationException(e);
979                 }
980
981         }
982
983
984         /**
985          * @param service
986          * @param pollingCount
987          * @param pollingInterval
988          * Recommended values for service distribution for pollingCount is 4 and for pollingInterval is 15000ms
989          * @throws Exception
990          */
991         public static Boolean distributeAndValidateService(Service service, int pollingCount, int pollingInterval) throws Exception {
992                 int firstPollingInterval = 30000; //this value define first be polling topic time, should change if DC configuration changed
993                 Boolean statusFlag = true;
994                 AtomicOperationUtils.distributeService(service,  true);
995                 TimeUnit.MILLISECONDS.sleep(firstPollingInterval);
996                 int timeOut = pollingCount * pollingInterval;
997                 com.clearspring.analytics.util.Pair<Boolean,Map<String,List<String>>> verifyDistributionStatus = null;
998
999                 while (timeOut > 0) {
1000                         Map<String,List<DistributionMonitorObject>> sortedDistributionStatusMap = AtomicOperationUtils.getSortedDistributionStatusMap(service, true).right().value();
1001                         verifyDistributionStatus = DistributionUtils.verifyDistributionStatus(sortedDistributionStatusMap);
1002                         if(verifyDistributionStatus.left.equals(false)){
1003                                 TimeUnit.MILLISECONDS.sleep(pollingInterval);
1004                                 timeOut-=pollingInterval;
1005                         }else {
1006                                 timeOut = 0;
1007                         }
1008                 }
1009
1010                 if((verifyDistributionStatus.right != null && ! verifyDistributionStatus.right.isEmpty())){
1011                         for(Entry<String, List<String>> entry : verifyDistributionStatus.right.entrySet()){
1012                                 if(ComponentBaseTest.getExtendTest() != null){
1013                                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Consumer: " + entry.getKey() + " failed on following: "+ entry.getValue());
1014                                 }else{
1015                                         System.out.println("Consumer: [" + entry.getKey() + "] failed on following: "+ entry.getValue());
1016                                 }
1017                         }
1018                         statusFlag = false;
1019                 }
1020                 return statusFlag;
1021         }
1022
1023         public static Boolean distributeAndValidateService(Service service) throws Exception {
1024                 return distributeAndValidateService(service, 10, 10000);
1025         }
1026         
1027           /**
1028      * @param resource to download csar file via API
1029      * @return Tosca definition object from main yaml file
1030      */
1031     public static ToscaDefinition downloadAndGetToscaMainYamlObjectApi(Resource resource, File filesFolder) throws Exception {
1032         File vfCsarFileName = new File(File.separator + "VfCsar_" + ElementFactory.generateUUIDforSufix() + ".csar");
1033         OnboardingUtillViaApis.downloadToscaCsarToDirectory(resource, new File(filesFolder.getPath() + vfCsarFileName));
1034         return ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(new File(filesFolder.getPath() + vfCsarFileName));
1035     }
1036
1037
1038     public static ComponentInstance getServiceComponentInstanceByName(Service service, String name, Boolean validateState){
1039         List<ComponentInstance> compInstances = service.getComponentInstances();
1040         for (ComponentInstance instance: compInstances){
1041             String compName = instance.getName();
1042             if (compName.equals(name))
1043                 return instance;
1044         }
1045         if (validateState) {
1046             assertEquals("Component instance name " + name + " not found", name, null);
1047         }
1048         return null;
1049     }
1050
1051         public static Pair<Component, ComponentInstance> updateComponentInstanceName(String newName, Component component, String canvasElementName, User user, Boolean validateState) throws Exception {
1052                 ComponentInstanceReqDetails componentInstanceReqDetails = ElementFactory.getDefaultComponentInstance();
1053                 ComponentInstance componentInstanceByName = AtomicOperationUtils.getComponentInstanceByName(component, canvasElementName);
1054                 componentInstanceReqDetails.setName(newName);
1055                 componentInstanceReqDetails.setComponentUid(componentInstanceByName.getComponentUid());
1056                 componentInstanceReqDetails.setUniqueId(componentInstanceByName.getUniqueId());
1057                 return AtomicOperationUtils.updateComponentInstance(componentInstanceReqDetails, user, component, validateState).left().value();
1058         }
1059
1060 }