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