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