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