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