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