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