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