2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
19 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
23 package org.openecomp.sdc.be.components.impl;
25 import static org.openecomp.sdc.be.dao.api.ActionStatus.MISMATCH_BETWEEN_ARTIFACT_TYPE_AND_COMPONENT_TYPE;
27 import com.google.common.annotations.VisibleForTesting;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import fj.data.Either;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.math.BigDecimal;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
41 import java.util.Map.Entry;
42 import java.util.Objects;
43 import java.util.Optional;
45 import java.util.function.Function;
46 import java.util.function.Predicate;
47 import java.util.function.Supplier;
48 import java.util.stream.Collectors;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.xml.XMLConstants;
51 import javax.xml.parsers.ParserConfigurationException;
52 import javax.xml.parsers.SAXParserFactory;
53 import org.apache.commons.codec.binary.Base64;
54 import org.apache.commons.collections.CollectionUtils;
55 import org.apache.commons.collections.MapUtils;
56 import org.apache.commons.io.FilenameUtils;
57 import org.apache.commons.lang.ArrayUtils;
58 import org.apache.commons.lang.StringUtils;
59 import org.apache.commons.lang3.tuple.ImmutablePair;
60 import org.openecomp.sdc.be.components.ArtifactsResolver;
61 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
62 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
63 import org.openecomp.sdc.be.components.impl.artifact.ArtifactTypeToPayloadTypeSelector;
64 import org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum;
65 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
66 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
67 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
68 import org.openecomp.sdc.be.components.utils.ArtifactUtils;
69 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
70 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
71 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
72 import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils;
73 import org.openecomp.sdc.be.config.ArtifactConfiguration;
74 import org.openecomp.sdc.be.config.BeEcompErrorManager;
75 import org.openecomp.sdc.be.config.Configuration;
76 import org.openecomp.sdc.be.config.Configuration.ArtifactTypeConfig;
77 import org.openecomp.sdc.be.config.ConfigurationManager;
78 import org.openecomp.sdc.be.dao.api.ActionStatus;
79 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
80 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
81 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
82 import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition;
83 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
84 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
85 import org.openecomp.sdc.be.datatypes.elements.GroupInstanceDataDefinition;
86 import org.openecomp.sdc.be.datatypes.elements.HeatParameterDataDefinition;
87 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
88 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
89 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
90 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
91 import org.openecomp.sdc.be.info.ArtifactTemplateInfo;
92 import org.openecomp.sdc.be.model.ArtifactDefinition;
93 import org.openecomp.sdc.be.model.Component;
94 import org.openecomp.sdc.be.model.ComponentInstance;
95 import org.openecomp.sdc.be.model.ComponentParametersView;
96 import org.openecomp.sdc.be.model.GroupDefinition;
97 import org.openecomp.sdc.be.model.GroupInstance;
98 import org.openecomp.sdc.be.model.HeatParameterDefinition;
99 import org.openecomp.sdc.be.model.InterfaceDefinition;
100 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
101 import org.openecomp.sdc.be.model.LifecycleStateEnum;
102 import org.openecomp.sdc.be.model.Operation;
103 import org.openecomp.sdc.be.model.Resource;
104 import org.openecomp.sdc.be.model.Service;
105 import org.openecomp.sdc.be.model.User;
106 import org.openecomp.sdc.be.model.heat.HeatParameterType;
107 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
108 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
109 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
110 import org.openecomp.sdc.be.model.operations.StorageException;
111 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
112 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
113 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
114 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
115 import org.openecomp.sdc.be.model.operations.api.IHeatParametersOperation;
116 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
117 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
118 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
119 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
120 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
121 import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation;
122 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
123 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
124 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
125 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
126 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
127 import org.openecomp.sdc.be.servlets.RepresentationUtils;
128 import org.openecomp.sdc.be.tosca.CsarUtils;
129 import org.openecomp.sdc.be.tosca.ToscaError;
130 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
131 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
132 import org.openecomp.sdc.be.user.Role;
133 import org.openecomp.sdc.be.user.UserBusinessLogic;
134 import org.openecomp.sdc.be.utils.TypeUtils;
135 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
136 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
137 import org.openecomp.sdc.common.api.Constants;
138 import org.openecomp.sdc.common.datastructure.Wrapper;
139 import org.openecomp.sdc.common.log.wrappers.Logger;
140 import org.openecomp.sdc.common.util.GeneralUtility;
141 import org.openecomp.sdc.common.util.ValidationUtils;
142 import org.openecomp.sdc.common.util.YamlToObjectConverter;
143 import org.openecomp.sdc.exception.ResponseFormat;
144 import org.springframework.beans.factory.annotation.Autowired;
145 import org.xml.sax.InputSource;
146 import org.xml.sax.SAXException;
147 import org.xml.sax.SAXNotRecognizedException;
148 import org.xml.sax.SAXNotSupportedException;
149 import org.xml.sax.XMLReader;
150 import org.yaml.snakeyaml.Yaml;
152 @org.springframework.stereotype.Component("artifactBusinessLogic")
153 public class ArtifactsBusinessLogic extends BaseBusinessLogic {
154 private static final String RESOURCE_INSTANCE = "resource instance";
155 private static final String ARTIFACT_TYPE_OTHER = "OTHER";
156 private static final String ARTIFACT_DESCRIPTION = "artifact description";
157 private static final String ARTIFACT_LABEL = "artifact label";
158 private static final String ARTIFACT_URL = "artifact url";
159 private static final String ARTIFACT_NAME = "artifact name";
160 private static final String ARTIFACT_PAYLOAD = "artifact payload";
162 private static final String ARTIFACT_PLACEHOLDER_TYPE = "type";
163 private static final String ARTIFACT_PLACEHOLDER_DISPLAY_NAME = "displayName";
164 private static final Object ARTIFACT_PLACEHOLDER_DESCRIPTION = "description";
166 public static final String HEAT_ENV_NAME = "heatEnv";
167 public static final String HEAT_VF_ENV_NAME = "VfHeatEnv";
168 public static final String HEAT_ENV_SUFFIX = "env";
169 private static final String ARTIFACT_PLACEHOLDER_FILE_EXTENSION = "fileExtension";
171 private static final Logger log = Logger.getLogger(ArtifactsBusinessLogic.class.getName());
172 private static final String FAILED_UPDATE_GROUPS = "Failed to update groups of the component {}. ";
173 private static final String FAILED_SAVE_ARTIFACT = "Failed to save the artifact.";
174 public static final String ARTIFACT_ACTION_LOCK = "Artifact action - lock ";
175 public static final String FAILED_UPLOAD_ARTIFACT_TO_COMPONENT = "Failed to upload artifact to component with type {} and uuid {}. Status is {}. ";
176 private static final String FAILED_FETCH_COMPONENT = "Could not fetch component with type {} and uuid {}. Status is {}. ";
177 private static final String NULL_PARAMETER = "One of the function parameteres is null";
178 public static final String COMPONENT_INSTANCE_NOT_FOUND = "Component instance {} was not found for component {}";
179 private static final String ROLLBACK = "all changes rollback";
180 private static final String COMMIT = "all changes committed";
181 private static final String UPDATE_ARTIFACT = "Update Artifact";
182 private static final String FOUND_DEPLOYMENT_ARTIFACT = "Found deployment artifact {}";
183 private Gson gson = new GsonBuilder().setPrettyPrinting().create();
185 @javax.annotation.Resource
186 private IInterfaceLifecycleOperation interfaceLifecycleOperation;
187 @javax.annotation.Resource
188 private UserAdminOperation userOperaton;
190 @javax.annotation.Resource
191 private IElementOperation elementOperation;
193 @javax.annotation.Resource
194 private IHeatParametersOperation heatParametersOperation;
196 private ArtifactCassandraDao artifactCassandraDao;
197 private ToscaExportHandler toscaExportUtils;
198 private CsarUtils csarUtils;
199 private LifecycleBusinessLogic lifecycleBusinessLogic;
200 private UserBusinessLogic userBusinessLogic;
201 private ArtifactsResolver artifactsResolver;
202 private NodeTemplateOperation nodeTemplateOperation;
205 public ArtifactsBusinessLogic(ArtifactCassandraDao artifactCassandraDao, ToscaExportHandler toscaExportUtils,
206 CsarUtils csarUtils, LifecycleBusinessLogic lifecycleBusinessLogic, UserBusinessLogic userBusinessLogic,
207 ArtifactsResolver artifactsResolver, IElementOperation elementDao, IGroupOperation groupOperation,
208 IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation, InterfaceOperation interfaceOperation,
209 InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsOperations artifactToscaOperation) {
210 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
211 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
212 this.artifactCassandraDao = artifactCassandraDao;
213 this.toscaExportUtils = toscaExportUtils;
214 this.csarUtils = csarUtils;
215 this.lifecycleBusinessLogic = lifecycleBusinessLogic;
216 this.userBusinessLogic = userBusinessLogic;
217 this.artifactsResolver = artifactsResolver;
220 public enum ArtifactOperationEnum {
221 CREATE, UPDATE, DELETE, DOWNLOAD, LINK;
223 public static boolean isCreateOrLink(ArtifactOperationEnum operation) {
224 return operation == CREATE || operation == LINK;
229 public Either<ArtifactDefinition, Operation> handleArtifactRequest(String componentId, String userId, ComponentTypeEnum componentType,
230 ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo,
231 String origMd5, String originData, String interfaceName, String operationName,
232 String parentId, String containerComponentType, boolean shouldLock, boolean inTransaction) {
234 // step 1 - detect auditing type
235 AuditingActionEnum auditingAction = detectAuditingType(operation, origMd5);
236 // step 2 - check header
237 if (userId == null) {
238 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
239 log.debug("handleArtifactRequest - no HTTP_CSP_HEADER , component id {}", componentId);
240 handleAuditing(auditingAction, null, componentId, null, null, null, artifactId, responseFormat, componentType, null);
241 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
243 // step 3 - check user existence
244 // step 4 - check user's role
245 User user = validateUserExists(userId, auditingAction, componentId, artifactId, componentType, inTransaction);
246 validateUserRole(user, auditingAction, componentId, artifactId, componentType, operation);
249 // 5. check service/resource existence
250 // 6. check service/resource check out
251 // 7. user is owner of checkout state
252 Component component = null;
253 String realComponentId = componentType == ComponentTypeEnum.RESOURCE_INSTANCE ? parentId : componentId;
254 component = validateComponentExists(realComponentId, auditingAction, user, artifactId, componentType, containerComponentType);
255 validateWorkOnComponent(component, userId, auditingAction, user, artifactId, operation);
256 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
257 validateResourceInstanceById(component, componentId);
261 return validateAndHandleArtifact(componentId, componentType, operation, artifactId, artifactInfo, origMd5,
262 originData, interfaceName, operationName, user, component, shouldLock, inTransaction, true);
265 public Either<ArtifactDefinition, Operation> handleArtifactRequest(String componentId, String userId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo,
266 String origMd5, String originData, String interfaceName, String operationName, String parentId, String containerComponentType) {
267 return handleArtifactRequest(componentId, userId, componentType, operation, artifactId, artifactInfo, origMd5, originData, interfaceName, operationName, parentId, containerComponentType, true, false);
271 * This Method validates only the Artifact and does not validate user / role / component ect...<br>
272 * For regular usage use <br>
273 * {@link #handleArtifactRequest(String, String, ComponentTypeEnum, ArtifactOperationInfo, String, ArtifactDefinition, String, String, String, String, String, String)}
277 public Either<ArtifactDefinition, Operation> validateAndHandleArtifact(
278 String componentUniqueId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactUniqueId,
279 ArtifactDefinition artifactDefinition, String origMd5, String originData, String interfaceName,
280 String operationName, User user, Component component, boolean shouldLock, boolean inTransaction, boolean needUpdateGroup) {
281 AuditingActionEnum auditingAction = detectAuditingType(operation, origMd5);
282 artifactDefinition = validateArtifact(componentUniqueId, componentType, operation,
283 artifactUniqueId, artifactDefinition, auditingAction, user,
284 component, shouldLock, inTransaction);
287 Either<ArtifactDefinition, Operation> result = doAction(componentUniqueId, componentType, operation, artifactUniqueId, artifactDefinition,
288 origMd5, originData, interfaceName, operationName, auditingAction, user, component, shouldLock, inTransaction, needUpdateGroup);
289 //TODO: audit positive action
294 ArtifactDefinition validateArtifact(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, User user,
295 Component component, boolean shouldLock, boolean inTransaction) {
296 ArtifactDefinition artifactInfoToReturn = artifactInfo;
297 ArtifactOperationEnum operationEnum = operation.getArtifactOperationEnum();
298 if (operationEnum == ArtifactOperationEnum.UPDATE || operationEnum == ArtifactOperationEnum.DELETE || operationEnum == ArtifactOperationEnum.DOWNLOAD) {
299 ArtifactDefinition dbArtifact = getArtifactIfBelongsToComponent(componentId, componentType, artifactId, component);
300 if (operation.isDownload()) {
301 artifactInfoToReturn = dbArtifact;
302 handleHeatEnvDownload(componentId, componentType, user, component, dbArtifact, shouldLock, inTransaction);
305 return artifactInfoToReturn;
309 void handleHeatEnvDownload(String componentId, ComponentTypeEnum componentType, User user, Component component,
310 ArtifactDefinition artifactDefinition, boolean shouldLock, boolean inTransaction) {
312 if (artifactDefinition.getArtifactType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV.getType())
313 && ComponentTypeEnum.SERVICE == component.getComponentType()) {
314 ComponentInstance componentInstance = component.getComponentInstances()
316 .filter(p -> p.getUniqueId().equals(componentId))
319 if (componentInstance == null) {
320 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentId,
321 "instance", "Service", component.getName());
323 Map<String, ArtifactDefinition> deploymentArtifacts = componentInstance.getDeploymentArtifacts();
325 ArtifactDefinition heatEnvWithHeatParams = deploymentArtifacts.values()
327 .filter(p -> p.getUniqueId()
328 .equals(artifactDefinition.getUniqueId()))
331 Either<ArtifactDefinition, ResponseFormat> eitherGenerated = generateHeatEnvArtifact(heatEnvWithHeatParams,
332 componentType, component, componentInstance.getName(), user, componentId, shouldLock, inTransaction);
333 if (eitherGenerated.isRight()) {
334 throw new ByResponseFormatComponentException((eitherGenerated.right().value()));
339 private boolean artifactGenerationRequired(Component component, ArtifactDefinition artifactInfo) {
340 boolean needGenerate;
341 needGenerate = artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA && (component.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || component
342 .getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
343 needGenerate = needGenerate || (ComponentTypeEnum.RESOURCE == component.getComponentType() && (artifactInfo.getArtifactType()
344 .equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV
345 .getType()) || isAbstractVfcEmptyCsar((Resource) component, artifactInfo)));
349 private boolean isAbstractVfcEmptyCsar(Resource resource, ArtifactDefinition artifactInfo) {
350 return resource.isAbstract() && artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA && artifactInfo
352 .equals(ArtifactTypeEnum.TOSCA_CSAR.getType()) && StringUtils.isEmpty(artifactInfo.getArtifactChecksum());
355 public Either<ArtifactDefinition, Operation> generateAndSaveToscaArtifact(
356 ArtifactDefinition artifactDefinition, Component component,
357 User user, boolean isInCertificationRequest, boolean shouldLock, boolean inTransaction,
358 boolean fetchTemplatesFromDB) {
359 generateToscaArtifact(component, artifactDefinition, isInCertificationRequest, fetchTemplatesFromDB);
360 byte[] decodedPayload = artifactDefinition.getPayloadData();
361 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
362 artifactDefinition.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
363 return lockComponentAndUpdateArtifact(component.getUniqueId(), artifactDefinition, AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, artifactDefinition.getUniqueId(),
364 user, component.getComponentType(), component, decodedPayload, null, null, shouldLock, inTransaction);
367 private ArtifactDefinition generateToscaArtifact(Component parent, ArtifactDefinition artifactInfo, boolean isInCertificationRequest, boolean fetchTemplatesFromDB) {
368 log.debug("tosca artifact generation");
369 if (ArtifactTypeEnum.TOSCA_CSAR.getType().equals(artifactInfo.getArtifactType())) {
370 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(parent, fetchTemplatesFromDB, isInCertificationRequest);
371 if (generated.isRight()) {
372 ResponseFormat error = generated.right().value();
373 log.debug("Failed to generate tosca csar for component {} error {}", parent.getUniqueId(), error);
374 throw new ByResponseFormatComponentException(error);
376 artifactInfo.setPayload(generated.left().value());
380 Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
381 if (exportComponent.isRight()) {
382 ToscaError toscaError = exportComponent.right().value();
383 log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), toscaError);
384 ActionStatus status = componentsUtils.convertFromToscaError(toscaError);
385 throw new ByActionStatusComponentException(status);
387 log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
388 artifactInfo.setPayloadData(exportComponent.left().value().getMainYaml());
393 private Either<ArtifactDefinition, Operation> doAction(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo, String origMd5,
394 String originData, String interfaceName, String operationName, AuditingActionEnum auditingAction, User user, Component parent, boolean shouldLock, boolean inTransaction, boolean needUpdateGroup) {
395 if (interfaceName != null && operationName != null) {
396 interfaceName = interfaceName.toLowerCase();
397 operationName = operationName.toLowerCase();
400 lockComponent(componentType, artifactId, auditingAction, user, parent);
402 Either<ArtifactDefinition, Operation> result;
403 boolean operationSucceeded = false;
405 switch (operation.getArtifactOperationEnum()) {
407 if (artifactGenerationRequired(parent, artifactInfo)) {
408 result = Either.left(generateNotSavedArtifact(parent, artifactInfo));
410 result = Either.left(handleDownload(componentId, artifactId, componentType, parent));
414 result = Either.left(handleDeleteInternal(componentId, artifactId, componentType, parent));
417 result = handleUpdate(componentId, componentType, operation, artifactId, artifactInfo, null, origMd5, originData, interfaceName, operationName,
418 auditingAction, user, parent, needUpdateGroup);
421 result = handleCreate(componentId, artifactInfo, operation, auditingAction, user, componentType, parent, origMd5, originData, interfaceName,
425 result = Either.left(handleLink(componentId, artifactInfo, componentType, parent));
428 throw new UnsupportedOperationException("In ArtifactsBusinessLogic received illegal operation: " + operation.getArtifactOperationEnum());
430 operationSucceeded = true;
434 handleLockingAndCommit(parent, shouldLock, inTransaction, operationSucceeded);
438 private void lockComponent(ComponentTypeEnum componentType, String artifactId, AuditingActionEnum auditingAction, User user, Component parent) {
440 lockComponent(parent, ARTIFACT_ACTION_LOCK);
441 }catch (ComponentException e){
442 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, null, null, artifactId, e.getResponseFormat(),
443 componentType, null);
449 public Either<ArtifactDefinition, Operation> handleUpdate(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId,
450 ArtifactDefinition artifactInfo, byte[] decodedPayload, String origMd5, String originData, String interfaceName,
451 String operationName, AuditingActionEnum auditingAction, User user, Component parent,
452 boolean needUpdateGroup) {
453 Either<ArtifactDefinition, Operation> result;
454 validateArtifactType(artifactInfo);
455 final String artifactType = artifactInfo.getArtifactType();
456 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE &&
457 (ArtifactTypeEnum.HEAT.getType().equals(artifactType) ||
458 ArtifactTypeEnum.HEAT_VOL.getType().equals(artifactType) ||
459 ArtifactTypeEnum.HEAT_NET.getType().equals(artifactType) ||
460 ArtifactTypeEnum.HEAT_ENV.getType().equals(artifactType))) {
461 result = handleUpdateHeatEnvAndHeatMeta(componentId, artifactInfo, auditingAction, artifactId, user, componentType, parent, originData, origMd5, operation);
462 if (needUpdateGroup) {
463 ActionStatus error = updateGroupInstance(artifactInfo, result.left().value(), parent, componentId);
464 if (error != ActionStatus.OK) {
465 throw new ByActionStatusComponentException(error);
469 else if (componentType == ComponentTypeEnum.RESOURCE && ArtifactTypeEnum.HEAT_ENV.getType().equals(artifactType)) {
470 result = handleUpdateHeatWithHeatEnvParams(componentId, artifactInfo, auditingAction, componentType, parent, originData, origMd5, operation, needUpdateGroup);
473 if (decodedPayload == null) {
474 decodedPayload = validateInput(componentId, artifactInfo, operation, auditingAction, artifactId, user,
475 componentType, parent, origMd5, originData, interfaceName, operationName);
477 result = updateArtifactFlow(parent, componentId, artifactId, artifactInfo, decodedPayload, componentType, auditingAction, interfaceName, operationName);
478 if (needUpdateGroup && result.isLeft()) {
479 ArtifactDefinition updatedArtifact = result.left().value();
480 updateGroupForHeat(artifactInfo, updatedArtifact, parent);
486 private void validateArtifactType(final ArtifactDefinition artifactInfo) {
487 if (!isArtifactSupported(artifactInfo.getArtifactType())) {
488 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
492 private void validateArtifactType(final ArtifactDefinition artifactInfo,
493 final ComponentTypeEnum componentType) {
494 final ArtifactConfiguration artifactConfiguration =
495 loadArtifactTypeConfig(artifactInfo.getArtifactType()).orElse(null);
496 if (artifactConfiguration == null) {
497 BeEcompErrorManager.getInstance()
498 .logBeMissingArtifactInformationError("Artifact Update / Upload", "artifactLabel");
499 log.debug("Missing artifact type for artifact {}", artifactInfo.getArtifactName());
500 final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_TYPE);
501 throw new ByResponseFormatComponentException(responseFormat);
504 final ArtifactGroupTypeEnum artifactGroupType = artifactInfo.getArtifactGroupType();
506 validateArtifactType(componentType, artifactGroupType, artifactConfiguration);
507 } catch (final ComponentException e) {
508 log.debug("Artifact is invalid", e);
509 BeEcompErrorManager.getInstance()
510 .logBeInvalidTypeError("Artifact Upload / Delete / Update - Not supported artifact type", artifactInfo
511 .getArtifactType(), "Artifact " + artifactInfo.getArtifactName());
512 log.debug("Not supported artifact type = {}", artifactInfo.getArtifactType());
513 final ResponseFormat responseFormat = componentsUtils
514 .getResponseFormat(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo
516 throw new ByResponseFormatComponentException(responseFormat);
520 private void validateArtifactType(final ComponentTypeEnum componentType, final ArtifactGroupTypeEnum groupType,
521 final ArtifactConfiguration artifactConfiguration) {
522 final boolean supportComponentType =
523 CollectionUtils.isNotEmpty(artifactConfiguration.getComponentTypes()) &&
524 artifactConfiguration.getComponentTypes().stream()
525 .anyMatch(componentType1 -> componentType1.getValue().equalsIgnoreCase(componentType.getValue()));
526 if (!supportComponentType) {
527 log.debug("Artifact Type '{}' not supported for Component Type '{}'",
528 artifactConfiguration.getType(), componentType.getValue());
529 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED,
530 artifactConfiguration.getType());
533 final boolean supportResourceType = artifactConfiguration.hasSupport(groupType);
534 if (!supportResourceType) {
535 log.debug("Artifact Type '{}' not supported for Component Type '{}' and Category '{}'",
536 artifactConfiguration.getType(), componentType.getValue(), groupType.getType());
537 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED,
538 artifactConfiguration.getType());
542 private boolean isArtifactSupported(final String artifactType) {
543 final Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
544 final List<ArtifactConfiguration> artifactConfigurationList = configuration.getArtifacts();
545 if (CollectionUtils.isEmpty(artifactConfigurationList)) {
548 return artifactConfigurationList.stream()
549 .anyMatch(artifactConfiguration -> artifactConfiguration.getType().equalsIgnoreCase(artifactType));
553 public ActionStatus updateGroupForHeat(ArtifactDefinition artifactInfo, ArtifactDefinition artAfterUpdate, Component parent) {
554 List<GroupDefinition> groups = parent.getGroups();
555 if (groups != null && !groups.isEmpty()) {
556 List<GroupDataDefinition> groupToUpdate = groups.stream()
557 .filter(g -> g.getArtifacts() != null && g.getArtifacts()
558 .contains(artifactInfo
560 .collect(Collectors.toList());
561 if (groupToUpdate != null && !groupToUpdate.isEmpty()) {
562 groupToUpdate.forEach(g -> {
563 g.getArtifacts().remove(artifactInfo.getUniqueId());
564 g.getArtifactsUuid().remove(artifactInfo.getArtifactUUID());
565 g.getArtifacts().add(artAfterUpdate.getUniqueId());
566 g.getArtifactsUuid().add(artAfterUpdate.getArtifactUUID());
567 if(!artifactInfo.getArtifactUUID().equals(artAfterUpdate.getArtifactUUID())){
568 g.setGroupUUID(UniqueIdBuilder.generateUUID());
571 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, groupToUpdate);
572 if (status.isRight()) {
573 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
574 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status.right().value()));
578 return ActionStatus.OK;
582 ActionStatus updateGroupForHeat(ArtifactDefinition artifactInfoHeat, ArtifactDefinition artHeatAfterUpdate, ArtifactDefinition artifactInfoHeatE, ArtifactDefinition artHEAfterUpdate, Component parent) {
583 List<GroupDefinition> groups = parent.getGroups();
584 if (groups != null && !groups.isEmpty()) {
585 List<GroupDataDefinition> groupToUpdate = groups.stream()
586 .filter(g -> g.getArtifacts() != null && g.getArtifacts()
587 .contains(artifactInfoHeat
589 .collect(Collectors.toList());
590 if (groupToUpdate != null && !groupToUpdate.isEmpty()) {
591 groupToUpdate.forEach(g -> {
592 g.getArtifacts().remove(artifactInfoHeat.getUniqueId());
593 g.getArtifactsUuid().remove(artifactInfoHeat.getArtifactUUID());
594 g.getArtifacts().remove(artifactInfoHeatE.getUniqueId());
595 g.getArtifacts().add(artHeatAfterUpdate.getUniqueId());
596 g.getArtifactsUuid().add(artHeatAfterUpdate.getArtifactUUID());
597 g.getArtifacts().add(artHEAfterUpdate.getUniqueId());
599 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, groupToUpdate);
600 if (status.isRight()) {
601 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
602 return componentsUtils.convertFromStorageResponse(status.right().value());
606 return ActionStatus.OK;
609 private ActionStatus updateGroupInstance(ArtifactDefinition artifactInfo, ArtifactDefinition artAfterUpdate, Component parent, String parentId) {
610 List<GroupInstance> updatedGroupInstances = new ArrayList<>();
611 List<GroupInstance> groupInstances = null;
612 Optional<ComponentInstance> componentInstOp = parent.getComponentInstances()
614 .filter(ci -> ci.getUniqueId().equals(parentId))
616 if (componentInstOp.isPresent()) {
617 groupInstances = componentInstOp.get().getGroupInstances();
619 if (CollectionUtils.isNotEmpty(groupInstances)) {
620 boolean isUpdated = false;
621 for (GroupInstance groupInstance : groupInstances) {
623 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifacts()) && groupInstance.getGroupInstanceArtifacts()
624 .contains(artifactInfo
626 groupInstance.getGroupInstanceArtifacts().remove(artifactInfo.getUniqueId());
627 groupInstance.getGroupInstanceArtifacts().add(artAfterUpdate.getUniqueId());
630 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifactsUuid()) && groupInstance.getGroupInstanceArtifactsUuid()
631 .contains(artifactInfo
632 .getArtifactUUID())) {
633 groupInstance.getGroupInstanceArtifactsUuid().remove(artifactInfo.getArtifactUUID());
634 groupInstance.getGroupInstanceArtifacts().add(artAfterUpdate.getArtifactUUID());
638 updatedGroupInstances.add(groupInstance);
642 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(parent, parentId, updatedGroupInstances);
643 if (status.isRight()) {
644 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
645 return componentsUtils.convertFromStorageResponse(status.right().value());
647 return ActionStatus.OK;
650 ArtifactDefinition generateNotSavedArtifact(Component parent, ArtifactDefinition artifactInfo) {
651 if (artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA) {
652 return generateToscaArtifact(parent, artifactInfo, false, false);
655 String heatArtifactId = artifactInfo.getGeneratedFromId();
656 Either<ArtifactDefinition, StorageOperationStatus> heatRes = artifactToscaOperation.getArtifactById(parent.getUniqueId(), heatArtifactId);
657 if (heatRes.isRight()) {
658 log.debug("Failed to fetch heat artifact by generated id {} for heat env {}", heatArtifactId, artifactInfo.getUniqueId());
659 throw new StorageException(heatRes.right().value());
661 String generatedPayload = generateHeatEnvPayload(heatRes.left().value());
662 artifactInfo.setPayloadData(generatedPayload);
667 private Either<ArtifactDefinition, Operation> handleUpdateHeatWithHeatEnvParams(String componentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction,
668 ComponentTypeEnum componentType, Component parent, String originData, String origMd5, ArtifactOperationInfo operation,
669 boolean needToUpdateGroup) {
670 Either<ArtifactDefinition, StorageOperationStatus> artifactHeatRes = artifactToscaOperation.getArtifactById(componentId, artifactInfo
671 .getGeneratedFromId());
672 ArtifactDefinition currHeatArtifact = artifactHeatRes.left().value();
673 if (origMd5 != null) {
674 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
675 if (ArrayUtils.isNotEmpty(artifactInfo.getPayloadData())) {
676 handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
677 } else { // duplicate
678 throw new ByActionStatusComponentException(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
681 return updateHeatParams(componentId, artifactInfo, auditingAction, parent, componentType, currHeatArtifact, needToUpdateGroup);
684 private void handleLockingAndCommit(Component parent, boolean shouldLock, boolean inTransaction, boolean actionSucceeded) {
685 if (actionSucceeded) {
687 if (!inTransaction) {
688 janusGraphDao.commit();
692 if (!inTransaction) {
693 janusGraphDao.rollback();
697 graphLockOperation.unlockComponent(parent.getUniqueId(), parent.getComponentType().getNodeType());
701 public ImmutablePair<String, byte[]> handleDownloadToscaModelRequest(Component component, ArtifactDefinition csarArtifact) {
702 if (artifactGenerationRequired(component, csarArtifact)) {
703 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(component, false, false);
705 if (generated.isRight()) {
706 log.debug("Failed to export tosca csar for component {} error {}", component.getUniqueId(), generated.right()
708 throw new ByResponseFormatComponentException(generated.right().value());
710 return new ImmutablePair<>(csarArtifact.getArtifactName(), generated.left().value());
712 return downloadArtifact(csarArtifact);
715 public ImmutablePair<String, byte[]> handleDownloadRequestById(String componentId, String artifactId, String userId, ComponentTypeEnum componentType, String parentId, String containerComponentType) {
716 // perform all validation in common flow
717 Either<ArtifactDefinition, Operation> result = handleArtifactRequest(componentId, userId, componentType, new ArtifactOperationInfo(false, false, ArtifactOperationEnum.DOWNLOAD), artifactId, null, null, null, null,
718 null, parentId, containerComponentType);
719 ArtifactDefinition artifactDefinition;
720 Either<ArtifactDefinition, Operation> insideValue = result;
721 if (insideValue.isLeft()) {
722 artifactDefinition = insideValue.left().value();
725 artifactDefinition = insideValue.right().value().getImplementationArtifact();
727 // for tosca artifacts and heat env on VF level generated on download without saving
728 if (artifactDefinition.getPayloadData() != null) {
729 return (new ImmutablePair<>(artifactDefinition.getArtifactName(), artifactDefinition
732 return downloadArtifact(artifactDefinition);
735 public Map<String, ArtifactDefinition> handleGetArtifactsByType(String containerComponentType, String parentId, ComponentTypeEnum componentType, String componentId, String artifactGroupType, String userId) {
737 // detect auditing type
738 Map<String, ArtifactDefinition> resMap = null;
743 if (userId == null) {
744 log.debug("handleGetArtifactsByType - no HTTP_CSP_HEADER , component id {}", componentId);
745 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
748 // check user existence
752 validateUserExists(userId);
754 // 5. check service/resource existence
755 // 6. check service/resource check out
756 // 7. user is owner of checkout state
757 String realComponentId = componentType == ComponentTypeEnum.RESOURCE_INSTANCE ? parentId : componentId;
758 ComponentParametersView componentFilter = new ComponentParametersView();
759 componentFilter.disableAll();
760 componentFilter.setIgnoreArtifacts(false);
761 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
762 componentFilter.setIgnoreComponentInstances(false);
765 Component component = validateComponentExistsByFilter(realComponentId, ComponentTypeEnum
766 .findByParamName(containerComponentType), componentFilter);
767 lockComponent(component, ARTIFACT_ACTION_LOCK);
768 boolean failed = false;
770 ArtifactGroupTypeEnum groupType = ArtifactGroupTypeEnum.findType(artifactGroupType);
772 if (groupType == null) {
773 log.debug("handleGetArtifactsByType - not failed groupType {} , component id {}", artifactGroupType, componentId);
774 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
776 if (groupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
777 List<ArtifactDefinition> list = getDeploymentArtifacts(component, componentId);
778 if (list != null && !list.isEmpty()) {
779 resMap = list.stream().collect(Collectors.toMap(ArtifactDataDefinition::getArtifactLabel, Function.identity()));
782 resMap = new HashMap<>();
787 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsMapStatus = getArtifacts(realComponentId, componentType
788 .getNodeType(), groupType, componentId);
789 if (artifactsMapStatus.isRight()) {
790 if (artifactsMapStatus.right().value() != StorageOperationStatus.NOT_FOUND) {
791 log.debug("handleGetArtifactsByType - not failed groupType {} , component id {}", artifactGroupType, componentId);
792 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
795 resMap = new HashMap<>();
799 resMap = artifactsMapStatus.left().value();
803 }catch (ComponentException e){
810 janusGraphDao.rollback();
814 janusGraphDao.commit();
817 componentType = component.getComponentType();
818 NodeTypeEnum nodeType = componentType.getNodeType();
819 graphLockOperation.unlockComponent(component.getUniqueId(), nodeType);
824 private ArtifactDefinition getArtifactIfBelongsToComponent(String componentId, ComponentTypeEnum componentType, String artifactId, Component component) {
825 // check artifact existence
826 Either<ArtifactDefinition, StorageOperationStatus> artifactResult = artifactToscaOperation.getArtifactById(componentId, artifactId,
827 componentType, component.getUniqueId());
828 if (artifactResult.isRight()) {
829 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_ARTIFACT_NOT_FOUND, artifactId, componentId);
831 // verify artifact belongs to component
833 switch (componentType) {
836 found = checkArtifactInComponent(component, artifactId);
838 case RESOURCE_INSTANCE:
839 found = checkArtifactInResourceInstance(component, componentId, artifactId);
845 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_ARTIFACT_NOT_FOUND, artifactId, componentType.name().toLowerCase());
847 return artifactResult.left().value();
850 private Either<ArtifactDefinition, Operation> handleCreate(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction, User user, ComponentTypeEnum componentType,
851 Component parent, String origMd5, String originData, String interfaceType, String operationName) {
852 byte[] decodedPayload = validateInput(componentId, artifactInfo, operation, auditingAction, null, user, componentType, parent, origMd5, originData, interfaceType, operationName);
853 return createArtifact(parent, componentId, artifactInfo, decodedPayload, componentType, auditingAction, interfaceType, operationName);
856 private ArtifactDefinition handleLink(String componentId, ArtifactDefinition artifactInfo, ComponentTypeEnum componentType,
858 ComponentInstance foundInstance = findComponentInstance(componentId, parent);
859 String instanceId = null;
860 if (foundInstance != null) {
861 instanceId = foundInstance.getUniqueId();
863 NodeTypeEnum nodeType = convertParentType(componentType);
864 Either<ArtifactDefinition, StorageOperationStatus> artifactDefinitionEither = artifactToscaOperation.addArtifactToComponent(artifactInfo, parent,
865 nodeType, true, instanceId);
866 if (artifactDefinitionEither.isRight()) {
867 throw new StorageException(artifactDefinitionEither.right().value(), artifactInfo.getArtifactDisplayName());
869 if (generateCustomizationUUIDOnInstance(parent.getUniqueId(), componentId, componentType) != StorageOperationStatus.OK) {
870 throw new StorageException(artifactDefinitionEither.right().value(), artifactInfo.getArtifactDisplayName());
872 return artifactDefinitionEither.left().value();
875 private Either<ArtifactDefinition, Operation> lockComponentAndUpdateArtifact(
876 String parentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, String artifactId,
877 User user, ComponentTypeEnum componentType, Component parent, byte[] decodedPayload, String interfaceType,
878 String operationName, boolean shouldLock, boolean inTransaction) {
880 Either<ArtifactDefinition, Operation> resultOp = null;
881 boolean failed = false;
882 boolean writeAudit = true;
884 lockComponent(parent, shouldLock, ARTIFACT_ACTION_LOCK);
886 resultOp = updateArtifactFlow(parent, parentId, artifactId, artifactInfo, decodedPayload, componentType, auditingAction, interfaceType, operationName);
889 catch (ComponentException ce) {
891 handleAuditing(auditingAction, parent, parentId, user, null, null, artifactId, ce.getResponseFormat(), componentType, null);
896 catch (StorageException se) {
903 unlockComponent(failed, parent, inTransaction);
908 private byte[] validateInput(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction, String artifactId, User user, ComponentTypeEnum componentType,
909 Component parent, String origMd5, String originData, String interfaceType, String operationName) {
910 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
911 return getValidPayload(componentId, artifactInfo, operation, auditingAction, artifactId, user, componentType, parent, interfaceType, operationName);
914 private byte[] getValidPayload(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction,
915 String artifactId, User user, ComponentTypeEnum componentType, Component parent, String interfaceType, String operationName) {
917 Either<ArtifactDefinition, ResponseFormat> validateResult = validateInput(componentId, artifactInfo, operation, artifactId, user, interfaceType, operationName, componentType, parent);
918 if (validateResult.isRight()) {
919 ResponseFormat responseFormat = validateResult.right().value();
920 handleAuditing(auditingAction, parent, componentId, user, null, null, artifactId, responseFormat, componentType, null);
921 throw new ByResponseFormatComponentException(responseFormat);
924 Either<byte[], ResponseFormat> payloadEither = handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
925 if (payloadEither.isRight()) {
926 ResponseFormat responseFormat = payloadEither.right().value();
927 handleAuditing(auditingAction, parent, componentId, user, null, null, artifactId, responseFormat, componentType, null);
928 log.debug("Error during handle payload");
929 throw new ByResponseFormatComponentException(responseFormat);
931 // validate heat parameters. this part must be after the parameters are
932 // extracted in "handlePayload"
933 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParameters = validateAndConvertHeatParameters(artifactInfo, artifactInfo
935 if (validateAndConvertHeatParameters.isRight()) {
936 ResponseFormat responseFormat = validateAndConvertHeatParameters.right().value();
937 handleAuditing(auditingAction, parent, componentId, user, artifactInfo, null, artifactId, responseFormat, componentType, null);
938 log.debug("Error during handle payload");
939 throw new ByResponseFormatComponentException(responseFormat);
941 return payloadEither.left().value();
944 public void handleAuditing(AuditingActionEnum auditingActionEnum, Component component, String componentId, User user,
945 ArtifactDefinition artifactDefinition, String prevArtifactUuid, String currentArtifactUuid,
946 ResponseFormat responseFormat, ComponentTypeEnum componentTypeEnum, String resourceInstanceName) {
948 if (componentsUtils.isExternalApiEvent(auditingActionEnum)) {
954 user.setUserId("UNKNOWN");
956 handleInternalAuditEvent(auditingActionEnum, component, componentId, user, artifactDefinition, prevArtifactUuid, currentArtifactUuid, responseFormat, componentTypeEnum, resourceInstanceName);
959 private void handleInternalAuditEvent(AuditingActionEnum auditingActionEnum, Component component, String componentId, User user, ArtifactDefinition artifactDefinition, String prevArtifactUuid, String currentArtifactUuid, ResponseFormat responseFormat, ComponentTypeEnum componentTypeEnum, String resourceInstanceName) {
960 switch (componentTypeEnum) {
962 Resource resource = (Resource) component;
963 if (resource == null) {
964 // In that case, component ID should be instead of name
965 resource = new Resource();
966 resource.setName(componentId);
968 componentsUtils.auditResource(responseFormat, user, resource, resource.getName(), auditingActionEnum,
969 ResourceVersionInfo.newBuilder()
970 .artifactUuid(prevArtifactUuid)
971 .build(), currentArtifactUuid, artifactDefinition);
975 Service service = (Service) component;
976 if (service == null) {
977 // In that case, component ID should be instead of name
978 service = new Service();
979 service.setName(componentId);
981 componentsUtils.auditComponent(responseFormat, user, service, auditingActionEnum, new ResourceCommonInfo(ComponentTypeEnum.SERVICE.getValue()),
982 ResourceVersionInfo.newBuilder()
983 .artifactUuid(prevArtifactUuid)
985 ResourceVersionInfo.newBuilder()
986 .artifactUuid(currentArtifactUuid)
988 null, artifactDefinition, null);
991 case RESOURCE_INSTANCE:
992 if (resourceInstanceName == null) {
993 resourceInstanceName = getResourceInstanceNameFromComponent(component, componentId);
995 componentsUtils.auditComponent(responseFormat, user, component, auditingActionEnum,
996 new ResourceCommonInfo(resourceInstanceName, ComponentTypeEnum.RESOURCE_INSTANCE.getValue()),
997 ResourceVersionInfo.newBuilder()
998 .artifactUuid(prevArtifactUuid)
1000 ResourceVersionInfo.newBuilder()
1001 .artifactUuid(currentArtifactUuid)
1003 null, artifactDefinition, null);
1010 private String getResourceInstanceNameFromComponent(Component component, String componentId) {
1011 ComponentInstance resourceInstance = component.getComponentInstances()
1013 .filter(p -> p.getUniqueId().equals(componentId))
1016 String resourceInstanceName = null;
1017 if (resourceInstance != null) {
1018 resourceInstanceName = resourceInstance.getName();
1020 return resourceInstanceName;
1023 private void validateMd5(String origMd5, String originData, byte[] payload, ArtifactOperationInfo operation) {
1024 if (origMd5 == null) {
1025 if (operation.isCreateOrLink() && ArrayUtils.isNotEmpty(payload)) {
1026 log.debug("Missing md5 header during artifact create");
1027 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_MD5);
1030 if (ArrayUtils.isNotEmpty(payload)) {
1031 log.debug("Cannot have payload while md5 header is missing");
1032 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
1035 String encodeBase64Str = GeneralUtility.calculateMD5Base64EncodedByString(originData);
1036 if (!encodeBase64Str.equals(origMd5)) {
1037 log.debug("The calculated md5 is different then the received one");
1038 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_MD5);
1043 private Either<ArtifactDefinition, ResponseFormat> validateInput(final String componentId,
1044 final ArtifactDefinition artifactInfo,
1045 final ArtifactOperationInfo operation,
1046 final String artifactId, final User user,
1047 String interfaceName,
1048 String operationName,
1049 final ComponentTypeEnum componentType,
1050 final Component parentComponent) {
1052 final ArtifactDefinition existingArtifactInfo =
1053 findArtifact(parentComponent, componentType, componentId, operation, artifactId);
1054 final boolean isCreateOrLinkOperation =
1055 ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum());
1056 if (!isCreateOrLinkOperation && existingArtifactInfo == null) {
1057 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactId);
1059 final Component component;
1060 if (parentComponent.getUniqueId().equals(componentId)) {
1061 component = parentComponent;
1063 final ComponentInstance componentInstance = findComponentInstance(componentId, parentComponent);
1064 component = findComponent(componentInstance.getComponentUid());
1065 component.setComponentType(componentType);
1067 if (!isCreateOrLinkOperation) {
1068 ignoreUnupdateableFieldsInUpdate(operation, artifactInfo, existingArtifactInfo);
1070 if (isInformationalArtifact(artifactInfo)) {
1071 validateInformationalArtifact(artifactInfo, component);
1073 Either<Boolean, ResponseFormat> validateAndSetArtifactname = validateAndSetArtifactName(artifactInfo);
1074 if (validateAndSetArtifactname.isRight()) {
1075 return Either.right(validateAndSetArtifactname.right().value());
1077 if (!validateArtifactNameUniqueness(componentId, parentComponent, artifactInfo, componentType)) {
1078 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_EXIST));
1080 if (operationName != null && interfaceName != null) {
1081 operationName = operationName.toLowerCase();
1082 interfaceName = interfaceName.toLowerCase();
1084 Either<ActionStatus, ResponseFormat> logicalNameStatus = handleArtifactLabel(componentId, parentComponent, operation, artifactInfo, operationName, componentType);
1085 if (logicalNameStatus.isRight()) {
1086 return Either.right(logicalNameStatus.right().value());
1088 // This is a patch to block possibility of updating service api fields
1089 // through other artifacts flow
1091 final ArtifactGroupTypeEnum artifactGroupType =
1092 operationName != null ? ArtifactGroupTypeEnum.LIFE_CYCLE : ArtifactGroupTypeEnum.INFORMATIONAL;
1093 if (operation.isNotCreateOrLink()) {
1094 checkAndSetUnUpdatableFields(user, artifactInfo, existingArtifactInfo, artifactGroupType);
1096 checkCreateFields(user, artifactInfo, artifactGroupType);
1099 composeArtifactId(componentId, artifactId, artifactInfo, interfaceName, operationName);
1100 if (existingArtifactInfo != null) {
1101 artifactInfo.setMandatory(existingArtifactInfo.getMandatory());
1102 if (operation.isNotCreateOrLink()) {
1103 validateArtifactTypeNotChanged(artifactInfo, existingArtifactInfo);
1107 // artifactGroupType is not allowed to be updated
1108 if (operation.isNotCreateOrLink()) {
1109 Either<ArtifactDefinition, ResponseFormat> validateGroupType = validateOrSetArtifactGroupType(artifactInfo, existingArtifactInfo);
1110 if (validateGroupType.isRight()) {
1111 return Either.right(validateGroupType.right().value());
1115 setArtifactTimeout(artifactInfo, existingArtifactInfo);
1116 if (isHeatArtifact(artifactInfo)) {
1117 validateHeatArtifact(parentComponent, componentId, artifactInfo);
1119 if (isDeploymentArtifact(artifactInfo)) {
1120 if (componentType != ComponentTypeEnum.RESOURCE_INSTANCE) {
1121 final String artifactName = artifactInfo.getArtifactName();
1122 if (operation.isCreateOrLink() || !artifactName.equalsIgnoreCase(existingArtifactInfo.getArtifactName())) {
1123 validateSingleDeploymentArtifactName(artifactName, parentComponent);
1126 validateDeploymentArtifact(artifactInfo, component);
1129 Either<Boolean, ResponseFormat> descriptionResult = validateAndCleanDescription(artifactInfo);
1130 if (descriptionResult.isRight()) {
1131 return Either.right(descriptionResult.right().value());
1134 validateArtifactType(artifactInfo, component.getComponentType());
1135 artifactInfo.setArtifactType(artifactInfo.getArtifactType().toUpperCase());
1136 if (existingArtifactInfo != null && existingArtifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.SERVICE_API) {
1137 // Change of type is not allowed and should be ignored
1139 artifactInfo.setArtifactType(ARTIFACT_TYPE_OTHER);
1141 Either<Boolean, ResponseFormat> validateUrl = validateAndServiceApiUrl(artifactInfo);
1142 if (validateUrl.isRight()) {
1143 return Either.right(validateUrl.right().value());
1146 Either<Boolean, ResponseFormat> validateUpdate = validateFirstUpdateHasPayload(artifactInfo, existingArtifactInfo);
1147 if (validateUpdate.isRight()) {
1148 log.debug("serviceApi first update cnnot be without payload.");
1149 return Either.right(validateUpdate.right().value());
1152 if (artifactInfo.getApiUrl() != null) {
1153 artifactInfo.setApiUrl(null);
1154 log.error("Artifact URL cannot be set through this API - ignoring");
1157 if (Boolean.TRUE.equals(artifactInfo.getServiceApi())) {
1158 artifactInfo.setServiceApi(false);
1159 log.error("Artifact service API flag cannot be changed - ignoring");
1163 return Either.left(artifactInfo);
1166 private Component findComponent(final String componentId) {
1167 Either<? extends Component, StorageOperationStatus> component = toscaOperationFacade.getToscaFullElement(componentId);
1168 if (component.isRight()) {
1169 log.debug("Component '{}' not found ", componentId);
1170 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, componentId);
1173 return component.left().value();
1176 private void ignoreUnupdateableFieldsInUpdate(final ArtifactOperationInfo operation,
1177 final ArtifactDefinition artifactInfo,
1178 final ArtifactDefinition currentArtifactInfo) {
1179 if (operation.isUpdate()) {
1180 artifactInfo.setArtifactType(currentArtifactInfo.getArtifactType());
1181 artifactInfo.setArtifactGroupType(currentArtifactInfo.getArtifactGroupType());
1182 artifactInfo.setArtifactLabel(currentArtifactInfo.getArtifactLabel());
1186 private ArtifactDefinition findArtifact(final Component parentComponent, final ComponentTypeEnum componentType,
1187 final String parentId, final ArtifactOperationInfo operation,
1188 final String artifactId) {
1189 ArtifactDefinition foundArtifact = null;
1190 if (StringUtils.isNotEmpty(artifactId)) {
1191 foundArtifact = findArtifact(parentComponent, componentType, parentId, artifactId);
1193 if (foundArtifact != null && operation.isCreateOrLink()) {
1194 log.debug("Artifact {} already exist", artifactId);
1195 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_EXIST, foundArtifact.getArtifactLabel());
1197 if (foundArtifact == null && operation.isNotCreateOrLink()) {
1198 log.debug("The artifact {} was not found on parent component or instance {}. ", artifactId, parentId);
1199 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, "");
1201 return foundArtifact;
1204 private ArtifactDefinition findArtifact(Component parentComponent, ComponentTypeEnum componentType, String parentId, String artifactId) {
1205 ArtifactDefinition foundArtifact;
1206 if (parentComponent.getUniqueId().equals(parentId)) {
1207 foundArtifact = artifactsResolver.findArtifactOnComponent(parentComponent, componentType, artifactId);
1210 ComponentInstance instance = findComponentInstance(parentId, parentComponent);
1211 foundArtifact = artifactsResolver.findArtifactOnComponentInstance(instance, artifactId);
1213 return foundArtifact;
1216 private void validateInformationalArtifact(final ArtifactDefinition artifactInfo, final Component component) {
1217 final ArtifactGroupTypeEnum groupType = artifactInfo.getArtifactGroupType();
1218 if (groupType != ArtifactGroupTypeEnum.INFORMATIONAL) {
1221 final ComponentTypeEnum parentComponentType = component.getComponentType();
1222 final String artifactType = artifactInfo.getArtifactType();
1223 final ArtifactConfiguration artifactConfiguration = loadArtifactTypeConfig(artifactType).orElse(null);
1224 if (artifactConfiguration == null) {
1225 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactType);
1227 validateArtifactType(parentComponentType, artifactInfo.getArtifactGroupType(), artifactConfiguration);
1229 if (component.getComponentType() == ComponentTypeEnum.RESOURCE ||
1230 component.getComponentType() == ComponentTypeEnum.RESOURCE_INSTANCE) {
1232 final ResourceTypeEnum resourceType = ((Resource) component).getResourceType();
1233 validateResourceType(resourceType, artifactInfo, artifactConfiguration.getResourceTypes());
1235 validateArtifactExtension(artifactConfiguration, artifactInfo);
1238 private NodeTypeEnum convertParentType(ComponentTypeEnum componentType) {
1239 if (componentType == ComponentTypeEnum.RESOURCE) {
1240 return NodeTypeEnum.Resource;
1242 else if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1243 return NodeTypeEnum.ResourceInstance;
1246 return NodeTypeEnum.Service;
1250 // This method is here for backward compatibility - when other parts of the code are cleaned can change to use the internal version
1251 public Either<ArtifactDefinition, ResponseFormat> handleDelete(
1252 String parentId, String artifactId, User user, Component parent,
1253 boolean shouldLock, boolean inTransaction) {
1255 ResponseFormat responseFormat;
1256 boolean operationSucceeded = false;
1258 lockComponent(ComponentTypeEnum.RESOURCE, artifactId, AuditingActionEnum.ARTIFACT_DELETE, user, parent);
1261 ArtifactDefinition artifactDefinition = handleDeleteInternal(parentId, artifactId,
1262 ComponentTypeEnum.RESOURCE, parent);
1263 operationSucceeded = true;
1264 return Either.left(artifactDefinition);
1266 catch (ComponentException ce) {
1267 responseFormat = componentsUtils.getResponseFormat(ce);
1268 handleAuditing(AuditingActionEnum.ARTIFACT_DELETE, parent, parentId, user, null, null,
1269 artifactId, responseFormat, ComponentTypeEnum.RESOURCE, null);
1270 return Either.right(responseFormat);
1272 catch (StorageException se) {
1273 responseFormat = componentsUtils.getResponseFormat(se);
1274 handleAuditing(AuditingActionEnum.ARTIFACT_DELETE, parent, parentId, user, null, null,
1275 artifactId, responseFormat, ComponentTypeEnum.RESOURCE, null);
1276 return Either.right(responseFormat);
1278 handleLockingAndCommit(parent, shouldLock, inTransaction, operationSucceeded);
1282 private ArtifactDefinition handleDeleteInternal(String parentId, String artifactId, ComponentTypeEnum componentType, Component parent) {
1283 NodeTypeEnum parentType = convertParentType(componentType);
1284 log.debug("Going to find the artifact {} on the component {}", artifactId, parent.getUniqueId());
1285 Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> getArtifactRes = findArtifact(artifactId, parent, parentId, componentType);
1286 if (getArtifactRes.isRight()) {
1287 log.debug("Failed to find the artifact {} belonging to {} on the component {}", artifactId, parentId, parent.getUniqueId());
1288 throw new ByActionStatusComponentException(getArtifactRes.right().value(), artifactId);
1290 ArtifactDefinition foundArtifact = getArtifactRes.left().value().getLeft();
1291 ComponentInstance foundInstance = getArtifactRes.left().value().getRight();
1292 String esId = foundArtifact.getEsId();
1293 boolean needToClone = false;
1294 if (StringUtils.isNotEmpty(esId)) {
1295 Either<Boolean, StorageOperationStatus> needCloneRes = null;
1296 needCloneRes = artifactToscaOperation.isCloneNeeded(parent.getUniqueId(), foundArtifact, parentType);
1297 if (needCloneRes.isRight()) {
1298 throw new StorageException(needCloneRes.right().value(), foundArtifact.getArtifactDisplayName());
1299 } else if (log.isDebugEnabled()) {
1300 needToClone = needCloneRes.left().value();
1301 log.debug("handleDelete: clone is needed for deleting {} held by {} in component {} ? {}",
1302 foundArtifact.getArtifactName(), parentType, parent.getUniqueId(), parent.getName(), needCloneRes.left().value());
1305 boolean isNeedToDeleteArtifactFromDB = true;
1306 boolean isDuplicated = false;
1307 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1308 isNeedToDeleteArtifactFromDB = isArtifactOnlyResourceInstanceArtifact(foundArtifact, parent, parentId);
1310 ArtifactDataDefinition updatedArtifact = deleteOrUpdateArtifactOnGraph(parent, parentId, artifactId, parentType, foundArtifact, needToClone);
1311 isDuplicated = updatedArtifact.getDuplicated();
1313 if (!needToClone && !isDuplicated && isNeedToDeleteArtifactFromDB) {
1314 log.debug("Going to delete the artifact {} from the database. ", artifactId);
1315 CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(esId);
1316 if (cassandraStatus != CassandraOperationStatus.OK) {
1317 log.debug("Failed to delete the artifact {} from the database. ", artifactId);
1318 throw new StorageException(convertToStorageOperationStatus(cassandraStatus), foundArtifact.getArtifactDisplayName());
1321 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1322 List<GroupInstance> updatedGroupInstances = getUpdatedGroupInstances(artifactId, foundArtifact, foundInstance.getGroupInstances());
1323 if (CollectionUtils.isNotEmpty(updatedGroupInstances)) {
1324 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(parent, parentId, updatedGroupInstances);
1325 if (status.isRight()) {
1326 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
1327 throw new StorageException(status.right().value(), foundArtifact.getArtifactDisplayName());
1330 StorageOperationStatus status = generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentType);
1331 if (status != StorageOperationStatus.OK) {
1332 log.debug("Failed to generate new customization UUID for the component instance {}. ", parentId);
1333 throw new StorageException(status, foundArtifact.getArtifactDisplayName());
1336 List<GroupDataDefinition> updatedGroups = getUpdatedGroups(artifactId, foundArtifact, parent.getGroups());
1337 if (CollectionUtils.isNotEmpty(updatedGroups)) {
1338 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, updatedGroups);
1339 if (status.isRight()) {
1340 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
1341 throw new StorageException(status.right().value(), foundArtifact.getArtifactDisplayName());
1345 return foundArtifact;
1348 private boolean isArtifactOnlyResourceInstanceArtifact(ArtifactDefinition foundArtifact, Component parent, String instanceId) {
1349 Optional<ComponentInstance> componentInstanceOpt = parent.getComponentInstanceById(instanceId);
1350 if (!componentInstanceOpt.isPresent()) {
1351 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "", "", parent.getName());
1353 ComponentInstance foundInstance = componentInstanceOpt.get();
1354 String componentUid = foundInstance.getComponentUid();
1355 Either<Component, StorageOperationStatus> getContainerRes = toscaOperationFacade.getToscaElement(componentUid);
1356 if (getContainerRes.isRight()) {
1357 log.debug("Failed to fetch the container component {}. ", componentUid);
1358 throw new StorageException(getContainerRes.right().value());
1360 Component origComponent = getContainerRes.left().value();
1361 Map<String, ArtifactDefinition> deploymentArtifacts = origComponent.getDeploymentArtifacts();
1362 if (MapUtils.isNotEmpty(deploymentArtifacts)) {
1363 Optional<String> op = deploymentArtifacts.keySet()
1365 .filter(a -> a.equals(foundArtifact.getArtifactLabel()))
1367 if (op.isPresent()) {
1371 Map<String, ArtifactDefinition> artifacts = origComponent.getArtifacts();
1372 if (MapUtils.isNotEmpty(artifacts)) {
1373 Optional<String> op = artifacts.keySet()
1375 .filter(a -> a.equals(foundArtifact.getArtifactLabel()))
1377 if (op.isPresent()) {
1384 private List<GroupDataDefinition> getUpdatedGroups(String artifactId, ArtifactDefinition foundArtifact, List<GroupDefinition> groups) {
1385 List<GroupDataDefinition> updatedGroups = new ArrayList<>();
1386 boolean isUpdated = false;
1387 if (groups != null) {
1388 for (GroupDefinition group : groups) {
1390 if (CollectionUtils.isNotEmpty(group.getArtifacts()) && group.getArtifacts().contains(artifactId)) {
1391 group.getArtifacts().remove(artifactId);
1394 if (CollectionUtils.isNotEmpty(group.getArtifactsUuid()) && group.getArtifactsUuid()
1395 .contains(foundArtifact.getArtifactUUID())) {
1396 group.getArtifactsUuid().remove(foundArtifact.getArtifactUUID());
1400 updatedGroups.add(group);
1404 return updatedGroups;
1407 private List<GroupInstance> getUpdatedGroupInstances(String artifactId, ArtifactDefinition foundArtifact, List<GroupInstance> groupInstances) {
1408 List<GroupInstance> updatedGroupInstances = new ArrayList<>();
1409 if (CollectionUtils.isNotEmpty(groupInstances)) {
1410 boolean isUpdated = false;
1411 for (GroupInstance groupInstance : groupInstances) {
1413 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifacts()) && groupInstance.getGroupInstanceArtifacts().contains(artifactId)) {
1414 groupInstance.getGroupInstanceArtifacts().remove(artifactId);
1417 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifactsUuid()) && groupInstance.getGroupInstanceArtifactsUuid()
1418 .contains(foundArtifact.getArtifactUUID())) {
1419 groupInstance.getGroupInstanceArtifactsUuid().remove(foundArtifact.getArtifactUUID());
1423 updatedGroupInstances.add(groupInstance);
1427 return updatedGroupInstances;
1430 private ArtifactDataDefinition deleteOrUpdateArtifactOnGraph(Component component, String parentId, String artifactId, NodeTypeEnum parentType, ArtifactDefinition foundArtifact, Boolean cloneIsNeeded) {
1432 Either<ArtifactDataDefinition, StorageOperationStatus> result;
1433 boolean isMandatory = foundArtifact.getMandatory() || foundArtifact.getServiceApi();
1434 String componentId = component.getUniqueId();
1435 String instanceId = componentId.equals(parentId) ? null : parentId;
1437 log.debug("Going to update mandatory artifact {} from the component {}", artifactId, parentId);
1438 resetMandatoryArtifactFields(foundArtifact);
1439 result = artifactToscaOperation.updateArtifactOnGraph(component, foundArtifact, parentType, artifactId, instanceId, true, true);
1441 else if (cloneIsNeeded) {
1442 log.debug("Going to clone artifacts and to delete the artifact {} from the component {}", artifactId, parentId);
1443 result = artifactToscaOperation.deleteArtifactWithCloningOnGraph(componentId, foundArtifact, parentType, instanceId, false);
1446 log.debug("Going to delete the artifact {} from the component {}", artifactId, parentId);
1447 result = artifactToscaOperation.removeArtifactOnGraph(foundArtifact, componentId, instanceId, parentType, false);
1449 if (result.isRight()) {
1450 throw new StorageException(result.right().value(), foundArtifact.getArtifactDisplayName());
1452 return result.left().value();
1455 private Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> findArtifact(String artifactId, Component fetchedContainerComponent, String parentId, ComponentTypeEnum componentType) {
1457 Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> result = null;
1458 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
1459 ComponentInstance foundInstance = null;
1460 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE && StringUtils.isNotEmpty(parentId)) {
1461 Optional<ComponentInstance> componentInstanceOpt = fetchedContainerComponent.getComponentInstances()
1463 .filter(i -> i.getUniqueId()
1466 if (!componentInstanceOpt.isPresent()) {
1467 result = Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER);
1470 foundInstance = componentInstanceOpt.get();
1471 fetchArtifactsFromInstance(artifactId, artifacts, foundInstance);
1475 fetchArtifactsFromComponent(artifactId, fetchedContainerComponent, artifacts);
1477 if (result == null) {
1478 if (artifacts.containsKey(artifactId)) {
1479 result = Either.left(new ImmutablePair<>(artifacts.get(artifactId), foundInstance));
1482 result = Either.right(ActionStatus.ARTIFACT_NOT_FOUND);
1488 private void fetchArtifactsFromComponent(String artifactId, Component component, Map<String, ArtifactDefinition> artifacts) {
1489 Map<String, ArtifactDefinition> currArtifacts;
1490 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getDeploymentArtifacts())) {
1491 currArtifacts = component.getDeploymentArtifacts()
1494 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, i -> i));
1495 if (MapUtils.isNotEmpty(currArtifacts)) {
1496 artifacts.putAll(currArtifacts);
1499 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getArtifacts())) {
1500 currArtifacts = component.getArtifacts()
1503 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1504 if (MapUtils.isNotEmpty(currArtifacts)) {
1505 artifacts.putAll(currArtifacts);
1508 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getArtifacts())) {
1509 currArtifacts = component.getToscaArtifacts()
1512 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1513 if (MapUtils.isNotEmpty(currArtifacts)) {
1514 artifacts.putAll(currArtifacts);
1519 private void fetchArtifactsFromInstance(String artifactId, Map<String, ArtifactDefinition> artifacts, ComponentInstance instance) {
1520 Map<String, ArtifactDefinition> currArtifacts;
1521 if (MapUtils.isNotEmpty(instance.getDeploymentArtifacts())) {
1522 currArtifacts = instance.getDeploymentArtifacts()
1525 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1526 if (MapUtils.isNotEmpty(currArtifacts)) {
1527 artifacts.putAll(currArtifacts);
1530 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(instance.getArtifacts())) {
1531 currArtifacts = instance.getArtifacts()
1534 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1535 if (MapUtils.isNotEmpty(currArtifacts)) {
1536 artifacts.putAll(currArtifacts);
1541 private StorageOperationStatus convertToStorageOperationStatus(CassandraOperationStatus cassandraStatus) {
1542 StorageOperationStatus result;
1543 switch (cassandraStatus) {
1545 result = StorageOperationStatus.OK;
1548 result = StorageOperationStatus.NOT_FOUND;
1550 case CLUSTER_NOT_CONNECTED:
1551 case KEYSPACE_NOT_CONNECTED:
1552 result = StorageOperationStatus.CONNECTION_FAILURE;
1555 result = StorageOperationStatus.GENERAL_ERROR;
1561 private void resetMandatoryArtifactFields(ArtifactDefinition fetchedArtifact) {
1562 if (fetchedArtifact != null) {
1563 log.debug("Going to reset mandatory artifact {} fields. ", fetchedArtifact.getUniqueId());
1564 fetchedArtifact.setEsId(null);
1565 fetchedArtifact.setArtifactName(null);
1566 fetchedArtifact.setDescription(null);
1567 fetchedArtifact.setApiUrl(null);
1568 fetchedArtifact.setArtifactChecksum(null);
1569 nodeTemplateOperation.setDefaultArtifactTimeout(fetchedArtifact.getArtifactGroupType(), fetchedArtifact);
1570 fetchedArtifact.setArtifactUUID(null);
1571 long time = System.currentTimeMillis();
1572 fetchedArtifact.setPayloadUpdateDate(time);
1573 fetchedArtifact.setHeatParameters(null);
1574 fetchedArtifact.setHeatParamsUpdateDate(null);
1578 private StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId, ComponentTypeEnum componentType) {
1579 StorageOperationStatus error = StorageOperationStatus.OK;
1580 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1581 log.debug("Need to re-generate customization UUID for instance {}", instanceId);
1582 error = toscaOperationFacade.generateCustomizationUUIDOnInstance(componentId, instanceId);
1587 private ArtifactDefinition handleDownload(String componentId, String artifactId, ComponentTypeEnum componentType,
1589 Either<ArtifactDefinition, StorageOperationStatus> artifactById = artifactToscaOperation.getArtifactById(componentId, artifactId, componentType,
1590 parent.getUniqueId());
1591 if (artifactById.isRight()) {
1592 throw new StorageException(artifactById.right().value());
1594 ArtifactDefinition artifactDefinition = artifactById.left().value();
1595 if (artifactDefinition == null) {
1596 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactId);
1598 return artifactDefinition;
1601 private Either<ActionStatus, ResponseFormat> handleArtifactLabel(String componentId, Component parentComponent, ArtifactOperationInfo operation, ArtifactDefinition artifactInfo, String operationName,
1602 ComponentTypeEnum componentType) {
1604 String artifactLabel = artifactInfo.getArtifactLabel();
1605 if (operationName == null && (artifactInfo.getArtifactLabel() == null || artifactInfo.getArtifactLabel()
1607 BeEcompErrorManager.getInstance()
1608 .logBeMissingArtifactInformationError("Artifact Update / Upload", "artifactLabel");
1609 log.debug("missing artifact logical name for component {}", componentId);
1610 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_LABEL));
1612 if (operation.isCreateOrLink() && !artifactInfo.getMandatory()) {
1614 if (operationName != null) {
1615 if (artifactInfo.getArtifactLabel() != null && !operationName.equals(artifactInfo.getArtifactLabel())) {
1616 log.debug("artifact label cannot be set {}", artifactLabel);
1617 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_LOGICAL_NAME_CANNOT_BE_CHANGED));
1620 artifactLabel = operationName;
1623 String displayName = artifactInfo.getArtifactDisplayName();
1624 if (displayName == null || displayName.isEmpty()) {
1625 displayName = artifactLabel;
1627 displayName = ValidationUtils.cleanArtifactDisplayName(displayName);
1628 artifactInfo.setArtifactDisplayName(displayName);
1630 if (!ValidationUtils.validateArtifactLabel(artifactLabel)) {
1631 log.debug("Invalid format form Artifact label : {}", artifactLabel);
1632 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1634 artifactLabel = ValidationUtils.normalizeArtifactLabel(artifactLabel);
1636 if (artifactLabel.isEmpty()) {
1637 log.debug("missing normalized artifact logical name for component {}", componentId);
1638 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_LABEL));
1641 if (!ValidationUtils.validateArtifactLabelLength(artifactLabel)) {
1642 log.debug("Invalid lenght form Artifact label : {}", artifactLabel);
1643 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_LABEL, String
1644 .valueOf(ValidationUtils.ARTIFACT_LABEL_LENGTH)));
1646 if (!validateLabelUniqueness(componentId, parentComponent, artifactLabel, componentType)) {
1647 log.debug("Non unique Artifact label : {}", artifactLabel);
1648 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_EXIST, artifactLabel));
1651 artifactInfo.setArtifactLabel(artifactLabel);
1653 return Either.left(ActionStatus.OK);
1656 private boolean validateLabelUniqueness(String componentId, Component parentComponent, String artifactLabel, ComponentTypeEnum componentType) {
1657 boolean isUnique = true;
1658 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifacts;
1659 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1660 artifacts = artifactToscaOperation.getAllInstanceArtifacts(parentComponent.getUniqueId(), componentId);
1663 artifacts = artifactToscaOperation.getArtifacts(componentId);
1666 if (artifacts.isLeft()) {
1667 for (String label : artifacts.left().value().keySet()) {
1668 if (label.equals(artifactLabel)) {
1674 if (componentType == ComponentTypeEnum.RESOURCE && isUnique) {
1675 isUnique = isUniqueLabelInResourceInterfaces(componentId, artifactLabel);
1680 boolean validateArtifactNameUniqueness(String componentId, Component parentComponent, ArtifactDefinition artifactInfo,
1681 ComponentTypeEnum componentType) {
1682 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifacts = getArtifacts(componentType,
1683 parentComponent, componentId, artifactInfo.getArtifactGroupType());
1684 String artifactName = artifactInfo.getArtifactName();
1685 if (artifacts.isLeft() && Objects.nonNull(artifacts.left().value())){
1686 if (artifacts.left().value().values().stream()
1687 .anyMatch(ad -> artifactName.equals(ad.getArtifactName())
1688 //check whether it is the same artifact we hold (by label)
1689 && !artifactInfo.getArtifactLabel().equals(ad.getArtifactLabel()))){
1693 if (ComponentTypeEnum.RESOURCE == componentType) {
1694 return isUniqueArtifactNameInResourceInterfaces(componentId, artifactName, artifactInfo.getArtifactLabel());
1699 private boolean isUniqueArtifactNameInResourceInterfaces(String componentId, String artifactName, String artifactLabel) {
1700 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> allInterfacesOfResource = interfaceLifecycleOperation
1701 .getAllInterfacesOfResource(componentId, true, true);
1703 if (allInterfacesOfResource.isLeft()){
1704 return allInterfacesOfResource.left().value()
1706 .stream().map(InterfaceDefinition :: getOperationsMap)
1707 .flatMap(map -> map.values().stream())
1708 .map(OperationDataDefinition::getImplementation)
1709 .filter(Objects::nonNull)
1710 .noneMatch(add -> artifactName.equals(add.getArtifactName())
1711 && !artifactLabel.equals(add.getArtifactLabel()));
1716 private boolean isUniqueLabelInResourceInterfaces(String componentId, String artifactLabel) {
1717 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> allInterfacesOfResource = interfaceLifecycleOperation
1718 .getAllInterfacesOfResource(componentId, true, true);
1720 if (allInterfacesOfResource.isLeft()){
1721 return allInterfacesOfResource.left().value()
1723 .stream().map(InterfaceDefinition :: getOperationsMap)
1724 .flatMap(map -> map.values().stream())
1725 .map(OperationDataDefinition::getImplementation)
1726 .filter(Objects::nonNull)
1727 .noneMatch(add -> artifactLabel.equals(add.getArtifactLabel()));
1732 private Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifacts(ComponentTypeEnum componentType, Component parentComponent,
1733 String componentId, ArtifactGroupTypeEnum artifactGroupType) {
1734 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsResponse;
1735 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1736 artifactsResponse = artifactToscaOperation.getAllInstanceArtifacts(parentComponent.getUniqueId(), componentId);
1739 artifactsResponse = artifactToscaOperation.getArtifacts(componentId);
1741 if (artifactsResponse.isRight() && artifactsResponse.right().value() == StorageOperationStatus.NOT_FOUND) {
1742 log.debug("failed to retrieve artifacts for {} ", componentId);
1743 return Either.right(artifactsResponse.right().value());
1745 return Either.left(artifactsResponse.left().value().entrySet()
1747 .filter(x -> artifactGroupType == x.getValue().getArtifactGroupType())
1748 .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
1751 // ***************************************************************
1753 private Either<ArtifactDefinition, Operation> createArtifact(Component parent, String parentId, ArtifactDefinition artifactInfo, byte[] decodedPayload,
1754 ComponentTypeEnum componentTypeEnum, AuditingActionEnum auditingActionEnum, String interfaceType, String operationName) {
1756 DAOArtifactData artifactData = createEsArtifactData(artifactInfo, decodedPayload);
1757 if (artifactData == null) {
1758 BeEcompErrorManager.getInstance().logBeDaoSystemError("Upload Artifact");
1759 log.debug("Failed to create artifact object for ES.");
1760 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
1762 ComponentInstance foundInstance = findComponentInstance(parentId, parent);
1763 String instanceId = null;
1764 if (foundInstance != null) {
1765 if (foundInstance.isArtifactExists(artifactInfo.getArtifactGroupType(), artifactInfo.getArtifactLabel())) {
1766 log.debug("Failed to create artifact, already exists");
1767 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_EXIST, artifactInfo.getArtifactLabel());
1769 instanceId = foundInstance.getUniqueId();
1771 // set on graph object id of artifact in ES!
1772 artifactInfo.setEsId(artifactData.getId());
1774 Either<ArtifactDefinition, Operation> operationResult;
1775 if (interfaceType != null && operationName != null) {
1776 // lifecycle artifact
1777 Operation operation = convertToOperation(artifactInfo, operationName);
1778 Either<Operation, StorageOperationStatus> result = interfaceLifecycleOperation.updateInterfaceOperation(parentId, interfaceType, operationName, operation);
1779 if (result.isRight()) {
1780 throw new StorageException(result.right().value());
1782 operationResult = Either.right(result.left().value());
1785 // information/deployment/api artifacts
1786 NodeTypeEnum nodeType = convertParentType(componentTypeEnum);
1787 Either<ArtifactDefinition, StorageOperationStatus> result = artifactToscaOperation.addArtifactToComponent(
1788 artifactInfo, parent, nodeType, true, instanceId);
1789 if (result.isRight()) {
1790 throw new StorageException(result.right().value());
1792 ArtifactDefinition artifactDefinition = result.left().value();
1793 artifactData.setId(artifactDefinition.getEsId());
1794 operationResult = Either.left(artifactDefinition);
1796 if (generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentTypeEnum) != StorageOperationStatus.OK) {
1797 throw new StorageException(generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentTypeEnum));
1800 saveArtifactInCassandra(artifactData, parent, artifactInfo, "", "", auditingActionEnum, componentTypeEnum);
1801 return operationResult;
1804 private ComponentInstance findComponentInstance(String componentInstanceId, Component containerComponent) {
1805 ComponentInstance foundInstance = null;
1806 if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
1807 foundInstance = containerComponent.getComponentInstances()
1809 .filter(i -> i.getUniqueId().equals(componentInstanceId))
1813 return foundInstance;
1816 private void validateDeploymentArtifact(final ArtifactDefinition artifactInfo, final Component component) {
1817 final ComponentTypeEnum componentType = component.getComponentType();
1818 if (componentType != ComponentTypeEnum.RESOURCE &&
1819 componentType != ComponentTypeEnum.SERVICE &&
1820 componentType != ComponentTypeEnum.RESOURCE_INSTANCE) {
1821 log.debug("Invalid component type '{}' for artifact. "
1822 + "Expected Resource, Component or Resource Instance", componentType.getValue());
1823 throw new ByActionStatusComponentException(MISMATCH_BETWEEN_ARTIFACT_TYPE_AND_COMPONENT_TYPE,
1824 componentType.getValue(), "Service, Resource or ResourceInstance", componentType.getValue());
1826 final String artifactType = artifactInfo.getArtifactType();
1827 final ArtifactConfiguration artifactConfiguration = loadArtifactTypeConfig(artifactType).orElse(null);
1828 if (artifactConfiguration == null) {
1829 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactType);
1831 validateArtifactType(componentType, artifactInfo.getArtifactGroupType(), artifactConfiguration);
1832 if (componentType == ComponentTypeEnum.RESOURCE || componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1833 final Resource resource = (Resource) component;
1834 final ResourceTypeEnum resourceType = resource.getResourceType();
1836 validateResourceType(resourceType, artifactInfo, artifactConfiguration.getResourceTypes());
1839 validateArtifactExtension(artifactConfiguration, artifactInfo);
1842 private void validateHeatArtifact(final Component parentComponent, final String componentId,
1843 final ArtifactDefinition artifactDefinition) {
1844 final String artifactType = artifactDefinition.getArtifactType();
1845 final ArtifactTypeEnum artifactTypeEnum = ArtifactTypeEnum.parse(artifactType);
1846 if (artifactTypeEnum == null) {
1850 switch (artifactTypeEnum) {
1854 validateHeatTimeoutValue(artifactDefinition);
1857 validateHeatEnvDeploymentArtifact(parentComponent, componentId, artifactDefinition);
1864 private void setArtifactTimeout(final ArtifactDefinition newArtifactInfo,
1865 final ArtifactDefinition existingArtifactInfo) {
1867 final String artifactType = newArtifactInfo.getArtifactType();
1868 final ArtifactTypeEnum artifactTypeEnum = ArtifactTypeEnum.parse(artifactType);
1869 if (artifactTypeEnum == null) {
1870 newArtifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
1873 switch (artifactTypeEnum) {
1877 if (newArtifactInfo.getTimeout() == null) {
1878 if (existingArtifactInfo == null) {
1879 newArtifactInfo.setTimeout(NodeTemplateOperation.getDefaultHeatTimeout());
1881 newArtifactInfo.setTimeout(existingArtifactInfo.getTimeout());
1886 newArtifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
1892 void validateDeploymentArtifactTypeIsLegalForParent(ArtifactDefinition artifactInfo, ArtifactTypeEnum artifactType, Map<String, ArtifactTypeConfig> resourceDeploymentArtifacts) {
1893 if ((resourceDeploymentArtifacts == null) || !resourceDeploymentArtifacts.containsKey(artifactType.name())) {
1894 log.debug("Artifact Type: {} Not found !", artifactInfo.getArtifactType());
1895 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
1899 Optional<ArtifactConfiguration> loadArtifactTypeConfig(final String artifactType) {
1900 if (artifactType == null) {
1901 return Optional.empty();
1903 final List<ArtifactConfiguration> artifactConfigurationList =
1904 ConfigurationManager.getConfigurationManager().getConfiguration().getArtifacts();
1905 if (CollectionUtils.isEmpty(artifactConfigurationList)) {
1906 return Optional.empty();
1909 return artifactConfigurationList.stream()
1910 .filter(artifactConfiguration -> artifactConfiguration.getType().equalsIgnoreCase(artifactType))
1914 private Either<Boolean, ResponseFormat> extractHeatParameters(ArtifactDefinition artifactInfo) {
1915 // extract heat parameters
1916 if (artifactInfo.getPayloadData() != null) {
1917 String heatDecodedPayload = new String(Base64.decodeBase64(artifactInfo.getPayloadData()));
1918 Either<List<HeatParameterDefinition>, ResultStatusEnum> heatParameters = ImportUtils.getHeatParamsWithoutImplicitTypes(heatDecodedPayload, artifactInfo
1919 .getArtifactType());
1920 if (heatParameters.isRight() && (heatParameters.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND)) {
1921 log.info("failed to parse heat parameters ");
1922 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, artifactInfo
1923 .getArtifactType());
1924 return Either.right(responseFormat);
1926 else if (heatParameters.isLeft() && heatParameters.left().value() != null) {
1927 artifactInfo.setListHeatParameters(heatParameters.left().value());
1930 return Either.left(true);
1935 void validateArtifactExtension(final ArtifactConfiguration artifactConfiguration,
1936 final ArtifactDefinition artifactDefinition) {
1937 final List<String> acceptedTypes = artifactConfiguration.getAcceptedTypes();
1939 * No need to check specific types. In case there are no acceptedTypes in configuration, then any type is accepted.
1941 if (CollectionUtils.isEmpty(acceptedTypes)) {
1944 final String artifactName = artifactDefinition.getArtifactName();
1945 final String fileExtension = FilenameUtils.getExtension(artifactName);
1947 if (fileExtension == null || !acceptedTypes.contains(fileExtension.toLowerCase())) {
1948 final String artifactType = artifactDefinition.getArtifactType();
1949 log.debug("File extension \"{}\" is not allowed for artifact type \"{}\"", fileExtension, artifactType);
1950 throw new ByActionStatusComponentException(ActionStatus.WRONG_ARTIFACT_FILE_EXTENSION, artifactType);
1955 void validateHeatEnvDeploymentArtifact(final Component parentComponent, final String parentId,
1956 final ArtifactDefinition artifactInfo) {
1957 final Wrapper<ArtifactDefinition> heatMDWrapper = new Wrapper<>();
1958 final Wrapper<byte[]> payloadWrapper = new Wrapper<>();
1960 validateYaml(artifactInfo);
1961 validateHeatExist(parentComponent.getUniqueId(), parentId, heatMDWrapper, artifactInfo, parentComponent.getComponentType());
1963 if (!heatMDWrapper.isEmpty()) {
1964 fillArtifactPayload(payloadWrapper, heatMDWrapper.getInnerElement());
1967 if (!heatMDWrapper.isEmpty()) {
1968 validateEnvVsHeat(artifactInfo, heatMDWrapper.getInnerElement(), payloadWrapper.getInnerElement());
1972 public void fillArtifactPayload(Wrapper<byte[]> payloadWrapper, ArtifactDefinition artifactDefinition) {
1973 if (ArrayUtils.isEmpty(artifactDefinition.getPayloadData())) {
1974 Either<DAOArtifactData, CassandraOperationStatus> eitherArtifactData = artifactCassandraDao.getArtifact(artifactDefinition.getEsId());
1975 if (eitherArtifactData.isLeft()) {
1976 byte[] data = eitherArtifactData.left().value().getDataAsArray();
1977 payloadWrapper.setInnerElement(Base64.encodeBase64(data));
1980 log.debug("Error getting payload for artifact:{}", artifactDefinition.getArtifactName());
1981 throw new StorageException(DaoStatusConverter.convertCassandraStatusToStorageStatus(eitherArtifactData.right().value()));
1985 payloadWrapper.setInnerElement(artifactDefinition.getPayloadData());
1989 private void validateEnvVsHeat(ArtifactDefinition envArtifact, ArtifactDefinition heatArtifact, byte[] heatPayloadData) {
1990 String envPayload = new String(Base64.decodeBase64(envArtifact.getPayloadData()));
1991 Map<String, Object> heatEnvToscaJson = (Map<String, Object>) new Yaml().load(envPayload);
1992 String heatDecodedPayload = new String(Base64.decodeBase64(heatPayloadData));
1993 Map<String, Object> heatToscaJson = (Map<String, Object>) new Yaml().load(heatDecodedPayload);
1995 Either<Map<String, Object>, ResultStatusEnum> eitherHeatEnvProperties = ImportUtils.findFirstToscaMapElement(heatEnvToscaJson, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
1996 if (eitherHeatEnvProperties.isRight()) {
1997 log.debug("Invalid heat env format for file:{}", envArtifact.getArtifactName());
1998 throw new ByActionStatusComponentException(ActionStatus.CORRUPTED_FORMAT, "Heat Env");
2000 Either<Map<String, Object>, ResultStatusEnum> eitherHeatProperties = ImportUtils.findFirstToscaMapElement(heatToscaJson, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
2001 if (eitherHeatProperties.isRight()) {
2002 log.debug("Invalid heat format for file:{}", heatArtifact.getArtifactName());
2003 throw new ByActionStatusComponentException(ActionStatus.CORRUPTED_FORMAT, "Heat");
2005 Set<String> heatPropertiesKeys = eitherHeatProperties.left().value().keySet();
2006 Set<String> heatEnvPropertiesKeys = eitherHeatEnvProperties.left().value().keySet();
2007 heatEnvPropertiesKeys.removeAll(heatPropertiesKeys);
2008 if (!heatEnvPropertiesKeys.isEmpty()) {
2009 log.debug("Validation of heat_env for artifact:{} vs heat artifact for artifact :{} failed", envArtifact.getArtifactName(), heatArtifact.getArtifactName());
2010 throw new ByActionStatusComponentException(ActionStatus.MISMATCH_HEAT_VS_HEAT_ENV, envArtifact.getArtifactName(), heatArtifact.getArtifactName());
2014 private void validateYaml(ArtifactDefinition artifactInfo) {
2015 YamlToObjectConverter yamlConverter = new YamlToObjectConverter();
2016 boolean isYamlValid = yamlConverter.isValidYamlEncoded64(artifactInfo.getPayloadData());
2018 log.debug("Yaml is not valid for artifact : {}", artifactInfo.getArtifactName());
2019 throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML, artifactInfo.getArtifactType());
2023 private boolean isValidXml(byte[] xmlToParse) {
2024 boolean isXmlValid = true;
2026 XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
2027 setFeatures(reader);
2028 reader.parse(new InputSource(new ByteArrayInputStream(xmlToParse)));
2030 catch (ParserConfigurationException | IOException | SAXException e) {
2031 log.debug("Xml is invalid : {}", e.getMessage(), e);
2037 private void setFeatures(XMLReader reader) throws SAXNotSupportedException {
2039 reader.setFeature("http://apache.org/xml/features/validation/schema", false);
2040 reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
2042 catch (SAXNotRecognizedException e) {
2043 log.debug("Xml parser couldn't set feature: \"http://apache.org/xml/features/validation/schema\", false", e.getMessage(), e);
2047 private void validateSingleDeploymentArtifactName(final String artifactName, final Component parentComponent) {
2048 boolean artifactNameFound = false;
2049 final Iterator<ArtifactDefinition> parentDeploymentArtifactsItr =
2050 getDeploymentArtifacts(parentComponent, null).iterator();
2052 while (!artifactNameFound && parentDeploymentArtifactsItr.hasNext()) {
2053 artifactNameFound = artifactName.equalsIgnoreCase(parentDeploymentArtifactsItr.next().getArtifactName());
2055 if (artifactNameFound) {
2056 final ComponentTypeEnum componentType = parentComponent.getComponentType();
2057 log.debug("Can't upload artifact: {}, because another artifact with this name already exist.", artifactName);
2058 throw new ByActionStatusComponentException(ActionStatus.DEPLOYMENT_ARTIFACT_NAME_ALREADY_EXISTS,
2059 componentType.getValue(), parentComponent.getName(), artifactName);
2063 private void validateHeatExist(String componentId, String parentRiId, Wrapper<ArtifactDefinition> heatArtifactMDWrapper, ArtifactDefinition heatEnvArtifact,
2064 ComponentTypeEnum componentType) {
2065 final Either<ArtifactDefinition, StorageOperationStatus> res = artifactToscaOperation
2066 .getHeatArtifactByHeatEnvId(parentRiId, heatEnvArtifact, componentId, componentType);
2067 if (res.isRight()) {
2068 throw new ByActionStatusComponentException(ActionStatus.MISSING_HEAT);
2071 heatArtifactMDWrapper.setInnerElement(res.left().value());
2075 void validateHeatTimeoutValue(final ArtifactDefinition artifactInfo) {
2076 log.trace("Started HEAT pre-payload validation for artifact {}", artifactInfo.getArtifactLabel());
2077 // timeout > 0 for HEAT artifacts
2078 if (artifactInfo.getTimeout() == null || artifactInfo.getTimeout() < 1) {
2079 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_TIMEOUT);
2081 // US649856 - Allow several HEAT files on Resource
2082 log.trace("Ended HEAT validation for artifact {}", artifactInfo.getArtifactLabel());
2086 void validateResourceType(final ResourceTypeEnum resourceType, final ArtifactDefinition artifactInfo,
2087 final List<String> typeList) {
2088 if (CollectionUtils.isEmpty(typeList) || typeList.contains(resourceType.getValue())) {
2091 final String listToString = typeList.stream().collect(Collectors.joining(", "));
2092 throw new ByActionStatusComponentException(MISMATCH_BETWEEN_ARTIFACT_TYPE_AND_COMPONENT_TYPE,
2093 artifactInfo.getArtifactGroupType().getType(), listToString, resourceType.getValue());
2097 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParameters(ArtifactDefinition artifactInfo, String artifactType) {
2098 if (artifactInfo.getHeatParameters() != null) {
2099 for (HeatParameterDefinition heatParam : artifactInfo.getListHeatParameters()) {
2100 String parameterType = heatParam.getType();
2101 HeatParameterType heatParameterType = HeatParameterType.isValidType(parameterType);
2102 String artifactTypeStr = artifactType != null ? artifactType : ArtifactTypeEnum.HEAT.getType();
2103 if (heatParameterType == null) {
2104 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_HEAT_PARAMETER_TYPE, artifactTypeStr, heatParam
2106 return Either.right(responseFormat);
2109 StorageOperationStatus validateAndUpdateProperty = heatParametersOperation.validateAndUpdateProperty(heatParam);
2110 if (validateAndUpdateProperty != StorageOperationStatus.OK) {
2111 log.debug("Heat parameter {} is invalid. Status is {}", heatParam.getName(), validateAndUpdateProperty);
2112 ActionStatus status = ActionStatus.INVALID_HEAT_PARAMETER_VALUE;
2113 ResponseFormat responseFormat = componentsUtils.getResponseFormat(status, artifactTypeStr, heatParam
2114 .getType(), heatParam.getName());
2115 return Either.right(responseFormat);
2119 return Either.left(artifactInfo);
2122 public List<ArtifactDefinition> getDeploymentArtifacts(final Component component, final String ciId) {
2123 final ComponentTypeEnum componentType = component.getComponentType();
2124 if (component.getDeploymentArtifacts() == null) {
2125 return Collections.emptyList();
2127 final List<ArtifactDefinition> deploymentArtifacts = new ArrayList<>();
2128 if (ComponentTypeEnum.RESOURCE == componentType && ciId != null) {
2129 final Either<ComponentInstance, ResponseFormat> getRI =
2130 getRIFromComponent(component, ciId, null, null, null);
2131 if (getRI.isRight()) {
2132 return Collections.emptyList();
2134 final ComponentInstance ri = getRI.left().value();
2135 if (ri.getDeploymentArtifacts() != null) {
2136 deploymentArtifacts.addAll(ri.getDeploymentArtifacts().values());
2139 deploymentArtifacts.addAll(component.getDeploymentArtifacts().values());
2141 return deploymentArtifacts;
2144 private void checkCreateFields(User user, ArtifactDefinition artifactInfo, ArtifactGroupTypeEnum type) {
2145 // on create if null add informational to current
2146 if (artifactInfo.getArtifactGroupType() == null) {
2147 artifactInfo.setArtifactGroupType(type);
2149 if (artifactInfo.getUniqueId() != null) {
2150 log.error("artifact uniqid cannot be set ignoring");
2152 artifactInfo.setUniqueId(null);
2154 if (artifactInfo.getArtifactRef() != null) {
2155 log.error("artifact ref cannot be set ignoring");
2157 artifactInfo.setArtifactRef(null);
2159 if (artifactInfo.getArtifactRepository() != null) {
2160 log.error("artifact repository cannot be set ignoring");
2162 artifactInfo.setArtifactRepository(null);
2164 if (artifactInfo.getUserIdCreator() != null) {
2165 log.error("creator uuid cannot be set ignoring");
2167 artifactInfo.setArtifactCreator(user.getUserId());
2169 if (artifactInfo.getUserIdLastUpdater() != null) {
2170 log.error("userId of last updater cannot be set ignoring");
2172 artifactInfo.setUserIdLastUpdater(user.getUserId());
2174 if (artifactInfo.getCreatorFullName() != null) {
2175 log.error("creator Full name cannot be set ignoring");
2177 String fullName = user.getFirstName() + " " + user.getLastName();
2178 artifactInfo.setUpdaterFullName(fullName);
2180 if (artifactInfo.getUpdaterFullName() != null) {
2181 log.error("updater Full name cannot be set ignoring");
2183 artifactInfo.setUpdaterFullName(fullName);
2185 if (artifactInfo.getCreationDate() != null) {
2186 log.error("Creation Date cannot be set ignoring");
2188 long time = System.currentTimeMillis();
2189 artifactInfo.setCreationDate(time);
2191 if (artifactInfo.getLastUpdateDate() != null) {
2192 log.error("Last Update Date cannot be set ignoring");
2194 artifactInfo.setLastUpdateDate(time);
2196 if (artifactInfo.getEsId() != null) {
2197 log.error("es id cannot be set ignoring");
2199 artifactInfo.setEsId(null);
2204 private String composeArtifactId(String resourceId, String artifactId, ArtifactDefinition artifactInfo, String interfaceName, String operationName) {
2205 String id = artifactId;
2206 if (artifactId == null || artifactId.isEmpty()) {
2207 String uniqueId = null;
2208 if (interfaceName != null && operationName != null) {
2209 uniqueId = UniqueIdBuilder.buildArtifactByInterfaceUniqueId(resourceId, interfaceName, operationName, artifactInfo
2210 .getArtifactLabel());
2213 uniqueId = UniqueIdBuilder.buildPropertyUniqueId(resourceId, artifactInfo.getArtifactLabel());
2215 artifactInfo.setUniqueId(uniqueId);
2216 artifactInfo.setEsId(uniqueId);
2220 artifactInfo.setUniqueId(artifactId);
2221 artifactInfo.setEsId(artifactId);
2226 private Either<Boolean, ResponseFormat> validateFirstUpdateHasPayload(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2227 if (currentArtifact.getEsId() == null && (artifactInfo.getPayloadData() == null || artifactInfo.getPayloadData().length == 0)) {
2228 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD));
2230 return Either.left(true);
2235 Either<Boolean, ResponseFormat> validateAndSetArtifactName(ArtifactDefinition artifactInfo) {
2236 if (artifactInfo.getArtifactName() == null || artifactInfo.getArtifactName().isEmpty()) {
2237 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_NAME));
2239 String normalizeFileName = ValidationUtils.normalizeFileName(artifactInfo.getArtifactName());
2240 if (normalizeFileName == null || normalizeFileName.isEmpty()) {
2241 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_NAME));
2243 artifactInfo.setArtifactName(normalizeFileName);
2245 if (!ValidationUtils.validateArtifactNameLength(artifactInfo.getArtifactName())) {
2246 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_NAME, String.valueOf(ValidationUtils.ARTIFACT_NAME_LENGTH)));
2249 return Either.left(true);
2252 private void validateArtifactTypeNotChanged(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2253 if (StringUtils.isEmpty(artifactInfo.getArtifactType())) {
2254 log.info("artifact type is missing operation ignored");
2255 throw new ByActionStatusComponentException(ActionStatus.MISSING_ARTIFACT_TYPE);
2258 if (!currentArtifact.getArtifactType().equalsIgnoreCase(artifactInfo.getArtifactType())) {
2259 log.info("artifact type cannot be changed operation ignored");
2260 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2264 private Either<ArtifactDefinition, ResponseFormat> validateOrSetArtifactGroupType(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2266 if (Objects.nonNull(artifactInfo) && Objects.nonNull(currentArtifact)) {
2267 if (artifactInfo.getArtifactGroupType() == null) {
2268 artifactInfo.setArtifactGroupType(currentArtifact.getArtifactGroupType());
2269 } else if (!currentArtifact.getArtifactGroupType()
2271 .equalsIgnoreCase(artifactInfo.getArtifactGroupType().getType())) {
2272 log.info("artifact group type cannot be changed. operation failed");
2273 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2276 return Either.left(artifactInfo);
2279 private void checkAndSetUnUpdatableFields(User user, ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact, ArtifactGroupTypeEnum type) {
2281 // on update if null add informational to current
2282 if (currentArtifact.getArtifactGroupType() == null && type != null) {
2283 currentArtifact.setArtifactGroupType(type);
2286 if (artifactInfo.getUniqueId() != null && !currentArtifact.getUniqueId().equals(artifactInfo.getUniqueId())) {
2287 log.error("artifact uniqid cannot be set ignoring");
2289 artifactInfo.setUniqueId(currentArtifact.getUniqueId());
2291 if (artifactInfo.getArtifactRef() != null && !currentArtifact.getArtifactRef()
2292 .equals(artifactInfo.getArtifactRef())) {
2293 log.error("artifact ref cannot be set ignoring");
2295 artifactInfo.setArtifactRef(currentArtifact.getArtifactRef());
2297 if (artifactInfo.getArtifactRepository() != null && !currentArtifact.getArtifactRepository()
2298 .equals(artifactInfo.getArtifactRepository())) {
2299 log.error("artifact repository cannot be set ignoring");
2301 artifactInfo.setArtifactRepository(currentArtifact.getArtifactRepository());
2303 if (artifactInfo.getUserIdCreator() != null && !currentArtifact.getUserIdCreator()
2304 .equals(artifactInfo.getUserIdCreator())) {
2305 log.error("creator uuid cannot be set ignoring");
2307 artifactInfo.setUserIdCreator(currentArtifact.getUserIdCreator());
2309 if (artifactInfo.getArtifactCreator() != null && !currentArtifact.getArtifactCreator()
2310 .equals(artifactInfo.getArtifactCreator())) {
2311 log.error("artifact creator cannot be set ignoring");
2313 artifactInfo.setArtifactCreator(currentArtifact.getArtifactCreator());
2315 if (artifactInfo.getUserIdLastUpdater() != null && !currentArtifact.getUserIdLastUpdater()
2316 .equals(artifactInfo.getUserIdLastUpdater())) {
2317 log.error("userId of last updater cannot be set ignoring");
2319 artifactInfo.setUserIdLastUpdater(user.getUserId());
2321 if (artifactInfo.getCreatorFullName() != null && !currentArtifact.getCreatorFullName()
2322 .equals(artifactInfo.getCreatorFullName())) {
2323 log.error("creator Full name cannot be set ignoring");
2325 artifactInfo.setCreatorFullName(currentArtifact.getCreatorFullName());
2327 if (artifactInfo.getUpdaterFullName() != null && !currentArtifact.getUpdaterFullName()
2328 .equals(artifactInfo.getUpdaterFullName())) {
2329 log.error("updater Full name cannot be set ignoring");
2331 String fullName = user.getFirstName() + " " + user.getLastName();
2332 artifactInfo.setUpdaterFullName(fullName);
2334 if (artifactInfo.getCreationDate() != null && !currentArtifact.getCreationDate()
2335 .equals(artifactInfo.getCreationDate())) {
2336 log.error("Creation Date cannot be set ignoring");
2338 artifactInfo.setCreationDate(currentArtifact.getCreationDate());
2340 if (artifactInfo.getLastUpdateDate() != null && !currentArtifact.getLastUpdateDate()
2341 .equals(artifactInfo.getLastUpdateDate())) {
2342 log.error("Last Update Date cannot be set ignoring");
2344 long time = System.currentTimeMillis();
2345 artifactInfo.setLastUpdateDate(time);
2347 if (artifactInfo.getEsId() != null && !currentArtifact.getEsId().equals(artifactInfo.getEsId())) {
2348 log.error("es id cannot be set ignoring");
2350 artifactInfo.setEsId(currentArtifact.getUniqueId());
2352 if (artifactInfo.getArtifactDisplayName() != null && !currentArtifact.getArtifactDisplayName()
2353 .equals(artifactInfo.getArtifactDisplayName())) {
2354 log.error(" Artifact Display Name cannot be set ignoring");
2356 artifactInfo.setArtifactDisplayName(currentArtifact.getArtifactDisplayName());
2358 if (artifactInfo.getServiceApi() != null && !currentArtifact.getServiceApi()
2359 .equals(artifactInfo.getServiceApi())) {
2360 log.debug("serviceApi cannot be set. ignoring.");
2362 artifactInfo.setServiceApi(currentArtifact.getServiceApi());
2364 if (artifactInfo.getArtifactGroupType() != null && currentArtifact.getArtifactGroupType() != artifactInfo.getArtifactGroupType()) {
2365 log.debug("artifact group cannot be set. ignoring.");
2367 artifactInfo.setArtifactGroupType(currentArtifact.getArtifactGroupType());
2369 artifactInfo.setArtifactVersion(currentArtifact.getArtifactVersion());
2371 if (artifactInfo.getArtifactUUID() != null && !artifactInfo.getArtifactUUID()
2372 .isEmpty() && !currentArtifact.getArtifactUUID()
2373 .equals(artifactInfo.getArtifactUUID())) {
2374 log.debug("artifact UUID cannot be set. ignoring.");
2376 artifactInfo.setArtifactUUID(currentArtifact.getArtifactUUID());
2378 if ((artifactInfo.getHeatParameters() != null) && (currentArtifact.getHeatParameters() != null) && !artifactInfo
2379 .getHeatParameters()
2380 .isEmpty() && !currentArtifact.getHeatParameters().isEmpty()) {
2381 checkAndSetUnupdatableHeatParams(artifactInfo.getListHeatParameters(), currentArtifact.getListHeatParameters());
2385 private void checkAndSetUnupdatableHeatParams(List<HeatParameterDefinition> heatParameters, List<HeatParameterDefinition> currentParameters) {
2387 Map<String, HeatParameterDefinition> currentParametersMap = getMapOfParameters(currentParameters);
2388 for (HeatParameterDefinition parameter : heatParameters) {
2389 HeatParameterDefinition currentParam = currentParametersMap.get(parameter.getUniqueId());
2391 if (currentParam != null) {
2393 if (parameter.getName() != null && !parameter.getName().equalsIgnoreCase(currentParam.getName())) {
2394 log.debug("heat parameter name cannot be updated ({}). ignoring.", parameter.getName());
2395 parameter.setName(currentParam.getName());
2397 if (parameter.getDefaultValue() != null && !parameter.getDefaultValue()
2398 .equalsIgnoreCase(currentParam.getDefaultValue())) {
2399 log.debug("heat parameter defaultValue cannot be updated ({}). ignoring.", parameter.getDefaultValue());
2400 parameter.setDefaultValue(currentParam.getDefaultValue());
2402 if (parameter.getType() != null && !parameter.getType().equalsIgnoreCase(currentParam.getType())) {
2403 log.debug("heat parameter type cannot be updated ({}). ignoring.", parameter.getType());
2404 parameter.setType(currentParam.getType());
2406 if (parameter.getDescription() != null && !parameter.getDescription()
2407 .equalsIgnoreCase(currentParam.getDescription())) {
2408 log.debug("heat parameter description cannot be updated ({}). ignoring.", parameter.getDescription());
2409 parameter.setDescription(currentParam.getDescription());
2412 // check and set current value
2413 if ((parameter.getCurrentValue() == null) && (currentParam.getDefaultValue() != null)) {
2414 log.debug("heat parameter current value is null. set it to default value {}). ignoring.", parameter.getDefaultValue());
2415 parameter.setCurrentValue(currentParam.getDefaultValue());
2421 private Map<String, HeatParameterDefinition> getMapOfParameters(List<HeatParameterDefinition> currentParameters) {
2423 Map<String, HeatParameterDefinition> currentParamsMap = new HashMap<>();
2424 for (HeatParameterDefinition param : currentParameters) {
2425 currentParamsMap.put(param.getUniqueId(), param);
2427 return currentParamsMap;
2430 private Either<Boolean, ResponseFormat> validateAndServiceApiUrl(ArtifactDefinition artifactInfo) {
2431 if (!ValidationUtils.validateStringNotEmpty(artifactInfo.getApiUrl())) {
2432 log.debug("Artifact url cannot be empty.");
2433 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_URL));
2435 artifactInfo.setApiUrl(artifactInfo.getApiUrl().toLowerCase());
2437 if (!ValidationUtils.validateUrl(artifactInfo.getApiUrl())) {
2438 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_SERVICE_API_URL));
2440 if (!ValidationUtils.validateUrlLength(artifactInfo.getApiUrl())) {
2441 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_URL, String.valueOf(ValidationUtils.API_URL_LENGTH)));
2444 return Either.left(true);
2447 private Either<Boolean, ResponseFormat> validateAndCleanDescription(ArtifactDefinition artifactInfo) {
2448 if (artifactInfo.getDescription() == null || artifactInfo.getDescription().isEmpty()) {
2449 log.debug("Artifact description cannot be empty.");
2450 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_DESCRIPTION));
2452 String description = artifactInfo.getDescription();
2453 description = ValidationUtils.removeNoneUtf8Chars(description);
2454 description = ValidationUtils.normaliseWhitespace(description);
2455 description = ValidationUtils.stripOctets(description);
2456 description = ValidationUtils.removeHtmlTagsOnly(description);
2457 if (!ValidationUtils.validateIsEnglish(description)) {
2458 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2460 if (!ValidationUtils.validateLength(description, ValidationUtils.ARTIFACT_DESCRIPTION_MAX_LENGTH)) {
2461 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_DESCRIPTION, String
2462 .valueOf(ValidationUtils.ARTIFACT_DESCRIPTION_MAX_LENGTH)));
2464 artifactInfo.setDescription(description);
2465 return Either.left(true);
2468 private Either<ArtifactDefinition, Operation> updateArtifactFlow(Component parent, String parentId, String artifactId, ArtifactDefinition artifactInfo, byte[] decodedPayload,
2469 ComponentTypeEnum componentType, AuditingActionEnum auditingAction, String interfaceType, String operationName) {
2470 DAOArtifactData artifactData = createEsArtifactData(artifactInfo, decodedPayload);
2471 if (artifactData == null) {
2472 BeEcompErrorManager.getInstance().logBeDaoSystemError(UPDATE_ARTIFACT);
2473 log.debug("Failed to create artifact object for ES.");
2474 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
2476 String prevArtifactId;
2477 String currArtifactId = artifactId;
2478 ArtifactDefinition artifactDefinition = artifactInfo;
2479 log.debug("Entry on graph is updated. Update artifact in ES");
2480 // Changing previous and current artifactId for auditing
2481 prevArtifactId = currArtifactId;
2482 currArtifactId = artifactDefinition.getUniqueId();
2484 NodeTypeEnum parentType = convertParentType(componentType);
2486 if (decodedPayload == null) {
2487 if (!artifactDefinition.getMandatory() || artifactDefinition.getEsId() != null) {
2488 Either<DAOArtifactData, CassandraOperationStatus> artifactFromCassandra = artifactCassandraDao.getArtifact(artifactDefinition.getEsId());
2489 if (artifactFromCassandra.isRight()) {
2490 throw new StorageException(artifactFromCassandra.right().value());
2492 // clone data to new artifact
2493 artifactData.setData(artifactFromCassandra.left().value().getData());
2494 artifactData.setId(artifactFromCassandra.left().value().getId());
2496 } else if (artifactDefinition.getEsId() == null) {
2497 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
2498 artifactData.setId(artifactDefinition.getUniqueId());
2501 Either<ArtifactDefinition, StorageOperationStatus> result = artifactToscaOperation.updateArtifactOnResource(artifactInfo,
2502 parent, artifactId, parentType, parentId, true);
2503 if (result.isRight()) {
2504 throw new StorageException(result.right().value());
2506 artifactDefinition = result.left().value();
2507 updateGeneratedIdInHeatEnv(parent, parentId, artifactId, artifactInfo, artifactDefinition, parentType);
2509 StorageOperationStatus storageOperationStatus = generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentType);
2510 if (storageOperationStatus != StorageOperationStatus.OK) {
2511 throw new StorageException(storageOperationStatus);
2513 if (artifactData.getData() != null) {
2514 if (!artifactDefinition.getDuplicated() || artifactData.getId() == null) {
2515 artifactData.setId(artifactDefinition.getEsId());
2517 saveArtifactInCassandra(artifactData, parent, artifactInfo, currArtifactId, prevArtifactId, auditingAction, componentType);
2519 return Either.left(artifactDefinition);
2522 private Either<Either<ArtifactDefinition, Operation>, ResponseFormat> updateArtifactsFlowForInterfaceOperations(
2523 Component parent, String parentId, String artifactId, ArtifactDefinition artifactInfo, User user,
2524 byte[] decodedPayload, ComponentTypeEnum componentType, AuditingActionEnum auditingAction, String interfaceType,
2525 String operationUuid, DAOArtifactData artifactData, String prevArtifactId, String currArtifactId,
2526 ArtifactDefinition artifactDefinition) {
2527 StorageOperationStatus error;
2528 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> resultOp;
2529 if (decodedPayload == null) {
2530 if (!artifactDefinition.getMandatory() || artifactDefinition.getEsId() != null) {
2531 Either<DAOArtifactData, CassandraOperationStatus> artifactFromCassandra = artifactCassandraDao.getArtifact(artifactDefinition
2533 if (artifactFromCassandra.isRight()) {
2534 log.debug("Failed to get artifact data from ES for artifact id {}", artifactId);
2535 error = DaoStatusConverter.convertCassandraStatusToStorageStatus(artifactFromCassandra.right()
2537 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(error));
2538 handleAuditing(auditingAction, parent, parentId, user, artifactInfo, prevArtifactId, currArtifactId, responseFormat, componentType, null);
2539 resultOp = Either.right(responseFormat);
2542 // clone data to new artifact
2543 artifactData.setData(artifactFromCassandra.left().value().getData());
2544 artifactData.setId(artifactFromCassandra.left().value().getId());
2546 // todo if not exist(first time)
2550 if (artifactDefinition.getEsId() == null) {
2551 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
2552 artifactData.setId(artifactDefinition.getUniqueId());
2555 NodeTypeEnum convertParentType = convertParentType(componentType);
2556 // Set additional fields for artifact
2557 artifactInfo.setArtifactLabel(artifactInfo.getArtifactName());
2558 artifactInfo.setArtifactDisplayName(artifactInfo.getArtifactName());
2560 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactOnResourceEither =
2561 artifactToscaOperation.updateArtifactOnResource(artifactInfo, parent, artifactId, convertParentType, parentId, true);
2562 if(updateArtifactOnResourceEither.isRight()){
2563 log.debug("Failed to persist operation artifact {} in resource, error is {}",artifactInfo.getArtifactName(), updateArtifactOnResourceEither.right().value());
2564 ActionStatus convertedFromStorageResponse = componentsUtils.convertFromStorageResponse(updateArtifactOnResourceEither.right().value());
2565 return Either.right(componentsUtils.getResponseFormat(convertedFromStorageResponse));
2567 if (artifactData.getData() != null) {
2568 CassandraOperationStatus cassandraOperationStatus = artifactCassandraDao.saveArtifact(artifactData);
2569 if(cassandraOperationStatus != CassandraOperationStatus.OK){
2570 log.debug("Failed to persist operation artifact {}, error is {}",artifactInfo.getArtifactName(),cassandraOperationStatus);
2571 StorageOperationStatus storageStatus = DaoStatusConverter.convertCassandraStatusToStorageStatus(cassandraOperationStatus);
2572 ActionStatus convertedFromStorageResponse = componentsUtils.convertFromStorageResponse(storageStatus);
2573 return Either.right(componentsUtils.getResponseFormat(convertedFromStorageResponse));
2577 Either<ArtifactDefinition, ResponseFormat> updateOprEither = updateOperationArtifact(parentId, interfaceType, operationUuid, updateArtifactOnResourceEither.left().value());
2578 if(updateOprEither.isRight()){
2579 return Either.right(updateOprEither.right().value());
2582 return Either.left(Either.left(updateOprEither.left().value()));
2585 private String updateGeneratedIdInHeatEnv(Component parent, String parentId, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType) {
2586 if (NodeTypeEnum.Resource == parentType) {
2587 return updateGeneratedIdInHeatEnv(parent.getDeploymentArtifacts(), parent, parentId, artifactId, artifactInfo, artifactDefinition, parentType, false);
2589 return artifactDefinition.getUniqueId();
2592 private String updateGeneratedIdInHeatEnv(Map<String, ArtifactDefinition> deploymentArtifacts, Component parentComponent, String parentId, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType, boolean isInstanceArtifact) {
2593 String artifactUniqueId;
2594 artifactUniqueId = artifactDefinition.getUniqueId();
2595 String artifactType = artifactInfo.getArtifactType();
2596 if ((ArtifactTypeEnum.HEAT.getType().equalsIgnoreCase(artifactType) ||
2597 ArtifactTypeEnum.HEAT_VOL.getType().equalsIgnoreCase(artifactType) ||
2598 ArtifactTypeEnum.HEAT_NET.getType().equalsIgnoreCase(artifactType))
2599 && !artifactUniqueId.equals(artifactId)) {
2600 // need to update the generated id in heat env
2601 Optional<Entry<String, ArtifactDefinition>> findFirst = deploymentArtifacts.entrySet()
2603 .filter(a -> artifactId.equals(a.getValue().getGeneratedFromId()))
2605 if (findFirst.isPresent()) {
2606 ArtifactDefinition artifactEnvInfo = findFirst.get().getValue();
2607 artifactEnvInfo.setIsFromCsar(artifactDefinition.getIsFromCsar());
2608 artifactEnvInfo.setArtifactChecksum(null);
2609 if (isInstanceArtifact) {
2610 artifactToscaOperation.updateHeatEnvArtifactOnInstance(parentComponent, artifactEnvInfo, artifactId, artifactUniqueId, parentType, parentId);
2612 artifactToscaOperation.updateHeatEnvArtifact(parentComponent, artifactEnvInfo, artifactId, artifactUniqueId, parentType, parentId);
2616 return artifactUniqueId;
2619 private String updateGeneratedIdInHeatEnvOnInstance(ComponentInstance parent, Component parentComponent, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType) {
2620 return updateGeneratedIdInHeatEnv(parent.getDeploymentArtifacts(), parentComponent, parent.getUniqueId(),artifactId, artifactInfo, artifactDefinition, parentType, true);
2624 private Either<byte[], ResponseFormat> handlePayload(ArtifactDefinition artifactInfo, boolean isArtifactMetadataUpdate) {
2625 log.trace("Starting payload handling");
2626 byte[] payload = artifactInfo.getPayloadData();
2627 byte[] decodedPayload = null;
2629 if (payload != null && payload.length != 0) {
2630 // the generated artifacts were already decoded by the handler
2631 decodedPayload = artifactInfo.getGenerated() ? payload : Base64.decodeBase64(payload);
2632 if (decodedPayload.length == 0) {
2633 log.debug("Failed to decode the payload.");
2634 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
2635 return Either.right(responseFormat);
2638 String checkSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload);
2639 artifactInfo.setArtifactChecksum(checkSum);
2640 log.trace("Calculated checksum, base64 payload: {}, checksum: {}", payload, checkSum);
2642 // Specific payload validations of different types
2643 Either<Boolean, ResponseFormat> result = Either.left(true);
2644 if (isDeploymentArtifact(artifactInfo)) {
2645 log.trace("Starting deployment artifacts payload validation");
2646 String artifactType = artifactInfo.getArtifactType();
2647 String fileExtension = GeneralUtility.getFilenameExtension(artifactInfo.getArtifactName());
2648 PayloadTypeEnum payloadType = ArtifactTypeToPayloadTypeSelector.getPayloadType(artifactType, fileExtension);
2649 Either<Boolean, ActionStatus> isPayloadValid = payloadType.isValid(decodedPayload);
2650 if (isPayloadValid.isRight()) {
2651 ResponseFormat responseFormat = componentsUtils.getResponseFormat(isPayloadValid.right().value(), artifactType);
2652 return Either.right(responseFormat);
2655 if (payloadType.isHeatRelated()) {
2656 log.trace("Payload is heat related so going to extract heat parameters for artifact type {}", artifactType);
2657 result = extractHeatParameters(artifactInfo);
2660 if (result.isRight()) {
2661 return Either.right(result.right().value());
2664 } // null/empty payload is normal if called from metadata update ONLY.
2665 // The validation of whether this is metadata/payload update case is
2666 // currently done separately
2668 if (!isArtifactMetadataUpdate) {
2669 log.debug("In artifact: {} Payload is missing.",artifactInfo.getArtifactName());
2670 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
2671 return Either.right(responseFormat);
2674 log.trace("Ended payload handling");
2675 return Either.left(decodedPayload);
2678 public Either<ArtifactDefinition, ResponseFormat> deleteArtifactByInterface(
2679 String resourceId, String userUserId, String artifactId, boolean inTransaction) {
2681 return toscaOperationFacade
2682 .getToscaElement(resourceId, JsonParseFlagEnum.ParseMetadata)
2683 .right().map(componentsUtils.toResponseFormat())
2684 .left().bind(parentComponent -> {
2685 User user = new User(userUserId);
2686 return handleDelete(resourceId, artifactId, user,
2688 false, inTransaction);
2692 private Operation convertToOperation(ArtifactDefinition artifactInfo, String operationName) {
2693 Operation op = new Operation();
2694 long time = System.currentTimeMillis();
2695 op.setCreationDate(time);
2697 String artifactName = artifactInfo.getArtifactName();
2698 artifactInfo.setArtifactName(createInterfaceArtifactNameFromOperation(operationName, artifactName));
2700 op.setImplementation(artifactInfo);
2701 op.setLastUpdateDate(time);
2705 private String createInterfaceArtifactNameFromOperation(String operationName, String artifactName) {
2706 String newArtifactName = operationName + "_" + artifactName;
2707 log.trace("converting artifact name {} to {}", artifactName, newArtifactName);
2708 return newArtifactName;
2712 public byte[] downloadRsrcArtifactByNames(String serviceName, String serviceVersion, String resourceName, String resourceVersion, String artifactName) {
2714 // General validation
2715 if (serviceName == null || serviceVersion == null || resourceName == null || resourceVersion == null || artifactName == null) {
2716 log.debug(NULL_PARAMETER);
2717 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2720 // Normalizing artifact name
2721 artifactName = ValidationUtils.normalizeFileName(artifactName);
2723 // Resource validation
2724 Resource resource = validateResourceNameAndVersion(resourceName, resourceVersion);
2725 String resourceId = resource.getUniqueId();
2727 // Service validation
2728 Service validateServiceNameAndVersion = validateServiceNameAndVersion(serviceName, serviceVersion);
2730 Map<String, ArtifactDefinition> artifacts = resource.getDeploymentArtifacts();
2731 if (artifacts == null || artifacts.isEmpty()) {
2732 log.debug("Deployment artifacts of resource {} are not found", resourceId);
2733 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactName);
2736 ArtifactDefinition deploymentArtifact = null;
2738 for (ArtifactDefinition artifactDefinition : artifacts.values()) {
2739 if (artifactDefinition.getArtifactName() != null && artifactDefinition.getArtifactName()
2740 .equals(artifactName)) {
2741 log.debug(FOUND_DEPLOYMENT_ARTIFACT, artifactName);
2742 deploymentArtifact = artifactDefinition;
2747 if (deploymentArtifact == null) {
2748 log.debug("No deployment artifact {} was found for resource {}", artifactName, resourceId);
2749 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactName);
2752 // Downloading the artifact
2753 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(deploymentArtifact);
2754 log.trace("Download of resource artifact succeeded, uniqueId {}", deploymentArtifact.getUniqueId());
2755 return downloadArtifactEither.getRight();
2759 public byte[] downloadRsrcInstArtifactByNames(String serviceName, String serviceVersion, String resourceInstanceName, String artifactName) {
2761 // General validation
2762 if (serviceName == null || serviceVersion == null || resourceInstanceName == null || artifactName == null) {
2763 log.debug(NULL_PARAMETER);
2764 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2767 // Normalizing artifact name
2768 artifactName = ValidationUtils.normalizeFileName(artifactName);
2770 // Service validation
2771 Service service = validateServiceNameAndVersion(serviceName, serviceVersion);
2773 // ResourceInstance validation
2774 ComponentInstance resourceInstance = validateResourceInstance(service, resourceInstanceName);
2776 Map<String, ArtifactDefinition> artifacts = resourceInstance.getDeploymentArtifacts();
2778 final String finalArtifactName = artifactName;
2779 Predicate<ArtifactDefinition> filterArtifactByName = p -> p.getArtifactName().equals(finalArtifactName);
2781 ArtifactDefinition deployableArtifact = artifacts==null ? null :
2782 artifacts.values().stream()
2783 .filter(filterArtifactByName)
2787 if (deployableArtifact == null) {
2788 log.debug("Deployment artifact with name {} not found", artifactName);
2789 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, artifactName));
2792 log.debug(FOUND_DEPLOYMENT_ARTIFACT, artifactName);
2793 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(deployableArtifact);
2795 log.trace("Download of resource artifact succeeded, uniqueId {}", deployableArtifact.getUniqueId());
2796 return downloadArtifactEither.getRight();
2799 private ComponentInstance validateResourceInstance(Service service, String resourceInstanceName) {
2801 List<ComponentInstance> riList = service.getComponentInstances();
2802 for (ComponentInstance ri : riList) {
2803 if (ri.getNormalizedName().equals(resourceInstanceName)) {
2807 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND, resourceInstanceName);
2810 private ComponentInstance validateResourceInstanceById(Component component, String resourceInstanceId) {
2812 List<ComponentInstance> riList = component.getComponentInstances();
2813 for (ComponentInstance ri : riList) {
2814 if (ri.getUniqueId().equals(resourceInstanceId)) {
2818 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceInstanceId);
2821 private Service validateServiceNameAndVersion(String serviceName, String serviceVersion) {
2823 Either<List<Service>, StorageOperationStatus> serviceListBySystemName = toscaOperationFacade.getBySystemName(ComponentTypeEnum.SERVICE, serviceName);
2824 if (serviceListBySystemName.isRight()) {
2825 log.debug("Couldn't fetch any service with name {}", serviceName);
2826 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceListBySystemName
2828 .value(), ComponentTypeEnum.SERVICE), serviceName);
2830 List<Service> serviceList = serviceListBySystemName.left().value();
2831 if (serviceList == null || serviceList.isEmpty()) {
2832 log.debug("Couldn't fetch any service with name {}", serviceName);
2833 throw new ByActionStatusComponentException(ActionStatus.SERVICE_NOT_FOUND, serviceName);
2836 Service foundService = null;
2837 for (Service service : serviceList) {
2838 if (service.getVersion().equals(serviceVersion)) {
2839 log.trace("Found service with version {}", serviceVersion);
2840 foundService = service;
2845 if (foundService == null) {
2846 log.debug("Couldn't find version {} for service {}", serviceVersion, serviceName);
2847 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_VERSION_NOT_FOUND, ComponentTypeEnum.SERVICE
2848 .getValue(), serviceVersion);
2850 return foundService;
2853 private Resource validateResourceNameAndVersion(String resourceName, String resourceVersion) {
2855 Either<Resource, StorageOperationStatus> resourceListBySystemName = toscaOperationFacade.getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion, JsonParseFlagEnum.ParseMetadata);
2856 if (resourceListBySystemName.isRight()) {
2857 log.debug("Couldn't fetch any resource with name {} and version {}. ", resourceName, resourceVersion);
2858 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(resourceListBySystemName
2860 .value()), resourceName);
2862 return resourceListBySystemName.left().value();
2865 public byte[] downloadServiceArtifactByNames(String serviceName, String serviceVersion, String artifactName) {
2867 log.trace("Starting download of service interface artifact, serviceName {}, serviceVersion {}, artifact name {}", serviceName, serviceVersion, artifactName);
2868 if (serviceName == null || serviceVersion == null || artifactName == null) {
2869 log.debug(NULL_PARAMETER);
2870 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2873 // Normalizing artifact name
2874 final String normalizedArtifactName = ValidationUtils.normalizeFileName(artifactName);
2876 // Service validation
2877 Service service = validateServiceNameAndVersion(serviceName, serviceVersion);
2878 // Looking for deployment or tosca artifacts
2879 String serviceId = service.getUniqueId();
2881 if (MapUtils.isEmpty(service.getDeploymentArtifacts()) && MapUtils.isEmpty(service.getToscaArtifacts())) {
2882 log.debug("Neither Deployment nor Tosca artifacts of service {} are found", serviceId);
2883 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, normalizedArtifactName);
2886 Optional<ArtifactDefinition> foundArtifactOptl = Optional.empty();
2888 if (!MapUtils.isEmpty(service.getDeploymentArtifacts())) {
2889 foundArtifactOptl = service.getDeploymentArtifacts().values().stream()
2890 // filters artifact by name
2891 .filter(a -> a.getArtifactName().equals(normalizedArtifactName)).findAny();
2893 if ((!foundArtifactOptl.isPresent()) && !MapUtils.isEmpty(service.getToscaArtifacts())) {
2894 foundArtifactOptl = service.getToscaArtifacts().values().stream()
2895 // filters TOSCA artifact by name
2896 .filter(a -> a.getArtifactName().equals(normalizedArtifactName)).findAny();
2898 if (!foundArtifactOptl.isPresent()) {
2899 log.debug("The artifact {} was not found for service {}", normalizedArtifactName, serviceId);
2900 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, normalizedArtifactName);
2902 log.debug(FOUND_DEPLOYMENT_ARTIFACT, normalizedArtifactName);
2903 // Downloading the artifact
2904 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(foundArtifactOptl
2906 log.trace("Download of service artifact succeeded, uniqueId {}", foundArtifactOptl.get().getUniqueId());
2907 return downloadArtifactEither.getRight();
2910 public ImmutablePair<String, byte[]> downloadArtifact(String parentId, String artifactUniqueId) {
2911 log.trace("Starting download of artifact, uniqueId {}", artifactUniqueId);
2912 Either<ArtifactDefinition, StorageOperationStatus> artifactById = artifactToscaOperation.getArtifactById(parentId, artifactUniqueId);
2913 if (artifactById.isRight()) {
2914 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(artifactById.right().value());
2915 log.debug("Error when getting artifact info by id{}, error: {}", artifactUniqueId, actionStatus);
2916 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatByArtifactId(actionStatus, ""));
2918 ArtifactDefinition artifactDefinition = artifactById.left().value();
2919 if (artifactDefinition == null) {
2920 log.debug("Empty artifact definition returned from DB by artifact id {}", artifactUniqueId);
2921 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, ""));
2924 return downloadArtifact(artifactDefinition);
2927 private boolean checkArtifactInComponent(Component component, String artifactId) {
2928 boolean found = false;
2929 Map<String, ArtifactDefinition> artifactsS = component.getArtifacts();
2930 if (artifactsS != null) {
2931 for (Map.Entry<String, ArtifactDefinition> entry : artifactsS.entrySet()) {
2932 if (entry.getValue().getUniqueId().equals(artifactId)) {
2938 Map<String, ArtifactDefinition> deploymentArtifactsS = component.getDeploymentArtifacts();
2939 if (!found && deploymentArtifactsS != null) {
2940 for (Map.Entry<String, ArtifactDefinition> entry : deploymentArtifactsS.entrySet()) {
2941 if (entry.getValue().getUniqueId().equals(artifactId)) {
2947 Map<String, ArtifactDefinition> toscaArtifactsS = component.getToscaArtifacts();
2948 if (!found && toscaArtifactsS != null) {
2949 for (Map.Entry<String, ArtifactDefinition> entry : toscaArtifactsS.entrySet()) {
2950 if (entry.getValue().getUniqueId().equals(artifactId)) {
2957 Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
2958 if (!found && interfaces != null) {
2959 for (Map.Entry<String, InterfaceDefinition> entry : interfaces.entrySet()) {
2960 Map<String, Operation> operations = entry.getValue().getOperationsMap();
2961 for (Map.Entry<String, Operation> entryOp : operations.entrySet()) {
2962 if (entryOp.getValue().getImplementation() != null && entryOp.getValue()
2963 .getImplementation()
2965 .equals(artifactId)) {
2972 switch (component.getComponentType()) {
2976 Map<String, ArtifactDefinition> apiArtifacts = ((Service) component).getServiceApiArtifacts();
2977 if (!found && apiArtifacts != null) {
2978 for (Map.Entry<String, ArtifactDefinition> entry : apiArtifacts.entrySet()) {
2979 if (entry.getValue().getUniqueId().equals(artifactId)) {
2993 private boolean checkArtifactInResourceInstance(Component component, String resourceInstanceId, String artifactId) {
2995 boolean found = false;
2996 List<ComponentInstance> resourceInstances = component.getComponentInstances();
2997 ComponentInstance resourceInstance = null;
2998 for (ComponentInstance ri : resourceInstances) {
2999 if (ri.getUniqueId().equals(resourceInstanceId)) {
3000 resourceInstance = ri;
3004 if (resourceInstance != null) {
3005 Map<String, ArtifactDefinition> artifacts = resourceInstance.getDeploymentArtifacts();
3006 if (artifacts != null) {
3007 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
3008 if (entry.getValue().getUniqueId().equals(artifactId)) {
3015 artifacts = resourceInstance.getArtifacts();
3016 if (artifacts != null) {
3017 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
3018 if (entry.getValue().getUniqueId().equals(artifactId)) {
3029 private Component validateComponentExists(String componentId, AuditingActionEnum auditingAction, User user, String artifactId, ComponentTypeEnum componentType,
3030 String containerComponentType) {
3032 ComponentTypeEnum componentForAudit = null == containerComponentType ? componentType : ComponentTypeEnum.findByParamName(containerComponentType);
3033 componentForAudit.getNodeType();
3035 Either<? extends Component, StorageOperationStatus> componentResult = toscaOperationFacade
3036 .getToscaFullElement(componentId);
3038 if (componentResult.isRight()) {
3039 ActionStatus status = componentForAudit == ComponentTypeEnum.RESOURCE ? ActionStatus.RESOURCE_NOT_FOUND : componentForAudit == ComponentTypeEnum.SERVICE ? ActionStatus.SERVICE_NOT_FOUND : ActionStatus.PRODUCT_NOT_FOUND;
3040 ResponseFormat responseFormat = componentsUtils.getResponseFormat(status, componentId);
3041 log.debug("Service not found, serviceId {}", componentId);
3042 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentForAudit, null);
3043 throw new ByActionStatusComponentException(status, componentId);
3045 return componentResult.left().value();
3048 private void validateWorkOnComponent(Component component, String userId, AuditingActionEnum auditingAction, User user, String artifactId, ArtifactOperationInfo operation) {
3049 if (operation.getArtifactOperationEnum() != ArtifactOperationEnum.DOWNLOAD && !operation.ignoreLifecycleState()) {
3051 validateCanWorkOnComponent(component, userId);
3052 }catch (ComponentException e) {
3053 String uniqueId = component.getUniqueId();
3054 log.debug("Service status isn't CHECKOUT or user isn't owner, serviceId {}", uniqueId);
3055 handleAuditing(auditingAction, component, uniqueId, user, null, null, artifactId, e.getResponseFormat(),
3056 component.getComponentType(), null);
3062 private void validateUserRole(User user, AuditingActionEnum auditingAction, String componentId, String artifactId, ComponentTypeEnum componentType, ArtifactOperationInfo operation) {
3064 if (operation.isNotDownload()) {
3065 String role = user.getRole();
3066 if (!role.equals(Role.ADMIN.name()) && !role.equals(Role.DESIGNER.name())) {
3067 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
3068 log.debug("addArtifact - user isn't permitted to perform operation, userId {}, role {}", user.getUserId(), role);
3069 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentType, null);
3070 throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
3075 private User validateUserExists(String userId, AuditingActionEnum auditingAction, String componentId, String artifactId, ComponentTypeEnum componentType, boolean inTransaction) {
3078 user = validateUserExists(userId);
3079 } catch(ByResponseFormatComponentException e){
3080 ResponseFormat responseFormat = e.getResponseFormat();
3081 handleComponentException(auditingAction, componentId, artifactId, responseFormat, componentType, userId);
3083 } catch(ByActionStatusComponentException e){
3084 ResponseFormat responseFormat = componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
3085 handleComponentException(auditingAction, componentId, artifactId, responseFormat, componentType, userId);
3091 private void handleComponentException(AuditingActionEnum auditingAction, String componentId, String artifactId,
3092 ResponseFormat responseFormat, ComponentTypeEnum componentType, String userId){
3093 User user = new User();
3094 user.setUserId(userId);
3095 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentType, null);
3098 protected AuditingActionEnum detectAuditingType(ArtifactOperationInfo operation, String origMd5) {
3099 AuditingActionEnum auditingAction = null;
3100 switch (operation.getArtifactOperationEnum()) {
3102 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_UPLOAD_BY_API : AuditingActionEnum.ARTIFACT_UPLOAD;
3105 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_UPLOAD_BY_API : origMd5 == null ? AuditingActionEnum.ARTIFACT_METADATA_UPDATE : AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE;
3108 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_DELETE_BY_API : AuditingActionEnum.ARTIFACT_DELETE;
3111 auditingAction = operation.isExternalApi() ? AuditingActionEnum.DOWNLOAD_ARTIFACT : AuditingActionEnum.ARTIFACT_DOWNLOAD;
3116 return auditingAction;
3119 private ImmutablePair<String, byte[]> downloadArtifact(ArtifactDefinition artifactDefinition) {
3120 String esArtifactId = artifactDefinition.getEsId();
3121 Either<DAOArtifactData, CassandraOperationStatus> artifactfromES = artifactCassandraDao.getArtifact(esArtifactId);
3122 if (artifactfromES.isRight()) {
3123 CassandraOperationStatus resourceUploadStatus = artifactfromES.right().value();
3124 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
3125 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageResponse);
3126 log.debug("Error when getting artifact from ES, error: {}", actionStatus);
3127 throw new ByActionStatusComponentException(actionStatus, artifactDefinition.getArtifactDisplayName());
3130 DAOArtifactData DAOArtifactData = artifactfromES.left().value();
3131 byte[] data = DAOArtifactData.getDataAsArray();
3133 log.debug("Artifact data from cassandra is null");
3134 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactDefinition.getArtifactDisplayName());
3136 String artifactName = artifactDefinition.getArtifactName();
3137 log.trace("Download of artifact succeeded, uniqueId {}, artifact file name {}", artifactDefinition.getUniqueId(), artifactName);
3138 return new ImmutablePair<>(artifactName, data);
3141 public DAOArtifactData createEsArtifactData(ArtifactDataDefinition artifactInfo, byte[] artifactPayload) {
3142 return new DAOArtifactData(artifactInfo.getEsId(), artifactPayload);
3145 private void saveArtifactInCassandra(DAOArtifactData artifactData, Component parent, ArtifactDefinition artifactInfo,
3146 String currArtifactId, String prevArtifactId, AuditingActionEnum auditingAction, ComponentTypeEnum componentType) {
3147 CassandraOperationStatus resourceUploadStatus = artifactCassandraDao.saveArtifact(artifactData);
3149 if (resourceUploadStatus == CassandraOperationStatus.OK) {
3150 log.debug("Artifact {} was saved in component {}.", artifactData.getId(), parent.getUniqueId());
3151 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
3152 handleAuditing(auditingAction, parent, parent.getUniqueId(), null, artifactInfo, prevArtifactId,
3153 currArtifactId, responseFormat, componentType, null);
3156 BeEcompErrorManager.getInstance().logBeDaoSystemError(UPDATE_ARTIFACT);
3157 log.info(FAILED_SAVE_ARTIFACT);
3158 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3159 handleAuditing(auditingAction, parent, parent.getUniqueId(), null, artifactInfo, prevArtifactId, currArtifactId, responseFormat, componentType, null);
3160 throw new StorageException(resourceUploadStatus);
3164 private boolean isArtifactMetadataUpdate(AuditingActionEnum auditingActionEnum) {
3165 return auditingActionEnum == AuditingActionEnum.ARTIFACT_METADATA_UPDATE;
3168 private boolean isDeploymentArtifact(ArtifactDefinition artifactInfo) {
3169 return ArtifactGroupTypeEnum.DEPLOYMENT == artifactInfo.getArtifactGroupType();
3172 private boolean isInformationalArtifact(final ArtifactDefinition artifactInfo) {
3173 return ArtifactGroupTypeEnum.INFORMATIONAL == artifactInfo.getArtifactGroupType();
3176 private boolean isHeatArtifact(final ArtifactDefinition artifactInfo) {
3177 final String artifactType = artifactInfo.getArtifactType();
3178 final ArtifactTypeEnum artifactTypeEnum = ArtifactTypeEnum.parse(artifactType);
3179 if (artifactTypeEnum == null) {
3180 artifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
3183 switch (artifactTypeEnum) {
3195 public ArtifactDefinition createArtifactPlaceHolderInfo(String resourceId, String logicalName, Map<String, Object> artifactInfoMap, String userUserId, ArtifactGroupTypeEnum groupType, boolean inTransaction) {
3196 User user = userBusinessLogic.getUser(userUserId, inTransaction);
3197 return createArtifactPlaceHolderInfo(resourceId, logicalName, artifactInfoMap, user, groupType);
3200 public ArtifactDefinition createArtifactPlaceHolderInfo(String resourceId, String logicalName, Map<String, Object> artifactInfoMap, User user, ArtifactGroupTypeEnum groupType) {
3201 ArtifactDefinition artifactInfo = new ArtifactDefinition();
3203 String artifactName = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_DISPLAY_NAME);
3204 String artifactType = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_TYPE);
3205 String artifactDescription = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_DESCRIPTION);
3207 artifactInfo.setArtifactDisplayName(artifactName);
3208 artifactInfo.setArtifactLabel(logicalName.toLowerCase());
3209 artifactInfo.setArtifactType(artifactType);
3210 artifactInfo.setDescription(artifactDescription);
3211 artifactInfo.setArtifactGroupType(groupType);
3212 nodeTemplateOperation.setDefaultArtifactTimeout(groupType, artifactInfo);
3214 setArtifactPlaceholderCommonFields(resourceId, user, artifactInfo);
3216 return artifactInfo;
3219 private void setArtifactPlaceholderCommonFields(String resourceId, User user, ArtifactDefinition artifactInfo) {
3220 String uniqueId = null;
3222 if (resourceId != null) {
3223 uniqueId = UniqueIdBuilder.buildPropertyUniqueId(resourceId.toLowerCase(), artifactInfo.getArtifactLabel()
3225 artifactInfo.setUniqueId(uniqueId);
3227 artifactInfo.setUserIdCreator(user.getUserId());
3228 String fullName = user.getFullName();
3229 artifactInfo.setUpdaterFullName(fullName);
3231 long time = System.currentTimeMillis();
3233 artifactInfo.setCreatorFullName(fullName);
3234 artifactInfo.setCreationDate(time);
3236 artifactInfo.setLastUpdateDate(time);
3237 artifactInfo.setUserIdLastUpdater(user.getUserId());
3239 artifactInfo.setMandatory(true);
3242 public Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifacts(String parentId, NodeTypeEnum parentType, ArtifactGroupTypeEnum groupType, String instanceId) {
3243 return artifactToscaOperation.getArtifacts(parentId, parentType, groupType, instanceId);
3246 public Either<ArtifactDefinition, StorageOperationStatus> addHeatEnvArtifact(ArtifactDefinition artifactHeatEnv, ArtifactDefinition artifact, Component component, NodeTypeEnum parentType, String instanceId) {
3247 return artifactToscaOperation.addHeatEnvArtifact(artifactHeatEnv, artifact, component, parentType, true, instanceId);
3250 private Either<DAOArtifactData, ResponseFormat> createEsHeatEnvArtifactDataFromString(ArtifactDefinition artifactDefinition, String payloadStr) {
3252 byte[] payload = payloadStr.getBytes();
3254 DAOArtifactData artifactData = createEsArtifactData(artifactDefinition, payload);
3255 return Either.left(artifactData);
3259 * @param artifactDefinition
3262 public Either<ArtifactDefinition, ResponseFormat> generateHeatEnvArtifact(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3263 String instanceId, boolean shouldLock, boolean inTransaction) {
3264 String payload = generateHeatEnvPayload(artifactDefinition);
3265 String prevUUID = artifactDefinition.getArtifactUUID();
3266 ArtifactDefinition clonedBeforeGenerate = new ArtifactDefinition(artifactDefinition);
3267 return generateAndSaveHeatEnvArtifact(artifactDefinition, payload, componentType, component, resourceInstanceName, modifier, instanceId, shouldLock, inTransaction)
3269 .bind(artifactDef -> updateArtifactOnGroupInstance(component, instanceId, prevUUID, clonedBeforeGenerate, artifactDef));
3272 public Either<ArtifactDefinition, ResponseFormat> forceGenerateHeatEnvArtifact(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3273 boolean shouldLock, boolean inTransaction, String instanceId) {
3274 String payload = generateHeatEnvPayload(artifactDefinition);
3275 String prevUUID = artifactDefinition.getArtifactUUID();
3276 ArtifactDefinition clonedBeforeGenerate = new ArtifactDefinition(artifactDefinition);
3277 return forceGenerateAndSaveHeatEnvArtifact(artifactDefinition, payload, componentType, component, resourceInstanceName, modifier, instanceId, shouldLock, inTransaction)
3279 .bind(artifactDef -> updateArtifactOnGroupInstance(component, instanceId, prevUUID, clonedBeforeGenerate, artifactDef));
3283 Either<ArtifactDefinition, ResponseFormat> updateArtifactOnGroupInstance(Component component, String instanceId, String prevUUID, ArtifactDefinition clonedBeforeGenerate, ArtifactDefinition updatedArtDef) {
3284 if (prevUUID == null || !prevUUID.equals(updatedArtDef.getArtifactUUID())) {
3285 List<ComponentInstance> componentInstances = component.getComponentInstances();
3286 if (componentInstances != null) {
3287 Optional<ComponentInstance> findFirst = componentInstances.stream()
3288 .filter(ci -> ci.getUniqueId()
3289 .equals(instanceId))
3291 if (findFirst.isPresent()) {
3292 ComponentInstance relevantInst = findFirst.get();
3293 List<GroupInstance> updatedGroupInstances = getUpdatedGroupInstances(updatedArtDef.getUniqueId(), clonedBeforeGenerate, relevantInst
3294 .getGroupInstances());
3296 if (CollectionUtils.isNotEmpty(updatedGroupInstances)) {
3297 updatedGroupInstances.forEach(gi -> {
3298 gi.getGroupInstanceArtifacts().add(updatedArtDef.getUniqueId());
3299 gi.getGroupInstanceArtifactsUuid().add(updatedArtDef.getArtifactUUID());
3301 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(component, instanceId, updatedGroupInstances);
3302 if (status.isRight()) {
3303 log.debug(FAILED_UPDATE_GROUPS, component.getUniqueId());
3304 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils
3305 .convertFromStorageResponse(status.right()
3306 .value()), clonedBeforeGenerate.getArtifactDisplayName());
3307 return Either.right(responseFormat);
3313 return Either.left(updatedArtDef);
3316 private String generateHeatEnvPayload(ArtifactDefinition artifactDefinition) {
3317 List<HeatParameterDefinition> heatParameters = artifactDefinition.getListHeatParameters();
3318 StringBuilder sb = new StringBuilder();
3319 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactHeader());
3320 sb.append("parameters:\n");
3321 if (heatParameters != null) {
3322 heatParameters.sort(Comparator.comparing(HeatParameterDataDefinition::getName));
3324 List<HeatParameterDefinition> empltyHeatValues = new ArrayList<>();
3326 for (HeatParameterDefinition heatParameterDefinition : heatParameters) {
3328 String heatValue = heatParameterDefinition.getCurrentValue();
3329 if (!ValidationUtils.validateStringNotEmpty(heatValue)) {
3330 heatValue = heatParameterDefinition.getDefaultValue();
3331 if (!ValidationUtils.validateStringNotEmpty(heatValue)) {
3332 empltyHeatValues.add(heatParameterDefinition);
3336 HeatParameterType type = HeatParameterType.isValidType(heatParameterDefinition.getType());
3341 .append(heatParameterDefinition.getName())
3344 .append(Boolean.parseBoolean(heatValue))
3349 .append(heatParameterDefinition.getName())
3352 .append(new BigDecimal(heatValue).toPlainString())
3355 case COMMA_DELIMITED_LIST:
3358 .append(heatParameterDefinition.getName())
3365 String value = heatValue;
3366 boolean starts = value.startsWith("\"");
3367 boolean ends = value.endsWith("\"");
3368 if (!(starts && ends)) {
3369 starts = value.startsWith("'");
3370 ends = value.endsWith("'");
3371 if (!(starts && ends)) {
3372 value = "\"" + value + "\"";
3376 .append(heatParameterDefinition.getName())
3386 if (!empltyHeatValues.isEmpty()) {
3387 empltyHeatValues.sort(Comparator.comparing(HeatParameterDataDefinition::getName));
3388 empltyHeatValues.forEach(hv -> {
3389 sb.append(" ").append(hv.getName()).append(":");
3390 HeatParameterType type = HeatParameterType.isValidType(hv.getType());
3391 if (type != null && type == HeatParameterType.STRING && (hv.getCurrentValue() != null && "".equals(hv
3392 .getCurrentValue()) || hv.getDefaultValue() != null && "".equals(hv.getDefaultValue()))) {
3393 sb.append(" \"\"").append("\n");
3396 sb.append(" ").append("\n");
3401 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter());
3404 return sb.toString().replaceAll("\\\\n", "\n");
3408 * @param artifactDefinition
3412 public Either<ArtifactDefinition, ResponseFormat> generateAndSaveHeatEnvArtifact(ArtifactDefinition artifactDefinition, String payload, ComponentTypeEnum componentType, Component component, String resourceInstanceName,
3413 User modifier, String instanceId, boolean shouldLock, boolean inTransaction) {
3414 return generateArtifactPayload(artifactDefinition, componentType, component, resourceInstanceName, modifier, shouldLock, inTransaction, artifactDefinition::getHeatParamsUpdateDate,
3415 () -> createEsHeatEnvArtifactDataFromString(artifactDefinition, payload), instanceId);
3419 public Either<ArtifactDefinition, ResponseFormat> forceGenerateAndSaveHeatEnvArtifact(ArtifactDefinition artifactDefinition, String payload, ComponentTypeEnum componentType, Component component, String resourceInstanceName,
3420 User modifier, String instanceId, boolean shouldLock, boolean inTransaction) {
3421 return generateArtifactPayload(artifactDefinition, componentType, component, resourceInstanceName, modifier, shouldLock, inTransaction, System::currentTimeMillis,
3422 () -> createEsHeatEnvArtifactDataFromString(artifactDefinition, payload), instanceId);
3426 protected Either<ArtifactDefinition, ResponseFormat> generateArtifactPayload(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3427 boolean shouldLock, boolean inTransaction, Supplier<Long> payloadUpdateDateGen, Supplier<Either<DAOArtifactData, ResponseFormat>> esDataCreator, String instanceId) {
3429 log.trace("Start generating payload for {} artifact {}", artifactDefinition.getArtifactType(), artifactDefinition
3431 if (artifactDefinition.getPayloadUpdateDate() == null || artifactDefinition.getPayloadUpdateDate() == 0 || artifactDefinition
3432 .getPayloadUpdateDate() <= payloadUpdateDateGen.get()) {
3434 log.trace("Generating payload for {} artifact {}", artifactDefinition.getArtifactType(), artifactDefinition.getEsId());
3435 Either<DAOArtifactData, ResponseFormat> artifactDataRes = esDataCreator.get();
3436 DAOArtifactData artifactData = null;
3438 if (artifactDataRes.isLeft()) {
3439 artifactData = artifactDataRes.left().value();
3442 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3443 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3444 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3445 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3447 return Either.right(artifactDataRes.right().value());
3449 String newCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(artifactData.getDataAsArray());
3451 String esArtifactId = artifactDefinition.getEsId();
3452 Either<DAOArtifactData, CassandraOperationStatus> artifactfromES;
3453 DAOArtifactData DAOArtifactData;
3454 if (esArtifactId != null && !esArtifactId.isEmpty() && artifactDefinition.getPayloadData() == null) {
3455 log.debug("Try to fetch artifact from cassandra with id : {}", esArtifactId);
3456 artifactfromES = artifactCassandraDao.getArtifact(esArtifactId);
3457 if (artifactfromES.isRight()) {
3458 CassandraOperationStatus resourceUploadStatus = artifactfromES.right().value();
3459 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
3460 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageResponse);
3461 log.debug("Error when getting artifact from ES, error: {} esid : {}", actionStatus, esArtifactId);
3462 return Either.right(componentsUtils.getResponseFormatByArtifactId(actionStatus, artifactDefinition.getArtifactDisplayName()));
3464 DAOArtifactData = artifactfromES.left().value();
3465 oldCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(DAOArtifactData.getDataAsArray());
3468 oldCheckSum = artifactDefinition.getArtifactChecksum();
3471 Either<ArtifactDefinition, StorageOperationStatus> updateArifactDefinitionStatus = null;
3475 lockComponent(component, "Update Artifact - lock resource: ");
3476 }catch (ComponentException e){
3477 handleAuditing(AuditingActionEnum.ARTIFACT_METADATA_UPDATE, component, component.getUniqueId(), modifier, null, null, artifactDefinition
3478 .getUniqueId(), e.getResponseFormat(), component.getComponentType(), null);
3483 if (oldCheckSum != null && oldCheckSum.equals(newCheckSum)) {
3485 artifactDefinition.setPayloadUpdateDate(payloadUpdateDateGen.get());
3486 updateArifactDefinitionStatus = artifactToscaOperation.updateArtifactOnResource(artifactDefinition, component
3487 ,artifactDefinition.getUniqueId(), componentType.getNodeType(), instanceId, true);
3488 log.trace("No real update done in payload for {} artifact, updating payloadUpdateDate {}", artifactDefinition
3489 .getArtifactType(), artifactDefinition.getEsId());
3490 if (updateArifactDefinitionStatus.isRight()) {
3491 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils.convertFromStorageResponse(updateArifactDefinitionStatus
3493 .value()), artifactDefinition.getArtifactDisplayName());
3494 log.trace("Failed to update payloadUpdateDate {}", artifactDefinition.getEsId());
3495 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3496 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3497 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3499 return Either.right(responseFormat);
3503 artifactDefinition.getArtifactChecksum();
3504 artifactDefinition.setArtifactChecksum(newCheckSum);
3505 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
3506 log.trace("No real update done in payload for {} artifact, updating payloadUpdateDate {}", artifactDefinition
3507 .getArtifactType(), artifactDefinition.getEsId());
3508 updateArifactDefinitionStatus = artifactToscaOperation.updateArtifactOnResource(artifactDefinition, component,
3509 artifactDefinition.getUniqueId(), componentType.getNodeType(), instanceId, true);
3511 log.trace("Update Payload {}", artifactDefinition.getEsId());
3513 if (updateArifactDefinitionStatus.isLeft()) {
3515 artifactDefinition = updateArifactDefinitionStatus.left().value();
3516 artifactData.setId(artifactDefinition.getUniqueId());
3517 CassandraOperationStatus saveArtifactStatus = artifactCassandraDao.saveArtifact(artifactData);
3519 if (saveArtifactStatus == CassandraOperationStatus.OK) {
3520 if (!inTransaction) {
3521 janusGraphDao.commit();
3523 log.debug("Artifact Saved In cassandra {}", artifactData.getId());
3524 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
3525 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3526 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3527 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3531 if (!inTransaction) {
3532 janusGraphDao.rollback();
3534 log.info("Failed to save artifact {}.", artifactData.getId());
3535 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3536 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3537 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3538 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3540 return Either.right(responseFormat);
3544 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils.convertFromStorageResponse(updateArifactDefinitionStatus
3546 .value()), artifactDefinition.getArtifactDisplayName());
3547 log.debug("Failed To update artifact {}", artifactData.getId());
3548 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3549 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3550 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3552 return Either.right(responseFormat);
3558 graphLockOperation.unlockComponent(component.getUniqueId(), component.getComponentType()
3564 return Either.left(artifactDefinition);
3568 public Map<String, Object> buildJsonForUpdateArtifact(ArtifactDefinition artifactDef, ArtifactGroupTypeEnum artifactGroupType, List<ArtifactTemplateInfo> updatedRequiredArtifacts) {
3569 return this.buildJsonForUpdateArtifact(artifactDef.getUniqueId(), artifactDef.getArtifactName(), artifactDef.getArtifactType(), artifactGroupType, artifactDef
3570 .getArtifactLabel(), artifactDef.getArtifactDisplayName(),
3571 artifactDef.getDescription(), artifactDef.getPayloadData(), updatedRequiredArtifacts, artifactDef.getListHeatParameters());
3575 public Map<String, Object> buildJsonForUpdateArtifact(String artifactId, String artifactName, String artifactType, ArtifactGroupTypeEnum artifactGroupType, String label, String displayName, String description, byte[] artifactContent,
3576 List<ArtifactTemplateInfo> updatedRequiredArtifacts, List<HeatParameterDefinition> heatParameters) {
3578 Map<String, Object> json = new HashMap<>();
3579 if (artifactId != null && !artifactId.isEmpty()) {
3580 json.put(Constants.ARTIFACT_ID, artifactId);
3583 json.put(Constants.ARTIFACT_NAME, artifactName);
3584 json.put(Constants.ARTIFACT_TYPE, artifactType);
3585 json.put(Constants.ARTIFACT_DESCRIPTION, description);
3587 if (artifactContent != null) {
3588 log.debug("payload is encoded. perform decode");
3589 String encodedPayload = Base64.encodeBase64String(artifactContent);
3590 json.put(Constants.ARTIFACT_PAYLOAD_DATA, encodedPayload);
3592 json.put(Constants.ARTIFACT_DISPLAY_NAME, displayName);
3593 json.put(Constants.ARTIFACT_LABEL, label);
3594 json.put(Constants.ARTIFACT_GROUP_TYPE, artifactGroupType.getType());
3595 json.put(Constants.REQUIRED_ARTIFACTS, (updatedRequiredArtifacts == null || updatedRequiredArtifacts.isEmpty()) ? new ArrayList<>()
3596 : updatedRequiredArtifacts.stream()
3597 .filter(e -> e.getType().equals(ArtifactTypeEnum.HEAT_ARTIFACT.getType()) || e
3599 .equals(ArtifactTypeEnum.HEAT_NESTED.getType()))
3600 .map(ArtifactTemplateInfo::getFileName)
3601 .collect(Collectors.toList()));
3602 json.put(Constants.ARTIFACT_HEAT_PARAMS, (heatParameters == null || heatParameters.isEmpty()) ? new ArrayList<>()
3607 public Either<ArtifactDefinition, Operation> updateResourceInstanceArtifactNoContent(String resourceId, Component containerComponent, User user, Map<String, Object> json, ArtifactOperationInfo operation, ArtifactDefinition artifactInfo) {
3609 String jsonStr = gson.toJson(json);
3610 ArtifactDefinition artifactDefinitionFromJson = artifactInfo == null ? RepresentationUtils.convertJsonToArtifactDefinition(jsonStr, ArtifactDefinition.class, false) : artifactInfo;
3611 String artifactUniqueId = artifactDefinitionFromJson == null ? null : artifactDefinitionFromJson.getUniqueId();
3612 Either<ArtifactDefinition, Operation> uploadArtifactToService = validateAndHandleArtifact(resourceId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactUniqueId,
3613 artifactDefinitionFromJson, null, jsonStr, null, null, user, containerComponent, false, false, true);
3615 return Either.left(uploadArtifactToService.left().value());
3618 private Either<ArtifactDefinition, Operation> handleUpdateHeatEnvAndHeatMeta(String componentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, String artifactId, User user, ComponentTypeEnum componentType,
3619 Component parent, String originData, String origMd5, ArtifactOperationInfo operation) {
3620 if (origMd5 != null) {
3621 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
3622 if (ArrayUtils.isNotEmpty(artifactInfo.getPayloadData())) {
3623 validateDeploymentArtifact(artifactInfo, parent);
3624 handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
3625 } else { // duplicate
3626 throw new ByActionStatusComponentException(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
3629 return updateHeatEnvParamsAndMetadata(componentId, artifactId, artifactInfo, user, auditingAction, parent, componentType, origMd5);
3632 private Either<ArtifactDefinition, Operation> updateHeatEnvParamsAndMetadata(String componentId, String artifactId, ArtifactDefinition artifactInfo, User user, AuditingActionEnum auditingAction, Component parent,
3633 ComponentTypeEnum componentType, String origMd5) {
3634 Either<ComponentInstance, ResponseFormat> getRI = getRIFromComponent(parent, componentId, artifactId, auditingAction, user);
3635 if (getRI.isRight()) {
3636 throw new ByResponseFormatComponentException(getRI.right().value());
3638 ComponentInstance ri = getRI.left().value();
3639 Either<ArtifactDefinition, ResponseFormat> getArtifactRes = getArtifactFromRI(parent, ri, componentId, artifactId, auditingAction, user);
3640 if (getArtifactRes.isRight()) {
3641 throw new ByResponseFormatComponentException(getArtifactRes.right().value());
3643 ArtifactDefinition currArtifact = getArtifactRes.left().value();
3645 if (currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT.getType()) ||
3646 currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT_VOL.getType()) ||
3647 currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT_NET.getType())) {
3648 throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
3650 List<HeatParameterDefinition> currentHeatEnvParams = currArtifact.getListHeatParameters();
3651 List<HeatParameterDefinition> updatedHeatEnvParams = artifactInfo.getListHeatParameters();
3654 if (origMd5 != null) {
3655 Either<List<HeatParameterDefinition>, ResponseFormat> uploadParamsValidationResult = validateUploadParamsFromEnvFile(auditingAction, parent, user, artifactInfo,
3656 artifactId, componentType, ri.getName(), currentHeatEnvParams, updatedHeatEnvParams, currArtifact.getArtifactName());
3657 if (uploadParamsValidationResult.isRight()) {
3658 throw new ByResponseFormatComponentException(uploadParamsValidationResult.right().value());
3660 artifactInfo.setListHeatParameters(updatedHeatEnvParams);
3663 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParamers = validateAndConvertHeatParameters(artifactInfo, ArtifactTypeEnum.HEAT_ENV.getType());
3664 if (validateAndConvertHeatParamers.isRight()) {
3665 throw new ByResponseFormatComponentException(validateAndConvertHeatParamers.right().value());
3668 if (updatedHeatEnvParams != null && !updatedHeatEnvParams.isEmpty()) {
3669 // fill reduced heat env parameters List for updating
3670 boolean updateRequired = replaceCurrHeatValueWithUpdatedValue(currentHeatEnvParams, updatedHeatEnvParams);
3671 if (updateRequired) {
3672 currArtifact.setHeatParamsUpdateDate(System.currentTimeMillis());
3673 currArtifact.setListHeatParameters(currentHeatEnvParams);
3674 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactRes = artifactToscaOperation.updateArtifactOnResource(
3675 currArtifact, parent, currArtifact.getUniqueId(), componentType.getNodeType(), componentId, true);
3676 if (updateArtifactRes.isRight()) {
3677 log.debug("Failed to update artifact on graph - {}", artifactId);
3678 throw new StorageException(updateArtifactRes.right().value());
3680 StorageOperationStatus error = generateCustomizationUUIDOnGroupInstance(ri, updateArtifactRes.left().value().getUniqueId(), parent.getUniqueId());
3681 if (error != StorageOperationStatus.OK) {
3682 throw new StorageException(error);
3686 updateHeatMetaDataIfNeeded(componentId,user,auditingAction,componentType, parent,ri,artifactInfo);
3687 StorageOperationStatus error = generateCustomizationUUIDOnInstance(parent.getUniqueId(), ri.getUniqueId(), componentType);
3688 if (error != StorageOperationStatus.OK) {
3689 throw new StorageException(error);
3692 return Either.left(currArtifact);
3696 updateHeatMetaDataIfNeeded(String componentId, User user, AuditingActionEnum auditingAction, ComponentTypeEnum componentType, Component parent, ComponentInstance resourceInstance, ArtifactDefinition updatedHeatEnvArtifact) {
3697 String heatArtifactId = updatedHeatEnvArtifact.getGeneratedFromId();
3698 Either<ArtifactDefinition, ResponseFormat> getArtifactRes = getArtifactFromRI(parent, resourceInstance, componentId, heatArtifactId, auditingAction, user);
3699 if (getArtifactRes.isRight()) {
3700 throw new ByResponseFormatComponentException(getArtifactRes.right().value());
3702 ArtifactDefinition heatArtifactToUpdate = getArtifactRes.left().value();
3703 if (isUpdateHeatMetaDataNeeded(updatedHeatEnvArtifact, heatArtifactToUpdate)) {
3704 validateHeatMetaData(updatedHeatEnvArtifact);
3705 updateHeatMetadataFromHeatEnv(updatedHeatEnvArtifact, heatArtifactToUpdate);
3706 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactRes = artifactToscaOperation.updateArtifactOnResource(heatArtifactToUpdate, parent,
3707 heatArtifactToUpdate.getUniqueId(), componentType.getNodeType(), componentId, false);
3709 if (updateArtifactRes.isRight()) {
3710 log.debug("Failed to update artifact on graph - {}", heatArtifactId);
3711 throw new StorageException(updateArtifactRes.right().value());
3713 ArtifactDefinition artifactDefinition = updateArtifactRes.left().value();
3714 updateGeneratedIdInHeatEnvOnInstance(resourceInstance, parent, heatArtifactId, heatArtifactToUpdate, artifactDefinition, componentType.getNodeType());
3715 StorageOperationStatus error = generateCustomizationUUIDOnGroupInstance(resourceInstance, artifactDefinition.getUniqueId(), parent.getUniqueId());
3716 if (error != StorageOperationStatus.OK) {
3717 throw new StorageException(error);
3722 private void validateHeatMetaData(ArtifactDefinition updatedHeatEnv) {
3723 Integer maxMinutes = ConfigurationManager.getConfigurationManager().getConfiguration().getHeatArtifactDeploymentTimeout().getMaxMinutes();
3724 Integer minMinutes = ConfigurationManager.getConfigurationManager().getConfiguration().getHeatArtifactDeploymentTimeout().getMinMinutes();
3725 Integer updateTimeout = updatedHeatEnv.getTimeout();
3726 if (updateTimeout > maxMinutes || updateTimeout < minMinutes) {
3727 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_TIMEOUT);
3731 private boolean isUpdateHeatMetaDataNeeded(ArtifactDefinition updatedHeatEnv, ArtifactDefinition origHeat) {
3732 // currently only timeout metadata can be updated
3733 return !origHeat.getTimeout().equals(updatedHeatEnv.getTimeout());
3736 private void updateHeatMetadataFromHeatEnv(ArtifactDefinition updatedHeatEnv, ArtifactDefinition origHeat) {
3737 // currently only timeout metadata can be updated
3738 origHeat.setTimeout(updatedHeatEnv.getTimeout());
3741 private boolean replaceCurrHeatValueWithUpdatedValue(List<HeatParameterDefinition> currentHeatEnvParams, List<HeatParameterDefinition> updatedHeatEnvParams) {
3742 boolean isUpdate = false;
3743 List<String> currentParamsNames = currentHeatEnvParams.stream().map(x -> x.getName()).collect(Collectors.toList());
3744 for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) {
3745 String paramName = heatEnvParam.getName();
3746 validateParamName(paramName, currentParamsNames);
3747 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3748 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3749 String updatedParamValue = heatEnvParam.getCurrentValue();
3750 if (!Objects.equals(updatedParamValue, currHeatParam.getCurrentValue())) {
3751 currHeatParam.setCurrentValue(updatedParamValue);
3760 private void validateParamName(String paramName, List<String> heatParamsNames) {
3761 if (!heatParamsNames.contains(paramName)) {
3762 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, paramName);
3766 private Either<ArtifactDefinition, Operation> updateHeatParams(String componentId, ArtifactDefinition artifactEnvInfo, AuditingActionEnum auditingAction, Component parent,
3767 ComponentTypeEnum componentType, ArtifactDefinition currHeatArtifact, boolean needToUpdateGroup) {
3768 Either<ArtifactDefinition, Operation> insideEither = null;
3769 String currentHeatId = currHeatArtifact.getUniqueId();
3771 String esArtifactId = currHeatArtifact.getEsId();
3772 Either<DAOArtifactData, CassandraOperationStatus> artifactFromES = artifactCassandraDao.getArtifact(esArtifactId);
3773 if (artifactFromES.isRight()) {
3774 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(artifactFromES.right().value());
3775 throw new StorageException(storageResponse, currHeatArtifact.getArtifactDisplayName());
3778 DAOArtifactData DAOArtifactData = artifactFromES.left().value();
3779 ArtifactDefinition updatedHeatArt = currHeatArtifact;
3780 List<HeatParameterDefinition> updatedHeatEnvParams = artifactEnvInfo.getListHeatParameters();
3781 List<HeatParameterDefinition> currentHeatEnvParams = currHeatArtifact.getListHeatParameters();
3782 List<HeatParameterDefinition> newHeatEnvParams = new ArrayList<>();
3784 if (CollectionUtils.isNotEmpty(updatedHeatEnvParams) && CollectionUtils.isNotEmpty(currentHeatEnvParams)) {
3785 //TODO: improve complexity - currently N^2
3787 for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) {
3788 paramName = heatEnvParam.getName();
3789 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3790 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3791 String updatedParamValue = heatEnvParam.getCurrentValue();
3792 if (updatedParamValue == null) {
3793 updatedParamValue = heatEnvParam.getDefaultValue();
3795 HeatParameterType paramType = HeatParameterType.isValidType(currHeatParam.getType());
3796 if (!paramType.getValidator().isValid(updatedParamValue, null)) {
3797 throw new ByActionStatusComponentException(ActionStatus.INVALID_HEAT_PARAMETER_VALUE,
3798 ArtifactTypeEnum.HEAT_ENV.getType(), paramType.getType(), paramName);
3800 currHeatParam.setCurrentValue(paramType.getConverter().convert(updatedParamValue, null, null));
3801 newHeatEnvParams.add(currHeatParam);
3806 if (!newHeatEnvParams.isEmpty()) {
3807 currHeatArtifact.setListHeatParameters(currentHeatEnvParams);
3808 Either<ArtifactDefinition, StorageOperationStatus> operationStatus = artifactToscaOperation.updateArtifactOnResource(
3809 currHeatArtifact, parent, currHeatArtifact.getUniqueId(), componentType.getNodeType(), componentId, true);
3811 if (operationStatus.isRight()) {
3812 log.debug("Failed to update artifact on graph - {}", currHeatArtifact.getUniqueId());
3813 throw new StorageException(operationStatus.right().value());
3815 updatedHeatArt = operationStatus.left().value();
3816 if (!updatedHeatArt.getDuplicated() || DAOArtifactData.getId() == null) {
3817 DAOArtifactData.setId(updatedHeatArt.getEsId());
3819 saveArtifactInCassandra(DAOArtifactData, parent, artifactEnvInfo, currentHeatId, updatedHeatArt
3820 .getUniqueId(), auditingAction, componentType);
3821 insideEither = Either.left(updatedHeatArt);
3824 Either<ArtifactDefinition, StorageOperationStatus> updateHeatEnvArtifact;
3825 if (!currentHeatId.equals(updatedHeatArt.getUniqueId())) {
3826 artifactEnvInfo.setArtifactChecksum(null);
3827 updateHeatEnvArtifact = artifactToscaOperation.updateHeatEnvArtifact(parent, artifactEnvInfo, currentHeatId, updatedHeatArt
3828 .getUniqueId(), componentType.getNodeType(), componentId);
3831 //TODO Andrey check if componentId = parent.getUniqeId
3832 updateHeatEnvArtifact = artifactToscaOperation.updateHeatEnvPlaceholder(artifactEnvInfo, parent, componentType
3836 if (needToUpdateGroup && updateHeatEnvArtifact.isLeft()) {
3837 ActionStatus result = updateGroupForHeat(currHeatArtifact, updatedHeatArt, artifactEnvInfo,
3838 updateHeatEnvArtifact.left().value(), parent);
3839 if (result != ActionStatus.OK) {
3840 throw new ByActionStatusComponentException(result);
3843 if (updatedHeatEnvParams.isEmpty()) {
3844 throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML, currHeatArtifact.getArtifactName());
3846 return insideEither;
3850 private StorageOperationStatus generateCustomizationUUIDOnGroupInstance(ComponentInstance ri, String artifactId, String componentId) {
3851 StorageOperationStatus error = StorageOperationStatus.OK;
3852 log.debug("Need to re-generate customization UUID for group instance on component instance {}", ri.getUniqueId());
3853 List<GroupInstance> groupsInstances = ri.getGroupInstances();
3854 List<String> groupInstancesId = null;
3855 if (groupsInstances != null && !groupsInstances.isEmpty()) {
3856 groupInstancesId = groupsInstances.stream()
3857 .filter(p -> p.getGroupInstanceArtifacts() != null && p.getGroupInstanceArtifacts()
3858 .contains(artifactId))
3859 .map(GroupInstanceDataDefinition::getUniqueId)
3860 .collect(Collectors.toList());
3862 if (groupInstancesId != null && !groupInstancesId.isEmpty()) {
3863 toscaOperationFacade.generateCustomizationUUIDOnInstanceGroup(componentId, ri.getUniqueId(), groupInstancesId);
3869 public Either<List<HeatParameterDefinition>, ResponseFormat> validateUploadParamsFromEnvFile(AuditingActionEnum auditingAction, Component parent, User user, ArtifactDefinition artifactInfo, String artifactId, ComponentTypeEnum componentType,
3870 String riName, List<HeatParameterDefinition> currentHeatEnvParams, List<HeatParameterDefinition> updatedHeatEnvParams, String currArtifactName) {
3872 if (updatedHeatEnvParams == null || updatedHeatEnvParams.isEmpty()) {
3873 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, artifactInfo
3874 .getArtifactName(), currArtifactName);
3875 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, artifactInfo, null, artifactId, responseFormat, componentType, riName);
3876 return Either.right(responseFormat);
3879 for (HeatParameterDefinition uploadedHeatParam : updatedHeatEnvParams) {
3880 String paramName = uploadedHeatParam.getName();
3881 boolean isExistsInHeat = false;
3882 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3883 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3885 isExistsInHeat = true;
3886 uploadedHeatParam.setType(currHeatParam.getType());
3887 uploadedHeatParam.setCurrentValue(uploadedHeatParam.getDefaultValue());
3888 uploadedHeatParam.setDefaultValue(currHeatParam.getDefaultValue());
3889 uploadedHeatParam.setUniqueId(currHeatParam.getUniqueId());
3893 if (!isExistsInHeat) {
3894 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISMATCH_HEAT_VS_HEAT_ENV, currArtifactName);
3895 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, artifactInfo, null, artifactId, responseFormat, componentType, riName);
3896 return Either.right(responseFormat);
3899 return Either.left(updatedHeatEnvParams);
3902 private Either<ComponentInstance, ResponseFormat> getRIFromComponent(Component component, String riID, String artifactId, AuditingActionEnum auditingAction, User user) {
3903 ResponseFormat responseFormat = null;
3904 List<ComponentInstance> ris = component.getComponentInstances();
3905 for (ComponentInstance ri : ris) {
3906 if (riID.equals(ri.getUniqueId())) {
3907 return Either.left(ri);
3910 responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, riID);
3911 log.debug("Resource Instance not found, resourceInstanceId {}", riID);
3912 handleAuditing(auditingAction, null, riID, user, null, null, artifactId, responseFormat, ComponentTypeEnum.RESOURCE_INSTANCE, null);
3913 return Either.right(responseFormat);
3916 private Either<ArtifactDefinition, ResponseFormat> getArtifactFromRI(Component component, ComponentInstance ri, String riID, String artifactId, AuditingActionEnum auditingAction, User user) {
3917 ResponseFormat responseFormat = null;
3918 Map<String, ArtifactDefinition> rtifactsMap = ri.getDeploymentArtifacts();
3919 for (ArtifactDefinition artifact : rtifactsMap.values()) {
3920 if (artifactId.equals(artifact.getUniqueId())) {
3921 return Either.left(artifact);
3924 responseFormat = componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, riID, component.getUniqueId());
3925 handleAuditing(auditingAction, component, riID, user, null, null, artifactId, responseFormat, ComponentTypeEnum.RESOURCE_INSTANCE, ri
3927 return Either.right(responseFormat);
3930 public ArtifactDefinition extractArtifactDefinition(Either<ArtifactDefinition, Operation> eitherArtifact) {
3931 ArtifactDefinition ret;
3932 if (eitherArtifact.isLeft()) {
3933 ret = eitherArtifact.left().value();
3936 ret = eitherArtifact.right().value().getImplementationArtifact();
3941 public byte[] downloadComponentArtifactByUUIDs(ComponentTypeEnum componentType, String componentUuid, String artifactUUID, ResourceCommonInfo resourceCommonInfo) {
3942 Component component = getComponentByUuid(componentType, componentUuid);
3943 resourceCommonInfo.setResourceName(component.getName());
3944 return downloadArtifact(component.getAllArtifacts(), artifactUUID, component.getName());
3948 * downloads an artifact of resource instance of component by UUIDs
3950 * @param componentType
3951 * @param componentUuid
3952 * @param resourceInstanceName
3953 * @param artifactUUID
3956 public byte[] downloadResourceInstanceArtifactByUUIDs(ComponentTypeEnum componentType, String componentUuid,
3957 String resourceInstanceName, String artifactUUID) {
3958 ComponentInstance resourceInstance = getRelatedComponentInstance(componentType, componentUuid, resourceInstanceName);
3959 return downloadArtifact(resourceInstance == null ? null : resourceInstance.getDeploymentArtifacts(),
3960 artifactUUID, resourceInstance.getName());
3964 * uploads an artifact to a component by UUID
3968 * @param componentType
3969 * @param componentUuid
3970 * @param resourceCommonInfo
3974 public ArtifactDefinition uploadArtifactToComponentByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType,
3975 String componentUuid, ResourceCommonInfo resourceCommonInfo, ArtifactOperationInfo operation) {
3976 Either<ArtifactDefinition, Operation> actionResult;
3977 Component component;
3979 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
3980 String origMd5 = request.getHeader(Constants.MD5_HEADER);
3981 String userId = request.getHeader(Constants.USER_ID_HEADER);
3983 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes =
3984 toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
3985 if (getComponentRes.isRight()) {
3986 StorageOperationStatus status = getComponentRes.right().value();
3987 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
3988 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), componentUuid);
3991 ComponentMetadataDataDefinition componentMetadataDataDefinition = getComponentRes.left().value().getMetadataDataDefinition();
3992 componentId = componentMetadataDataDefinition.getUniqueId();
3993 String componentName = componentMetadataDataDefinition.getName();
3995 if (!componentMetadataDataDefinition
3997 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
3998 component = checkoutParentComponent(componentType, componentId, userId);
3999 if (component != null) {
4000 componentId = component.getUniqueId();
4001 componentName = component.getName();
4004 resourceCommonInfo.setResourceName(componentName);
4006 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, null, artifactInfo,
4007 origMd5, data, null, null, null, null);
4008 return actionResult.left().value();
4012 * upload an artifact to a resource instance by UUID
4016 * @param componentType
4017 * @param componentUuid
4018 * @param resourceInstanceName
4022 public ArtifactDefinition uploadArtifactToRiByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName,
4023 ArtifactOperationInfo operation) {
4024 Either<ArtifactDefinition, Operation> actionResult;
4025 Component component = null;
4026 String componentInstanceId;
4028 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4029 String userId = request.getHeader(Constants.USER_ID_HEADER);
4031 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4032 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4033 if (getComponentRes.isRight()) {
4034 StorageOperationStatus status = getComponentRes.right().value();
4035 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4036 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), resourceInstanceName);
4038 if (!getComponentRes.left()
4040 .getMetadataDataDefinition()
4042 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4043 component = checkoutParentComponent(componentType, getComponentRes.left()
4045 .getMetadataDataDefinition()
4046 .getUniqueId(), userId);
4048 if (component == null) {
4049 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4052 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4054 componentInstanceId = componentRiPair.getRight().getUniqueId();
4055 componentId = componentRiPair.getLeft().getUniqueId();
4056 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
4058 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE,
4059 operation, null, artifactInfo, origMd5, data, null, null,
4060 componentId, ComponentTypeEnum.findParamByType(componentType));
4062 return actionResult.left().value();
4066 * updates an artifact on a component by UUID
4070 * @param componentType
4071 * @param componentUuid
4072 * @param artifactUUID
4073 * @param resourceCommonInfo
4074 * @param operation TODO
4077 public ArtifactDefinition updateArtifactOnComponentByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String artifactUUID,
4078 ResourceCommonInfo resourceCommonInfo, ArtifactOperationInfo operation) {
4079 Either<ArtifactDefinition, Operation> actionResult;
4080 Component component;
4083 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinitionForUpdate(data, ArtifactDefinition.class);
4084 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4085 String userId = request.getHeader(Constants.USER_ID_HEADER);
4087 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4088 if (getComponentRes.isRight()) {
4089 StorageOperationStatus status = getComponentRes.right().value();
4090 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4091 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4093 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4094 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4096 if (!getComponentRes.left()
4098 .getMetadataDataDefinition()
4100 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4101 component = checkoutParentComponent(componentType, componentId, userId);
4102 if (component != null) {
4103 componentId = component.getUniqueId();
4104 componentName = component.getName();
4107 resourceCommonInfo.setResourceName(componentName);
4108 artifactId = getLatestParentArtifactDataIdByArtifactUUID(artifactUUID, componentId, componentType);
4109 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, artifactId, artifactInfo,
4110 origMd5, data, null, null, null, null);
4111 if (actionResult.isRight()) {
4112 log.debug(FAILED_UPLOAD_ARTIFACT_TO_COMPONENT, componentType, componentUuid, actionResult
4117 return actionResult.left().value();
4121 * updates an artifact on a resource instance by UUID
4125 * @param componentType
4126 * @param componentUuid
4127 * @param resourceInstanceName
4128 * @param artifactUUID
4129 * @param operation TODO
4132 public ArtifactDefinition updateArtifactOnRiByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName, String artifactUUID,
4133 ArtifactOperationInfo operation) {
4135 Either<ArtifactDefinition, Operation> actionResult;
4136 Component component = null;
4137 String componentInstanceId;
4140 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4141 String userId = request.getHeader(Constants.USER_ID_HEADER);
4143 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4144 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4145 if (getComponentRes.isRight()) {
4146 StorageOperationStatus status = getComponentRes.right().value();
4147 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4148 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4150 if (!getComponentRes.left()
4152 .getMetadataDataDefinition()
4154 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4155 component = checkoutParentComponent(componentType, getComponentRes.left()
4157 .getMetadataDataDefinition()
4158 .getUniqueId(), userId);
4160 if (component == null) {
4161 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4164 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4166 componentInstanceId = componentRiPair.getRight().getUniqueId();
4167 componentId = componentRiPair.getLeft().getUniqueId();
4168 artifactId = findArtifactId(componentRiPair.getRight(), artifactUUID);
4169 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
4171 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactId, artifactInfo, origMd5, data, null, null, componentId, ComponentTypeEnum
4172 .findParamByType(componentType));
4173 return actionResult.left().value();
4176 private Either<ArtifactDefinition, ResponseFormat> updateOperationArtifact(String componentId, String interfaceType, String operationUuid, ArtifactDefinition artifactInfo){
4177 Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId);
4178 if (componentStorageOperationStatusEither.isRight()) {
4179 StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value();
4180 log.debug("Failed to fetch resource information by resource id, error {}", errorStatus);
4181 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus)));
4183 Component storedComponent = componentStorageOperationStatusEither.left().value();
4185 Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils.getInterfaceDefinitionFromComponentByInterfaceType(storedComponent, interfaceType);
4186 if(!optionalInterface.isPresent()) {
4187 log.debug("Failed to get resource interface for resource Id {}", componentId);
4188 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceType));
4191 //fetch the operation from storage
4192 InterfaceDefinition gotInterface = optionalInterface.get();
4193 Map<String, Operation> operationsMap = gotInterface.getOperationsMap();
4194 Optional<Operation> optionalOperation = operationsMap.values()
4196 .filter(o -> o.getUniqueId().equals(operationUuid))
4198 if (!optionalOperation.isPresent()) {
4199 log.debug("Failed to get resource interface operation for resource Id {} and operationId {}", componentId, operationUuid);
4200 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, componentId);
4201 return Either.right(responseFormat);
4204 Operation operation = optionalOperation.get();
4205 ArtifactDefinition implementationArtifact = operation.getImplementationArtifact();
4206 implementationArtifact.setArtifactUUID(artifactInfo.getArtifactUUID());
4207 implementationArtifact.setUniqueId(artifactInfo.getUniqueId());
4208 implementationArtifact.setArtifactName(artifactInfo.getArtifactName());
4209 implementationArtifact.setDescription(artifactInfo.getDescription());
4210 implementationArtifact.setArtifactType(artifactInfo.getArtifactType());
4211 implementationArtifact.setArtifactLabel(artifactInfo.getArtifactLabel());
4212 implementationArtifact.setArtifactDisplayName(artifactInfo.getArtifactDisplayName());
4213 implementationArtifact.setEsId(artifactInfo.getEsId());
4214 operation.setImplementation(implementationArtifact);
4215 gotInterface.setOperationsMap(operationsMap);
4216 Either<List<InterfaceDefinition>, StorageOperationStatus> interfaceDefinitionStorageOperationStatusEither =
4217 interfaceOperation.updateInterfaces(storedComponent.getUniqueId(), Collections.singletonList(gotInterface));
4218 if (interfaceDefinitionStorageOperationStatusEither.isRight()){
4219 StorageOperationStatus storageOperationStatus = interfaceDefinitionStorageOperationStatusEither.right().value();
4220 ActionStatus actionStatus =
4221 componentsUtils.convertFromStorageResponseForDataType(storageOperationStatus);
4222 return Either.right(componentsUtils.getResponseFormat(actionStatus));
4225 return Either.left(artifactInfo);
4229 * updates an artifact on a component by UUID
4233 * @param componentType
4234 * @param componentUuid
4235 * @param artifactUUID
4239 public Either<ArtifactDefinition, ResponseFormat> updateArtifactOnInterfaceOperationByResourceUUID(
4240 String data, HttpServletRequest request, ComponentTypeEnum componentType,
4241 String componentUuid, String interfaceUUID, String operationUUID, String artifactUUID,
4242 ResourceCommonInfo resourceCommonInfo,ArtifactOperationInfo operation) {
4243 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
4244 Either<ArtifactDefinition, ResponseFormat> updateArtifactResult;
4245 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> actionResult = null;
4246 ArtifactDefinition updateArtifact = null;
4247 String componentId = null;
4248 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinitionForUpdate(data, ArtifactDefinition.class);
4249 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4250 String userId = request.getHeader(Constants.USER_ID_HEADER);
4251 ArtifactDefinition existingArtifactInfo = null;
4252 String interfaceName = null;
4254 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4255 if (getComponentRes.isRight()) {
4256 StorageOperationStatus status = getComponentRes.right().value();
4257 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4258 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status)));
4261 if (errorWrapper.isEmpty()) {
4262 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4263 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4264 if (!getComponentRes.left()
4266 .getMetadataDataDefinition()
4268 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4269 Component component = checkoutParentComponent(componentType, componentId, userId);
4270 if (component != null) {
4271 componentId = component.getUniqueId();
4272 componentName = component.getName();
4276 resourceCommonInfo.setResourceName(componentName);
4279 if(errorWrapper.isEmpty()){
4280 Either<String, ResponseFormat> interfaceNameEither = fetchInterfaceName(componentId, interfaceUUID);
4281 if (interfaceNameEither.isRight()) {
4282 errorWrapper.setInnerElement(interfaceNameEither.right().value());
4285 interfaceName = interfaceNameEither.left().value();
4288 if(errorWrapper.isEmpty()){
4289 Either<Component, StorageOperationStatus> toscaComponentEither = toscaOperationFacade.getToscaElement(componentId);
4290 if (toscaComponentEither.isRight()) {
4291 StorageOperationStatus status = toscaComponentEither.right().value();
4292 log.debug("Could not fetch component with type {} and id {}. Status is {}. ", componentType, componentId, status);
4293 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status)));
4296 if (errorWrapper.isEmpty()) {
4297 NodeTypeEnum parentType = convertParentType(componentType);
4298 final List<ArtifactDefinition> existingDeploymentArtifacts =
4299 getDeploymentArtifacts(toscaComponentEither.left().value(),null);
4300 for (ArtifactDefinition artifactDefinition: existingDeploymentArtifacts){
4301 if(artifactInfo.getArtifactName().equalsIgnoreCase(artifactDefinition.getArtifactName())){
4302 existingArtifactInfo = artifactDefinition;
4306 if(existingArtifactInfo != null){
4307 return updateOperationArtifact(componentId, interfaceName, operationUUID, existingArtifactInfo);
4313 if (errorWrapper.isEmpty()) {
4315 actionResult = Either.left(handleArtifactRequest(componentId, userId, componentType, operation,
4316 artifactUUID, artifactInfo, origMd5, data, interfaceName,
4317 operationUUID, null, null));
4318 }catch (ComponentException e){
4319 errorWrapper.setInnerElement(e.getResponseFormat());
4323 if (errorWrapper.isEmpty()) {
4324 updateArtifact = actionResult.left().value().left().value();
4325 updateArtifactResult = Either.left(updateArtifact);
4329 updateArtifactResult = Either.right(errorWrapper.getInnerElement());
4331 return updateArtifactResult;
4334 private Either<String, ResponseFormat> fetchInterfaceName(String componentId, String interfaceUUID) {
4335 Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId);
4336 if (componentStorageOperationStatusEither.isRight()) {
4337 StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value();
4338 log.debug("Failed to fetch component information by component id, error {}", errorStatus);
4339 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus)));
4341 Component storedComponent = componentStorageOperationStatusEither.left().value();
4343 Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils
4344 .getInterfaceDefinitionFromComponentByInterfaceId(storedComponent, interfaceUUID);
4345 if(!optionalInterface.isPresent()) {
4346 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceUUID));
4348 return Either.left(optionalInterface.get().getType());
4352 * deletes an artifact on a component by UUID
4355 * @param componentType
4356 * @param componentUuid
4357 * @param artifactUUID
4358 * @param resourceCommonInfo
4359 * @param operation TODO
4362 public ArtifactDefinition deleteArtifactOnComponentByUUID(HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String artifactUUID, ResourceCommonInfo resourceCommonInfo,
4363 ArtifactOperationInfo operation) {
4365 Either<ArtifactDefinition, Operation> actionResult;
4366 Component component;
4367 String componentId ;
4369 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4370 String userId = request.getHeader(Constants.USER_ID_HEADER);
4372 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4373 if (getComponentRes.isRight()) {
4374 StorageOperationStatus status = getComponentRes.right().value();
4375 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4376 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), componentUuid);
4378 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4379 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4380 if (!getComponentRes.left()
4382 .getMetadataDataDefinition()
4384 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4385 component = checkoutParentComponent(componentType, componentId, userId);
4386 if (component != null) {
4387 componentId = component.getUniqueId();
4388 componentName = component.getName();
4391 resourceCommonInfo.setResourceName(componentName);
4392 artifactId = getLatestParentArtifactDataIdByArtifactUUID(artifactUUID, componentId, componentType);
4393 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, artifactId, null, origMd5, null, null, null, null, null);
4394 return actionResult.left().value();
4398 * deletes an artifact from a resource instance by UUID
4401 * @param componentType
4402 * @param componentUuid
4403 * @param resourceInstanceName
4404 * @param artifactUUID
4405 * @param operation TODO
4408 public ArtifactDefinition deleteArtifactOnRiByUUID(HttpServletRequest request, ComponentTypeEnum componentType,
4409 String componentUuid, String resourceInstanceName,
4410 String artifactUUID, ArtifactOperationInfo operation) {
4412 Either<ArtifactDefinition, Operation> actionResult;
4413 Component component = null;
4414 String componentInstanceId;
4417 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4418 String userId = request.getHeader(Constants.USER_ID_HEADER);
4419 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4420 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes =
4421 toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4422 if (getComponentRes.isRight()) {
4423 StorageOperationStatus status = getComponentRes.right().value();
4424 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4425 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4427 if (!getComponentRes.left()
4429 .getMetadataDataDefinition()
4431 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4432 component = checkoutParentComponent(componentType, getComponentRes.left()
4434 .getMetadataDataDefinition()
4435 .getUniqueId(), userId);
4437 if (component == null) {
4438 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4441 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4443 componentInstanceId = componentRiPair.getRight().getUniqueId();
4444 componentId = componentRiPair.getLeft().getUniqueId();
4445 artifactId = findArtifactId(componentRiPair.getRight(), artifactUUID);
4447 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactId, null, origMd5, null, null, null, componentId, ComponentTypeEnum
4448 .findParamByType(componentType));
4449 return actionResult.left().value();
4452 private String findArtifactId(ComponentInstance instance, String artifactUUID) {
4453 String artifactId = null;
4454 ArtifactDefinition foundArtifact = null;
4455 if (instance.getDeploymentArtifacts() != null) {
4456 foundArtifact = instance.getDeploymentArtifacts()
4459 .filter(e -> e.getArtifactUUID() != null && e.getArtifactUUID()
4460 .equals(artifactUUID))
4464 if (foundArtifact == null && instance.getArtifacts() != null) {
4465 foundArtifact = instance.getArtifacts()
4468 .filter(e -> e.getArtifactUUID() != null && e.getArtifactUUID()
4469 .equals(artifactUUID))
4473 if (foundArtifact == null) {
4474 log.debug("The artifact {} was not found on instance {}. ", artifactUUID, instance.getUniqueId());
4475 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactUUID);
4478 artifactId = foundArtifact.getUniqueId();
4483 @SuppressWarnings("unchecked")
4484 public ArtifactDefinition createHeatEnvPlaceHolder(List<ArtifactDefinition> createdArtifacts, ArtifactDefinition heatArtifact,
4485 String envType, String parentId, NodeTypeEnum parentType,
4486 String parentName, User user, Component component,
4487 Map<String, String> existingEnvVersions) {
4488 Map<String, Object> deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager()
4490 .getDeploymentResourceInstanceArtifacts();
4491 if (deploymentResourceArtifacts == null) {
4492 log.debug("no deployment artifacts are configured for generated artifacts");
4493 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
4495 Map<String, Object> placeHolderData = (Map<String, Object>) deploymentResourceArtifacts.get(envType);
4496 if (placeHolderData == null) {
4497 log.debug("no env type {} are configured for generated artifacts", envType);
4498 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
4501 String envLabel = (heatArtifact.getArtifactLabel() + HEAT_ENV_SUFFIX).toLowerCase();
4502 ArtifactDefinition createArtifactPlaceHolder = createArtifactPlaceHolderInfo(parentId, envLabel, placeHolderData, user
4503 .getUserId(), ArtifactGroupTypeEnum.DEPLOYMENT, true);
4504 ArtifactDefinition artifactHeatEnv = createArtifactPlaceHolder;
4505 artifactHeatEnv.setGeneratedFromId(heatArtifact.getUniqueId());
4506 artifactHeatEnv.setHeatParamsUpdateDate(System.currentTimeMillis());
4507 artifactHeatEnv.setTimeout(0);
4508 artifactHeatEnv.setIsFromCsar(heatArtifact.getIsFromCsar());
4509 buildHeatEnvFileName(heatArtifact, artifactHeatEnv, placeHolderData);
4510 // rbetzer - keep env artifactVersion - changeComponentInstanceVersion flow
4511 handleEnvArtifactVersion(artifactHeatEnv, existingEnvVersions);
4512 ArtifactDefinition heatEnvPlaceholder;
4513 // Evg : for resource instance artifact will be added later as block with other env artifacts from BL
4514 if (parentType != NodeTypeEnum.ResourceInstance) {
4515 String checkSum = artifactToscaOperation.sortAndCalculateChecksumForHeatParameters(heatArtifact.getHeatParameters());
4516 artifactHeatEnv.setArtifactChecksum(checkSum);
4517 Either<ArtifactDefinition, StorageOperationStatus> addHeatEnvArtifact = addHeatEnvArtifact(artifactHeatEnv, heatArtifact, component, parentType, parentId);
4518 if (addHeatEnvArtifact.isRight()) {
4519 log.debug("failed to create heat env artifact on resource instance");
4520 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(addHeatEnvArtifact
4522 .value(), false), "", null));
4524 heatEnvPlaceholder = createArtifactPlaceHolder;
4527 heatEnvPlaceholder = artifactHeatEnv;
4528 artifactToscaOperation.generateUUID(heatEnvPlaceholder, heatEnvPlaceholder.getArtifactVersion());
4529 setHeatCurrentValuesOnHeatEnvDefaultValues(heatArtifact, heatEnvPlaceholder);
4531 ComponentTypeEnum componentType = component.getComponentType();
4532 if (parentType == NodeTypeEnum.ResourceInstance) {
4533 componentType = ComponentTypeEnum.RESOURCE_INSTANCE;
4535 createdArtifacts.add(heatEnvPlaceholder);
4536 componentsUtils.auditComponent(componentsUtils.getResponseFormat(ActionStatus.OK), user, component, AuditingActionEnum.ARTIFACT_UPLOAD,
4537 new ResourceCommonInfo(parentName, componentType.getValue()),
4538 ResourceVersionInfo.newBuilder().build(),
4539 ResourceVersionInfo.newBuilder().artifactUuid(heatEnvPlaceholder.getUniqueId()).build(),
4540 null, heatEnvPlaceholder, null);
4541 return heatEnvPlaceholder;
4544 private void setHeatCurrentValuesOnHeatEnvDefaultValues(ArtifactDefinition artifact, ArtifactDefinition artifactDefinition) {
4545 if (artifact.getListHeatParameters() == null) {
4548 List<HeatParameterDefinition> heatEnvParameters = new ArrayList<>();
4549 for (HeatParameterDefinition parameter : artifact.getListHeatParameters()) {
4550 HeatParameterDefinition heatEnvParameter = new HeatParameterDefinition(parameter);
4551 heatEnvParameter.setDefaultValue(parameter.getCurrentValue());
4552 heatEnvParameter.setCurrentValue(null);
4553 heatEnvParameters.add(heatEnvParameter);
4555 artifactDefinition.setListHeatParameters(heatEnvParameters);
4558 private void buildHeatEnvFileName(ArtifactDefinition heatArtifact, ArtifactDefinition heatEnvArtifact, Map<String, Object> placeHolderData) {
4559 String heatExtension = GeneralUtility.getFilenameExtension(heatArtifact.getArtifactName());
4560 String envExtension = (String) placeHolderData.get(ARTIFACT_PLACEHOLDER_FILE_EXTENSION);
4561 String name = heatArtifact.getArtifactName();
4564 name = heatArtifact.getArtifactLabel();
4565 fileName = name + "." + envExtension;
4568 fileName = name.replaceAll("." + heatExtension, "." + envExtension);
4570 heatEnvArtifact.setArtifactName(fileName);
4573 private void handleEnvArtifactVersion(ArtifactDefinition heatEnvArtifact, Map<String, String> existingEnvVersions) {
4574 if (null != existingEnvVersions) {
4575 String prevVersion = existingEnvVersions.get(heatEnvArtifact.getArtifactName());
4576 if (null != prevVersion) {
4577 heatEnvArtifact.setArtifactVersion(prevVersion);
4582 public List<ArtifactDefinition> handleArtifactsForInnerVfcComponent(List<ArtifactDefinition> artifactsToHandle, Resource component, User user, List<ArtifactDefinition> vfcsNewCreatedArtifacts,
4583 ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction) {
4584 ComponentTypeEnum componentType = component.getComponentType();
4585 List<ArtifactDefinition> uploadedArtifacts = new ArrayList<>();
4586 Either<ArtifactDefinition, Operation> result;
4588 for (ArtifactDefinition artifactDefinition : artifactsToHandle) {
4589 result = handleLoadedArtifact(component, user, operation, shouldLock, inTransaction, componentType, artifactDefinition);
4590 uploadedArtifacts.add(result.left().value());
4592 } catch (ComponentException e) {
4593 log.debug(FAILED_UPLOAD_ARTIFACT_TO_COMPONENT, componentType, component
4594 .getName(), e.getResponseFormat());
4595 if (operation.isCreateOrLink()) {
4596 vfcsNewCreatedArtifacts.addAll(uploadedArtifacts);
4600 return uploadedArtifacts;
4603 public Either<ArtifactDefinition, Operation> handleLoadedArtifact(Resource component, User user, ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction,
4604 ComponentTypeEnum componentType, ArtifactDefinition artifactDefinition) {
4605 AuditingActionEnum auditingAction = detectAuditingType(operation, "");
4606 String componentId = component.getUniqueId();
4607 String artifactId = artifactDefinition.getUniqueId();
4608 Either<ArtifactDefinition, Operation> result;
4609 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
4610 //artifact validation
4611 artifactDefinition = validateArtifact(componentId, componentType, operation,
4612 artifactId, artifactDefinition, auditingAction, user,
4613 component, shouldLock, inTransaction);
4614 switch (operation.getArtifactOperationEnum()) {
4616 byte[] validPayload = getValidPayload(componentId, artifactDefinition, operation, auditingAction, artifactId, user, componentType, component, null, null);
4617 result = createArtifact(component, componentId, artifactDefinition, validPayload,
4618 componentType, auditingAction, null, null);
4621 validPayload = getValidPayload(componentId, artifactDefinition, operation, auditingAction, artifactId, user, componentType, component, null, null);
4622 result = handleUpdate(componentId, componentType, operation, artifactId, artifactDefinition, validPayload, null, null, null, null,
4623 auditingAction, user, component, true);
4626 result = Either.left(handleDeleteInternal(componentId, artifactId, componentType, component));
4629 if (artifactGenerationRequired(component, artifactDefinition)) {
4630 result = Either.left(generateNotSavedArtifact(component, artifactDefinition));
4632 result = Either.left(handleDownload(componentId, artifactId, componentType, component));
4636 result = Either.left(handleLink(componentId, artifactDefinition, componentType, component));
4639 throw new UnsupportedOperationException("In ArtifactsBusinessLogic received illegal operation: " + operation.getArtifactOperationEnum());
4644 public List<ArtifactDefinition> handleArtifactsRequestForInnerVfcComponent(List<ArtifactDefinition> artifactsToHandle, Resource component, User user, List<ArtifactDefinition> vfcsNewCreatedArtifacts,
4645 ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction) {
4647 List<ArtifactDefinition> handleArtifactsResult;
4648 ComponentTypeEnum componentType = component.getComponentType();
4649 List<ArtifactDefinition> uploadedArtifacts = new ArrayList<>();
4650 Either<ArtifactDefinition, Operation> actionResult;
4654 for (ArtifactDefinition artifact : artifactsToHandle) {
4655 originData = ArtifactUtils.buildJsonStringForCsarVfcArtifact(artifact);
4656 origMd5 = GeneralUtility.calculateMD5Base64EncodedByString(originData);
4657 actionResult = handleArtifactRequest(component.getUniqueId(), user.getUserId(), componentType, operation, artifact
4658 .getUniqueId(), artifact, origMd5, originData, null, null, null, null, shouldLock, inTransaction);
4659 uploadedArtifacts.add(actionResult.left().value());
4661 handleArtifactsResult = uploadedArtifacts;
4662 }catch (ComponentException e){
4663 if (operation.isCreateOrLink()) {
4664 vfcsNewCreatedArtifacts.addAll(uploadedArtifacts);
4668 return handleArtifactsResult;
4671 private ComponentInstance getRelatedComponentInstance(ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName) {
4672 ComponentInstance componentInstance;
4673 String normalizedName = ValidationUtils.normalizeComponentInstanceName(resourceInstanceName);
4674 Component component = getComponentByUuid(componentType, componentUuid);
4675 componentInstance = (component == null) ? null : component.getComponentInstances()
4677 .filter(ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName())
4678 .equals(normalizedName))
4681 if (componentInstance == null) {
4682 log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName());
4683 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, resourceInstanceName,
4684 RESOURCE_INSTANCE, component.getComponentType().getValue(), component.getName());
4686 return componentInstance;
4689 private ImmutablePair<Component, ComponentInstance> getRelatedComponentComponentInstance(Component component, String resourceInstanceName) {
4691 ImmutablePair<Component, ComponentInstance> relatedComponentComponentInstancePair = null;
4692 String normalizedName = ValidationUtils.normalizeComponentInstanceName(resourceInstanceName);
4693 ComponentInstance componentInstance = component.getComponentInstances()
4695 .filter(ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName())
4696 .equals(normalizedName))
4699 if (componentInstance == null) {
4700 log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName());
4701 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, resourceInstanceName,
4702 RESOURCE_INSTANCE, component.getComponentType().getValue(), component.getName());
4705 relatedComponentComponentInstancePair = new ImmutablePair<>(component, componentInstance);
4707 return relatedComponentComponentInstancePair;
4710 private ImmutablePair<Component, ComponentInstance> getRelatedComponentComponentInstance(ComponentTypeEnum componentType,
4711 String componentUuid, String resourceInstanceName) {
4712 ComponentInstance componentInstance;
4713 ImmutablePair<Component, ComponentInstance> relatedComponentComponentInstancePair;
4714 Component component = getLatestComponentByUuid(componentType, componentUuid);
4715 componentInstance = component.getComponentInstances()
4717 .filter(ci -> ci.getNormalizedName().equals(resourceInstanceName))
4720 if (componentInstance == null) {
4721 log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName());
4722 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
4723 resourceInstanceName, RESOURCE_INSTANCE, component
4724 .getComponentType().getValue(), component.getName());
4727 relatedComponentComponentInstancePair = new ImmutablePair<>(component, componentInstance);
4729 return relatedComponentComponentInstancePair;
4732 private byte[] downloadArtifact(Map<String, ArtifactDefinition> artifacts, String artifactUUID, String componentName) {
4734 ImmutablePair<String, byte[]> downloadArtifact;
4735 List<ArtifactDefinition> artifactsList = null;
4736 ArtifactDefinition deploymentArtifact;
4737 if (artifacts != null && !artifacts.isEmpty()) {
4738 artifactsList = artifacts.values()
4740 .filter(art -> art.getArtifactUUID() != null && art.getArtifactUUID()
4741 .equals(artifactUUID))
4742 .collect(Collectors.toList());
4744 if (artifactsList == null || artifactsList.isEmpty()) {
4745 log.debug("Deployment artifact with uuid {} was not found for component {}", artifactUUID, componentName);
4746 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactUUID);
4748 deploymentArtifact = artifactsList.get(0);
4749 downloadArtifact = downloadArtifact(deploymentArtifact);
4750 log.trace("Succeeded to download artifact with uniqueId {}", deploymentArtifact.getUniqueId());
4751 return downloadArtifact.getRight();
4754 private Component getLatestComponentByUuid(ComponentTypeEnum componentType, String componentUuid) {
4755 Component component;
4756 Either<Component, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentByUuid(componentUuid);
4757 if (getComponentRes.isRight()) {
4758 StorageOperationStatus status = getComponentRes.right().value();
4759 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4760 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4763 component = getComponentRes.left().value();
4768 private Component getComponentByUuid(ComponentTypeEnum componentType, String componentUuid) {
4769 Component component;
4770 Either<List<Component>, StorageOperationStatus> getComponentRes = toscaOperationFacade.getComponentListByUuid(componentUuid, null);
4771 if (getComponentRes.isRight()) {
4772 StorageOperationStatus status = getComponentRes.right().value();
4773 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4774 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4777 List<Component> value = getComponentRes.left().value();
4778 if (value.isEmpty()) {
4779 log.debug("Could not fetch component with type {} and uuid {}.", componentType, componentUuid);
4780 ActionStatus status = componentType == ComponentTypeEnum.RESOURCE ? ActionStatus.RESOURCE_NOT_FOUND : ActionStatus.SERVICE_NOT_FOUND;
4781 throw new ByActionStatusComponentException(status);
4784 component = value.get(0);
4790 private String getLatestParentArtifactDataIdByArtifactUUID(String artifactUUID, String parentId, ComponentTypeEnum componentType) {
4791 ActionStatus actionStatus = ActionStatus.ARTIFACT_NOT_FOUND;
4792 StorageOperationStatus storageStatus;
4793 ArtifactDefinition latestArtifact;
4794 List<ArtifactDefinition> artifacts;
4795 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifactsRes = artifactToscaOperation.getArtifacts(parentId);
4796 if (getArtifactsRes.isRight()) {
4797 storageStatus = getArtifactsRes.right().value();
4798 log.debug("Couldn't fetch artifacts data for parent component {} with uid {}, error: {}", componentType, parentId, storageStatus);
4799 if (storageStatus != StorageOperationStatus.NOT_FOUND) {
4800 actionStatus = componentsUtils.convertFromStorageResponse(storageStatus);
4802 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4804 artifacts = getArtifactsRes.left()
4808 .filter(a -> a.getArtifactUUID() != null && a.getArtifactUUID()
4809 .equals(artifactUUID))
4810 .collect(Collectors.toList());
4811 if (artifacts == null || artifacts.isEmpty()) {
4812 log.debug("Couldn't fetch artifact with UUID {} data for parent component {} with uid {}, error: {}", artifactUUID, componentType, parentId, actionStatus);
4813 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4815 latestArtifact = artifacts.stream().max((a1, a2) -> {
4816 int compareRes = Double.compare(Double.parseDouble(a1.getArtifactVersion()), Double.parseDouble(a2.getArtifactVersion()));
4817 if (compareRes == 0) {
4818 compareRes = Long.compare(a1.getLastUpdateDate() == null ? 0 : a1.getLastUpdateDate(), a2.getLastUpdateDate() == null ? 0 : a2
4819 .getLastUpdateDate());
4823 if (latestArtifact == null) {
4824 log.debug("Couldn't fetch latest artifact with UUID {} data for parent component {} with uid {}, error: {}", artifactUUID, componentType, parentId, actionStatus);
4825 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4827 return latestArtifact.getUniqueId();
4830 private Component checkoutParentComponent(ComponentTypeEnum componentType, String parentId, String userId) {
4832 Component component = null;
4833 User modifier = userBusinessLogic.getUser(userId, false);
4834 LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction("External API checkout", LifecycleChanceActionEnum.UPDATE_FROM_EXTERNAL_API);
4835 Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic.changeComponentState(componentType, parentId, modifier, LifeCycleTransitionEnum.CHECKOUT, changeInfo, false, true);
4836 if (checkoutRes.isRight()) {
4837 log.debug("Could not change state of component {} with uid {} to checked out. Status is {}. ", componentType
4838 .getNodeType(), parentId, checkoutRes.right().value().getStatus());
4839 throw new ByResponseFormatComponentException(checkoutRes.right().value());
4841 return checkoutRes.left().value();
4845 void setNodeTemplateOperation(NodeTemplateOperation nodeTemplateOperation) {
4846 this.nodeTemplateOperation = nodeTemplateOperation;
4849 public List<ArtifactConfiguration> getConfiguration() {
4850 return ConfigurationManager.getConfigurationManager().getConfiguration().getArtifacts();