Improve error reporting during the service import
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / csar / CsarArtifactsAndGroupsBusinessLogic.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 package org.openecomp.sdc.be.components.csar;
21
22 import static org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.FAILED_UPLOAD_ARTIFACT_TO_COMPONENT;
23 import static org.openecomp.sdc.be.tosca.CsarUtils.ARTIFACTS_PATH;
24
25 import com.google.gson.Gson;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import java.util.Optional;
38 import java.util.OptionalInt;
39 import java.util.Scanner;
40 import java.util.Set;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43 import java.util.stream.Collectors;
44 import java.util.stream.IntStream;
45 import org.apache.commons.codec.binary.Base64;
46 import org.apache.commons.collections.CollectionUtils;
47 import org.apache.commons.collections.MapUtils;
48 import org.apache.commons.lang3.StringUtils;
49 import org.apache.commons.lang3.tuple.ImmutablePair;
50 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
52 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
53 import org.openecomp.sdc.be.components.impl.CsarValidationUtils;
54 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
55 import org.openecomp.sdc.be.components.impl.ImportUtils;
56 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
57 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
58 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
59 import org.openecomp.sdc.be.config.BeEcompErrorManager;
60 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
61 import org.openecomp.sdc.be.config.Configuration.VfModuleProperty;
62 import org.openecomp.sdc.be.config.ConfigurationManager;
63 import org.openecomp.sdc.be.dao.api.ActionStatus;
64 import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
65 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
67 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
68 import org.openecomp.sdc.be.impl.ComponentsUtils;
69 import org.openecomp.sdc.be.info.ArtifactTemplateInfo;
70 import org.openecomp.sdc.be.info.MergedArtifactInfo;
71 import org.openecomp.sdc.be.model.ArtifactDefinition;
72 import org.openecomp.sdc.be.model.Component;
73 import org.openecomp.sdc.be.model.ComponentParametersView;
74 import org.openecomp.sdc.be.model.GroupDefinition;
75 import org.openecomp.sdc.be.model.GroupProperty;
76 import org.openecomp.sdc.be.model.GroupTypeDefinition;
77 import org.openecomp.sdc.be.model.HeatParameterDefinition;
78 import org.openecomp.sdc.be.model.Operation;
79 import org.openecomp.sdc.be.model.PropertyDefinition;
80 import org.openecomp.sdc.be.model.Resource;
81 import org.openecomp.sdc.be.model.Service;
82 import org.openecomp.sdc.be.model.User;
83 import org.openecomp.sdc.be.model.heat.HeatParameterType;
84 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
85 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
86 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
87 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
88 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
89 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
90 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
91 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
92 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
93 import org.openecomp.sdc.be.servlets.RepresentationUtils;
94 import org.openecomp.sdc.be.tosca.CsarUtils;
95 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
96 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
97 import org.openecomp.sdc.common.api.Constants;
98 import org.openecomp.sdc.common.log.wrappers.Logger;
99 import org.openecomp.sdc.exception.ResponseFormat;
100 import org.springframework.beans.factory.annotation.Autowired;
101
102 @org.springframework.stereotype.Component("csarArtifactsAndGroupsBusinessLogic")
103 public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic {
104
105     private static final Logger log = Logger.getLogger(CsarArtifactsAndGroupsBusinessLogic.class.getName());
106     private static final String ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME = "Artifact  file is not in expected format, fileName  {}";
107     private static final String ARTIFACT_INTERNALS_ARE_INVALID = "Artifact internals are invalid";
108     private static final String ARTIFACT_WITH_NAME_AND_TYPE_ALREADY_EXIST_WITH_TYPE = "Artifact with name {} and type {} already exist with type  {}";
109     private static final Pattern pattern = Pattern.compile("\\..(.*?)\\..");
110     private static final String LABEL_COUNTER_DELIMITER = "[^0-9]+";
111     protected final ArtifactsBusinessLogic artifactsBusinessLogic;
112     private final Gson gson = new Gson();
113     private final GroupBusinessLogic groupBusinessLogic;
114
115     @Autowired
116     public CsarArtifactsAndGroupsBusinessLogic(IElementOperation elementDao, IGroupOperation groupOperation,
117                                                IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation,
118                                                GroupBusinessLogic groupBusinessLogic, InterfaceOperation interfaceOperation,
119                                                InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
120                                                ArtifactsBusinessLogic artifactsBusinessLogic, ArtifactsOperations artifactToscaOperation) {
121         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation,
122             artifactToscaOperation);
123         this.artifactsBusinessLogic = artifactsBusinessLogic;
124         this.groupBusinessLogic = groupBusinessLogic;
125     }
126
127     public Either<Resource, ResponseFormat> createResourceArtifactsFromCsar(CsarInfo csarInfo, Resource resource, String artifactsMetaFile,
128                                                                             String artifactsMetaFileName, List<ArtifactDefinition> createdArtifacts) {
129         log.debug("parseResourceArtifactsInfoFromFile start");
130         return parseResourceArtifactsInfoFromFile(resource, artifactsMetaFile, artifactsMetaFileName).left()
131             .bind(p -> createResourceArtifacts(csarInfo, resource, p, createdArtifacts)).right().map(rf -> {
132                 componentsUtils.auditResource(rf, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
133                 return rf;
134             }).left().bind(c -> checkoutRes(c));
135     }
136
137     private Either<Resource, ResponseFormat> checkoutRes(Component component) {
138         return Either.left((Resource) getResourcetFromGraph(component).left().value());
139     }
140
141     public Either<Service, ResponseFormat> createResourceArtifactsFromCsar(CsarInfo csarInfo, Service resource, String artifactsMetaFile,
142                                                                            String artifactsMetaFileName, List<ArtifactDefinition> createdArtifacts) {
143         log.debug("parseResourceArtifactsInfoFromFile start");
144         return parseResourceArtifactsInfoFromFile(resource, artifactsMetaFile, artifactsMetaFileName).left()
145             .bind(p -> createResourceArtifacts(csarInfo, resource, p, createdArtifacts)).right().map(rf -> rf).left().bind(c ->
146                 Either.left((Service) getResourcetFromGraph(c).left().value())
147             );
148     }
149
150     public Either<Component, ResponseFormat> updateResourceArtifactsFromCsar(CsarInfo csarInfo, Component resource, String artifactsMetaFile,
151                                                                              String artifactsMetaFileName,
152                                                                              List<ArtifactDefinition> createdNewArtifacts, boolean shouldLock,
153                                                                              boolean inTransaction) {
154         Component updatedResource = resource;
155         Either<Map<String, List<ArtifactTemplateInfo>>, ResponseFormat> parseResourceInfoFromYamlEither = parseResourceArtifactsInfoFromFile(
156             updatedResource, artifactsMetaFile, artifactsMetaFileName);
157         if (parseResourceInfoFromYamlEither.isRight()) {
158             ResponseFormat responseFormat = parseResourceInfoFromYamlEither.right().value();
159             if (resource instanceof Resource) {
160                 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), (Resource) resource, AuditingActionEnum.IMPORT_RESOURCE);
161             }
162             return Either.right(responseFormat);
163         }
164         List<GroupDefinition> groups = updatedResource.getGroups();
165         Map<String, ArtifactDefinition> deploymentArtifact = updatedResource.getDeploymentArtifacts();
166         if (MapUtils.isEmpty(deploymentArtifact)) {
167             deleteGroupsByType(groups, Constants.DEFAULT_GROUP_VF_MODULE, updatedResource);
168             return createResourceArtifacts(csarInfo, updatedResource, parseResourceInfoFromYamlEither.left().value(), createdNewArtifacts);
169         }
170         List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete = deploymentArtifact.values().stream().collect(Collectors.toList());
171         int labelCounter =
172             createdDeploymentArtifactsAfterDelete.stream().map(ArtifactDefinition::getArtifactLabel).filter(this::isLastCharacterInLabelADigit)
173                 .map(this::getNextInt).flatMapToInt(this::toStream).max().orElse(-1) + 1;
174         ////////////////////////////////////// create set parsed
175
176         ////////////////////////////////////// artifacts///////////////////////////////////////////
177         Map<String, List<ArtifactTemplateInfo>> parsedArtifactsMap = parseResourceInfoFromYamlEither.left().value();
178         List<ArtifactTemplateInfo> artifactsWithoutGroups = parsedArtifactsMap.remove(ArtifactTemplateInfo.CSAR_ARTIFACT);
179         Collection<List<ArtifactTemplateInfo>> parsedArifactsCollection = parsedArtifactsMap.values();
180         Either<Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>>, ResponseFormat> parsedArtifactsPerGroupEither = createArtifactsTemplateCollection(
181             csarInfo, updatedResource, createdNewArtifacts, createdDeploymentArtifactsAfterDelete, labelCounter, parsedArifactsCollection);
182         if (parsedArtifactsPerGroupEither.isRight()) {
183             log.error("Failed to parse artifacts. Status is {} ", parsedArtifactsPerGroupEither.right().value());
184             return Either.right(parsedArtifactsPerGroupEither.right().value());
185         }
186         Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup = parsedArtifactsPerGroupEither.left().value();
187         // find master in group
188         Map<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> groupArtifact = findMasterArtifactInGroup(groups, deploymentArtifact);
189         ///////////////////////////////// find artifacts to
190
191         ///////////////////////////////// delete////////////////////////////////////////////////////
192         Map<String, List<ArtifactDefinition>> groupToDelete = new HashMap<>();
193         Set<ArtifactDefinition> artifactsToDelete = findArtifactThatNotInGroupToDelete(parsedGroup, createdDeploymentArtifactsAfterDelete);
194         Set<ArtifactTemplateInfo> jsonMasterArtifacts = parsedGroup.keySet();
195         Map<GroupDefinition, MergedArtifactInfo> mergedgroup = mergeGroupInUpdateFlow(groupArtifact, parsedGroup, artifactsToDelete, groupToDelete,
196             jsonMasterArtifacts, createdDeploymentArtifactsAfterDelete);
197         List<ArtifactDefinition> deletedArtifacts = new ArrayList<>();
198         Either<Component, ResponseFormat> deletedArtifactsEither = deleteArtifactsInUpdateCsarFlow(updatedResource, csarInfo.getModifier(),
199             shouldLock, inTransaction, artifactsToDelete, groupToDelete, deletedArtifacts);
200         if (deletedArtifactsEither.isRight()) {
201             log.debug("Failed to delete artifacts. Status is {} ", deletedArtifactsEither.right().value());
202             return Either.right(deletedArtifactsEither.right().value());
203         }
204         updatedResource = deletedArtifactsEither.left().value();
205         // need to update resource if we updated artifacts
206         excludeDeletedArtifacts(deletedArtifacts, createdDeploymentArtifactsAfterDelete);
207         ////////////// dissociate, associate or create
208
209         ////////////// artifacts////////////////////////////
210         Either<Component, ResponseFormat> assDissotiateEither = associateAndDissociateArtifactsToGroup(csarInfo, updatedResource, createdNewArtifacts,
211             labelCounter, createdDeploymentArtifactsAfterDelete, mergedgroup, deletedArtifacts);
212         groups = updatedResource.getGroups();
213         if (assDissotiateEither.isRight()) {
214             log.debug("Failed to delete artifacts. Status is {} ", assDissotiateEither.right().value());
215             return Either.right(assDissotiateEither.right().value());
216         }
217         updatedResource = assDissotiateEither.left().value();
218         deploymentArtifact = updatedResource.getDeploymentArtifacts();
219         createdDeploymentArtifactsAfterDelete = deploymentArtifact.values().stream().collect(Collectors.toList());
220         // update vfModule names
221         Set<GroupDefinition> groupForAssociateWithMembers = mergedgroup.keySet();
222         Either<Component, ResponseFormat> validateUpdateVfGroupNamesRes = updateVfModuleNames(createdNewArtifacts, updatedResource, groups,
223             createdDeploymentArtifactsAfterDelete, groupForAssociateWithMembers);
224         if (validateUpdateVfGroupNamesRes != null) {
225             return validateUpdateVfGroupNamesRes;
226         }
227         //////////////// create new artifacts in update
228
229         //////////////// flow////////////////////////////
230         List<ArtifactTemplateInfo> newArtifactsGroup = createNewArtifcats(parsedGroup, groupArtifact);
231         Either<Component, ResponseFormat> validateGroupNamesRes = handleArtifactsInGroup(csarInfo, createdNewArtifacts, updatedResource, groups,
232             createdDeploymentArtifactsAfterDelete, labelCounter, newArtifactsGroup);
233         if (validateGroupNamesRes != null) {
234             return validateGroupNamesRes;
235         }
236         // updatedGroup
237         Either<Component, ResponseFormat> updateVersionEither = updateGroupVersion(updatedResource, groupForAssociateWithMembers);
238         if (updateVersionEither != null) {
239             return updateVersionEither;
240         }
241         if (!CollectionUtils.isEmpty(artifactsWithoutGroups)) {
242             for (ArtifactTemplateInfo t : artifactsWithoutGroups) {
243                 List<ArtifactTemplateInfo> artifacts = new ArrayList<>();
244                 artifacts.add(t);
245                 Either<Component, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, updatedResource, artifacts,
246                     createdNewArtifacts, createdDeploymentArtifactsAfterDelete, labelCounter);
247                 if (checkResponse(resStatus)) {
248                     return resStatus;
249                 }
250             }
251         }
252         Either<Component, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(updatedResource.getUniqueId());
253         return mapResult(eitherGetResource, updatedResource);
254     }
255
256     private Either<Component, ResponseFormat> handleArtifactsInGroup(CsarInfo csarInfo, List<ArtifactDefinition> createdNewArtifacts,
257                                                                      Component updatedResource, List<GroupDefinition> groups,
258                                                                      List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete, int labelCounter,
259                                                                      List<ArtifactTemplateInfo> newArtifactsGroup) {
260         if (!newArtifactsGroup.isEmpty()) {
261             Collections.sort(newArtifactsGroup, ArtifactTemplateInfo::compareByGroupName);
262             int startGroupCounter = groupBusinessLogic.getNextVfModuleNameCounter(groups);
263             Either<Boolean, ResponseFormat> validateGroupNamesRes = groupBusinessLogic
264                 .validateGenerateVfModuleGroupNames(newArtifactsGroup, updatedResource.getSystemName(), startGroupCounter);
265             if (validateGroupNamesRes.isRight()) {
266                 return Either.right(validateGroupNamesRes.right().value());
267             }
268             Either<Component, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, updatedResource, newArtifactsGroup,
269                 createdNewArtifacts, createdDeploymentArtifactsAfterDelete, labelCounter);
270             checkResponse(resStatus);
271         }
272         return null;
273     }
274
275     private boolean checkResponse(Either<Component, ResponseFormat> resStatus) {
276         return (resStatus.isRight());
277     }
278
279     private Either<Component, ResponseFormat> updateVfModuleNames(List<ArtifactDefinition> createdNewArtifacts, Component updatedResource,
280                                                                   List<GroupDefinition> groups,
281                                                                   List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete,
282                                                                   Set<GroupDefinition> groupForAssociateWithMembers) {
283         if (!CollectionUtils.isEmpty(groups)) {
284             Either<List<GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic
285                 .validateUpdateVfGroupNamesOnGraph(groups, updatedResource);
286             if (validateUpdateVfGroupNamesRes.isRight()) {
287                 return Either.right(validateUpdateVfGroupNamesRes.right().value());
288             }
289             updateGroupMemebers(groups, groupForAssociateWithMembers, createdNewArtifacts, createdDeploymentArtifactsAfterDelete);
290         }
291         return null;
292     }
293
294     private Either<Component, ResponseFormat> updateGroupVersion(Component updatedResource, Set<GroupDefinition> groupForAssociateWithMembers) {
295         if (!groupForAssociateWithMembers.isEmpty()) {
296             List<GroupDefinition> groupsId = groupForAssociateWithMembers.stream().collect(Collectors.toList());
297             Either<List<GroupDefinition>, ResponseFormat> updateVersionEither = groupBusinessLogic.updateGroups(updatedResource, groupsId, true);
298             if (updateVersionEither.isRight()) {
299                 log.debug("Failed to update groups version. Status is {} ", updateVersionEither.right().value());
300                 return Either.right(updateVersionEither.right().value());
301             }
302         }
303         return null;
304     }
305
306     private IntStream toStream(OptionalInt optionalInt) {
307         if (optionalInt.isPresent()) {
308             return IntStream.of(optionalInt.getAsInt());
309         }
310         return IntStream.empty();
311     }
312
313     private OptionalInt getNextInt(String artifactLabel) {
314         try (Scanner scanner = new Scanner(artifactLabel).useDelimiter(LABEL_COUNTER_DELIMITER)) {
315             if (scanner.hasNextInt()) {
316                 return OptionalInt.of(scanner.nextInt());
317             }
318             return OptionalInt.empty();
319         }
320     }
321
322     private boolean isLastCharacterInLabelADigit(String artifactLabel) {
323         return Character.isDigit(artifactLabel.charAt(artifactLabel.length() - 1));
324     }
325
326     private Either<Component, ResponseFormat> mapResult(Either<Component, StorageOperationStatus> result, Component resource) {
327         return result.right()
328             .map(status -> componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(status), resource));
329     }
330
331     private void updateGroupMemebers(List<GroupDefinition> groups, Set<GroupDefinition> groupForAssociateWithMembers,
332                                      List<ArtifactDefinition> createdNewArtifacts, List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete) {
333         List<GroupDefinition> heatGroups = collectGroupsWithMembers(groups);
334         for (GroupDefinition updatedGroupDef : groupForAssociateWithMembers) {
335             Map<String, String> members = new HashMap<>();
336             Set<String> artifactsGroup = new HashSet<>();
337             artifactsGroup.addAll(updatedGroupDef.getArtifacts());
338             associateMembersToArtifacts(createdNewArtifacts, createdDeploymentArtifactsAfterDelete, heatGroups, artifactsGroup, members);
339             updatedGroupDef.setMembers(members);
340         }
341     }
342
343     /**
344      * @param groups
345      * @return
346      */
347     private List<GroupDefinition> collectGroupsWithMembers(List<GroupDefinition> groups) {
348         return groups.stream().filter(e -> e.getMembers() != null).collect(Collectors.toList());
349     }
350
351     /**
352      * Exclude deleted Artificats from Deployment Artifcats
353      *
354      * @param deletedArtifacts
355      * @param createdDeploymentArtifactsAfterDelete
356      */
357     private void excludeDeletedArtifacts(List<ArtifactDefinition> deletedArtifacts, List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete) {
358         for (ArtifactDefinition deletedArtifact : deletedArtifacts) {
359             ArtifactDefinition artToRemove = null;
360             for (ArtifactDefinition artFromResource : createdDeploymentArtifactsAfterDelete) {
361                 if (deletedArtifact.getUniqueId().equalsIgnoreCase(artFromResource.getUniqueId())) {
362                     artToRemove = artFromResource;
363                     break;
364                 }
365             }
366             if (artToRemove != null) {
367                 createdDeploymentArtifactsAfterDelete.remove(artToRemove);
368             }
369         }
370     }
371
372     private void deleteGroupsByType(List<GroupDefinition> groups, String groupType, Component resource) {
373         if (groups != null) {
374             List<GroupDefinition> listToDelete = groups.stream().filter(g -> g.getType().equals(groupType)).collect(Collectors.toList());
375             groupBusinessLogic.deleteGroups(resource, listToDelete);
376         }
377     }
378
379     private List<ArtifactTemplateInfo> createNewArtifcats(Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup,
380                                                           Map<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> groupArtifact) {
381         List<ArtifactTemplateInfo> newArtifactsGroup = new ArrayList<>();
382         for (Entry<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroupSetEntry : parsedGroup.entrySet()) {
383             ArtifactTemplateInfo parsedArtifactMaster = parsedGroupSetEntry.getKey();
384             boolean isNewGroup = true;
385             for (Entry<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> groupListEntry : groupArtifact.entrySet()) {
386                 Map<ArtifactDefinition, List<ArtifactDefinition>> groupArtifacts = groupListEntry.getValue();
387                 Set<ArtifactDefinition> group = groupArtifacts.keySet();
388                 for (ArtifactDefinition artifactInfo : group) {
389                     if (parsedArtifactMaster.getFileName().equalsIgnoreCase(artifactInfo.getArtifactName())) {
390                         parsedArtifactMaster.setGroupName(groupListEntry.getKey().getName());
391                         isNewGroup = false;
392                     }
393                 }
394             }
395             if (isNewGroup) {
396                 newArtifactsGroup.add(parsedArtifactMaster);
397             }
398         }
399         return newArtifactsGroup;
400     }
401
402     private Set<ArtifactDefinition> findArtifactThatNotInGroupToDelete(Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup,
403                                                                        List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete) {
404         Set<ArtifactDefinition> artifactsToDelete = new HashSet<>();
405         for (Entry<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroupSetEntry : parsedGroup.entrySet()) {
406             Set<ArtifactTemplateInfo> artifactsNames = parsedGroupSetEntry.getValue();
407             for (ArtifactTemplateInfo template : artifactsNames) {
408                 if (template.getType().equals(ArtifactTypeEnum.HEAT_ARTIFACT.getType())) {
409                     Optional<ArtifactDefinition> op = createdDeploymentArtifactsAfterDelete.stream()
410                         .filter(a -> a.getArtifactName().equalsIgnoreCase(template.getFileName())).findAny();
411                     if (op.isPresent() && !op.get().getArtifactType().equalsIgnoreCase(template.getType())) {
412                         artifactsToDelete.add(op.get());
413                     }
414                 }
415             }
416         }
417         return artifactsToDelete;
418     }
419
420     private Either<Component, ResponseFormat> createResourceArtifacts(CsarInfo csarInfo, Component resource,
421                                                                       Map<String, List<ArtifactTemplateInfo>> artifactsMap,
422                                                                       List<ArtifactDefinition> createdArtifacts) {
423         Either<Component, ResponseFormat> resStatus = Either.left(resource);
424         Collection<List<ArtifactTemplateInfo>> arifactsCollection = artifactsMap.values();
425         for (List<ArtifactTemplateInfo> groupTemplateList : arifactsCollection) {
426             if (groupTemplateList != null) {
427                 resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, resource, groupTemplateList, createdArtifacts, 0);
428                 if (resStatus.isRight()) {
429                     return resStatus;
430                 }
431             }
432         }
433         return resStatus;
434     }
435
436     private Either<Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>>, ResponseFormat> createArtifactsTemplateCollection(CsarInfo csarInfo,
437                                                                                                                            Component resource,
438                                                                                                                            List<ArtifactDefinition> createdNewArtifacts,
439                                                                                                                            List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete,
440                                                                                                                            int labelCounter,
441                                                                                                                            Collection<List<ArtifactTemplateInfo>> parsedArifactsCollection) {
442         Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup = new HashMap<>();
443         for (List<ArtifactTemplateInfo> parsedGroupTemplateList : parsedArifactsCollection) {
444             for (ArtifactTemplateInfo parsedGroupTemplate : parsedGroupTemplateList) {
445                 if (parsedGroupTemplate.getGroupName() != null) {
446                     parsedGroupTemplate.setGroupName("");
447                     Set<ArtifactTemplateInfo> parsedArtifactsNames = new HashSet<>();
448                     parsedArtifactsNames.add(parsedGroupTemplate);
449                     List<ArtifactTemplateInfo> relatedGroupTemplateList = parsedGroupTemplate.getRelatedArtifactsInfo();
450                     if (relatedGroupTemplateList != null && !relatedGroupTemplateList.isEmpty()) {
451                         createArtifactsGroupSet(parsedGroupTemplateList, parsedArtifactsNames);
452                     }
453                     parsedGroup.put(parsedGroupTemplate, parsedArtifactsNames);
454                 } else {
455                     List<ArtifactTemplateInfo> arrtifacts = new ArrayList<>();
456                     arrtifacts.add(parsedGroupTemplate);
457                     Either<Component, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, resource, arrtifacts,
458                         createdNewArtifacts, createdDeploymentArtifactsAfterDelete, labelCounter);
459                     if (resStatus.isRight()) {
460                         return Either.right(resStatus.right().value());
461                     }
462                 }
463             }
464         }
465         return Either.left(parsedGroup);
466     }
467
468     @SuppressWarnings({"unchecked", "static-access"})
469     public Either<Map<String, List<ArtifactTemplateInfo>>, ResponseFormat> parseResourceArtifactsInfoFromFile(Component resource,
470                                                                                                               String artifactsMetaFile,
471                                                                                                               String artifactFileName) {
472         try {
473             JsonObject jsonElement = new JsonObject();
474             jsonElement = gson.fromJson(artifactsMetaFile, jsonElement.getClass());
475             JsonElement importStructureElement = jsonElement.get(Constants.IMPORT_STRUCTURE);
476             if (importStructureElement == null || importStructureElement.isJsonNull()) {
477                 log.error(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName);
478                 BeEcompErrorManager.getInstance()
479                     .logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName, ARTIFACT_INTERNALS_ARE_INVALID,
480                         ErrorSeverity.ERROR);
481                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName,
482                     ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName));
483             }
484             Map<String, List<Map<String, Object>>> artifactTemplateMap;
485             artifactTemplateMap = ComponentsUtils.parseJsonToObject(importStructureElement.toString(), HashMap.class);
486             if (artifactTemplateMap.isEmpty()) {
487                 log.error(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName);
488                 BeEcompErrorManager.getInstance()
489                     .logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName, ARTIFACT_INTERNALS_ARE_INVALID,
490                         ErrorSeverity.ERROR);
491                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName,
492                     ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName));
493             }
494             Set<String> artifactsTypeKeys = artifactTemplateMap.keySet();
495             Map<String, List<ArtifactTemplateInfo>> artifactsMap = new HashMap<>();
496             List<ArtifactTemplateInfo> allGroups = new ArrayList<>();
497             for (String artifactsTypeKey : artifactsTypeKeys) {
498                 Either<List<ArtifactTemplateInfo>, ResponseFormat> artifactTemplateInfoListEither = parseArtifactTemplateList(artifactFileName,
499                     artifactTemplateMap, allGroups, artifactsTypeKey);
500                 if (artifactTemplateInfoListEither.isRight()) {
501                     return Either.right(artifactTemplateInfoListEither.right().value());
502                 }
503                 artifactsMap.put(artifactsTypeKey, artifactTemplateInfoListEither.left().value());
504             }
505             int counter = groupBusinessLogic.getNextVfModuleNameCounter(resource.getGroups());
506             Either<Boolean, ResponseFormat> validateGroupNamesRes = groupBusinessLogic
507                 .validateGenerateVfModuleGroupNames(allGroups, resource.getSystemName(), counter);
508             if (validateGroupNamesRes.isRight()) {
509                 return Either.right(validateGroupNamesRes.right().value());
510             }
511             return Either.left(artifactsMap);
512         } catch (Exception e) {
513             log.error(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName);
514             log.error("failed with exception.", e);
515             BeEcompErrorManager.getInstance()
516                 .logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName, ARTIFACT_INTERNALS_ARE_INVALID,
517                     ErrorSeverity.ERROR);
518             return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName,
519                 ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName));
520         }
521     }
522
523     private Either<List<ArtifactTemplateInfo>, ResponseFormat> parseArtifactTemplateList(String artifactFileName,
524                                                                                          Map<String, List<Map<String, Object>>> artifactTemplateMap,
525                                                                                          List<ArtifactTemplateInfo> allGroups,
526                                                                                          String artifactsTypeKey) {
527         List<Map<String, Object>> o = artifactTemplateMap.get(artifactsTypeKey);
528         Either<List<ArtifactTemplateInfo>, ResponseFormat> artifactTemplateInfoListPairStatus = createArtifactTemplateInfoModule(artifactsTypeKey, o);
529         if (artifactTemplateInfoListPairStatus.isRight()) {
530             log.error(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName);
531             BeEcompErrorManager.getInstance()
532                 .logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName, ARTIFACT_INTERNALS_ARE_INVALID,
533                     ErrorSeverity.ERROR);
534             return Either.right(artifactTemplateInfoListPairStatus.right().value());
535         }
536         List<ArtifactTemplateInfo> artifactTemplateInfoList = artifactTemplateInfoListPairStatus.left().value();
537         if (artifactTemplateInfoList == null) {
538             log.error(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName);
539             BeEcompErrorManager.getInstance()
540                 .logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName, ARTIFACT_INTERNALS_ARE_INVALID,
541                     ErrorSeverity.ERROR);
542             return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName,
543                 ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME, artifactFileName));
544         }
545         if (!artifactsTypeKey.equalsIgnoreCase(ArtifactTemplateInfo.CSAR_ARTIFACT)) {
546             allGroups.addAll(artifactTemplateInfoList);
547         }
548         return Either.left(artifactTemplateInfoList);
549     }
550
551     private Either<List<ArtifactTemplateInfo>, ResponseFormat> createArtifactTemplateInfoModule(String artifactsTypeKey,
552                                                                                                 List<Map<String, Object>> jsonObject) {
553         List<ArtifactTemplateInfo> artifactTemplateInfoList = new ArrayList<>();
554         for (Map<String, Object> o : jsonObject) {
555             Either<ArtifactTemplateInfo, ResponseFormat> artifacttemplateInfoStatus = ArtifactTemplateInfo
556                 .createArtifactTemplateInfoFromJson(componentsUtils, artifactsTypeKey, o, artifactTemplateInfoList, null);
557             if (artifacttemplateInfoStatus.isRight()) {
558                 return Either.right(artifacttemplateInfoStatus.right().value());
559             }
560             ArtifactTemplateInfo artifacttemplateInfo = artifacttemplateInfoStatus.left().value();
561             if (artifacttemplateInfo != null) {
562                 artifactTemplateInfoList.add(artifacttemplateInfo);
563             }
564         }
565         return Either.left(artifactTemplateInfoList);
566     }
567
568     private Either<Component, ResponseFormat> createGroupDeploymentArtifactsFromCsar(CsarInfo csarInfo, Component resource,
569                                                                                      List<ArtifactTemplateInfo> artifactsTemplateList,
570                                                                                      List<ArtifactDefinition> createdArtifacts, int labelCounter) {
571         List<GroupDefinition> createdGroups = resource.getGroups();
572         List<GroupDefinition> heatGroups = null;
573         if (!CollectionUtils.isEmpty(createdGroups)) {
574             heatGroups = collectGroupsWithMembers(createdGroups);
575         }
576         List<GroupDefinition> needToCreate = new ArrayList<>();
577         for (ArtifactTemplateInfo groupTemplateInfo : artifactsTemplateList) {
578             String groupName = groupTemplateInfo.getGroupName();
579             Set<String> artifactsGroup = new HashSet<>();
580             Set<String> artifactsUUIDGroup = new HashSet<>();
581             log.debug("createDeploymentArtifactsFromCsar start");
582             Either<Component, ResponseFormat> resStatus = createDeploymentArtifactFromCsar(csarInfo, ARTIFACTS_PATH, resource, artifactsGroup,
583                 artifactsUUIDGroup, groupTemplateInfo, createdArtifacts, labelCounter);
584             log.debug("createDeploymentArtifactsFromCsar end");
585             if (resStatus.isRight()) {
586                 return resStatus;
587             }
588             Map<String, ArtifactDefinition> createdArtifactsMap = createdArtifacts.stream()
589                 .collect(Collectors.toMap(ArtifactDataDefinition::getArtifactLabel, artifact -> artifact));
590             resource.setDeploymentArtifacts(createdArtifactsMap);
591             if (groupName != null && !groupName.isEmpty()) {
592                 Either<GroupDefinition, ResponseFormat> groupDefinitionEither = buildGroupDefinition(createdArtifacts, heatGroups, groupTemplateInfo,
593                     groupName, artifactsGroup, artifactsUUIDGroup, resource.getModel());
594                 if (groupDefinitionEither.isRight()) {
595                     return Either.right(groupDefinitionEither.right().value());
596                 }
597                 needToCreate.add(groupDefinitionEither.left().value());
598             }
599         }
600         Map<String, ArtifactDefinition> createdArtifactsMap = createdArtifacts.stream()
601             .collect(Collectors.toMap(ArtifactDataDefinition::getArtifactLabel, artifact -> artifact));
602         resource.setDeploymentArtifacts(createdArtifactsMap);
603         Either<List<GroupDefinition>, ResponseFormat> createGroups = groupBusinessLogic.addGroups(resource, needToCreate, false);
604         if (createGroups.isRight()) {
605             return Either.right(createGroups.right().value());
606         }
607         return Either.left(resource);
608     }
609
610     private Either<GroupDefinition, ResponseFormat> buildGroupDefinition(List<ArtifactDefinition> createdArtifacts, List<GroupDefinition> heatGroups,
611                                                                          ArtifactTemplateInfo groupTemplateInfo, String groupName,
612                                                                          Set<String> artifactsGroup, Set<String> artifactsUUIDGroup,
613                                                                          String model) {
614         Map<String, String> members = new HashMap<>();
615         associateMembersToArtifacts(createdArtifacts, null, heatGroups, artifactsGroup, members);
616         List<String> artifactsList = new ArrayList<>(artifactsGroup);
617         List<String> artifactsUUIDList = new ArrayList<>(artifactsUUIDGroup);
618         GroupDefinition groupDefinition = new GroupDefinition();
619         groupDefinition.setName(groupName);
620         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
621         groupDefinition.setArtifacts(artifactsList);
622         groupDefinition.setArtifactsUuid(artifactsUUIDList);
623         if (!members.isEmpty()) {
624             groupDefinition.setMembers(members);
625         }
626         List<GroupProperty> properties = new ArrayList<>();
627         GroupProperty prop = new GroupProperty();
628         prop.setName(Constants.IS_BASE);
629         prop.setValue(Boolean.toString(groupTemplateInfo.isBase()));
630         properties.add(prop);
631         Either<GroupTypeDefinition, StorageOperationStatus> getLatestGroupTypeRes = groupTypeOperation
632             .getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, model);
633         if (getLatestGroupTypeRes.isRight()) {
634             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getLatestGroupTypeRes.right().value())));
635         }
636         properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties, createdArtifacts, artifactsList,
637             getLatestGroupTypeRes.left().value());
638         groupDefinition.convertFromGroupProperties(properties);
639         log.debug("createGroup start");
640         return Either.left(groupDefinition);
641     }
642
643     private Either<Component, ResponseFormat> createDeploymentArtifactFromCsar(CsarInfo csarInfo, String artifactPath, Component resource,
644                                                                                Set<String> artifactsGroup, Set<String> artifactsUUIDGroup,
645                                                                                ArtifactTemplateInfo artifactTemplateInfo,
646                                                                                List<ArtifactDefinition> createdArtifacts, int labelCounter) {
647         Either<Component, ResponseFormat> resStatus = Either.left(resource);
648         String artifactUid = "";
649         String artifactEnvUid = "";
650         String artifactUUID = "";
651         // check if artifacts already exist
652         Either<ArtifactDefinition, ResponseFormat> createdArtifactEther = checkIfArtifactAlreadyExist(artifactTemplateInfo, createdArtifacts);
653         if (createdArtifactEther.isRight()) {
654             return Either.right(createdArtifactEther.right().value());
655         }
656         ArtifactDefinition createdArtifact = createdArtifactEther.left().value();
657         if (createdArtifact == null) {
658             Either<ArtifactDefinition, ResponseFormat> newArtifactEither = createDeploymentArtifact(csarInfo, resource, artifactPath,
659                 artifactTemplateInfo, createdArtifacts, labelCounter);
660             if (newArtifactEither.isRight()) {
661                 resStatus = Either.right(newArtifactEither.right().value());
662                 return resStatus;
663             }
664             ArtifactDefinition newArtifact = newArtifactEither.left().value();
665             artifactUid = newArtifact.getUniqueId();
666             artifactUUID = newArtifact.getArtifactUUID();
667             final ArtifactTypeEnum artifactType = ArtifactTypeEnum.parse(newArtifact.getArtifactType());
668             if (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_NET || artifactType == ArtifactTypeEnum.HEAT_VOL) {
669                 ArtifactDefinition createHeatEnvPlaceHolder = artifactsBusinessLogic
670                     .createHeatEnvPlaceHolder(createdArtifacts, newArtifact, ArtifactsBusinessLogic.HEAT_VF_ENV_NAME, resource.getUniqueId(),
671                         NodeTypeEnum.Resource, resource.getName(), csarInfo.getModifier(), resource, null);
672                 artifactEnvUid = createHeatEnvPlaceHolder.getUniqueId();
673             }
674         } else {
675             artifactUid = createdArtifact.getUniqueId();
676             artifactUUID = createdArtifact.getArtifactUUID();
677             artifactEnvUid = checkAndGetHeatEnvId(createdArtifact);
678         }
679         artifactsGroup.add(artifactUid);
680         artifactsUUIDGroup.add(artifactUUID);
681         if (!artifactEnvUid.isEmpty()) {
682             artifactsGroup.add(artifactEnvUid);
683         }
684         List<ArtifactTemplateInfo> relatedArtifacts = artifactTemplateInfo.getRelatedArtifactsInfo();
685         if (relatedArtifacts != null) {
686             for (ArtifactTemplateInfo relatedArtifactTemplateInfo : relatedArtifacts) {
687                 resStatus = createDeploymentArtifactFromCsar(csarInfo, artifactPath, resource, artifactsGroup, artifactsUUIDGroup,
688                     relatedArtifactTemplateInfo, createdArtifacts, labelCounter);
689                 if (resStatus.isRight()) {
690                     return resStatus;
691                 }
692             }
693         }
694         return resStatus;
695     }
696
697     private String checkAndGetHeatEnvId(ArtifactDefinition createdArtifact) {
698         String artifactEnvUid = "";
699         final ArtifactTypeEnum artifactType = ArtifactTypeEnum.parse(createdArtifact.getArtifactType());
700         if (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_NET || artifactType == ArtifactTypeEnum.HEAT_VOL) {
701             artifactEnvUid = createdArtifact.getUniqueId() + ArtifactsBusinessLogic.HEAT_ENV_SUFFIX;
702         }
703         return artifactEnvUid;
704     }
705
706     private Either<ArtifactDefinition, ResponseFormat> checkIfArtifactAlreadyExist(ArtifactTemplateInfo artifactTemplateInfo,
707                                                                                    List<ArtifactDefinition> createdArtifacts) {
708         ArtifactDefinition res = null;
709         String artifactFileName = artifactTemplateInfo.getFileName();
710         Optional<ArtifactDefinition> op = createdArtifacts.stream().filter(a -> a.getArtifactName().equals(artifactFileName)).findAny();
711         if (op.isPresent()) {
712             res = op.get();
713             if (!res.getArtifactType().equalsIgnoreCase(artifactTemplateInfo.getType())) {
714                 log.error(ARTIFACT_WITH_NAME_AND_TYPE_ALREADY_EXIST_WITH_TYPE, artifactFileName, artifactTemplateInfo.getType(),
715                     res.getArtifactType());
716                 BeEcompErrorManager.getInstance()
717                     .logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName, ARTIFACT_INTERNALS_ARE_INVALID,
718                         ErrorSeverity.ERROR);
719                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, artifactFileName,
720                     artifactTemplateInfo.getType(), res.getArtifactType()));
721             }
722         }
723         return Either.left(res);
724     }
725
726     private Either<ArtifactDefinition, ResponseFormat> createDeploymentArtifact(CsarInfo csarInfo, Component resource, String artifactPath,
727                                                                                 ArtifactTemplateInfo artifactTemplateInfo,
728                                                                                 List<ArtifactDefinition> createdArtifacts, int label) {
729         int updatedlabel = label;
730         final String artifactFileName = artifactTemplateInfo.getFileName();
731         Either<ImmutablePair<String, byte[]>, ResponseFormat> artifactContentStatus = CsarValidationUtils
732             .getArtifactContent(csarInfo.getCsarUUID(), csarInfo.getCsar(), artifactPath + artifactFileName, artifactFileName, componentsUtils);
733         if (artifactContentStatus.isRight()) {
734             return Either.right(artifactContentStatus.right().value());
735         }
736         updatedlabel += createdArtifacts.size();
737         Map<String, Object> json = ArtifactUtils
738             .buildJsonForArtifact(artifactTemplateInfo, artifactContentStatus.left().value().getValue(), updatedlabel, true);
739         Either<Either<ArtifactDefinition, Operation>, ResponseFormat> uploadArtifactToService = createOrUpdateCsarArtifactFromJson(resource,
740             csarInfo.getModifier(), json, new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE));
741         if (uploadArtifactToService.isRight()) {
742             return Either.right(uploadArtifactToService.right().value());
743         }
744         ArtifactDefinition currentInfo = uploadArtifactToService.left().value().left().value();
745         if (currentInfo.getHeatParameters() != null) {
746             Either<ArtifactDefinition, ResponseFormat> updateEnvEither = updateHeatParamsFromCsar(resource, csarInfo, artifactTemplateInfo,
747                 currentInfo, false);
748             if (updateEnvEither.isRight()) {
749                 log.debug("failed to update parameters to artifact {}", artifactFileName);
750                 return Either.right(updateEnvEither.right().value());
751             }
752             currentInfo = updateEnvEither.left().value();
753         }
754         createdArtifacts.add(currentInfo);
755         return Either.left(currentInfo);
756     }
757
758     private Either<ArtifactDefinition, ResponseFormat> updateHeatParamsFromCsar(Component resource, CsarInfo csarInfo,
759                                                                                 ArtifactTemplateInfo artifactTemplateInfo,
760                                                                                 ArtifactDefinition currentInfo, boolean isUpdateEnv) {
761         Component updatedResource = resource;
762         Either<ArtifactDefinition, ResponseFormat> resStatus = Either.left(currentInfo);
763         if (artifactTemplateInfo.getEnv() != null && !artifactTemplateInfo.getEnv().isEmpty()) {
764             Either<ImmutablePair<String, byte[]>, ResponseFormat> artifactParamsStatus = CsarValidationUtils
765                 .getArtifactContent(csarInfo.getCsarUUID(), csarInfo.getCsar(), CsarUtils.ARTIFACTS_PATH + artifactTemplateInfo.getEnv(),
766                     artifactTemplateInfo.getEnv(), componentsUtils);
767             if (artifactParamsStatus.isRight()) {
768                 resStatus = Either.right(artifactParamsStatus.right().value());
769                 return resStatus;
770             }
771             Either<List<HeatParameterDefinition>, ResponseFormat> propsStatus = extractHeatParameters(ArtifactTypeEnum.HEAT_ENV.getType(),
772                 artifactTemplateInfo.getEnv(), artifactParamsStatus.left().value().getValue(), false);
773             if (propsStatus.isLeft()) {
774                 List<HeatParameterDefinition> updatedHeatEnvParams = propsStatus.left().value();
775                 resStatus = updateHeatParams(updatedResource, currentInfo, updatedHeatEnvParams);
776                 if (resStatus.isRight()) {
777                     return resStatus;
778                 }
779             }
780         }
781         if (isUpdateEnv) {
782             Map<String, ArtifactDefinition> artifacts = updatedResource.getDeploymentArtifacts();
783             Optional<ArtifactDefinition> op = artifacts.values().stream()
784                 .filter(p -> p.getGeneratedFromId() != null && p.getGeneratedFromId().equals(currentInfo.getUniqueId())).findAny();
785             if (op.isPresent()) {
786                 ArtifactDefinition artifactInfoHeatEnv = op.get();
787                 artifactInfoHeatEnv.setHeatParamUpdated(true);
788                 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactOnResource = artifactToscaOperation
789                     .updateArtifactOnResource(artifactInfoHeatEnv, updatedResource, artifactInfoHeatEnv.getUniqueId(), null, null, true);
790                 if (updateArtifactOnResource.isRight()) {
791                     log.error("Failed to update heat env on CSAR flow for component {} artifact {} label {}", updatedResource.getUniqueId(),
792                         artifactInfoHeatEnv.getUniqueId(), artifactInfoHeatEnv.getArtifactLabel());
793                     return Either.right(
794                         componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateArtifactOnResource.right().value())));
795                 }
796                 resource.getDeploymentArtifacts()
797                     .put(updateArtifactOnResource.left().value().getArtifactLabel(), updateArtifactOnResource.left().value());
798                 resStatus = Either.left(updateArtifactOnResource.left().value());
799             }
800         }
801         return resStatus;
802     }
803
804     private Either<List<HeatParameterDefinition>, ResponseFormat> extractHeatParameters(String artifactType, String fileName, byte[] content,
805                                                                                         boolean is64Encoded) {
806         // extract heat parameters
807         String heatDecodedPayload = is64Encoded ? new String(Base64.decodeBase64(content)) : new String(content);
808         Either<List<HeatParameterDefinition>, ResultStatusEnum> heatParameters = ImportUtils
809             .getHeatParamsWithoutImplicitTypes(heatDecodedPayload, artifactType);
810         if (heatParameters.isRight()) {
811             log.error("File {} is not in expected key-value form in csar ", fileName);
812             BeEcompErrorManager.getInstance()
813                 .logInternalDataError("File " + fileName + " is not in expected key-value form in csar ", "CSAR internals are invalid",
814                     ErrorSeverity.ERROR);
815             return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, fileName));
816         }
817         return Either.left(heatParameters.left().value());
818     }
819
820     private Either<ArtifactDefinition, ResponseFormat> updateHeatParams(Component resource, ArtifactDefinition currentInfo,
821                                                                         List<HeatParameterDefinition> updatedHeatEnvParams) {
822         Either<ArtifactDefinition, ResponseFormat> resStatus = Either.left(currentInfo);
823         List<HeatParameterDefinition> currentHeatEnvParams = currentInfo.getListHeatParameters();
824         if (updatedHeatEnvParams != null && !updatedHeatEnvParams.isEmpty() && currentHeatEnvParams != null && !currentHeatEnvParams.isEmpty()) {
825             String paramName;
826             for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) {
827                 paramName = heatEnvParam.getName();
828                 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
829                     if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
830                         String updatedParamValue = heatEnvParam.getCurrentValue();
831                         if (updatedParamValue == null) {
832                             updatedParamValue = heatEnvParam.getDefaultValue();
833                         }
834                         HeatParameterType paramType = HeatParameterType.isValidType(currHeatParam.getType());
835                         if (!paramType.getValidator().isValid(updatedParamValue, null)) {
836                             ActionStatus status = ActionStatus.INVALID_HEAT_PARAMETER_VALUE;
837                             ResponseFormat responseFormat = componentsUtils
838                                 .getResponseFormat(status, ArtifactTypeEnum.HEAT_ENV.getType(), paramType.getType(), paramName);
839                             resStatus = Either.right(responseFormat);
840                             return resStatus;
841                         }
842                         currHeatParam.setCurrentValue(paramType.getConverter().convert(updatedParamValue, null, null));
843                         break;
844                     }
845                 }
846             }
847             currentInfo.setListHeatParameters(currentHeatEnvParams);
848             Either<ArtifactDefinition, StorageOperationStatus> updateArtifactOnResource = artifactToscaOperation
849                 .updateArtifactOnResource(currentInfo, resource, currentInfo.getUniqueId(), null, null, true);
850             if (updateArtifactOnResource.isRight()) {
851                 log.error("Failed to update heat parameters of heat on CSAR flow for component {} artifact {} label {}", resource.getUniqueId(),
852                     currentInfo.getUniqueId(), currentInfo.getArtifactLabel());
853                 return Either
854                     .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateArtifactOnResource.right().value())));
855             }
856             resource.getDeploymentArtifacts().put(currentInfo.getArtifactLabel(), currentInfo);
857             resStatus = Either.left(updateArtifactOnResource.left().value());
858         }
859         return resStatus;
860     }
861
862     public Either<Either<ArtifactDefinition, Operation>, ResponseFormat> createOrUpdateCsarArtifactFromJson(Component component, User user,
863                                                                                                             Map<String, Object> json,
864                                                                                                             ArtifactOperationInfo operation) {
865         String jsonStr = gson.toJson(json);
866         ArtifactDefinition artifactDefinitionFromJson = RepresentationUtils.convertJsonToArtifactDefinition(jsonStr, ArtifactDefinition.class, false);
867         Either<ArtifactDefinition, Operation> result;
868         try {
869             result = artifactsBusinessLogic
870                 .handleLoadedArtifact(component, user, operation, false, true, component.getComponentType(), artifactDefinitionFromJson);
871         } catch (ComponentException e) {
872             log.error(FAILED_UPLOAD_ARTIFACT_TO_COMPONENT, component.getComponentType(), component.getName());
873             return Either.right(componentsUtils.getResponseFormat(e));
874         } catch (Exception e) {
875             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
876             log.error("Exception occurred when createOrUpdateCsarArtifactFromJson, error is:{}", e.getMessage(), e);
877             return Either.right(responseFormat);
878         }
879         return Either.left(result);
880     }
881
882     private void associateMembersToArtifacts(List<ArtifactDefinition> createdArtifacts, List<ArtifactDefinition> artifactsFromResource,
883                                              List<GroupDefinition> heatGroups, Set<String> artifactsGroup, Map<String, String> members) {
884         if (heatGroups != null && !heatGroups.isEmpty()) {
885             for (GroupDefinition heatGroup : heatGroups) {
886                 List<GroupProperty> grpoupProps = heatGroup.convertToGroupProperties();
887                 if (grpoupProps != null) {
888                     associateMembersToVFgroups(createdArtifacts, artifactsFromResource, grpoupProps, artifactsGroup, heatGroup, members);
889                 }
890             }
891         }
892     }
893
894     private void associateMembersToVFgroups(List<ArtifactDefinition> createdArtifacts, List<ArtifactDefinition> artifactsFromResource,
895                                             List<GroupProperty> grpoupProps, Set<String> artifactsGroup, GroupDefinition heatGroup,
896                                             Map<String, String> members) {
897         Optional<GroupProperty> op = grpoupProps.stream().filter(p -> p.getName().equals(Constants.HEAT_FILE_PROPS)).findAny();
898         if (op.isPresent()) {
899             GroupProperty prop = op.get();
900             String heatFileNAme = prop.getValue();
901             if (null == heatFileNAme || heatFileNAme.isEmpty()) {
902                 return;
903             }
904             List<ArtifactDefinition> artifacts = new ArrayList<>();
905             for (String artifactId : artifactsGroup) {
906                 Optional<ArtifactDefinition> opArt = createdArtifacts.stream().filter(p -> p.getUniqueId().equals(artifactId)).findAny();
907                 opArt.ifPresent(artifacts::add);
908                 if (artifactsFromResource != null) {
909                     opArt = artifactsFromResource.stream().filter(p -> p.getUniqueId().equals(artifactId)).findAny();
910                     opArt.ifPresent(artifacts::add);
911                 }
912             }
913             Optional<ArtifactDefinition> resOp = artifacts.stream().filter(p -> heatFileNAme.contains(p.getArtifactName())).findAny();
914             resOp.ifPresent(artifactDefinition -> members.putAll(heatGroup.getMembers()));
915         }
916     }
917
918     public List<GroupProperty> createVfModuleAdditionalProperties(boolean isBase, String moduleName, List<GroupProperty> properties,
919                                                                   List<ArtifactDefinition> deploymentArtifacts, List<String> artifactsInGroup,
920                                                                   GroupTypeDefinition groupType) {
921         Map<String, VfModuleProperty> vfModuleProperties = ConfigurationManager.getConfigurationManager().getConfiguration().getVfModuleProperties();
922         vfModuleProperties.entrySet().forEach(p -> {
923             GroupProperty prop = new GroupProperty();
924             prop.setName(p.getKey());
925             if (isBase) {
926                 prop.setValue(p.getValue().getForBaseModule());
927                 prop.setDefaultValue(p.getValue().getForBaseModule());
928             } else {
929                 prop.setValue(p.getValue().getForNonBaseModule());
930                 prop.setDefaultValue(p.getValue().getForNonBaseModule());
931             }
932             properties.add(prop);
933         });
934         GroupProperty proplabel = new GroupProperty();
935         proplabel.setName("vf_module_label");
936         Matcher matcher = pattern.matcher(moduleName);
937         if (matcher.find()) {
938             proplabel.setValue(matcher.group(1));
939             proplabel.setDefaultValue(matcher.group(1));
940         } else {
941             proplabel.setValue(moduleName);
942             proplabel.setDefaultValue(moduleName);
943         }
944         properties.add(proplabel);
945         GroupProperty propvolume = new GroupProperty();
946         propvolume.setName("volume_group");
947         boolean isVolume = false;
948         for (String artifactId : artifactsInGroup) {
949             ArtifactDefinition artifactDef = null;
950             artifactDef = ArtifactUtils.findArtifactInList(deploymentArtifacts, artifactId);
951             if (artifactDef != null && artifactDef.getArtifactType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType())) {
952                 isVolume = true;
953                 break;
954             }
955         }
956         propvolume.setValue(String.valueOf(isVolume));
957         propvolume.setDefaultValue(String.valueOf(isVolume));
958         properties.add(propvolume);
959         mergeWithGroupTypeProperties(properties, groupType.getProperties());
960         return properties;
961     }
962
963     private void mergeWithGroupTypeProperties(List<GroupProperty> properties, List<PropertyDefinition> groupTypeProperties) {
964         Map<String, GroupProperty> propertiesMap = properties.stream().collect(Collectors.toMap(PropertyDataDefinition::getName, p -> p));
965         for (PropertyDefinition groupTypeProperty : groupTypeProperties) {
966             if (!propertiesMap.containsKey(groupTypeProperty.getName())) {
967                 properties.add(new GroupProperty(groupTypeProperty));
968             }
969         }
970     }
971
972     private Map<GroupDefinition, MergedArtifactInfo> mergeGroupInUpdateFlow(
973         Map<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> groupArtifact,
974         Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup, Set<ArtifactDefinition> artifactsToDelete,
975         Map<String, List<ArtifactDefinition>> groupToDelete, Set<ArtifactTemplateInfo> jsonMasterArtifacts,
976         List<ArtifactDefinition> createdDeploymentArtifacts) {
977         Map<GroupDefinition, MergedArtifactInfo> mergedgroup = new HashMap<>();
978         for (Entry<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> groupListEntry : groupArtifact.entrySet()) {
979             Map<ArtifactDefinition, List<ArtifactDefinition>> createdArtifactMap = groupListEntry.getValue();
980             boolean isNeedToDeleteGroup = true;
981             List<ArtifactDefinition> listToDelete = null;
982             for (ArtifactDefinition maserArtifact : createdArtifactMap.keySet()) {
983                 listToDelete = prepareArtifactsToDelete(parsedGroup, artifactsToDelete, createdDeploymentArtifacts, createdArtifactMap,
984                     maserArtifact);
985                 if (artifactsToDelete != null && !artifactsToDelete.isEmpty()) {
986                     GroupDefinition group = groupListEntry.getKey();
987                     deleteArtifacts(artifactsToDelete, group);
988                 }
989                 for (ArtifactTemplateInfo jsonMasterArtifact : jsonMasterArtifacts) {
990                     isNeedToDeleteGroup = isNeedToDeleteGroup(mergedgroup, groupListEntry, createdArtifactMap, isNeedToDeleteGroup, maserArtifact,
991                         jsonMasterArtifact);
992                 }
993             }
994             if (isNeedToDeleteGroup) {
995                 groupToDelete.put(groupListEntry.getKey().getUniqueId(), listToDelete);
996             }
997         }
998         return mergedgroup;
999     }
1000
1001     private boolean isNeedToDeleteGroup(Map<GroupDefinition, MergedArtifactInfo> mergedgroup,
1002                                         Entry<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> groupListEntry,
1003                                         Map<ArtifactDefinition, List<ArtifactDefinition>> createdArtifactMap, boolean isNeedToDeleteGroup,
1004                                         ArtifactDefinition maserArtifact, ArtifactTemplateInfo jsonMasterArtifact) {
1005         if (maserArtifact.getArtifactName().equalsIgnoreCase(jsonMasterArtifact.getFileName())) {
1006             MergedArtifactInfo mergedGroup = new MergedArtifactInfo();
1007             mergedGroup.setJsonArtifactTemplate(jsonMasterArtifact);
1008             mergedGroup.setCreatedArtifact(createdArtifactMap.get(maserArtifact));
1009             mergedgroup.put(groupListEntry.getKey(), mergedGroup);
1010             isNeedToDeleteGroup = false;
1011         }
1012         return isNeedToDeleteGroup;
1013     }
1014
1015     private List<ArtifactDefinition> prepareArtifactsToDelete(Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup,
1016                                                               Set<ArtifactDefinition> artifactsToDelete,
1017                                                               List<ArtifactDefinition> createdDeploymentArtifacts,
1018                                                               Map<ArtifactDefinition, List<ArtifactDefinition>> createdArtifactMap,
1019                                                               ArtifactDefinition maserArtifact) {
1020         List<ArtifactDefinition> listToDelete;
1021         listToDelete = createdArtifactMap.get(maserArtifact);
1022         for (ArtifactDefinition artToDelete : listToDelete) {
1023             findArtifactToDelete(parsedGroup, artifactsToDelete, artToDelete, createdDeploymentArtifacts);
1024         }
1025         return listToDelete;
1026     }
1027
1028     private void deleteArtifacts(Set<ArtifactDefinition> artifactsToDelete, GroupDefinition group) {
1029         for (ArtifactDefinition artifactDefinition : artifactsToDelete) {
1030             if (CollectionUtils.isNotEmpty(group.getArtifacts()) && group.getArtifacts().contains(artifactDefinition.getUniqueId())) {
1031                 group.getArtifacts().remove(artifactDefinition.getUniqueId());
1032             }
1033             if (CollectionUtils.isNotEmpty(group.getArtifactsUuid()) && group.getArtifactsUuid().contains(artifactDefinition.getArtifactUUID())) {
1034                 group.getArtifactsUuid().remove(artifactDefinition.getArtifactUUID());
1035             }
1036         }
1037     }
1038
1039     private void findArtifactToDelete(Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup, Set<ArtifactDefinition> artifactsToDelete,
1040                                       ArtifactDefinition artifact, List<ArtifactDefinition> createdDeploymentArtifacts) {
1041         ArtifactDefinition generatedFromArt = null;
1042         if (artifact.getGeneratedFromId() != null && !artifact.getGeneratedFromId().isEmpty()) {
1043             Optional<ArtifactDefinition> op = createdDeploymentArtifacts.stream().filter(p -> p.getUniqueId().equals(artifact.getGeneratedFromId()))
1044                 .findAny();
1045             if (op.isPresent()) {
1046                 generatedFromArt = op.get();
1047             }
1048         }
1049         isNeedToDeleteArtifact(parsedGroup, artifactsToDelete, artifact, generatedFromArt);
1050     }
1051
1052     private void isNeedToDeleteArtifact(Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup, Set<ArtifactDefinition> artifactsToDelete,
1053                                         ArtifactDefinition artifact, ArtifactDefinition generatedFromArt) {
1054         final String artifactType = artifact.getArtifactType();
1055         final String artifactName = artifact.getArtifactName();
1056         boolean isNeedToDeleteArtifact = true;
1057         for (final Entry<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroupSetEntry : parsedGroup.entrySet()) {
1058             if (isNeedToDeleteArtifact) {
1059                 for (final ArtifactTemplateInfo template : parsedGroupSetEntry.getValue()) {
1060                     if (isNeedToDeleteArtifact) {
1061                         final String fileName = template.getFileName();
1062                         final String type = template.getType();
1063                         if ((artifactName.equalsIgnoreCase(fileName) && artifactType.equalsIgnoreCase(type))
1064                             || (generatedFromArt != null && generatedFromArt.getArtifactName().equalsIgnoreCase(fileName) &&
1065                             generatedFromArt.getArtifactType().equalsIgnoreCase(type))) {
1066                             isNeedToDeleteArtifact = false;
1067                         }
1068                     }
1069                 }
1070             }
1071         }
1072         if (isNeedToDeleteArtifact) {
1073             artifactsToDelete.add(artifact);
1074         }
1075     }
1076
1077     private Map<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> findMasterArtifactInGroup(List<GroupDefinition> groups,
1078                                                                                                               Map<String, ArtifactDefinition> deploymentArtifact) {
1079         Map<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> groupArtifact = new HashMap<>();
1080         for (GroupDefinition group : groups) {
1081             Map<ArtifactDefinition, List<ArtifactDefinition>> gupsMap = new HashMap<>();
1082             List<ArtifactDefinition> artifacts = new ArrayList<>();
1083             List<String> artifactsList = group.getArtifacts();
1084             if (artifactsList != null && !artifactsList.isEmpty()) {
1085                 ArtifactDefinition masterArtifact = ArtifactUtils.findMasterArtifact(deploymentArtifact, artifacts, artifactsList);
1086                 if (masterArtifact != null) {
1087                     gupsMap.put(masterArtifact, artifacts);
1088                 }
1089                 groupArtifact.put(group, gupsMap);
1090             }
1091         }
1092         return groupArtifact;
1093     }
1094
1095     private Either<Component, ResponseFormat> deleteArtifactsInUpdateCsarFlow(Component resource, User user, boolean shouldLock,
1096                                                                               boolean inTransaction, Set<ArtifactDefinition> artifactsToDelete,
1097                                                                               Map<String, List<ArtifactDefinition>> groupToDelete,
1098                                                                               List<ArtifactDefinition> deletedArtifacts) {
1099         Component updatedResource = resource;
1100         String resourceId = updatedResource.getUniqueId();
1101         if (!artifactsToDelete.isEmpty()) {
1102             for (ArtifactDefinition artifact : artifactsToDelete) {
1103                 String artifactType = artifact.getArtifactType();
1104                 final ArtifactTypeEnum artifactTypeEnum = ArtifactTypeEnum.parse(artifactType);
1105                 if (artifactTypeEnum != ArtifactTypeEnum.HEAT_ENV) {
1106                     Either<ArtifactDefinition, ResponseFormat> handleDelete = artifactsBusinessLogic
1107                         .handleDelete(resourceId, artifact.getUniqueId(), user, updatedResource, shouldLock, inTransaction);
1108                     if (handleDelete.isRight()) {
1109                         return Either.right(handleDelete.right().value());
1110                     }
1111                     deletedArtifacts.add(handleDelete.left().value());
1112                 }
1113             }
1114         }
1115         if (!groupToDelete.isEmpty()) {
1116             log.debug("try to delete group");
1117             List<GroupDefinition> groupDefinitionstoDelete = new ArrayList<>();
1118             List<GroupDefinition> groups = updatedResource.getGroups();
1119             for (Entry<String, List<ArtifactDefinition>> deleteGroup : groupToDelete.entrySet()) {
1120                 Optional<GroupDefinition> op = groups.stream().filter(gr -> gr.getUniqueId().equals(deleteGroup.getKey())).findAny();
1121                 if (op.isPresent()) {
1122                     groupDefinitionstoDelete.add(op.get());
1123                 }
1124             }
1125             if (!groupDefinitionstoDelete.isEmpty()) {
1126                 Either<List<GroupDefinition>, ResponseFormat> prepareGroups = groupBusinessLogic.deleteGroups(resource, groupDefinitionstoDelete);
1127                 if (prepareGroups.isRight()) {
1128                     return Either.right(prepareGroups.right().value());
1129                 }
1130             }
1131         }
1132         List<GroupDefinition> oldGroups = updatedResource.getGroups();
1133         Either<Resource, StorageOperationStatus> eitherGerResource = toscaOperationFacade.getToscaElement(updatedResource.getUniqueId());
1134         if (eitherGerResource.isRight()) {
1135             ResponseFormat responseFormat = componentsUtils
1136                 .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), updatedResource,
1137                     resource.getComponentType());
1138             return Either.right(responseFormat);
1139         }
1140         updatedResource = eitherGerResource.left().value();
1141         updatedResource.setGroups(oldGroups);
1142         return Either.left(updatedResource);
1143     }
1144
1145     private void createArtifactsGroupSet(List<ArtifactTemplateInfo> parsedGroupTemplateList, Set<ArtifactTemplateInfo> parsedArtifactsName) {
1146         for (ArtifactTemplateInfo parsedGroupTemplate : parsedGroupTemplateList) {
1147             parsedArtifactsName.add(parsedGroupTemplate);
1148             List<ArtifactTemplateInfo> relatedArtifacts = parsedGroupTemplate.getRelatedArtifactsInfo();
1149             if (relatedArtifacts != null && !relatedArtifacts.isEmpty()) {
1150                 createArtifactsGroupSet(relatedArtifacts, parsedArtifactsName);
1151             }
1152         }
1153     }
1154
1155     private Either<Component, ResponseFormat> createGroupDeploymentArtifactsFromCsar(CsarInfo csarInfo, Component resource,
1156                                                                                      List<ArtifactTemplateInfo> artifactsTemplateList,
1157                                                                                      List<ArtifactDefinition> createdNewArtifacts,
1158                                                                                      List<ArtifactDefinition> artifactsFromResource,
1159                                                                                      int labelCounter) {
1160         Component updatedResource = resource;
1161         Either<Component, ResponseFormat> resStatus = Either.left(updatedResource);
1162         List<GroupDefinition> createdGroups = updatedResource.getGroups();
1163         List<GroupDefinition> heatGroups = null;
1164         if (createdGroups != null && !createdGroups.isEmpty()) {
1165             heatGroups = collectGroupsWithMembers(createdGroups);
1166         }
1167         List<GroupDefinition> needToAdd = new ArrayList<>();
1168         for (ArtifactTemplateInfo groupTemplateInfo : artifactsTemplateList) {
1169             String groupName = groupTemplateInfo.getGroupName();
1170             Set<String> artifactsGroup = new HashSet<>();
1171             Set<String> artifactsUUIDGroup = new HashSet<>();
1172             resStatus = createDeploymentArtifactsFromCsar(csarInfo, updatedResource, artifactsGroup, artifactsUUIDGroup, groupTemplateInfo,
1173                 createdNewArtifacts, artifactsFromResource, labelCounter);
1174             if (resStatus.isRight()) {
1175                 return resStatus;
1176             }
1177             if (!StringUtils.isEmpty(groupName)) {
1178                 Map<String, String> members = new HashMap<>();
1179                 associateMembersToArtifacts(createdNewArtifacts, artifactsFromResource, heatGroups, artifactsGroup, members);
1180                 List<String> artifactsList = new ArrayList<>(artifactsGroup);
1181                 List<String> artifactsUUIDList = new ArrayList<>(artifactsUUIDGroup);
1182                 GroupDefinition groupDefinition = new GroupDefinition();
1183                 groupDefinition.setName(groupName);
1184                 groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
1185                 groupDefinition.setArtifacts(artifactsList);
1186                 groupDefinition.setArtifactsUuid(artifactsUUIDList);
1187                 if (!members.isEmpty()) {
1188                     groupDefinition.setMembers(members);
1189                 }
1190                 List<GroupProperty> properties = new ArrayList<>();
1191                 GroupProperty prop = new GroupProperty();
1192                 prop.setName(Constants.IS_BASE);
1193                 prop.setValue(Boolean.toString(groupTemplateInfo.isBase()));
1194                 properties.add(prop);
1195                 List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
1196                 createdArtifacts.addAll(createdNewArtifacts);
1197                 createdArtifacts.addAll(artifactsFromResource);
1198                 Either<GroupTypeDefinition, StorageOperationStatus> getLatestGroupTypeRes = groupTypeOperation
1199                     .getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, resource.getModel());
1200                 if (getLatestGroupTypeRes.isRight()) {
1201                     return Either
1202                         .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getLatestGroupTypeRes.right().value())));
1203                 }
1204                 properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties, createdArtifacts, artifactsList,
1205                     getLatestGroupTypeRes.left().value());
1206                 groupDefinition.convertFromGroupProperties(properties);
1207                 needToAdd.add(groupDefinition);
1208             }
1209         }
1210         ComponentParametersView componentParametersView = new ComponentParametersView();
1211         componentParametersView.disableAll();
1212         componentParametersView.setIgnoreArtifacts(false);
1213         componentParametersView.setIgnoreGroups(false);
1214         componentParametersView.setIgnoreComponentInstances(false);
1215         Either<Resource, StorageOperationStatus> component = toscaOperationFacade
1216             .getToscaElement(updatedResource.getUniqueId(), componentParametersView);
1217         if (component.isRight()) {
1218             return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1219         }
1220         updatedResource = component.left().value();
1221         Either<List<GroupDefinition>, ResponseFormat> addGroups = groupBusinessLogic.addGroups(updatedResource, needToAdd, false);
1222         if (addGroups.isRight()) {
1223             return Either.right(addGroups.right().value());
1224         }
1225         return resStatus;
1226     }
1227
1228     private Either<Component, ResponseFormat> createDeploymentArtifactsFromCsar(CsarInfo csarInfo, Component resource, Set<String> artifactsGroup,
1229                                                                                 Set<String> artifactsUUIDGroup,
1230                                                                                 ArtifactTemplateInfo artifactTemplateInfo,
1231                                                                                 List<ArtifactDefinition> createdArtifacts,
1232                                                                                 List<ArtifactDefinition> artifactsFromResource, int labelCounter) {
1233         Either<Component, ResponseFormat> resStatus = Either.left(resource);
1234         String artifactFileName = artifactTemplateInfo.getFileName();
1235         String artifactUid = "";
1236         String artifactUUID = "";
1237         String artifactEnvUid = "";
1238         boolean alreadyExist = false;
1239         // check if artifacts already exist
1240         if (artifactsFromResource != null && !artifactsFromResource.isEmpty()) {
1241             for (ArtifactDefinition artifactFromResource : artifactsFromResource) {
1242                 if (artifactFromResource.getArtifactName().equals(artifactFileName)) {
1243                     artifactUid = artifactFromResource.getUniqueId();
1244                     artifactUUID = artifactFromResource.getArtifactUUID();
1245                     if (!artifactFromResource.getArtifactType().equalsIgnoreCase(artifactTemplateInfo.getType())) {
1246                         log.error(ARTIFACT_WITH_NAME_AND_TYPE_ALREADY_EXIST_WITH_TYPE, artifactFileName, artifactTemplateInfo.getType(),
1247                             artifactFromResource.getArtifactType());
1248                         BeEcompErrorManager.getInstance().logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName,
1249                             ARTIFACT_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
1250                         return Either.right(componentsUtils
1251                             .getResponseFormat(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, artifactFileName,
1252                                 artifactTemplateInfo.getType(), artifactFromResource.getArtifactType()));
1253                     }
1254                     alreadyExist = true;
1255                     artifactEnvUid = checkAndGetHeatEnvId(artifactFromResource);
1256                     break;
1257                 }
1258             }
1259         }
1260         if (!alreadyExist) {
1261             for (ArtifactDefinition createdArtifact : createdArtifacts) {
1262                 if (createdArtifact.getArtifactName().equals(artifactFileName)) {
1263                     artifactUid = createdArtifact.getUniqueId();
1264                     artifactUUID = createdArtifact.getArtifactUUID();
1265                     if (!createdArtifact.getArtifactType().equalsIgnoreCase(artifactTemplateInfo.getType())) {
1266                         log.error(ARTIFACT_WITH_NAME_AND_TYPE_ALREADY_EXIST_WITH_TYPE, artifactFileName, artifactTemplateInfo.getType(),
1267                             createdArtifact.getArtifactType());
1268                         BeEcompErrorManager.getInstance().logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName,
1269                             ARTIFACT_INTERNALS_ARE_INVALID, ErrorSeverity.ERROR);
1270                         return Either.right(componentsUtils
1271                             .getResponseFormat(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, artifactFileName,
1272                                 artifactTemplateInfo.getType(), createdArtifact.getArtifactType()));
1273                     }
1274                     alreadyExist = true;
1275                     artifactEnvUid = checkAndGetHeatEnvId(createdArtifact);
1276                     break;
1277                 }
1278             }
1279         }
1280         // if not exist need to create
1281         if (!alreadyExist) {
1282             Either<ArtifactDefinition, ResponseFormat> newArtifactEither = createDeploymentArtifact(csarInfo, resource, ARTIFACTS_PATH,
1283                 artifactTemplateInfo, createdArtifacts, labelCounter);
1284             if (newArtifactEither.isRight()) {
1285                 resStatus = Either.right(newArtifactEither.right().value());
1286                 return resStatus;
1287             }
1288             ArtifactDefinition newArtifact = newArtifactEither.left().value();
1289             artifactUid = newArtifact.getUniqueId();
1290             artifactUUID = newArtifact.getArtifactUUID();
1291             final ArtifactTypeEnum artifactType = ArtifactTypeEnum.parse(newArtifact.getArtifactType());
1292             if (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_NET || artifactType == ArtifactTypeEnum.HEAT_VOL) {
1293                 ArtifactDefinition createHeatEnvPlaceHolder = artifactsBusinessLogic
1294                     .createHeatEnvPlaceHolder(createdArtifacts, newArtifact, ArtifactsBusinessLogic.HEAT_VF_ENV_NAME, resource.getUniqueId(),
1295                         NodeTypeEnum.Resource, resource.getName(), csarInfo.getModifier(), resource, null);
1296                 artifactEnvUid = createHeatEnvPlaceHolder.getUniqueId();
1297             }
1298         }
1299         artifactsGroup.add(artifactUid);
1300         artifactsUUIDGroup.add(artifactUUID);
1301         if (!artifactEnvUid.isEmpty()) {
1302             artifactsGroup.add(artifactEnvUid);
1303         }
1304         List<ArtifactTemplateInfo> relatedArtifacts = artifactTemplateInfo.getRelatedArtifactsInfo();
1305         if (relatedArtifacts != null) {
1306             for (ArtifactTemplateInfo relatedArtifactTemplateInfo : relatedArtifacts) {
1307                 resStatus = createDeploymentArtifactsFromCsar(csarInfo, resource, artifactsGroup, artifactsUUIDGroup, relatedArtifactTemplateInfo,
1308                     createdArtifacts, artifactsFromResource, labelCounter);
1309                 if (resStatus.isRight()) {
1310                     return resStatus;
1311                 }
1312             }
1313         }
1314         return resStatus;
1315     }
1316
1317     private Either<Component, ResponseFormat> associateAndDissociateArtifactsToGroup(CsarInfo csarInfo, Component resource,
1318                                                                                      List<ArtifactDefinition> createdNewArtifacts, int labelCounter,
1319                                                                                      List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete,
1320                                                                                      Map<GroupDefinition, MergedArtifactInfo> mergedgroup,
1321                                                                                      List<ArtifactDefinition> deletedArtifacts) {
1322         Map<GroupDefinition, List<ArtifactTemplateInfo>> artifactsToAssotiate = new HashMap<>();
1323         Map<GroupDefinition, List<ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo>>> artifactsToUpdateMap = new HashMap<>();
1324         Either<Component, ResponseFormat> resEither;
1325         for (Entry<GroupDefinition, MergedArtifactInfo> entry : mergedgroup.entrySet()) {
1326             List<ArtifactDefinition> dissArtifactsInGroup = entry.getValue().getListToDissotiateArtifactFromGroup(deletedArtifacts);
1327             GroupDefinition grDef = entry.getKey();
1328             if (dissArtifactsInGroup != null && !dissArtifactsInGroup.isEmpty()) {
1329                 for (ArtifactDefinition art : dissArtifactsInGroup) {
1330                     grDef.getArtifacts().remove(art.getUniqueId());
1331                     grDef.getArtifactsUuid().remove(art.getArtifactUUID());
1332                 }
1333             }
1334             List<ArtifactTemplateInfo> newArtifactsInGroup = entry.getValue().getListToAssociateArtifactToGroup();
1335             if (newArtifactsInGroup != null && !newArtifactsInGroup.isEmpty()) {
1336                 artifactsToAssotiate.put(entry.getKey(), newArtifactsInGroup);
1337             }
1338             List<ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo>> artifactsToUpdate = entry.getValue().getListToUpdateArtifactInGroup();
1339             if (artifactsToUpdate != null && !artifactsToUpdate.isEmpty()) {
1340                 artifactsToUpdateMap.put(entry.getKey(), artifactsToUpdate);
1341             }
1342         }
1343         if (!artifactsToUpdateMap.isEmpty()) {
1344             List<ArtifactDefinition> updatedArtifacts = new ArrayList<>();
1345             for (Entry<GroupDefinition, List<ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo>>> artifactsToUpdateEntry : artifactsToUpdateMap
1346                 .entrySet()) {
1347                 List<ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo>> artifactsToUpdateList = artifactsToUpdateEntry.getValue();
1348                 GroupDefinition groupToUpdate = artifactsToUpdateEntry.getKey();
1349                 for (ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo> artifact : artifactsToUpdateList) {
1350                     String prevUUID = artifact.getKey().getArtifactUUID();
1351                     String prevId = artifact.getKey().getUniqueId();
1352                     String prevHeatEnvId = checkAndGetHeatEnvId(artifact.getKey());
1353                     Either<ArtifactDefinition, ResponseFormat> updateArtifactEither = updateDeploymentArtifactsFromCsar(csarInfo, resource,
1354                         artifact.getKey(), artifact.getValue(), updatedArtifacts, artifact.getRight().getRelatedArtifactsInfo());
1355                     if (updateArtifactEither.isRight()) {
1356                         log.debug("failed to update artifacts. status is {}", updateArtifactEither.right().value());
1357                         resEither = Either.right(updateArtifactEither.right().value());
1358                         return resEither;
1359                     }
1360                     ArtifactDefinition artAfterUpdate = updateArtifactEither.left().value();
1361                     if (!prevUUID.equals(artAfterUpdate.getArtifactUUID()) || !prevId.equals(artAfterUpdate.getUniqueId())) {
1362                         groupToUpdate.getArtifacts().remove(prevId);
1363                         groupToUpdate.getArtifactsUuid().remove(prevUUID);
1364                         groupToUpdate.getArtifacts().add(artAfterUpdate.getUniqueId());
1365                         groupToUpdate.getArtifactsUuid().add(artAfterUpdate.getArtifactUUID());
1366                     }
1367                     Optional<ArtifactDefinition> op = updatedArtifacts.stream()
1368                         .filter(p -> p.getGeneratedFromId() != null && p.getGeneratedFromId().equals(artAfterUpdate.getUniqueId())).findAny();
1369                     if (op.isPresent()) {
1370                         ArtifactDefinition artifactInfoHeatEnv = op.get();
1371                         groupToUpdate.getArtifacts().remove(prevHeatEnvId);
1372                         groupToUpdate.getArtifacts().add(artifactInfoHeatEnv.getUniqueId());
1373                     }
1374                 }
1375             }
1376         }
1377         for (Entry<GroupDefinition, List<ArtifactTemplateInfo>> associateEntry : artifactsToAssotiate.entrySet()) {
1378             List<ArtifactTemplateInfo> associatedArtifact = associateEntry.getValue();
1379             Set<String> arifactsUids = new HashSet<>();
1380             Set<String> arifactsUuids = new HashSet<>();
1381             for (ArtifactTemplateInfo artifactTemplate : associatedArtifact) { // try to find artifact in resource
1382                 boolean isCreate = true;
1383                 for (ArtifactDefinition createdArtifact : createdDeploymentArtifactsAfterDelete) {
1384                     if (artifactTemplate.getFileName().equalsIgnoreCase(createdArtifact.getArtifactName())) {
1385                         arifactsUids.add(createdArtifact.getUniqueId());
1386                         arifactsUuids.add(createdArtifact.getArtifactUUID());
1387                         isCreate = false;
1388                         String heatEnvId = checkAndGetHeatEnvId(createdArtifact);
1389                         if (!heatEnvId.isEmpty()) {
1390                             arifactsUids.add(heatEnvId);
1391                             Optional<ArtifactDefinition> op = createdDeploymentArtifactsAfterDelete.stream()
1392                                 .filter(p -> p.getUniqueId().equals(heatEnvId)).findAny();
1393                             if (op.isPresent()) {
1394                                 this.artifactToscaOperation.updateHeatEnvPlaceholder(op.get(), resource, resource.getComponentType().getNodeType());
1395                             }
1396                         }
1397                         break;
1398                     }
1399                 }
1400                 if (isCreate) { // check if already created
1401                     for (ArtifactDefinition createdNewArtifact : createdNewArtifacts) {
1402                         if (artifactTemplate.getFileName().equalsIgnoreCase(createdNewArtifact.getArtifactName())) {
1403                             arifactsUids.add(createdNewArtifact.getUniqueId());
1404                             arifactsUuids.add(createdNewArtifact.getArtifactUUID());
1405                             isCreate = false;
1406                             String heatEnvId = checkAndGetHeatEnvId(createdNewArtifact);
1407                             if (!heatEnvId.isEmpty()) {
1408                                 arifactsUids.add(heatEnvId);
1409                             }
1410                             break;
1411                         }
1412                     }
1413                 }
1414                 if (isCreate) {
1415                     Either<ArtifactDefinition, ResponseFormat> createArtifactEither = createDeploymentArtifact(csarInfo, resource, ARTIFACTS_PATH,
1416                         artifactTemplate, createdNewArtifacts, labelCounter);
1417                     if (createArtifactEither.isRight()) {
1418                         resEither = Either.right(createArtifactEither.right().value());
1419                         return resEither;
1420                     }
1421                     ArtifactDefinition createdArtifact = createArtifactEither.left().value();
1422                     arifactsUids.add(createdArtifact.getUniqueId());
1423                     arifactsUuids.add(createdArtifact.getArtifactUUID());
1424                     final ArtifactTypeEnum artifactType = ArtifactTypeEnum.parse(createdArtifact.getArtifactType());
1425                     if (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_NET
1426                         || artifactType == ArtifactTypeEnum.HEAT_VOL) {
1427                         ArtifactDefinition createHeatEnvPlaceHolder = artifactsBusinessLogic
1428                             .createHeatEnvPlaceHolder(new ArrayList<>(), createdArtifact, ArtifactsBusinessLogic.HEAT_VF_ENV_NAME,
1429                                 resource.getUniqueId(), NodeTypeEnum.Resource, resource.getName(), csarInfo.getModifier(), resource, null);
1430                         String heatEnvId = createHeatEnvPlaceHolder.getUniqueId();
1431                         arifactsUids.add(heatEnvId);
1432                     }
1433                 }
1434             }
1435             if (arifactsUids != null && !arifactsUids.isEmpty()) {
1436                 List<String> artifactsToAssociate = new ArrayList<>();
1437                 artifactsToAssociate.addAll(arifactsUids);
1438                 GroupDefinition assotiateGroup = associateEntry.getKey();
1439                 assotiateGroup.getArtifacts().addAll(arifactsUids);
1440                 assotiateGroup.getArtifactsUuid().addAll(arifactsUuids);
1441             }
1442         }
1443         ComponentParametersView parametersView = new ComponentParametersView();
1444         parametersView.disableAll();
1445         parametersView.setIgnoreComponentInstances(false);
1446         parametersView.setIgnoreUsers(false);
1447         parametersView.setIgnoreArtifacts(false);
1448         parametersView.setIgnoreGroups(false);
1449         Either<Resource, StorageOperationStatus> eitherGerResource = toscaOperationFacade.getToscaElement(resource.getUniqueId(), parametersView);
1450         if (eitherGerResource.isRight()) {
1451             ResponseFormat responseFormat = componentsUtils
1452                 .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource,
1453                     resource.getComponentType());
1454             resEither = Either.right(responseFormat);
1455             return resEither;
1456         }
1457         resEither = Either.left(eitherGerResource.left().value());
1458         return resEither;
1459     }
1460
1461     private Either<ArtifactDefinition, ResponseFormat> updateDeploymentArtifactsFromCsar(CsarInfo csarInfo, Component resource,
1462                                                                                          ArtifactDefinition oldArtifact,
1463                                                                                          ArtifactTemplateInfo artifactTemplateInfo,
1464                                                                                          List<ArtifactDefinition> updatedArtifacts,
1465                                                                                          List<ArtifactTemplateInfo> updatedRequiredArtifacts) {
1466         String artifactFileName = artifactTemplateInfo.getFileName();
1467         // check if artifacts already exist
1468         for (ArtifactDefinition updatedArtifact : updatedArtifacts) {
1469             if (updatedArtifact.getArtifactName().equals(artifactFileName)) {
1470                 if (!updatedArtifact.getArtifactType().equalsIgnoreCase(artifactTemplateInfo.getType())) {
1471                     log.error("Artifact with name {} and type {} already updated with type  {}", artifactFileName, artifactTemplateInfo.getType(),
1472                         updatedArtifact.getArtifactType());
1473                     BeEcompErrorManager.getInstance()
1474                         .logInternalDataError(ARTIFACT_FILE_IS_NOT_IN_EXPECTED_FORMAT_FILE_NAME + artifactFileName, ARTIFACT_INTERNALS_ARE_INVALID,
1475                             ErrorSeverity.ERROR);
1476                     return Either.right(componentsUtils
1477                         .getResponseFormat(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, artifactFileName,
1478                             artifactTemplateInfo.getType(), updatedArtifact.getArtifactType()));
1479                 }
1480                 return Either.left(updatedArtifact);
1481             }
1482         }
1483         Either<ImmutablePair<String, byte[]>, ResponseFormat> artifactContententStatus = CsarValidationUtils
1484             .getArtifactContent(csarInfo.getCsarUUID(), csarInfo.getCsar(), CsarUtils.ARTIFACTS_PATH + artifactFileName, artifactFileName,
1485                 componentsUtils);
1486         if (artifactContententStatus.isRight()) {
1487             return Either.right(artifactContententStatus.right().value());
1488         }
1489         Map<String, Object> json = ArtifactUtils
1490             .buildJsonForUpdateArtifact(oldArtifact.getUniqueId(), artifactFileName, oldArtifact.getArtifactType(), ArtifactGroupTypeEnum.DEPLOYMENT,
1491                 oldArtifact.getArtifactLabel(), oldArtifact.getArtifactDisplayName(), oldArtifact.getDescription(),
1492                 artifactContententStatus.left().value().getRight(), updatedRequiredArtifacts, oldArtifact.getIsFromCsar());
1493         Either<Either<ArtifactDefinition, Operation>, ResponseFormat> uploadArtifactToService = createOrUpdateCsarArtifactFromJson(resource,
1494             csarInfo.getModifier(), json, new ArtifactOperationInfo(false, false, ArtifactOperationEnum.UPDATE));
1495         if (uploadArtifactToService.isRight()) {
1496             return Either.right(uploadArtifactToService.right().value());
1497         }
1498         ArtifactDefinition previousInfo = uploadArtifactToService.left().value().left().value();
1499         ArtifactDefinition currentInfo = uploadArtifactToService.left().value().left().value();
1500         updatedArtifacts.add(currentInfo);
1501         Either<ArtifactDefinition, ResponseFormat> updateEnvEither = updateHeatParamsFromCsar(resource, csarInfo, artifactTemplateInfo, currentInfo,
1502             true);
1503         if (updateEnvEither.isRight()) {
1504             log.error("failed to update parameters to artifact {}", artifactFileName);
1505             return Either.right(updateEnvEither.right().value());
1506         }
1507         artifactsBusinessLogic.updateGroupForHeat(previousInfo, updateEnvEither.left().value(), resource);
1508         updatedArtifacts.add(updateEnvEither.left().value());
1509         return Either.left(currentInfo);
1510     }
1511
1512     public Either<Resource, ResponseFormat> deleteVFModules(Resource resource, CsarInfo csarInfo, boolean shouldLock, boolean inTransaction) {
1513         Resource updatedResource = resource;
1514         List<GroupDefinition> groupsToDelete = updatedResource.getGroups();
1515         if (groupsToDelete != null && !groupsToDelete.isEmpty()) {
1516             List<GroupDefinition> vfGroupsToDelete = groupsToDelete.stream().filter(g -> g.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE))
1517                 .collect(Collectors.toList());
1518             if (!vfGroupsToDelete.isEmpty()) {
1519                 for (GroupDefinition gr : vfGroupsToDelete) {
1520                     List<String> artifacts = gr.getArtifacts();
1521                     for (String artifactId : artifacts) {
1522                         Either<ArtifactDefinition, ResponseFormat> handleDelete = artifactsBusinessLogic
1523                             .handleDelete(updatedResource.getUniqueId(), artifactId, csarInfo.getModifier(), updatedResource, shouldLock,
1524                                 inTransaction);
1525                         if (handleDelete.isRight()) {
1526                             log.error("Couldn't delete  artifact {}", artifactId);
1527                             return Either.right(handleDelete.right().value());
1528                         }
1529                     }
1530                 }
1531                 groupBusinessLogic.deleteGroups(updatedResource, vfGroupsToDelete);
1532                 Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(updatedResource.getUniqueId());
1533                 if (eitherGetResource.isRight()) {
1534                     ResponseFormat responseFormat = componentsUtils
1535                         .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), updatedResource);
1536                     return Either.right(responseFormat);
1537                 }
1538                 updatedResource = eitherGetResource.left().value();
1539             }
1540         }
1541         return Either.left(updatedResource);
1542     }
1543
1544     public Either<Service, ResponseFormat> deleteVFModules(Service resource, CsarInfo csarInfo, boolean shouldLock, boolean inTransaction) {
1545         Service updatedResource = resource;
1546         List<GroupDefinition> groupsToDelete = updatedResource.getGroups();
1547         if (groupsToDelete != null && !groupsToDelete.isEmpty()) {
1548             List<GroupDefinition> vfGroupsToDelete = groupsToDelete.stream().filter(g -> g.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE))
1549                 .collect(Collectors.toList());
1550             if (CollectionUtils.isNotEmpty(vfGroupsToDelete)) {
1551                 Either<Service, ResponseFormat> eitherGetResource = deleteVfGroups(vfGroupsToDelete, updatedResource, csarInfo, shouldLock,
1552                     inTransaction);
1553                 if (eitherGetResource.isRight()) {
1554                     return Either.right(eitherGetResource.right().value());
1555                 }
1556                 updatedResource = eitherGetResource.left().value();
1557             }
1558         }
1559         return Either.left(updatedResource);
1560     }
1561
1562     private Either<Service, ResponseFormat> deleteVfGroups(List<GroupDefinition> vfGroupsToDelete, Service resource, CsarInfo csarInfo,
1563                                                            boolean shouldLock, boolean inTransaction) {
1564         if (vfGroupsToDelete != null && !vfGroupsToDelete.isEmpty()) {
1565             for (GroupDefinition gr : vfGroupsToDelete) {
1566                 List<String> artifacts = gr.getArtifacts();
1567                 for (String artifactId : artifacts) {
1568                     Either<ArtifactDefinition, ResponseFormat> handleDelete = artifactsBusinessLogic
1569                         .handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), resource, shouldLock, inTransaction);
1570                     if (handleDelete.isRight()) {
1571                         log.error("Couldn't delete  artifact {}", artifactId);
1572                         return Either.right(handleDelete.right().value());
1573                     }
1574                 }
1575             }
1576             groupBusinessLogic.deleteGroups(resource, vfGroupsToDelete);
1577             Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId());
1578             if (eitherGetResource.isRight()) {
1579                 return Either.right(componentsUtils.getResponseFormatByComponent(
1580                     componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource, resource.getComponentType()));
1581             }
1582             return Either.left(eitherGetResource.left().value());
1583         }
1584         return Either.right(componentsUtils.getResponseFormatByComponent(ActionStatus.INVALID_CONTENT, resource, resource.getComponentType()));
1585     }
1586
1587     private Either<? extends Component, ResponseFormat> getResourcetFromGraph(Component component) {
1588         log.debug("getResource start");
1589         return toscaOperationFacade.getToscaElement(component.getUniqueId()).right().map(rf -> componentsUtils
1590             .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(rf), component, component.getComponentType())).left()
1591             .map(c -> c);
1592     }
1593 }