2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.csar;
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;
72 import java.util.Map.Entry;
73 import java.util.regex.Matcher;
74 import java.util.regex.Pattern;
75 import java.util.stream.Collectors;
77 import static org.openecomp.sdc.be.tosca.CsarUtils.ARTIFACTS_PATH;
80 @org.springframework.stereotype.Component("csarArtifactsAndGroupsBusinessLogic")
81 public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic {
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;
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;
110 public Either<Resource, ResponseFormat> createResourceArtifactsFromCsar(CsarInfo csarInfo, Resource resource,
111 String artifactsMetaFile, String artifactsMetaFileName, List<ArtifactDefinition> createdArtifacts,
112 boolean shouldLock, boolean inTransaction) {
114 log.debug("parseResourceArtifactsInfoFromFile start");
115 return parseResourceArtifactsInfoFromFile(resource, artifactsMetaFile, artifactsMetaFileName)
117 .bind( p-> createResourceArtifacts(csarInfo, resource, p, createdArtifacts,shouldLock, inTransaction))
119 .map(rf -> { componentsUtils.auditResource(rf, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE); return rf;})
121 .bind(this::getResourcetFromGraph);
125 public Either<Resource, ResponseFormat> updateResourceArtifactsFromCsar(CsarInfo csarInfo, Resource resource,
126 String artifactsMetaFile, String artifactsMetaFileName, List<ArtifactDefinition> createdNewArtifacts,
127 boolean shouldLock, boolean inTransaction){
129 Resource updatedResource = resource;
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);
139 List<GroupDefinition> groups = updatedResource.getGroups();
140 Map<String, ArtifactDefinition> deplymentArtifact = updatedResource.getDeploymentArtifacts();
141 if (deplymentArtifact == null || deplymentArtifact.isEmpty()) {
143 List<GroupDefinition> listToDelete = groups.stream().filter(g -> g.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)).collect(Collectors.toList());
144 groupBusinessLogic.deleteGroups(updatedResource, listToDelete);
146 return createResourceArtifacts(csarInfo, updatedResource, parseResourceInfoFromYamlEither.left().value(),
147 createdNewArtifacts, shouldLock, inTransaction);
150 List<ArtifactDefinition> createdDeplymentArtifactsAfterDelete = deplymentArtifact.values().stream().collect(Collectors.toList());
152 int labelCounter = createdDeplymentArtifactsAfterDelete.size();
155 ////////////////////////////////////// create set parsed
156 ////////////////////////////////////// artifacts///////////////////////////////////////////
157 Map<String, List<ArtifactTemplateInfo>> parsedArtifactsMap = parseResourceInfoFromYamlEither.left().value();
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);
165 Collection<List<ArtifactTemplateInfo>> parsedArifactsCollection = parsedArtifactsMap.values();
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());
174 Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup = parsedArtifactsPerGroupEither.left().value();
176 // find master in group
177 Map<GroupDefinition, Map<ArtifactDefinition, List<ArtifactDefinition>>> groupArtifact = findMasterArtifactInGroup(
178 groups, deplymentArtifact);
180 ///////////////////////////////// find artifacts to
181 ///////////////////////////////// delete////////////////////////////////////////////////////
184 Map<String, List<ArtifactDefinition>> groupToDelete = new HashMap<>();
185 Set<ArtifactDefinition> artifactsToDelete = findArtifactThatNotInGroupToDelete(parsedGroup, createdDeplymentArtifactsAfterDelete);
187 Set<ArtifactTemplateInfo> jsonMasterArtifacts = parsedGroup.keySet();
188 Map<GroupDefinition, MergedArtifactInfo> mergedgroup = mergeGroupInUpdateFlow(groupArtifact, parsedGroup,
189 artifactsToDelete, groupToDelete, jsonMasterArtifacts, createdDeplymentArtifactsAfterDelete);
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());
197 return Either.right(deletedArtifactsEither.right().value());
200 updatedResource = deletedArtifactsEither.left().value();
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;
212 if (artToRemove != null) {
213 createdDeplymentArtifactsAfterDelete.remove(artToRemove);
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());
228 return Either.right(assDissotiateEither.right().value());
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());
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());
248 List<GroupDefinition> heatGroups = null;
250 heatGroups = groups.stream().filter(e -> e.getMembers() != null).collect(Collectors.toList());
252 for (GroupDefinition updatedGroupDef : groupForAssociateWithMembers) {
254 if (updatedGroupDef.getMembers() != null && !updatedGroupDef.getMembers().isEmpty()) {
255 updatedGroupDef.getMembers().clear();
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);
271 //////////////// create new artifacts in update
272 //////////////// flow////////////////////////////
273 List<ArtifactTemplateInfo> newArtifactsGroup = new ArrayList<>();
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
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());
290 newArtifactsGroup.add(parsedArtifactMaster);
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());
302 Either<Resource, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, updatedResource,
303 newArtifactsGroup, createdNewArtifacts, createdDeplymentArtifactsAfterDelete, labelCounter,
304 shouldLock, inTransaction);
305 if (resStatus.isRight()) {
311 if (!groupForAssociateWithMembers.isEmpty()) {
313 List<GroupDefinition> groupsId = groupForAssociateWithMembers.stream().map(e -> e)
314 .collect(Collectors.toList());
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());
321 return Either.right(updateVersionEither.right().value());
325 if (artifactsWithoutGroups != null && !artifactsWithoutGroups.isEmpty()) {
326 for (ArtifactTemplateInfo t : artifactsWithoutGroups) {
327 List<ArtifactTemplateInfo> arrtifacts = new ArrayList<>();
329 Either<Resource, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, updatedResource,
330 arrtifacts, createdNewArtifacts, createdDeplymentArtifactsAfterDelete, labelCounter, shouldLock,
332 if (resStatus.isRight()) {
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);
345 return Either.right(responseFormat);
348 return Either.left(eitherGerResource.left().value());
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();
361 if(!op.get().getArtifactType().equalsIgnoreCase(template.getType())){
362 artifactsToDelete.add(op.get());
370 return artifactsToDelete;
374 private Either<Resource, ResponseFormat> createResourceArtifacts(CsarInfo csarInfo, Resource resource,
375 Map<String, List<ArtifactTemplateInfo>> artifactsMap,
376 List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction) {
378 Either<Resource, ResponseFormat> resStatus = Either.left(resource);
380 Collection<List<ArtifactTemplateInfo>> arifactsCollection = artifactsMap.values();
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()) {
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) {
402 Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>> parsedGroup = new HashMap<>();
404 for (List<ArtifactTemplateInfo> parsedGroupTemplateList : parsedArifactsCollection) {
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);
415 parsedGroup.put(parsedGroupTemplate, parsedArtifactsNames);
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());
430 return Either.left(parsedGroup);
433 @SuppressWarnings({ "unchecked", "static-access" })
434 public Either<Map<String, List<ArtifactTemplateInfo>>, ResponseFormat> parseResourceArtifactsInfoFromFile(
435 Resource resource, String artifactsMetaFile, String artifactFileName) {
438 JsonObject jsonElement = new JsonObject();
439 jsonElement = gson.fromJson(artifactsMetaFile, jsonElement.getClass());
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);
448 .right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName));
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);
459 .right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName));
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) {
467 Either <List<ArtifactTemplateInfo>, ResponseFormat> artifactTemplateInfoListEither = parseArtifactTemplateList(artifactFileName,
468 artifactTemplateMap, allGroups, artifactsTypeKey);
469 if(artifactTemplateInfoListEither.isRight()){
470 return Either.right(artifactTemplateInfoListEither.right().value());
472 artifactsMap.put(artifactsTypeKey, artifactTemplateInfoListEither.left().value());
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());
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));
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());
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);
512 componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName));
515 if (!artifactsTypeKey.equalsIgnoreCase(ArtifactTemplateInfo.CSAR_ARTIFACT)) {
516 allGroups.addAll(artifactTemplateInfoList);
518 return Either.left(artifactTemplateInfoList);
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,
528 if (artifacttemplateInfoStatus.isRight()) {
529 return Either.right(artifacttemplateInfoStatus.right().value());
532 ArtifactTemplateInfo artifacttemplateInfo = artifacttemplateInfoStatus.left().value();
533 if (artifacttemplateInfo != null) {
534 artifactTemplateInfoList.add(artifacttemplateInfo);
538 return Either.left(artifactTemplateInfoList);
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()) {
551 heatGroups = createdGroups.stream().filter(e -> e.getMembers() != null).collect(Collectors.toList());
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<>();
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()) {
567 if (groupName != null && !groupName.isEmpty()) {
569 Either<GroupDefinition, ResponseFormat> groupDefinitionEither = buildGroupDefinition(createdArtifacts, heatGroups, groupTemplateInfo,
570 groupName, artifactsGroup, artifactsUUIDGroup);
571 if (groupDefinitionEither.isRight()) {
572 return Either.right(groupDefinitionEither.right().value());
574 needToCreate.add(groupDefinitionEither.left().value());
578 ComponentParametersView componentParametersView = new ComponentParametersView();
579 componentParametersView.disableAll();
580 componentParametersView.setIgnoreUsers(false);
581 componentParametersView.setIgnoreArtifacts(false);
582 componentParametersView.setIgnoreGroups(false);
584 componentParametersView.setIgnoreComponentInstances(false);
586 Either<Resource, StorageOperationStatus> component = toscaOperationFacade
587 .getToscaElement(resource.getUniqueId(), componentParametersView);
589 if (component.isRight()) {
590 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
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());
599 return Either.left(component.left().value());
602 private Either<GroupDefinition, ResponseFormat> buildGroupDefinition(List<ArtifactDefinition> createdArtifacts,
603 List<GroupDefinition> heatGroups, ArtifactTemplateInfo groupTemplateInfo, String groupName,
604 Set<String> artifactsGroup, Set<String> artifactsUUIDGroup) {
606 Map<String, String> members = new HashMap<>();
608 associateMembersToArtifacts(createdArtifacts, null, heatGroups, artifactsGroup, members);
610 List<String> artifactsList = new ArrayList<>(artifactsGroup);
611 List<String> artifactsUUIDList = new ArrayList<>(artifactsUUIDGroup);
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);
619 if (!members.isEmpty()) {
620 groupDefinition.setMembers(members);
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())));
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);
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);
646 String artifactUid = "";
647 String artifactEnvUid = "";
648 String artifactUUID = "";
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());
656 ArtifactDefinition createdArtifact = createdArtifactEther.left().value();
657 if(createdArtifact == null){
659 Either<ArtifactDefinition, ResponseFormat> newArtifactEither = createDeploymentArtifact(csarInfo, resource,
660 artifactPath, artifactTemplateInfo, createdArtifacts, labelCounter);
661 if (newArtifactEither.isRight()) {
662 resStatus = Either.right(newArtifactEither.right().value());
665 ArtifactDefinition newArtifact = newArtifactEither.left().value();
666 artifactUid = newArtifact.getUniqueId();
667 artifactUUID = newArtifact.getArtifactUUID();
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());
679 artifactEnvUid = createHeatEnvPlaceHolder.left().value().getUniqueId();
682 artifactUid = createdArtifact.getUniqueId();
683 artifactUUID = createdArtifact.getArtifactUUID();
684 artifactEnvUid = checkAndGetHeatEnvId(createdArtifact);
686 artifactsGroup.add(artifactUid);
687 artifactsUUIDGroup.add(artifactUUID);
688 if (!artifactEnvUid.isEmpty()) {
689 artifactsGroup.add(artifactEnvUid);
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,
698 if (resStatus.isRight()) {
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;
713 return artifactEnvUid;
716 private Either<ArtifactDefinition, ResponseFormat> checkIfArtifactAlreadyExist(ArtifactTemplateInfo artifactTemplateInfo, List<ArtifactDefinition> createdArtifacts){
718 ArtifactDefinition res = null;
719 String artifactFileName = artifactTemplateInfo.getFileName();
720 Optional<ArtifactDefinition> op = createdArtifacts.stream().filter(a -> a.getArtifactName().equals(artifactFileName)).findAny();
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()));
734 return Either.left(res);
739 private Either<ArtifactDefinition, ResponseFormat> createDeploymentArtifact(CsarInfo csarInfo, Resource resource,
740 String artifactPath, ArtifactTemplateInfo artifactTemplateInfo, List<ArtifactDefinition> createdArtifacts,
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());
750 updatedlabel += createdArtifacts.size();
752 Map<String, Object> json = ArtifactUtils.buildJsonForArtifact(artifactTemplateInfo,
753 artifactContententStatus.left().value().getValue(), updatedlabel, true);
755 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> uploadArtifactToService = createOrUpdateCsarArtifactFromJson(
756 resource, csarInfo.getModifier(), json,
757 artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE));
759 if (uploadArtifactToService.isRight()) {
760 return Either.right(uploadArtifactToService.right().value());
763 ArtifactDefinition currentInfo = uploadArtifactToService.left().value().left().value();
764 if (currentInfo.getHeatParameters() != null) {
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());
773 currentInfo = updateEnvEither.left().value();
777 createdArtifacts.add(currentInfo);
779 return Either.left(currentInfo);
785 private Either<ArtifactDefinition, ResponseFormat> updateHeatParamsFromCsar(Resource resource, CsarInfo csarInfo,
786 ArtifactTemplateInfo artifactTemplateInfo, ArtifactDefinition currentInfo, boolean isUpdateEnv) {
788 Resource updatedResource = resource;
789 Either<ArtifactDefinition, ResponseFormat> resStatus = Either.left(currentInfo);
790 if (artifactTemplateInfo.getEnv() != null && !artifactTemplateInfo.getEnv().isEmpty()) {
792 Either<ImmutablePair<String, byte[]>, ResponseFormat> artifactparamsStatus = CsarValidationUtils
793 .getArtifactsContent(csarInfo.getCsarUUID(), csarInfo.getCsar(),
794 CsarUtils.ARTIFACTS_PATH + artifactTemplateInfo.getEnv(), artifactTemplateInfo.getEnv(),
796 if (artifactparamsStatus.isRight()) {
797 resStatus = Either.right(artifactparamsStatus.right().value());
800 Either<List<HeatParameterDefinition>, ResponseFormat> propsStatus = extractHeatParameters(
801 ArtifactTypeEnum.HEAT_ENV.getType(), artifactTemplateInfo.getEnv(),
802 artifactparamsStatus.left().value().getValue(), false);
804 if (propsStatus.isLeft()) {
805 List<HeatParameterDefinition> updatedHeatEnvParams = propsStatus.left().value();
806 resStatus = updateHeatParams(updatedResource, currentInfo, updatedHeatEnvParams);
807 if (resStatus.isRight()) {
814 ComponentParametersView parametersView = new ComponentParametersView();
815 parametersView.disableAll();
816 parametersView.setIgnoreComponentInstances(false);
817 parametersView.setIgnoreUsers(false);
818 parametersView.setIgnoreArtifacts(false);
819 parametersView.setIgnoreGroups(false);
821 Either<Resource, StorageOperationStatus> eitherGerResource = toscaOperationFacade
822 .getToscaElement(updatedResource.getUniqueId(), parametersView);
824 if (eitherGerResource.isRight()) {
825 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(
826 componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), updatedResource);
828 resStatus = Either.right(responseFormat);
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()))
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())));
850 resStatus = Either.left(updateArifactOnResource.left().value());
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);
868 .right(componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, fileName));
871 return Either.left(heatParameters.left().value());
875 private Either<ArtifactDefinition, ResponseFormat> updateHeatParams(Resource resource,
876 ArtifactDefinition currentInfo, List<HeatParameterDefinition> updatedHeatEnvParams) {
878 Either<ArtifactDefinition, ResponseFormat> resStatus = Either.left(currentInfo);
879 List<HeatParameterDefinition> currentHeatEnvParams = currentInfo.getListHeatParameters();
881 if (updatedHeatEnvParams != null && !updatedHeatEnvParams.isEmpty() && currentHeatEnvParams != null
882 && !currentHeatEnvParams.isEmpty()) {
885 for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) {
887 paramName = heatEnvParam.getName();
888 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
889 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
891 String updatedParamValue = heatEnvParam.getCurrentValue();
892 if (updatedParamValue == null) {
893 updatedParamValue = heatEnvParam.getDefaultValue();
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);
903 currHeatParam.setCurrentValue(
904 paramType.getConverter().convert(updatedParamValue, null, null));
910 currentInfo.setListHeatParameters(currentHeatEnvParams);
911 Either<ArtifactDefinition, StorageOperationStatus> updateArifactOnResource = artifactToscaOperation
912 .updateArtifactOnResource(currentInfo, resource.getUniqueId(), currentInfo.getUniqueId(),
914 if (updateArifactOnResource.isRight()) {
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())));
921 resStatus = Either.left(updateArifactOnResource.left().value());
929 public Either<Either<ArtifactDefinition, Operation>, ResponseFormat> createOrUpdateCsarArtifactFromJson(
930 Resource resource, User user, Map<String, Object> json, ArtifactOperationInfo operation) {
932 String jsonStr = gson.toJson(json);
934 String origMd5 = GeneralUtility.calculateMD5Base64EncodedByString(jsonStr);
935 ArtifactDefinition artifactDefinitionFromJson = RepresentationUtils.convertJsonToArtifactDefinition(jsonStr,
936 ArtifactDefinition.class);
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());
947 return Either.left(uploadArtifactToService.left().value());
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);
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()) {
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());
980 if (artifactsFromResource != null) {
981 opArt = artifactsFromResource.stream().filter(p -> p.getUniqueId().equals(artifactId))
983 if (opArt.isPresent()) {
984 artifacts.add(opArt.get());
988 Optional<ArtifactDefinition> resOp = artifacts.stream()
989 .filter(p -> heatFileNAme.contains(p.getArtifactName())).findAny();
990 if (resOp.isPresent()) {
991 members.putAll(heatGroup.getMembers());
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());
1005 prop.setValue(p.getValue().getForBaseModule());
1006 prop.setDefaultValue(p.getValue().getForBaseModule());
1008 prop.setValue(p.getValue().getForNonBaseModule());
1009 prop.setDefaultValue(p.getValue().getForNonBaseModule());
1011 properties.add(prop);
1014 GroupProperty proplabel = new GroupProperty();
1015 proplabel.setName("vf_module_label");
1017 Matcher matcher = pattern.matcher(moduleName);
1019 if (matcher.find()) {
1020 proplabel.setValue(matcher.group(1));
1021 proplabel.setDefaultValue(matcher.group(1));
1023 proplabel.setValue(moduleName);
1024 proplabel.setDefaultValue(moduleName);
1026 properties.add(proplabel);
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())) {
1040 propvolume.setValue(String.valueOf(isVolume));
1041 propvolume.setDefaultValue(String.valueOf(isVolume));
1042 properties.add(propvolume);
1043 mergeWithGroupTypeProperties(properties, groupType.getProperties());
1047 private void mergeWithGroupTypeProperties(List<GroupProperty> properties,
1048 List<PropertyDefinition> groupTypeProperties) {
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));
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
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);
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());
1083 if (CollectionUtils.isNotEmpty(group.getArtifactsUuid())
1084 && group.getArtifactsUuid().contains(artifactDefinition.getArtifactUUID())) {
1085 group.getArtifactsUuid().remove(artifactDefinition.getArtifactUUID());
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;
1104 if (isNeedToDeleteGroup) {
1105 groupToDelete.put(groupListEntry.getKey().getUniqueId(), listToDelete);
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();
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;
1137 if (generatedFromArt != null) {
1138 if (generatedFromArt.getArtifactName().equalsIgnoreCase(template.getFileName())
1139 && generatedFromArt.getArtifactType().equalsIgnoreCase(template.getType())) {
1140 isNeedToDeleteArtifact = false;
1148 if (isNeedToDeleteArtifact) {
1149 artifactsToDelete.add(artifact);
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<>();
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()) {
1164 ArtifactDefinition masterArtifact = ArtifactUtils.findMasterArtifact(deplymentArtifact, artifacts,
1166 if (masterArtifact != null) {
1167 gupsMap.put(masterArtifact, artifacts);
1169 groupArtifact.put(group, gupsMap);
1173 return groupArtifact;
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) {
1180 Resource updatedResource = resource;
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());
1195 deletedArtifacts.add(handleDelete.left().value().left().value());
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());
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());
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);
1226 return Either.right(responseFormat);
1229 updatedResource = eitherGerResource.left().value();
1230 updatedResource.setGroups(oldGroups);
1231 return Either.left(updatedResource);
1234 private void createArtifactsGroupSet(List<ArtifactTemplateInfo> parsedGroupTemplateList,
1235 Set<ArtifactTemplateInfo> parsedArtifactsName) {
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);
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) {
1251 Resource updatedResource = resource;
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());
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<>();
1266 resStatus = createDeploymentArtifactsFromCsar(csarInfo, updatedResource, artifactsGroup, artifactsUUIDGroup,
1267 groupTemplateInfo, createdNewArtifacts, artifactsFromResource, labelCounter, shouldLock,
1269 if (resStatus.isRight()) {
1272 if (groupName != null && !groupName.isEmpty()) {
1273 Map<String, String> members = new HashMap<>();
1274 associateMembersToArtifacts(createdNewArtifacts, artifactsFromResource, heatGroups, artifactsGroup,
1277 List<String> artifactsList = new ArrayList<>(artifactsGroup);
1278 List<String> artifactsUUIDList = new ArrayList<>(artifactsUUIDGroup);
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);
1286 if (!members.isEmpty()) {
1287 groupDefinition.setMembers(members);
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);
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())));
1305 properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties,
1306 createdArtifacts, artifactsList, getLatestGroupTypeRes.left().value());
1307 groupDefinition.convertFromGroupProperties(properties);
1309 needToAdd.add(groupDefinition);
1312 ComponentParametersView componentParametersView = new ComponentParametersView();
1313 componentParametersView.disableAll();
1314 componentParametersView.setIgnoreArtifacts(false);
1315 componentParametersView.setIgnoreGroups(false);
1316 componentParametersView.setIgnoreComponentInstances(false);
1318 Either<Resource, StorageOperationStatus> component = toscaOperationFacade
1319 .getToscaElement(updatedResource.getUniqueId(), componentParametersView);
1320 if (component.isRight()) {
1321 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1323 updatedResource = component.left().value();
1325 Either<List<GroupDefinition>, ResponseFormat> addGroups = groupBusinessLogic.addGroups(updatedResource, needToAdd, false);
1326 if (addGroups.isRight()) {
1327 return Either.right(addGroups.right().value());
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;
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()));
1360 alreadyExist = true;
1361 artifactEnvUid = checkAndGetHeatEnvId(artifactFromResource);
1368 if (!alreadyExist) {
1369 for (ArtifactDefinition createdArtifact : createdArtifacts) {
1370 if (createdArtifact.getArtifactName().equals(artifactFileName)) {
1371 artifactUid = createdArtifact.getUniqueId();
1372 artifactUUID = createdArtifact.getArtifactUUID();
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()));
1384 alreadyExist = true;
1385 artifactEnvUid = checkAndGetHeatEnvId(createdArtifact);
1391 // if not exist need to create
1392 if (!alreadyExist) {
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());
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());
1413 artifactEnvUid = createHeatEnvPlaceHolder.left().value().getUniqueId();
1417 artifactsGroup.add(artifactUid);
1418 artifactsUUIDGroup.add(artifactUUID);
1419 if (!artifactEnvUid.isEmpty()) {
1420 artifactsGroup.add(artifactEnvUid);
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,
1429 if (resStatus.isRight()) {
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());
1455 List<ArtifactTemplateInfo> newArtifactsInGroup = entry.getValue().getListToAssociateArtifactToGroup();
1456 if (newArtifactsInGroup != null && !newArtifactsInGroup.isEmpty()) {
1457 artifactsToAssotiate.put(entry.getKey(), newArtifactsInGroup);
1460 List<ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo>> artifactsToUpdate = entry.getValue()
1461 .getListToUpdateArtifactInGroup();
1462 if (artifactsToUpdate != null && !artifactsToUpdate.isEmpty()) {
1463 artifactsToUpdateMap.put(entry.getKey(), artifactsToUpdate);
1467 if (!artifactsToUpdateMap.isEmpty()) {
1468 List<ArtifactDefinition> updatedArtifacts = new ArrayList<>();
1469 for (Entry<GroupDefinition, List<ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo>>> artifactsToUpdateEntry : artifactsToUpdateMap
1471 List<ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo>> artifactsToUpdateList = artifactsToUpdateEntry
1473 GroupDefinition groupToUpdate = artifactsToUpdateEntry.getKey();
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());
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());
1495 Optional<ArtifactDefinition> op = updatedArtifacts.stream()
1496 .filter(p -> p.getGeneratedFromId() != null
1497 && p.getGeneratedFromId().equals(artAfterUpdate.getUniqueId()))
1499 if (op.isPresent()) {
1500 ArtifactDefinition artifactInfoHeatEnv = op.get();
1501 groupToUpdate.getArtifacts().remove(prevHeatEnvId);
1502 groupToUpdate.getArtifacts().add(artifactInfoHeatEnv.getUniqueId());
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
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());
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());
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());
1547 String heatEnvId = checkAndGetHeatEnvId(createdNewArtifact);
1548 if (!heatEnvId.isEmpty()) {
1549 arifactsUids.add(heatEnvId);
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());
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());
1576 String heatEnvId = createHeatEnvPlaceHolder.left().value().getUniqueId();
1577 arifactsUids.add(heatEnvId);
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);
1591 ComponentParametersView parametersView = new ComponentParametersView();
1592 parametersView.disableAll();
1593 parametersView.setIgnoreComponentInstances(false);
1594 parametersView.setIgnoreUsers(false);
1595 parametersView.setIgnoreArtifacts(false);
1596 parametersView.setIgnoreGroups(false);
1598 Either<Resource, StorageOperationStatus> eitherGerResource = toscaOperationFacade
1599 .getToscaElement(resource.getUniqueId(), parametersView);
1601 if (eitherGerResource.isRight()) {
1602 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(
1603 componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource);
1605 resEither = Either.right(responseFormat);
1609 resEither = Either.left(eitherGerResource.left().value());
1613 private Either<ArtifactDefinition, ResponseFormat> updateDeploymentArtifactsFromCsar(CsarInfo csarInfo,
1614 Resource resource, ArtifactDefinition oldArtifact, ArtifactTemplateInfo artifactTemplateInfo,
1615 List<ArtifactDefinition> updatedArtifacts, List<ArtifactTemplateInfo> updatedRequiredArtifacts) {
1617 Either<ArtifactDefinition, ResponseFormat> resStatus = null;
1618 String artifactFileName = artifactTemplateInfo.getFileName();
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()));
1634 resStatus = Either.left(updatedArtifact);
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());
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());
1653 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> uploadArtifactToService = createOrUpdateCsarArtifactFromJson(
1654 resource, csarInfo.getModifier(), json,
1655 artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactOperationEnum.UPDATE));
1657 if (uploadArtifactToService.isRight()) {
1658 resStatus = Either.right(uploadArtifactToService.right().value());
1661 ArtifactDefinition currentInfo = uploadArtifactToService.left().value().left().value();
1662 updatedArtifacts.add(currentInfo);
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());
1672 updatedArtifacts.add(updateEnvEither.left().value());
1673 resStatus = Either.left(currentInfo);
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());
1697 groupBusinessLogic.deleteGroups(updatedResource, vfGroupsToDelete);
1699 Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(updatedResource.getUniqueId());
1700 if (eitherGetResource.isRight()) {
1701 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), updatedResource);
1703 return Either.right(responseFormat);
1706 updatedResource = eitherGetResource.left().value();
1709 return Either.left(updatedResource);
1712 private Either<Resource, ResponseFormat> getResourcetFromGraph(Resource component){
1713 log.debug("getResource start");
1714 return toscaOperationFacade.getToscaElement(component.getUniqueId())
1716 .map(rf -> componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(rf), component))
1718 .map (c -> (Resource) c);