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