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;
31 import fj.data.Either;
32 import io.vavr.control.Option;
33 import java.io.ByteArrayInputStream;
34 import java.io.IOException;
35 import java.math.BigDecimal;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.Comparator;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
43 import java.util.Map.Entry;
44 import java.util.Objects;
45 import java.util.Optional;
47 import java.util.function.Consumer;
48 import java.util.function.Function;
49 import java.util.function.Predicate;
50 import java.util.function.Supplier;
51 import java.util.stream.Collectors;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.xml.XMLConstants;
54 import javax.xml.parsers.ParserConfigurationException;
55 import javax.xml.parsers.SAXParserFactory;
56 import org.apache.commons.codec.binary.Base64;
57 import org.apache.commons.collections.CollectionUtils;
58 import org.apache.commons.collections.MapUtils;
59 import org.apache.commons.io.FilenameUtils;
60 import org.apache.commons.lang.ArrayUtils;
61 import org.apache.commons.lang.StringUtils;
62 import org.apache.commons.lang3.tuple.ImmutablePair;
63 import org.openecomp.sdc.be.components.ArtifactsResolver;
64 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
65 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
66 import org.openecomp.sdc.be.components.impl.artifact.ArtifactTypeToPayloadTypeSelector;
67 import org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum;
68 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
69 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
70 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
71 import org.openecomp.sdc.be.components.utils.ArtifactUtils;
72 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
73 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
74 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
75 import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils;
76 import org.openecomp.sdc.be.config.ArtifactConfiguration;
77 import org.openecomp.sdc.be.config.BeEcompErrorManager;
78 import org.openecomp.sdc.be.config.Configuration;
79 import org.openecomp.sdc.be.config.Configuration.ArtifactTypeConfig;
80 import org.openecomp.sdc.be.config.ConfigurationManager;
81 import org.openecomp.sdc.be.dao.api.ActionStatus;
82 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
83 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
84 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
85 import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition;
86 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
87 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
88 import org.openecomp.sdc.be.datatypes.elements.GroupInstanceDataDefinition;
89 import org.openecomp.sdc.be.datatypes.elements.HeatParameterDataDefinition;
90 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
91 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
92 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
93 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
94 import org.openecomp.sdc.be.info.ArtifactTemplateInfo;
95 import org.openecomp.sdc.be.model.ArtifactDefinition;
96 import org.openecomp.sdc.be.model.Component;
97 import org.openecomp.sdc.be.model.ComponentInstance;
98 import org.openecomp.sdc.be.model.ComponentParametersView;
99 import org.openecomp.sdc.be.model.GroupDefinition;
100 import org.openecomp.sdc.be.model.GroupInstance;
101 import org.openecomp.sdc.be.model.HeatParameterDefinition;
102 import org.openecomp.sdc.be.model.InterfaceDefinition;
103 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
104 import org.openecomp.sdc.be.model.LifecycleStateEnum;
105 import org.openecomp.sdc.be.model.Operation;
106 import org.openecomp.sdc.be.model.Resource;
107 import org.openecomp.sdc.be.model.Service;
108 import org.openecomp.sdc.be.model.User;
109 import org.openecomp.sdc.be.model.heat.HeatParameterType;
110 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
111 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
112 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
113 import org.openecomp.sdc.be.model.operations.StorageException;
114 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
115 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
116 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
117 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
118 import org.openecomp.sdc.be.model.operations.api.IHeatParametersOperation;
119 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
120 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
121 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
122 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
123 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
124 import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation;
125 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
126 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
127 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
128 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
129 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
130 import org.openecomp.sdc.be.servlets.RepresentationUtils;
131 import org.openecomp.sdc.be.tosca.CsarUtils;
132 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
133 import org.openecomp.sdc.be.user.Role;
134 import org.openecomp.sdc.be.user.UserBusinessLogic;
135 import org.openecomp.sdc.be.utils.TypeUtils;
136 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
137 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
138 import org.openecomp.sdc.common.api.Constants;
139 import org.openecomp.sdc.common.datastructure.Wrapper;
140 import org.openecomp.sdc.common.log.wrappers.Logger;
141 import org.openecomp.sdc.common.util.GeneralUtility;
142 import org.openecomp.sdc.common.util.ValidationUtils;
143 import org.openecomp.sdc.common.util.YamlToObjectConverter;
144 import org.openecomp.sdc.exception.ResponseFormat;
145 import org.springframework.beans.factory.annotation.Autowired;
146 import org.xml.sax.InputSource;
147 import org.xml.sax.SAXException;
148 import org.xml.sax.SAXNotRecognizedException;
149 import org.xml.sax.SAXNotSupportedException;
150 import org.xml.sax.XMLReader;
151 import org.yaml.snakeyaml.Yaml;
153 @org.springframework.stereotype.Component("artifactBusinessLogic")
154 public class ArtifactsBusinessLogic extends BaseBusinessLogic {
155 private static final String RESOURCE_INSTANCE = "resource instance";
156 private static final String ARTIFACT_TYPE_OTHER = "OTHER";
157 private static final String ARTIFACT_DESCRIPTION = "artifact description";
158 private static final String ARTIFACT_LABEL = "artifact label";
159 private static final String ARTIFACT_URL = "artifact url";
160 private static final String ARTIFACT_NAME = "artifact name";
161 private static final String ARTIFACT_PAYLOAD = "artifact payload";
163 private static final String ARTIFACT_PLACEHOLDER_TYPE = "type";
164 private static final String ARTIFACT_PLACEHOLDER_DISPLAY_NAME = "displayName";
165 private static final Object ARTIFACT_PLACEHOLDER_DESCRIPTION = "description";
167 public static final String HEAT_ENV_NAME = "heatEnv";
168 public static final String HEAT_VF_ENV_NAME = "VfHeatEnv";
169 public static final String HEAT_ENV_SUFFIX = "env";
170 private static final String ARTIFACT_PLACEHOLDER_FILE_EXTENSION = "fileExtension";
172 private static final Logger log = Logger.getLogger(ArtifactsBusinessLogic.class.getName());
173 private static final String FAILED_UPDATE_GROUPS = "Failed to update groups of the component {}. ";
174 private static final String FAILED_SAVE_ARTIFACT = "Failed to save the artifact.";
175 public static final String ARTIFACT_ACTION_LOCK = "Artifact action - lock ";
176 public static final String FAILED_UPLOAD_ARTIFACT_TO_COMPONENT = "Failed to upload artifact to component with type {} and uuid {}. Status is {}. ";
177 private static final String FAILED_FETCH_COMPONENT = "Could not fetch component with type {} and uuid {}. Status is {}. ";
178 private static final String NULL_PARAMETER = "One of the function parameteres is null";
179 public static final String COMPONENT_INSTANCE_NOT_FOUND = "Component instance {} was not found for component {}";
180 private static final String ROLLBACK = "all changes rollback";
181 private static final String COMMIT = "all changes committed";
182 private static final String UPDATE_ARTIFACT = "Update Artifact";
183 private static final String FOUND_DEPLOYMENT_ARTIFACT = "Found deployment artifact {}";
184 private Gson gson = new GsonBuilder().setPrettyPrinting().create();
186 @javax.annotation.Resource
187 private IInterfaceLifecycleOperation interfaceLifecycleOperation;
188 @javax.annotation.Resource
189 private UserAdminOperation userOperaton;
191 @javax.annotation.Resource
192 private IElementOperation elementOperation;
194 @javax.annotation.Resource
195 private IHeatParametersOperation heatParametersOperation;
197 private ArtifactCassandraDao artifactCassandraDao;
198 private ToscaExportHandler toscaExportUtils;
199 private CsarUtils csarUtils;
200 private LifecycleBusinessLogic lifecycleBusinessLogic;
201 private UserBusinessLogic userBusinessLogic;
202 private ArtifactsResolver artifactsResolver;
203 private NodeTemplateOperation nodeTemplateOperation;
206 public ArtifactsBusinessLogic(ArtifactCassandraDao artifactCassandraDao, ToscaExportHandler toscaExportUtils,
207 CsarUtils csarUtils, LifecycleBusinessLogic lifecycleBusinessLogic, UserBusinessLogic userBusinessLogic,
208 ArtifactsResolver artifactsResolver, IElementOperation elementDao, IGroupOperation groupOperation,
209 IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation, InterfaceOperation interfaceOperation,
210 InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsOperations artifactToscaOperation) {
211 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
212 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
213 this.artifactCassandraDao = artifactCassandraDao;
214 this.toscaExportUtils = toscaExportUtils;
215 this.csarUtils = csarUtils;
216 this.lifecycleBusinessLogic = lifecycleBusinessLogic;
217 this.userBusinessLogic = userBusinessLogic;
218 this.artifactsResolver = artifactsResolver;
221 public enum ArtifactOperationEnum {
222 CREATE, UPDATE, DELETE, DOWNLOAD, LINK;
224 public static boolean isCreateOrLink(ArtifactOperationEnum operation) {
225 return operation == CREATE || operation == LINK;
230 public Either<ArtifactDefinition, Operation> handleArtifactRequest(String componentId, String userId, ComponentTypeEnum componentType,
231 ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo,
232 String origMd5, String originData, String interfaceName, String operationName,
233 String parentId, String containerComponentType, boolean shouldLock, boolean inTransaction) {
235 // step 1 - detect auditing type
236 AuditingActionEnum auditingAction = detectAuditingType(operation, origMd5);
237 // step 2 - check header
238 if (userId == null) {
239 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
240 log.debug("handleArtifactRequest - no HTTP_CSP_HEADER , component id {}", componentId);
241 handleAuditing(auditingAction, null, componentId, null, null, null, artifactId, responseFormat, componentType, null);
242 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
244 // step 3 - check user existence
245 // step 4 - check user's role
246 User user = validateUserExists(userId, auditingAction, componentId, artifactId, componentType, inTransaction);
247 validateUserRole(user, auditingAction, componentId, artifactId, componentType, operation);
250 // 5. check service/resource existence
251 // 6. check service/resource check out
252 // 7. user is owner of checkout state
253 Component component = null;
254 String realComponentId = componentType == ComponentTypeEnum.RESOURCE_INSTANCE ? parentId : componentId;
255 component = validateComponentExists(realComponentId, auditingAction, user, artifactId, componentType, containerComponentType);
256 validateWorkOnComponent(component, userId, auditingAction, user, artifactId, operation);
257 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
258 validateResourceInstanceById(component, componentId);
262 return validateAndHandleArtifact(componentId, componentType, operation, artifactId, artifactInfo, origMd5,
263 originData, interfaceName, operationName, user, component, shouldLock, inTransaction, true);
266 public Either<ArtifactDefinition, Operation> handleArtifactRequest(String componentId, String userId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo,
267 String origMd5, String originData, String interfaceName, String operationName, String parentId, String containerComponentType) {
268 return handleArtifactRequest(componentId, userId, componentType, operation, artifactId, artifactInfo, origMd5, originData, interfaceName, operationName, parentId, containerComponentType, true, false);
272 * This Method validates only the Artifact and does not validate user / role / component ect...<br>
273 * For regular usage use <br>
274 * {@link #handleArtifactRequest(String, String, ComponentTypeEnum, ArtifactOperationInfo, String, ArtifactDefinition, String, String, String, String, String, String)}
278 public Either<ArtifactDefinition, Operation> validateAndHandleArtifact(
279 String componentUniqueId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactUniqueId,
280 ArtifactDefinition artifactDefinition, String origMd5, String originData, String interfaceName,
281 String operationName, User user, Component component, boolean shouldLock, boolean inTransaction, boolean needUpdateGroup) {
282 AuditingActionEnum auditingAction = detectAuditingType(operation, origMd5);
283 artifactDefinition = validateArtifact(componentUniqueId, componentType, operation,
284 artifactUniqueId, artifactDefinition, auditingAction, user,
285 component, shouldLock, inTransaction);
288 Either<ArtifactDefinition, Operation> result = doAction(componentUniqueId, componentType, operation, artifactUniqueId, artifactDefinition,
289 origMd5, originData, interfaceName, operationName, auditingAction, user, component, shouldLock, inTransaction, needUpdateGroup);
290 //TODO: audit positive action
295 ArtifactDefinition validateArtifact(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, User user,
296 Component component, boolean shouldLock, boolean inTransaction) {
297 ArtifactDefinition artifactInfoToReturn = artifactInfo;
298 ArtifactOperationEnum operationEnum = operation.getArtifactOperationEnum();
299 if (operationEnum == ArtifactOperationEnum.UPDATE || operationEnum == ArtifactOperationEnum.DELETE || operationEnum == ArtifactOperationEnum.DOWNLOAD) {
300 ArtifactDefinition dbArtifact = getArtifactIfBelongsToComponent(componentId, componentType, artifactId, component);
301 if (operation.isDownload()) {
302 artifactInfoToReturn = dbArtifact;
303 handleHeatEnvDownload(componentId, componentType, user, component, dbArtifact, shouldLock, inTransaction);
306 return artifactInfoToReturn;
310 void handleHeatEnvDownload(String componentId, ComponentTypeEnum componentType, User user, Component component,
311 ArtifactDefinition artifactDefinition, boolean shouldLock, boolean inTransaction) {
313 if (artifactDefinition.getArtifactType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV.getType())
314 && ComponentTypeEnum.SERVICE == component.getComponentType()) {
315 ComponentInstance componentInstance = component.getComponentInstances()
317 .filter(p -> p.getUniqueId().equals(componentId))
320 if (componentInstance == null) {
321 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentId,
322 "instance", "Service", component.getName());
324 Map<String, ArtifactDefinition> deploymentArtifacts = componentInstance.getDeploymentArtifacts();
326 ArtifactDefinition heatEnvWithHeatParams = deploymentArtifacts.values()
328 .filter(p -> p.getUniqueId()
329 .equals(artifactDefinition.getUniqueId()))
332 Either<ArtifactDefinition, ResponseFormat> eitherGenerated = generateHeatEnvArtifact(heatEnvWithHeatParams,
333 componentType, component, componentInstance.getName(), user, componentId, shouldLock, inTransaction);
334 if (eitherGenerated.isRight()) {
335 throw new ByResponseFormatComponentException((eitherGenerated.right().value()));
340 private boolean artifactGenerationRequired(Component component, ArtifactDefinition artifactInfo) {
341 boolean needGenerate;
342 needGenerate = artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA && (component.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || component
343 .getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
344 needGenerate = needGenerate || (ComponentTypeEnum.RESOURCE == component.getComponentType() && (artifactInfo.getArtifactType()
345 .equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV
346 .getType()) || isAbstractVfcEmptyCsar((Resource) component, artifactInfo)));
350 private boolean isAbstractVfcEmptyCsar(Resource resource, ArtifactDefinition artifactInfo) {
351 return resource.isAbstract() && artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA && artifactInfo
353 .equals(ArtifactTypeEnum.TOSCA_CSAR.getType()) && StringUtils.isEmpty(artifactInfo.getArtifactChecksum());
356 public Either<ArtifactDefinition, Operation> generateAndSaveToscaArtifact(
357 ArtifactDefinition artifactDefinition, Component component,
358 User user, boolean isInCertificationRequest, boolean shouldLock, boolean inTransaction,
359 boolean fetchTemplatesFromDB) {
361 return decodeToscaArtifactPayload(
362 component, isInCertificationRequest, fetchTemplatesFromDB, artifactDefinition.getArtifactType()
363 ).left().bind(payload -> {
364 // TODO: Avoid output argument
365 artifactDefinition.setPayload(payload);
366 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
367 artifactDefinition.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(payload));
368 return lockComponentAndUpdateArtifact(component.getUniqueId(), artifactDefinition,
369 AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, artifactDefinition.getUniqueId(),
370 user, component.getComponentType(), component, payload, shouldLock, inTransaction
372 }).right().map(ex -> {
373 // TODO: This should not be done but in order to keep this refactoring small enough, we stop here.
374 // Bubble up this exception
379 private Either<byte[], ComponentException> decodeToscaArtifactPayload(
381 boolean isInCertificationRequest,
382 boolean fetchTemplatesFromDB,
385 log.debug("tosca artifact generation");
386 if (ArtifactTypeEnum.TOSCA_CSAR.getType().equals(artifactType)) {
388 .createCsar(parent, fetchTemplatesFromDB, isInCertificationRequest)
389 .right().map(error -> {
390 log.debug("Failed to generate tosca csar for component {} error {}", parent.getUniqueId(), error);
391 return new ByResponseFormatComponentException(error);
394 return toscaExportUtils
395 .exportComponent(parent)
396 .left().map(toscaRepresentation -> {
397 log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
398 return toscaRepresentation.getMainYaml().getBytes();
399 }).right().map(toscaError -> {
400 log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), toscaError);
401 return new ByActionStatusComponentException(componentsUtils.convertFromToscaError(toscaError));
406 private Either<ArtifactDefinition, Operation> doAction(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo, String origMd5,
407 String originData, String interfaceName, String operationName, AuditingActionEnum auditingAction, User user, Component parent, boolean shouldLock, boolean inTransaction, boolean needUpdateGroup) {
408 if (interfaceName != null && operationName != null) {
409 interfaceName = interfaceName.toLowerCase();
410 operationName = operationName.toLowerCase();
413 lockComponent(componentType, artifactId, auditingAction, user, parent);
415 Either<ArtifactDefinition, Operation> result;
416 boolean operationSucceeded = false;
418 switch (operation.getArtifactOperationEnum()) {
420 if (artifactGenerationRequired(parent, artifactInfo)) {
421 result = Either.left(generateNotSavedArtifact(parent, artifactInfo));
423 result = Either.left(handleDownload(componentId, artifactId, componentType, parent));
427 result = Either.left(handleDeleteInternal(componentId, artifactId, componentType, parent));
430 result = handleUpdate(componentId, componentType, operation, artifactId, artifactInfo, null, origMd5, originData, interfaceName, operationName,
431 auditingAction, user, parent, needUpdateGroup);
434 result = handleCreate(componentId, artifactInfo, operation, auditingAction, user, componentType, parent, origMd5, originData, interfaceName,
438 result = Either.left(handleLink(componentId, artifactInfo, componentType, parent));
441 throw new UnsupportedOperationException("In ArtifactsBusinessLogic received illegal operation: " + operation.getArtifactOperationEnum());
443 operationSucceeded = true;
447 handleLockingAndCommit(parent, shouldLock, inTransaction, operationSucceeded);
451 private void lockComponent(ComponentTypeEnum componentType, String artifactId, AuditingActionEnum auditingAction, User user, Component parent) {
453 lockComponent(parent, ARTIFACT_ACTION_LOCK);
454 }catch (ComponentException e){
455 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, null, null, artifactId, e.getResponseFormat(),
456 componentType, null);
462 public Either<ArtifactDefinition, Operation> handleUpdate(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId,
463 ArtifactDefinition artifactInfo, byte[] decodedPayload, String origMd5, String originData, String interfaceName,
464 String operationName, AuditingActionEnum auditingAction, User user, Component parent,
465 boolean needUpdateGroup) {
466 Either<ArtifactDefinition, Operation> result;
467 validateArtifactType(artifactInfo);
468 final String artifactType = artifactInfo.getArtifactType();
469 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE &&
470 (ArtifactTypeEnum.HEAT.getType().equals(artifactType) ||
471 ArtifactTypeEnum.HEAT_VOL.getType().equals(artifactType) ||
472 ArtifactTypeEnum.HEAT_NET.getType().equals(artifactType) ||
473 ArtifactTypeEnum.HEAT_ENV.getType().equals(artifactType))) {
474 result = handleUpdateHeatEnvAndHeatMeta(componentId, artifactInfo, auditingAction, artifactId, user, componentType, parent, originData, origMd5, operation);
475 if (needUpdateGroup) {
476 ActionStatus error = updateGroupInstance(artifactInfo, result.left().value(), parent, componentId);
477 if (error != ActionStatus.OK) {
478 throw new ByActionStatusComponentException(error);
482 else if (componentType == ComponentTypeEnum.RESOURCE && ArtifactTypeEnum.HEAT_ENV.getType().equals(artifactType)) {
483 result = handleUpdateHeatWithHeatEnvParams(componentId, artifactInfo, auditingAction, componentType, parent, originData, origMd5, operation, needUpdateGroup);
486 if (decodedPayload == null) {
487 decodedPayload = validateInput(componentId, artifactInfo, operation, auditingAction, artifactId, user,
488 componentType, parent, origMd5, originData, interfaceName, operationName);
490 result = updateArtifactFlow(parent, componentId, artifactId, artifactInfo, decodedPayload, componentType, auditingAction
492 if (needUpdateGroup && result.isLeft()) {
493 ArtifactDefinition updatedArtifact = result.left().value();
494 updateGroupForHeat(artifactInfo, updatedArtifact, parent);
500 private void validateArtifactType(final ArtifactDefinition artifactInfo) {
501 if (!isArtifactSupported(artifactInfo.getArtifactType())) {
502 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
506 private void validateArtifactType(final ArtifactDefinition artifactInfo,
507 final ComponentTypeEnum componentType) {
508 final ArtifactConfiguration artifactConfiguration =
509 loadArtifactTypeConfig(artifactInfo.getArtifactType()).orElse(null);
510 if (artifactConfiguration == null) {
511 BeEcompErrorManager.getInstance()
512 .logBeMissingArtifactInformationError("Artifact Update / Upload", "artifactLabel");
513 log.debug("Missing artifact type for artifact {}", artifactInfo.getArtifactName());
514 final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_TYPE);
515 throw new ByResponseFormatComponentException(responseFormat);
518 final ArtifactGroupTypeEnum artifactGroupType = artifactInfo.getArtifactGroupType();
520 validateArtifactType(componentType, artifactGroupType, artifactConfiguration);
521 } catch (final ComponentException e) {
522 log.debug("Artifact is invalid", e);
523 BeEcompErrorManager.getInstance()
524 .logBeInvalidTypeError("Artifact Upload / Delete / Update - Not supported artifact type", artifactInfo
525 .getArtifactType(), "Artifact " + artifactInfo.getArtifactName());
526 log.debug("Not supported artifact type = {}", artifactInfo.getArtifactType());
527 final ResponseFormat responseFormat = componentsUtils
528 .getResponseFormat(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo
530 throw new ByResponseFormatComponentException(responseFormat);
534 private void validateArtifactType(final ComponentTypeEnum componentType, final ArtifactGroupTypeEnum groupType,
535 final ArtifactConfiguration artifactConfiguration) {
536 final boolean supportComponentType =
537 CollectionUtils.isNotEmpty(artifactConfiguration.getComponentTypes()) &&
538 artifactConfiguration.getComponentTypes().stream()
539 .anyMatch(componentType1 -> componentType1.getValue().equalsIgnoreCase(componentType.getValue()));
540 if (!supportComponentType) {
541 log.debug("Artifact Type '{}' not supported for Component Type '{}'",
542 artifactConfiguration.getType(), componentType.getValue());
543 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED,
544 artifactConfiguration.getType());
547 final boolean supportResourceType = artifactConfiguration.hasSupport(groupType);
548 if (!supportResourceType) {
549 log.debug("Artifact Type '{}' not supported for Component Type '{}' and Category '{}'",
550 artifactConfiguration.getType(), componentType.getValue(), groupType.getType());
551 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED,
552 artifactConfiguration.getType());
556 private boolean isArtifactSupported(final String artifactType) {
557 final Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
558 final List<ArtifactConfiguration> artifactConfigurationList = configuration.getArtifacts();
559 if (CollectionUtils.isEmpty(artifactConfigurationList)) {
562 return artifactConfigurationList.stream()
563 .anyMatch(artifactConfiguration -> artifactConfiguration.getType().equalsIgnoreCase(artifactType));
567 public ActionStatus updateGroupForHeat(ArtifactDefinition artifactInfo, ArtifactDefinition artAfterUpdate, Component parent) {
568 List<GroupDefinition> groups = parent.getGroups();
569 if (groups != null && !groups.isEmpty()) {
570 List<GroupDataDefinition> groupToUpdate = groups.stream()
571 .filter(g -> g.getArtifacts() != null && g.getArtifacts()
572 .contains(artifactInfo
574 .collect(Collectors.toList());
575 if (groupToUpdate != null && !groupToUpdate.isEmpty()) {
576 groupToUpdate.forEach(g -> {
577 g.getArtifacts().remove(artifactInfo.getUniqueId());
578 g.getArtifactsUuid().remove(artifactInfo.getArtifactUUID());
579 g.getArtifacts().add(artAfterUpdate.getUniqueId());
580 g.getArtifactsUuid().add(artAfterUpdate.getArtifactUUID());
581 if(!artifactInfo.getArtifactUUID().equals(artAfterUpdate.getArtifactUUID())){
582 g.setGroupUUID(UniqueIdBuilder.generateUUID());
585 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, groupToUpdate);
586 if (status.isRight()) {
587 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
588 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status.right().value()));
592 return ActionStatus.OK;
596 ActionStatus updateGroupForHeat(ArtifactDefinition artifactInfoHeat, ArtifactDefinition artHeatAfterUpdate, ArtifactDefinition artifactInfoHeatE, ArtifactDefinition artHEAfterUpdate, Component parent) {
597 List<GroupDefinition> groups = parent.getGroups();
598 if (groups != null && !groups.isEmpty()) {
599 List<GroupDataDefinition> groupToUpdate = groups.stream()
600 .filter(g -> g.getArtifacts() != null && g.getArtifacts()
601 .contains(artifactInfoHeat
603 .collect(Collectors.toList());
604 if (groupToUpdate != null && !groupToUpdate.isEmpty()) {
605 groupToUpdate.forEach(g -> {
606 g.getArtifacts().remove(artifactInfoHeat.getUniqueId());
607 g.getArtifactsUuid().remove(artifactInfoHeat.getArtifactUUID());
608 g.getArtifacts().remove(artifactInfoHeatE.getUniqueId());
609 g.getArtifacts().add(artHeatAfterUpdate.getUniqueId());
610 g.getArtifactsUuid().add(artHeatAfterUpdate.getArtifactUUID());
611 g.getArtifacts().add(artHEAfterUpdate.getUniqueId());
613 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, groupToUpdate);
614 if (status.isRight()) {
615 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
616 return componentsUtils.convertFromStorageResponse(status.right().value());
620 return ActionStatus.OK;
623 private ActionStatus updateGroupInstance(ArtifactDefinition artifactInfo, ArtifactDefinition artAfterUpdate, Component parent, String parentId) {
624 List<GroupInstance> updatedGroupInstances = new ArrayList<>();
625 List<GroupInstance> groupInstances = null;
626 Optional<ComponentInstance> componentInstOp = parent.getComponentInstances()
628 .filter(ci -> ci.getUniqueId().equals(parentId))
630 if (componentInstOp.isPresent()) {
631 groupInstances = componentInstOp.get().getGroupInstances();
633 if (CollectionUtils.isNotEmpty(groupInstances)) {
634 boolean isUpdated = false;
635 for (GroupInstance groupInstance : groupInstances) {
637 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifacts()) && groupInstance.getGroupInstanceArtifacts()
638 .contains(artifactInfo
640 groupInstance.getGroupInstanceArtifacts().remove(artifactInfo.getUniqueId());
641 groupInstance.getGroupInstanceArtifacts().add(artAfterUpdate.getUniqueId());
644 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifactsUuid()) && groupInstance.getGroupInstanceArtifactsUuid()
645 .contains(artifactInfo
646 .getArtifactUUID())) {
647 groupInstance.getGroupInstanceArtifactsUuid().remove(artifactInfo.getArtifactUUID());
648 groupInstance.getGroupInstanceArtifacts().add(artAfterUpdate.getArtifactUUID());
652 updatedGroupInstances.add(groupInstance);
656 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(parent, parentId, updatedGroupInstances);
657 if (status.isRight()) {
658 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
659 return componentsUtils.convertFromStorageResponse(status.right().value());
661 return ActionStatus.OK;
664 ArtifactDefinition generateNotSavedArtifact(Component parent, ArtifactDefinition artifactDefinition) {
665 if (artifactDefinition.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA) {
666 Either<byte[], ComponentException> decodedPayload = decodeToscaArtifactPayload(parent, false,
667 false, artifactDefinition.getArtifactType());
668 // TODO: This should not be done, but in order to keep this refactoring relatively small, we stop here
669 if(decodedPayload.isRight())
670 throw decodedPayload.right().value();
672 artifactDefinition.setPayload(decodedPayload.left().value());
673 return artifactDefinition;
677 String heatArtifactId = artifactDefinition.getGeneratedFromId();
678 Either<ArtifactDefinition, StorageOperationStatus> heatRes = artifactToscaOperation.getArtifactById(parent.getUniqueId(), heatArtifactId);
679 if (heatRes.isRight()) {
680 log.debug("Failed to fetch heat artifact by generated id {} for heat env {}", heatArtifactId, artifactDefinition.getUniqueId());
681 throw new StorageException(heatRes.right().value());
683 String generatedPayload = generateHeatEnvPayload(heatRes.left().value());
684 artifactDefinition.setPayloadData(generatedPayload);
685 return artifactDefinition;
689 private Either<ArtifactDefinition, Operation> handleUpdateHeatWithHeatEnvParams(String componentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction,
690 ComponentTypeEnum componentType, Component parent, String originData, String origMd5, ArtifactOperationInfo operation,
691 boolean needToUpdateGroup) {
692 Either<ArtifactDefinition, StorageOperationStatus> artifactHeatRes = artifactToscaOperation.getArtifactById(componentId, artifactInfo
693 .getGeneratedFromId());
694 ArtifactDefinition currHeatArtifact = artifactHeatRes.left().value();
695 if (origMd5 != null) {
696 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
697 if (ArrayUtils.isNotEmpty(artifactInfo.getPayloadData())) {
698 handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
699 } else { // duplicate
700 throw new ByActionStatusComponentException(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
703 return updateHeatParams(componentId, artifactInfo, auditingAction, parent, componentType, currHeatArtifact, needToUpdateGroup);
706 private void handleLockingAndCommit(Component parent, boolean shouldLock, boolean inTransaction, boolean actionSucceeded) {
707 if (actionSucceeded) {
709 if (!inTransaction) {
710 janusGraphDao.commit();
714 if (!inTransaction) {
715 janusGraphDao.rollback();
719 graphLockOperation.unlockComponent(parent.getUniqueId(), parent.getComponentType().getNodeType());
723 public ImmutablePair<String, byte[]> handleDownloadToscaModelRequest(Component component, ArtifactDefinition csarArtifact) {
724 if (artifactGenerationRequired(component, csarArtifact)) {
725 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(component, false, false);
727 if (generated.isRight()) {
728 log.debug("Failed to export tosca csar for component {} error {}", component.getUniqueId(), generated.right()
730 throw new ByResponseFormatComponentException(generated.right().value());
732 return new ImmutablePair<>(csarArtifact.getArtifactName(), generated.left().value());
734 return downloadArtifact(csarArtifact);
737 public ImmutablePair<String, byte[]> handleDownloadRequestById(String componentId, String artifactId, String userId, ComponentTypeEnum componentType, String parentId, String containerComponentType) {
738 // perform all validation in common flow
739 Either<ArtifactDefinition, Operation> result = handleArtifactRequest(componentId, userId, componentType, new ArtifactOperationInfo(false, false, ArtifactOperationEnum.DOWNLOAD), artifactId, null, null, null, null,
740 null, parentId, containerComponentType);
741 ArtifactDefinition artifactDefinition;
742 Either<ArtifactDefinition, Operation> insideValue = result;
743 if (insideValue.isLeft()) {
744 artifactDefinition = insideValue.left().value();
747 artifactDefinition = insideValue.right().value().getImplementationArtifact();
749 // for tosca artifacts and heat env on VF level generated on download without saving
750 if (artifactDefinition.getPayloadData() != null) {
751 return (new ImmutablePair<>(artifactDefinition.getArtifactName(), artifactDefinition
754 return downloadArtifact(artifactDefinition);
757 public Map<String, ArtifactDefinition> handleGetArtifactsByType(String containerComponentType, String parentId, ComponentTypeEnum componentType, String componentId, String artifactGroupType, String userId) {
759 // detect auditing type
760 Map<String, ArtifactDefinition> resMap = null;
765 if (userId == null) {
766 log.debug("handleGetArtifactsByType - no HTTP_CSP_HEADER , component id {}", componentId);
767 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
770 // check user existence
774 validateUserExists(userId);
776 // 5. check service/resource existence
777 // 6. check service/resource check out
778 // 7. user is owner of checkout state
779 String realComponentId = componentType == ComponentTypeEnum.RESOURCE_INSTANCE ? parentId : componentId;
780 ComponentParametersView componentFilter = new ComponentParametersView();
781 componentFilter.disableAll();
782 componentFilter.setIgnoreArtifacts(false);
783 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
784 componentFilter.setIgnoreComponentInstances(false);
787 Component component = validateComponentExistsByFilter(realComponentId, ComponentTypeEnum
788 .findByParamName(containerComponentType), componentFilter);
789 lockComponent(component, ARTIFACT_ACTION_LOCK);
790 boolean failed = false;
792 ArtifactGroupTypeEnum groupType = ArtifactGroupTypeEnum.findType(artifactGroupType);
794 if (groupType == null) {
795 log.debug("handleGetArtifactsByType - not failed groupType {} , component id {}", artifactGroupType, componentId);
796 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
798 if (groupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
799 List<ArtifactDefinition> list = getDeploymentArtifacts(component, componentId);
800 if (list != null && !list.isEmpty()) {
801 resMap = list.stream().collect(Collectors.toMap(ArtifactDataDefinition::getArtifactLabel, Function.identity()));
804 resMap = new HashMap<>();
809 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsMapStatus = getArtifacts(realComponentId, componentType
810 .getNodeType(), groupType, componentId);
811 if (artifactsMapStatus.isRight()) {
812 if (artifactsMapStatus.right().value() != StorageOperationStatus.NOT_FOUND) {
813 log.debug("handleGetArtifactsByType - not failed groupType {} , component id {}", artifactGroupType, componentId);
814 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
817 resMap = new HashMap<>();
821 resMap = artifactsMapStatus.left().value();
825 }catch (ComponentException e){
832 janusGraphDao.rollback();
836 janusGraphDao.commit();
839 componentType = component.getComponentType();
840 NodeTypeEnum nodeType = componentType.getNodeType();
841 graphLockOperation.unlockComponent(component.getUniqueId(), nodeType);
846 private ArtifactDefinition getArtifactIfBelongsToComponent(String componentId, ComponentTypeEnum componentType, String artifactId, Component component) {
847 // check artifact existence
848 Either<ArtifactDefinition, StorageOperationStatus> artifactResult = artifactToscaOperation.getArtifactById(componentId, artifactId,
849 componentType, component.getUniqueId());
850 if (artifactResult.isRight()) {
851 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_ARTIFACT_NOT_FOUND, artifactId, componentId);
853 // verify artifact belongs to component
855 switch (componentType) {
858 found = checkArtifactInComponent(component, artifactId);
860 case RESOURCE_INSTANCE:
861 found = checkArtifactInResourceInstance(component, componentId, artifactId);
867 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_ARTIFACT_NOT_FOUND, artifactId, componentType.name().toLowerCase());
869 return artifactResult.left().value();
872 private Either<ArtifactDefinition, Operation> handleCreate(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction, User user, ComponentTypeEnum componentType,
873 Component parent, String origMd5, String originData, String interfaceType, String operationName) {
874 byte[] decodedPayload = validateInput(componentId, artifactInfo, operation, auditingAction, null, user, componentType, parent, origMd5, originData, interfaceType, operationName);
875 return createArtifact(parent, componentId, artifactInfo, decodedPayload, componentType, auditingAction, interfaceType, operationName);
878 private ArtifactDefinition handleLink(String componentId, ArtifactDefinition artifactInfo, ComponentTypeEnum componentType,
880 ComponentInstance foundInstance = findComponentInstance(componentId, parent);
881 String instanceId = null;
882 if (foundInstance != null) {
883 instanceId = foundInstance.getUniqueId();
885 NodeTypeEnum nodeType = convertParentType(componentType);
886 Either<ArtifactDefinition, StorageOperationStatus> artifactDefinitionEither = artifactToscaOperation.addArtifactToComponent(artifactInfo, parent,
887 nodeType, true, instanceId);
888 if (artifactDefinitionEither.isRight()) {
889 throw new StorageException(artifactDefinitionEither.right().value(), artifactInfo.getArtifactDisplayName());
891 if (generateCustomizationUUIDOnInstance(parent.getUniqueId(), componentId, componentType) != StorageOperationStatus.OK) {
892 throw new StorageException(artifactDefinitionEither.right().value(), artifactInfo.getArtifactDisplayName());
894 return artifactDefinitionEither.left().value();
897 private <T> Either<ArtifactDefinition, T> lockComponentAndUpdateArtifact(
898 String parentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, String artifactId,
899 User user, ComponentTypeEnum componentType, Component parent, byte[] decodedPayload,
900 boolean shouldLock, boolean inTransaction) {
902 boolean failed = false;
903 boolean writeAudit = true;
905 lockComponent(parent, shouldLock, ARTIFACT_ACTION_LOCK);
907 return updateArtifactFlow(parent, parentId, artifactId, artifactInfo, decodedPayload, componentType,
910 catch (ComponentException ce) {
912 handleAuditing(auditingAction, parent, parentId, user, null, null, artifactId, ce.getResponseFormat(),
913 componentType, null);
918 catch (StorageException se) {
925 unlockComponent(failed, parent, inTransaction);
930 private byte[] validateInput(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction, String artifactId, User user, ComponentTypeEnum componentType,
931 Component parent, String origMd5, String originData, String interfaceType, String operationName) {
932 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
933 return getValidPayload(componentId, artifactInfo, operation, auditingAction, artifactId, user, componentType, parent, interfaceType, operationName);
936 private byte[] getValidPayload(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction,
937 String artifactId, User user, ComponentTypeEnum componentType, Component parent, String interfaceType, String operationName) {
939 Either<ArtifactDefinition, ResponseFormat> validateResult = validateInput(componentId, artifactInfo, operation, artifactId, user, interfaceType, operationName, componentType, parent);
940 if (validateResult.isRight()) {
941 ResponseFormat responseFormat = validateResult.right().value();
942 handleAuditing(auditingAction, parent, componentId, user, null, null, artifactId, responseFormat, componentType, null);
943 throw new ByResponseFormatComponentException(responseFormat);
946 Either<byte[], ResponseFormat> payloadEither = handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
947 if (payloadEither.isRight()) {
948 ResponseFormat responseFormat = payloadEither.right().value();
949 handleAuditing(auditingAction, parent, componentId, user, null, null, artifactId, responseFormat, componentType, null);
950 log.debug("Error during handle payload");
951 throw new ByResponseFormatComponentException(responseFormat);
953 // validate heat parameters. this part must be after the parameters are
954 // extracted in "handlePayload"
955 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParameters = validateAndConvertHeatParameters(artifactInfo, artifactInfo
957 if (validateAndConvertHeatParameters.isRight()) {
958 ResponseFormat responseFormat = validateAndConvertHeatParameters.right().value();
959 handleAuditing(auditingAction, parent, componentId, user, artifactInfo, null, artifactId, responseFormat, componentType, null);
960 log.debug("Error during handle payload");
961 throw new ByResponseFormatComponentException(responseFormat);
963 return payloadEither.left().value();
966 public void handleAuditing(AuditingActionEnum auditingActionEnum, Component component, String componentId, User user,
967 ArtifactDefinition artifactDefinition, String prevArtifactUuid, String currentArtifactUuid,
968 ResponseFormat responseFormat, ComponentTypeEnum componentTypeEnum, String resourceInstanceName) {
970 if (componentsUtils.isExternalApiEvent(auditingActionEnum)) {
976 user.setUserId("UNKNOWN");
978 handleInternalAuditEvent(auditingActionEnum, component, componentId, user, artifactDefinition, prevArtifactUuid, currentArtifactUuid, responseFormat, componentTypeEnum, resourceInstanceName);
981 private void handleInternalAuditEvent(AuditingActionEnum auditingActionEnum, Component component, String componentId, User user, ArtifactDefinition artifactDefinition, String prevArtifactUuid, String currentArtifactUuid, ResponseFormat responseFormat, ComponentTypeEnum componentTypeEnum, String resourceInstanceName) {
982 switch (componentTypeEnum) {
984 Resource resource = (Resource) component;
985 if (resource == null) {
986 // In that case, component ID should be instead of name
987 resource = new Resource();
988 resource.setName(componentId);
990 componentsUtils.auditResource(responseFormat, user, resource, resource.getName(), auditingActionEnum,
991 ResourceVersionInfo.newBuilder()
992 .artifactUuid(prevArtifactUuid)
993 .build(), currentArtifactUuid, artifactDefinition);
997 Service service = (Service) component;
998 if (service == null) {
999 // In that case, component ID should be instead of name
1000 service = new Service();
1001 service.setName(componentId);
1003 componentsUtils.auditComponent(responseFormat, user, service, auditingActionEnum, new ResourceCommonInfo(ComponentTypeEnum.SERVICE.getValue()),
1004 ResourceVersionInfo.newBuilder()
1005 .artifactUuid(prevArtifactUuid)
1007 ResourceVersionInfo.newBuilder()
1008 .artifactUuid(currentArtifactUuid)
1010 null, artifactDefinition, null);
1013 case RESOURCE_INSTANCE:
1014 if (resourceInstanceName == null) {
1015 resourceInstanceName = getResourceInstanceNameFromComponent(component, componentId);
1017 componentsUtils.auditComponent(responseFormat, user, component, auditingActionEnum,
1018 new ResourceCommonInfo(resourceInstanceName, ComponentTypeEnum.RESOURCE_INSTANCE.getValue()),
1019 ResourceVersionInfo.newBuilder()
1020 .artifactUuid(prevArtifactUuid)
1022 ResourceVersionInfo.newBuilder()
1023 .artifactUuid(currentArtifactUuid)
1025 null, artifactDefinition, null);
1032 private String getResourceInstanceNameFromComponent(Component component, String componentId) {
1033 ComponentInstance resourceInstance = component.getComponentInstances()
1035 .filter(p -> p.getUniqueId().equals(componentId))
1038 String resourceInstanceName = null;
1039 if (resourceInstance != null) {
1040 resourceInstanceName = resourceInstance.getName();
1042 return resourceInstanceName;
1045 private void validateMd5(String origMd5, String originData, byte[] payload, ArtifactOperationInfo operation) {
1046 if (origMd5 == null) {
1047 if (operation.isCreateOrLink() && ArrayUtils.isNotEmpty(payload)) {
1048 log.debug("Missing md5 header during artifact create");
1049 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_MD5);
1052 if (ArrayUtils.isNotEmpty(payload)) {
1053 log.debug("Cannot have payload while md5 header is missing");
1054 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
1057 String encodeBase64Str = GeneralUtility.calculateMD5Base64EncodedByString(originData);
1058 if (!encodeBase64Str.equals(origMd5)) {
1059 log.debug("The calculated md5 is different then the received one");
1060 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_MD5);
1065 private Either<ArtifactDefinition, ResponseFormat> validateInput(final String componentId,
1066 final ArtifactDefinition artifactInfo,
1067 final ArtifactOperationInfo operation,
1068 final String artifactId, final User user,
1069 String interfaceName,
1070 String operationName,
1071 final ComponentTypeEnum componentType,
1072 final Component parentComponent) {
1074 final ArtifactDefinition existingArtifactInfo =
1075 findArtifact(parentComponent, componentType, componentId, operation, artifactId);
1076 final boolean isCreateOrLinkOperation =
1077 ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum());
1078 if (!isCreateOrLinkOperation && existingArtifactInfo == null) {
1079 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactId);
1081 final Component component;
1082 if (parentComponent.getUniqueId().equals(componentId)) {
1083 component = parentComponent;
1085 final ComponentInstance componentInstance = findComponentInstance(componentId, parentComponent);
1086 component = findComponent(componentInstance.getComponentUid());
1087 component.setComponentType(componentType);
1089 if (!isCreateOrLinkOperation) {
1090 ignoreUnupdateableFieldsInUpdate(operation, artifactInfo, existingArtifactInfo);
1092 if (isInformationalArtifact(artifactInfo)) {
1093 validateInformationalArtifact(artifactInfo, component);
1095 Either<Boolean, ResponseFormat> validateAndSetArtifactname = validateAndSetArtifactName(artifactInfo);
1096 if (validateAndSetArtifactname.isRight()) {
1097 return Either.right(validateAndSetArtifactname.right().value());
1099 if (!validateArtifactNameUniqueness(componentId, parentComponent, artifactInfo, componentType)) {
1100 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_EXIST));
1102 if (operationName != null && interfaceName != null) {
1103 operationName = operationName.toLowerCase();
1104 interfaceName = interfaceName.toLowerCase();
1106 Either<ActionStatus, ResponseFormat> logicalNameStatus = handleArtifactLabel(componentId, parentComponent, operation, artifactInfo, operationName, componentType);
1107 if (logicalNameStatus.isRight()) {
1108 return Either.right(logicalNameStatus.right().value());
1110 // This is a patch to block possibility of updating service api fields
1111 // through other artifacts flow
1113 final ArtifactGroupTypeEnum artifactGroupType =
1114 operationName != null ? ArtifactGroupTypeEnum.LIFE_CYCLE : ArtifactGroupTypeEnum.INFORMATIONAL;
1115 if (operation.isNotCreateOrLink()) {
1116 checkAndSetUnUpdatableFields(user, artifactInfo, existingArtifactInfo, artifactGroupType);
1118 checkCreateFields(user, artifactInfo, artifactGroupType);
1121 composeArtifactId(componentId, artifactId, artifactInfo, interfaceName, operationName);
1122 if (existingArtifactInfo != null) {
1123 artifactInfo.setMandatory(existingArtifactInfo.getMandatory());
1124 if (operation.isNotCreateOrLink()) {
1125 validateArtifactTypeNotChanged(artifactInfo, existingArtifactInfo);
1129 // artifactGroupType is not allowed to be updated
1130 if (operation.isNotCreateOrLink()) {
1131 Either<ArtifactDefinition, ResponseFormat> validateGroupType = validateOrSetArtifactGroupType(artifactInfo, existingArtifactInfo);
1132 if (validateGroupType.isRight()) {
1133 return Either.right(validateGroupType.right().value());
1137 setArtifactTimeout(artifactInfo, existingArtifactInfo);
1138 if (isHeatArtifact(artifactInfo)) {
1139 validateHeatArtifact(parentComponent, componentId, artifactInfo);
1141 if (isDeploymentArtifact(artifactInfo)) {
1142 if (componentType != ComponentTypeEnum.RESOURCE_INSTANCE) {
1143 final String artifactName = artifactInfo.getArtifactName();
1144 if (operation.isCreateOrLink() || !artifactName.equalsIgnoreCase(existingArtifactInfo.getArtifactName())) {
1145 validateSingleDeploymentArtifactName(artifactName, parentComponent);
1148 validateDeploymentArtifact(artifactInfo, component);
1151 Either<Boolean, ResponseFormat> descriptionResult = validateAndCleanDescription(artifactInfo);
1152 if (descriptionResult.isRight()) {
1153 return Either.right(descriptionResult.right().value());
1156 validateArtifactType(artifactInfo, component.getComponentType());
1157 artifactInfo.setArtifactType(artifactInfo.getArtifactType().toUpperCase());
1158 if (existingArtifactInfo != null && existingArtifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.SERVICE_API) {
1159 // Change of type is not allowed and should be ignored
1161 artifactInfo.setArtifactType(ARTIFACT_TYPE_OTHER);
1163 Either<Boolean, ResponseFormat> validateUrl = validateAndServiceApiUrl(artifactInfo);
1164 if (validateUrl.isRight()) {
1165 return Either.right(validateUrl.right().value());
1168 Either<Boolean, ResponseFormat> validateUpdate = validateFirstUpdateHasPayload(artifactInfo, existingArtifactInfo);
1169 if (validateUpdate.isRight()) {
1170 log.debug("serviceApi first update cnnot be without payload.");
1171 return Either.right(validateUpdate.right().value());
1174 if (artifactInfo.getApiUrl() != null) {
1175 artifactInfo.setApiUrl(null);
1176 log.error("Artifact URL cannot be set through this API - ignoring");
1179 if (Boolean.TRUE.equals(artifactInfo.getServiceApi())) {
1180 artifactInfo.setServiceApi(false);
1181 log.error("Artifact service API flag cannot be changed - ignoring");
1185 return Either.left(artifactInfo);
1188 private Component findComponent(final String componentId) {
1189 Either<? extends Component, StorageOperationStatus> component = toscaOperationFacade.getToscaFullElement(componentId);
1190 if (component.isRight()) {
1191 log.debug("Component '{}' not found ", componentId);
1192 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, componentId);
1195 return component.left().value();
1198 private void ignoreUnupdateableFieldsInUpdate(final ArtifactOperationInfo operation,
1199 final ArtifactDefinition artifactInfo,
1200 final ArtifactDefinition currentArtifactInfo) {
1201 if (operation.isUpdate()) {
1202 artifactInfo.setArtifactType(currentArtifactInfo.getArtifactType());
1203 artifactInfo.setArtifactGroupType(currentArtifactInfo.getArtifactGroupType());
1204 artifactInfo.setArtifactLabel(currentArtifactInfo.getArtifactLabel());
1208 private ArtifactDefinition findArtifact(final Component parentComponent, final ComponentTypeEnum componentType,
1209 final String parentId, final ArtifactOperationInfo operation,
1210 final String artifactId) {
1211 ArtifactDefinition foundArtifact = null;
1212 if (StringUtils.isNotEmpty(artifactId)) {
1213 foundArtifact = findArtifact(parentComponent, componentType, parentId, artifactId);
1215 if (foundArtifact != null && operation.isCreateOrLink()) {
1216 log.debug("Artifact {} already exist", artifactId);
1217 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_EXIST, foundArtifact.getArtifactLabel());
1219 if (foundArtifact == null && operation.isNotCreateOrLink()) {
1220 log.debug("The artifact {} was not found on parent component or instance {}. ", artifactId, parentId);
1221 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, "");
1223 return foundArtifact;
1226 private ArtifactDefinition findArtifact(Component parentComponent, ComponentTypeEnum componentType, String parentId, String artifactId) {
1227 ArtifactDefinition foundArtifact;
1228 if (parentComponent.getUniqueId().equals(parentId)) {
1229 foundArtifact = artifactsResolver.findArtifactOnComponent(parentComponent, componentType, artifactId);
1232 ComponentInstance instance = findComponentInstance(parentId, parentComponent);
1233 foundArtifact = artifactsResolver.findArtifactOnComponentInstance(instance, artifactId);
1235 return foundArtifact;
1238 private void validateInformationalArtifact(final ArtifactDefinition artifactInfo, final Component component) {
1239 final ArtifactGroupTypeEnum groupType = artifactInfo.getArtifactGroupType();
1240 if (groupType != ArtifactGroupTypeEnum.INFORMATIONAL) {
1243 final ComponentTypeEnum parentComponentType = component.getComponentType();
1244 final String artifactType = artifactInfo.getArtifactType();
1245 final ArtifactConfiguration artifactConfiguration = loadArtifactTypeConfig(artifactType).orElse(null);
1246 if (artifactConfiguration == null) {
1247 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactType);
1249 validateArtifactType(parentComponentType, artifactInfo.getArtifactGroupType(), artifactConfiguration);
1251 if (component.getComponentType() == ComponentTypeEnum.RESOURCE ||
1252 component.getComponentType() == ComponentTypeEnum.RESOURCE_INSTANCE) {
1254 final ResourceTypeEnum resourceType = ((Resource) component).getResourceType();
1255 validateResourceType(resourceType, artifactInfo, artifactConfiguration.getResourceTypes());
1257 validateArtifactExtension(artifactConfiguration, artifactInfo);
1260 private NodeTypeEnum convertParentType(ComponentTypeEnum componentType) {
1261 if (componentType == ComponentTypeEnum.RESOURCE) {
1262 return NodeTypeEnum.Resource;
1264 else if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1265 return NodeTypeEnum.ResourceInstance;
1268 return NodeTypeEnum.Service;
1272 // This method is here for backward compatibility - when other parts of the code are cleaned can change to use the internal version
1273 public Either<ArtifactDefinition, ResponseFormat> handleDelete(
1274 String parentId, String artifactId, User user, Component parent,
1275 boolean shouldLock, boolean inTransaction) {
1277 ResponseFormat responseFormat;
1278 boolean operationSucceeded = false;
1280 lockComponent(ComponentTypeEnum.RESOURCE, artifactId, AuditingActionEnum.ARTIFACT_DELETE, user, parent);
1283 ArtifactDefinition artifactDefinition = handleDeleteInternal(parentId, artifactId,
1284 ComponentTypeEnum.RESOURCE, parent);
1285 operationSucceeded = true;
1286 return Either.left(artifactDefinition);
1288 catch (ComponentException ce) {
1289 responseFormat = componentsUtils.getResponseFormat(ce);
1290 handleAuditing(AuditingActionEnum.ARTIFACT_DELETE, parent, parentId, user, null, null,
1291 artifactId, responseFormat, ComponentTypeEnum.RESOURCE, null);
1292 return Either.right(responseFormat);
1294 catch (StorageException se) {
1295 responseFormat = componentsUtils.getResponseFormat(se);
1296 handleAuditing(AuditingActionEnum.ARTIFACT_DELETE, parent, parentId, user, null, null,
1297 artifactId, responseFormat, ComponentTypeEnum.RESOURCE, null);
1298 return Either.right(responseFormat);
1300 handleLockingAndCommit(parent, shouldLock, inTransaction, operationSucceeded);
1304 private ArtifactDefinition handleDeleteInternal(
1305 String parentId, String artifactId,
1306 ComponentTypeEnum componentType, Component parent
1308 NodeTypeEnum parentType = convertParentType(componentType);
1309 log.debug("Going to find the artifact {} on the component {}", artifactId, parent.getUniqueId());
1311 Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> getArtifactRes =
1312 findArtifact(artifactId, parent, parentId, componentType);
1313 if (getArtifactRes.isRight()) {
1314 log.debug("Failed to find the artifact {} belonging to {} on the component {}",
1315 artifactId, parentId, parent.getUniqueId());
1316 throw new ByActionStatusComponentException(getArtifactRes.right().value(), artifactId);
1318 ArtifactDefinition foundArtifact = getArtifactRes.left().value().getLeft();
1319 ComponentInstance foundInstance = getArtifactRes.left().value().getRight();
1321 String esId = foundArtifact.getEsId();
1322 Either<Boolean, StorageOperationStatus> needClone = ifTrue(StringUtils.isNotEmpty(esId), () -> forEach(
1323 artifactToscaOperation.isCloneNeeded(parent.getUniqueId(), foundArtifact, parentType),
1324 b -> log.debug("handleDelete: clone is needed for deleting {} held by {} in component {} {}? {}",
1325 foundArtifact.getArtifactName(), parentType, parent.getUniqueId(), parent.getName(), b)
1328 boolean needToClone = false;
1329 // TODO: This should not be done, but in order to keep this refactoring small, we stop here.
1330 // Remove this block once the above refactoring is merged.
1331 if(needClone.isLeft()) {
1332 needToClone = needClone.left().value();
1334 throw new StorageException(needClone.right().value(), foundArtifact.getArtifactDisplayName());
1337 boolean isNeedToDeleteArtifactFromDB =
1338 componentType == ComponentTypeEnum.RESOURCE_INSTANCE &&
1339 isArtifactOnlyResourceInstanceArtifact(foundArtifact, parent, parentId);
1341 boolean isDuplicated = false;
1343 ArtifactDataDefinition updatedArtifact = deleteOrUpdateArtifactOnGraph(parent, parentId, artifactId, parentType, foundArtifact, needToClone);
1344 isDuplicated = updatedArtifact.getDuplicated();
1346 if (!needToClone && !isDuplicated && isNeedToDeleteArtifactFromDB) {
1347 log.debug("Going to delete the artifact {} from the database. ", artifactId);
1348 CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(esId);
1349 if (cassandraStatus != CassandraOperationStatus.OK) {
1350 log.debug("Failed to delete the artifact {} from the database. ", artifactId);
1351 throw new StorageException(convertToStorageOperationStatus(cassandraStatus), foundArtifact.getArtifactDisplayName());
1354 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1355 List<GroupInstance> updatedGroupInstances = getUpdatedGroupInstances(artifactId, foundArtifact, foundInstance.getGroupInstances());
1356 if (CollectionUtils.isNotEmpty(updatedGroupInstances)) {
1357 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(parent, parentId, updatedGroupInstances);
1358 if (status.isRight()) {
1359 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
1360 throw new StorageException(status.right().value(), foundArtifact.getArtifactDisplayName());
1363 StorageOperationStatus status = generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentType);
1364 if (status != StorageOperationStatus.OK) {
1365 log.debug("Failed to generate new customization UUID for the component instance {}. ", parentId);
1366 throw new StorageException(status, foundArtifact.getArtifactDisplayName());
1369 List<GroupDataDefinition> updatedGroups = getUpdatedGroups(artifactId, foundArtifact, parent.getGroups());
1370 if (CollectionUtils.isNotEmpty(updatedGroups)) {
1371 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, updatedGroups);
1372 if (status.isRight()) {
1373 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
1374 throw new StorageException(status.right().value(), foundArtifact.getArtifactDisplayName());
1378 return foundArtifact;
1381 public static <R> Either<Boolean, R> ifTrue(boolean predicate, Supplier<Either<Boolean, R>> ifTrue) {
1382 return predicate ? ifTrue.get() : Either.left(false);
1385 public static <L, R> Either<L, R> forEach(Either<L, R> e, Consumer<L> c) {
1386 return e.left().map(l -> {
1392 private boolean isArtifactOnlyResourceInstanceArtifact(ArtifactDefinition foundArtifact, Component parent, String instanceId) {
1393 Optional<ComponentInstance> componentInstanceOpt = parent.getComponentInstanceById(instanceId);
1394 if (!componentInstanceOpt.isPresent()) {
1395 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "", "", parent.getName());
1397 ComponentInstance foundInstance = componentInstanceOpt.get();
1398 String componentUid = foundInstance.getComponentUid();
1399 Either<Component, StorageOperationStatus> getContainerRes = toscaOperationFacade.getToscaElement(componentUid);
1400 if (getContainerRes.isRight()) {
1401 log.debug("Failed to fetch the container component {}. ", componentUid);
1402 throw new StorageException(getContainerRes.right().value());
1404 Component origComponent = getContainerRes.left().value();
1405 Map<String, ArtifactDefinition> deploymentArtifacts = origComponent.getDeploymentArtifacts();
1406 if (MapUtils.isNotEmpty(deploymentArtifacts)) {
1407 Optional<String> op = deploymentArtifacts.keySet()
1409 .filter(a -> a.equals(foundArtifact.getArtifactLabel()))
1411 if (op.isPresent()) {
1415 Map<String, ArtifactDefinition> artifacts = origComponent.getArtifacts();
1416 if (MapUtils.isNotEmpty(artifacts)) {
1417 Optional<String> op = artifacts.keySet()
1419 .filter(a -> a.equals(foundArtifact.getArtifactLabel()))
1421 if (op.isPresent()) {
1428 private List<GroupDataDefinition> getUpdatedGroups(String artifactId, ArtifactDefinition foundArtifact, List<GroupDefinition> groups) {
1429 List<GroupDataDefinition> updatedGroups = new ArrayList<>();
1430 boolean isUpdated = false;
1431 if (groups != null) {
1432 for (GroupDefinition group : groups) {
1434 if (CollectionUtils.isNotEmpty(group.getArtifacts()) && group.getArtifacts().contains(artifactId)) {
1435 group.getArtifacts().remove(artifactId);
1438 if (CollectionUtils.isNotEmpty(group.getArtifactsUuid()) && group.getArtifactsUuid()
1439 .contains(foundArtifact.getArtifactUUID())) {
1440 group.getArtifactsUuid().remove(foundArtifact.getArtifactUUID());
1444 updatedGroups.add(group);
1448 return updatedGroups;
1451 private List<GroupInstance> getUpdatedGroupInstances(String artifactId, ArtifactDefinition foundArtifact, List<GroupInstance> groupInstances) {
1452 List<GroupInstance> updatedGroupInstances = new ArrayList<>();
1453 if (CollectionUtils.isNotEmpty(groupInstances)) {
1454 boolean isUpdated = false;
1455 for (GroupInstance groupInstance : groupInstances) {
1457 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifacts()) && groupInstance.getGroupInstanceArtifacts().contains(artifactId)) {
1458 groupInstance.getGroupInstanceArtifacts().remove(artifactId);
1461 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifactsUuid()) && groupInstance.getGroupInstanceArtifactsUuid()
1462 .contains(foundArtifact.getArtifactUUID())) {
1463 groupInstance.getGroupInstanceArtifactsUuid().remove(foundArtifact.getArtifactUUID());
1467 updatedGroupInstances.add(groupInstance);
1471 return updatedGroupInstances;
1474 private ArtifactDataDefinition deleteOrUpdateArtifactOnGraph(Component component, String parentId, String artifactId, NodeTypeEnum parentType, ArtifactDefinition foundArtifact, Boolean cloneIsNeeded) {
1476 Either<ArtifactDataDefinition, StorageOperationStatus> result;
1477 boolean isMandatory = foundArtifact.getMandatory() || foundArtifact.getServiceApi();
1478 String componentId = component.getUniqueId();
1479 String instanceId = componentId.equals(parentId) ? null : parentId;
1481 log.debug("Going to update mandatory artifact {} from the component {}", artifactId, parentId);
1482 resetMandatoryArtifactFields(foundArtifact);
1483 result = artifactToscaOperation.updateArtifactOnGraph(component, foundArtifact, parentType, artifactId, instanceId, true, true);
1485 else if (cloneIsNeeded) {
1486 log.debug("Going to clone artifacts and to delete the artifact {} from the component {}", artifactId, parentId);
1487 result = artifactToscaOperation.deleteArtifactWithCloningOnGraph(componentId, foundArtifact, parentType, instanceId, false);
1490 log.debug("Going to delete the artifact {} from the component {}", artifactId, parentId);
1491 result = artifactToscaOperation.removeArtifactOnGraph(foundArtifact, componentId, instanceId, parentType, false);
1493 if (result.isRight()) {
1494 throw new StorageException(result.right().value(), foundArtifact.getArtifactDisplayName());
1496 return result.left().value();
1499 private Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> findArtifact(String artifactId, Component fetchedContainerComponent, String parentId, ComponentTypeEnum componentType) {
1501 Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> result = null;
1502 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
1503 ComponentInstance foundInstance = null;
1504 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE && StringUtils.isNotEmpty(parentId)) {
1505 Optional<ComponentInstance> componentInstanceOpt = fetchedContainerComponent.getComponentInstances()
1507 .filter(i -> i.getUniqueId()
1510 if (!componentInstanceOpt.isPresent()) {
1511 result = Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER);
1514 foundInstance = componentInstanceOpt.get();
1515 fetchArtifactsFromInstance(artifactId, artifacts, foundInstance);
1519 fetchArtifactsFromComponent(artifactId, fetchedContainerComponent, artifacts);
1521 if (result == null) {
1522 if (artifacts.containsKey(artifactId)) {
1523 result = Either.left(new ImmutablePair<>(artifacts.get(artifactId), foundInstance));
1526 result = Either.right(ActionStatus.ARTIFACT_NOT_FOUND);
1532 private void fetchArtifactsFromComponent(String artifactId, Component component, Map<String, ArtifactDefinition> artifacts) {
1533 Map<String, ArtifactDefinition> currArtifacts;
1534 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getDeploymentArtifacts())) {
1535 currArtifacts = component.getDeploymentArtifacts()
1538 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, i -> i));
1539 if (MapUtils.isNotEmpty(currArtifacts)) {
1540 artifacts.putAll(currArtifacts);
1543 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getArtifacts())) {
1544 currArtifacts = component.getArtifacts()
1547 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1548 if (MapUtils.isNotEmpty(currArtifacts)) {
1549 artifacts.putAll(currArtifacts);
1552 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getArtifacts())) {
1553 currArtifacts = component.getToscaArtifacts()
1556 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1557 if (MapUtils.isNotEmpty(currArtifacts)) {
1558 artifacts.putAll(currArtifacts);
1563 private void fetchArtifactsFromInstance(String artifactId, Map<String, ArtifactDefinition> artifacts, ComponentInstance instance) {
1564 Map<String, ArtifactDefinition> currArtifacts;
1565 if (MapUtils.isNotEmpty(instance.getDeploymentArtifacts())) {
1566 currArtifacts = instance.getDeploymentArtifacts()
1569 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1570 if (MapUtils.isNotEmpty(currArtifacts)) {
1571 artifacts.putAll(currArtifacts);
1574 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(instance.getArtifacts())) {
1575 currArtifacts = instance.getArtifacts()
1578 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1579 if (MapUtils.isNotEmpty(currArtifacts)) {
1580 artifacts.putAll(currArtifacts);
1585 private StorageOperationStatus convertToStorageOperationStatus(CassandraOperationStatus cassandraStatus) {
1586 StorageOperationStatus result;
1587 switch (cassandraStatus) {
1589 result = StorageOperationStatus.OK;
1592 result = StorageOperationStatus.NOT_FOUND;
1594 case CLUSTER_NOT_CONNECTED:
1595 case KEYSPACE_NOT_CONNECTED:
1596 result = StorageOperationStatus.CONNECTION_FAILURE;
1599 result = StorageOperationStatus.GENERAL_ERROR;
1605 private void resetMandatoryArtifactFields(ArtifactDefinition fetchedArtifact) {
1606 if (fetchedArtifact != null) {
1607 log.debug("Going to reset mandatory artifact {} fields. ", fetchedArtifact.getUniqueId());
1608 fetchedArtifact.setEsId(null);
1609 fetchedArtifact.setArtifactName(null);
1610 fetchedArtifact.setDescription(null);
1611 fetchedArtifact.setApiUrl(null);
1612 fetchedArtifact.setArtifactChecksum(null);
1613 nodeTemplateOperation.setDefaultArtifactTimeout(fetchedArtifact.getArtifactGroupType(), fetchedArtifact);
1614 fetchedArtifact.setArtifactUUID(null);
1615 long time = System.currentTimeMillis();
1616 fetchedArtifact.setPayloadUpdateDate(time);
1617 fetchedArtifact.setHeatParameters(null);
1618 fetchedArtifact.setHeatParamsUpdateDate(null);
1622 private StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId, ComponentTypeEnum componentType) {
1623 StorageOperationStatus error = StorageOperationStatus.OK;
1624 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1625 log.debug("Need to re-generate customization UUID for instance {}", instanceId);
1626 error = toscaOperationFacade.generateCustomizationUUIDOnInstance(componentId, instanceId);
1631 private ArtifactDefinition handleDownload(String componentId, String artifactId, ComponentTypeEnum componentType,
1633 Either<ArtifactDefinition, StorageOperationStatus> artifactById = artifactToscaOperation.getArtifactById(componentId, artifactId, componentType,
1634 parent.getUniqueId());
1635 if (artifactById.isRight()) {
1636 throw new StorageException(artifactById.right().value());
1638 ArtifactDefinition artifactDefinition = artifactById.left().value();
1639 if (artifactDefinition == null) {
1640 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactId);
1642 return artifactDefinition;
1645 private Either<ActionStatus, ResponseFormat> handleArtifactLabel(String componentId, Component parentComponent, ArtifactOperationInfo operation, ArtifactDefinition artifactInfo, String operationName,
1646 ComponentTypeEnum componentType) {
1648 String artifactLabel = artifactInfo.getArtifactLabel();
1649 if (operationName == null && (artifactInfo.getArtifactLabel() == null || artifactInfo.getArtifactLabel()
1651 BeEcompErrorManager.getInstance()
1652 .logBeMissingArtifactInformationError("Artifact Update / Upload", "artifactLabel");
1653 log.debug("missing artifact logical name for component {}", componentId);
1654 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_LABEL));
1656 if (operation.isCreateOrLink() && !artifactInfo.getMandatory()) {
1658 if (operationName != null) {
1659 if (artifactInfo.getArtifactLabel() != null && !operationName.equals(artifactInfo.getArtifactLabel())) {
1660 log.debug("artifact label cannot be set {}", artifactLabel);
1661 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_LOGICAL_NAME_CANNOT_BE_CHANGED));
1664 artifactLabel = operationName;
1667 String displayName = artifactInfo.getArtifactDisplayName();
1668 if (displayName == null || displayName.isEmpty()) {
1669 displayName = artifactLabel;
1671 displayName = ValidationUtils.cleanArtifactDisplayName(displayName);
1672 artifactInfo.setArtifactDisplayName(displayName);
1674 if (!ValidationUtils.validateArtifactLabel(artifactLabel)) {
1675 log.debug("Invalid format form Artifact label : {}", artifactLabel);
1676 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1678 artifactLabel = ValidationUtils.normalizeArtifactLabel(artifactLabel);
1680 if (artifactLabel.isEmpty()) {
1681 log.debug("missing normalized artifact logical name for component {}", componentId);
1682 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_LABEL));
1685 if (!ValidationUtils.validateArtifactLabelLength(artifactLabel)) {
1686 log.debug("Invalid lenght form Artifact label : {}", artifactLabel);
1687 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_LABEL, String
1688 .valueOf(ValidationUtils.ARTIFACT_LABEL_LENGTH)));
1690 if (!validateLabelUniqueness(componentId, parentComponent, artifactLabel, componentType)) {
1691 log.debug("Non unique Artifact label : {}", artifactLabel);
1692 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_EXIST, artifactLabel));
1695 artifactInfo.setArtifactLabel(artifactLabel);
1697 return Either.left(ActionStatus.OK);
1700 private boolean validateLabelUniqueness(String componentId, Component parentComponent, String artifactLabel, ComponentTypeEnum componentType) {
1701 boolean isUnique = true;
1702 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifacts;
1703 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1704 artifacts = artifactToscaOperation.getAllInstanceArtifacts(parentComponent.getUniqueId(), componentId);
1707 artifacts = artifactToscaOperation.getArtifacts(componentId);
1710 if (artifacts.isLeft()) {
1711 for (String label : artifacts.left().value().keySet()) {
1712 if (label.equals(artifactLabel)) {
1718 if (componentType == ComponentTypeEnum.RESOURCE && isUnique) {
1719 isUnique = isUniqueLabelInResourceInterfaces(componentId, artifactLabel);
1724 boolean validateArtifactNameUniqueness(String componentId, Component parentComponent, ArtifactDefinition artifactInfo,
1725 ComponentTypeEnum componentType) {
1726 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifacts = getArtifacts(componentType,
1727 parentComponent, componentId, artifactInfo.getArtifactGroupType());
1728 String artifactName = artifactInfo.getArtifactName();
1729 if (artifacts.isLeft() && Objects.nonNull(artifacts.left().value())){
1730 if (artifacts.left().value().values().stream()
1731 .anyMatch(ad -> artifactName.equals(ad.getArtifactName())
1732 //check whether it is the same artifact we hold (by label)
1733 && !artifactInfo.getArtifactLabel().equals(ad.getArtifactLabel()))){
1737 if (ComponentTypeEnum.RESOURCE == componentType) {
1738 return isUniqueArtifactNameInResourceInterfaces(componentId, artifactName, artifactInfo.getArtifactLabel());
1743 private boolean isUniqueArtifactNameInResourceInterfaces(String componentId, String artifactName, String artifactLabel) {
1744 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> allInterfacesOfResource = interfaceLifecycleOperation
1745 .getAllInterfacesOfResource(componentId, true, true);
1747 if (allInterfacesOfResource.isLeft()){
1748 return allInterfacesOfResource.left().value()
1750 .stream().map(InterfaceDefinition :: getOperationsMap)
1751 .flatMap(map -> map.values().stream())
1752 .map(OperationDataDefinition::getImplementation)
1753 .filter(Objects::nonNull)
1754 .noneMatch(add -> artifactName.equals(add.getArtifactName())
1755 && !artifactLabel.equals(add.getArtifactLabel()));
1760 private boolean isUniqueLabelInResourceInterfaces(String componentId, String artifactLabel) {
1761 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> allInterfacesOfResource = interfaceLifecycleOperation
1762 .getAllInterfacesOfResource(componentId, true, true);
1764 if (allInterfacesOfResource.isLeft()){
1765 return allInterfacesOfResource.left().value()
1767 .stream().map(InterfaceDefinition :: getOperationsMap)
1768 .flatMap(map -> map.values().stream())
1769 .map(OperationDataDefinition::getImplementation)
1770 .filter(Objects::nonNull)
1771 .noneMatch(add -> artifactLabel.equals(add.getArtifactLabel()));
1776 private Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifacts(ComponentTypeEnum componentType, Component parentComponent,
1777 String componentId, ArtifactGroupTypeEnum artifactGroupType) {
1778 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsResponse;
1779 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1780 artifactsResponse = artifactToscaOperation.getAllInstanceArtifacts(parentComponent.getUniqueId(), componentId);
1783 artifactsResponse = artifactToscaOperation.getArtifacts(componentId);
1785 if (artifactsResponse.isRight() && artifactsResponse.right().value() == StorageOperationStatus.NOT_FOUND) {
1786 log.debug("failed to retrieve artifacts for {} ", componentId);
1787 return Either.right(artifactsResponse.right().value());
1789 return Either.left(artifactsResponse.left().value().entrySet()
1791 .filter(x -> artifactGroupType == x.getValue().getArtifactGroupType())
1792 .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
1795 // ***************************************************************
1797 private Either<ArtifactDefinition, Operation> createArtifact(Component parent, String parentId, ArtifactDefinition artifactInfo, byte[] decodedPayload,
1798 ComponentTypeEnum componentTypeEnum, AuditingActionEnum auditingActionEnum, String interfaceType, String operationName) {
1800 DAOArtifactData artifactData = createEsArtifactData(artifactInfo, decodedPayload);
1801 if (artifactData == null) {
1802 BeEcompErrorManager.getInstance().logBeDaoSystemError("Upload Artifact");
1803 log.debug("Failed to create artifact object for ES.");
1804 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
1806 ComponentInstance foundInstance = findComponentInstance(parentId, parent);
1807 String instanceId = null;
1808 if (foundInstance != null) {
1809 if (foundInstance.isArtifactExists(artifactInfo.getArtifactGroupType(), artifactInfo.getArtifactLabel())) {
1810 log.debug("Failed to create artifact, already exists");
1811 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_EXIST, artifactInfo.getArtifactLabel());
1813 instanceId = foundInstance.getUniqueId();
1815 // set on graph object id of artifact in ES!
1816 artifactInfo.setEsId(artifactData.getId());
1818 Either<ArtifactDefinition, Operation> operationResult;
1819 if (interfaceType != null && operationName != null) {
1820 // lifecycle artifact
1821 Operation operation = convertToOperation(artifactInfo, operationName);
1822 Either<Operation, StorageOperationStatus> result = interfaceLifecycleOperation.updateInterfaceOperation(parentId, interfaceType, operationName, operation);
1823 if (result.isRight()) {
1824 throw new StorageException(result.right().value());
1826 operationResult = Either.right(result.left().value());
1829 // information/deployment/api artifacts
1830 NodeTypeEnum nodeType = convertParentType(componentTypeEnum);
1831 Either<ArtifactDefinition, StorageOperationStatus> result = artifactToscaOperation.addArtifactToComponent(
1832 artifactInfo, parent, nodeType, true, instanceId);
1833 if (result.isRight()) {
1834 throw new StorageException(result.right().value());
1836 ArtifactDefinition artifactDefinition = result.left().value();
1837 artifactData.setId(artifactDefinition.getEsId());
1838 operationResult = Either.left(artifactDefinition);
1840 if (generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentTypeEnum) != StorageOperationStatus.OK) {
1841 throw new StorageException(generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentTypeEnum));
1844 saveArtifactInCassandra(artifactData, parent, artifactInfo, "", "", auditingActionEnum, componentTypeEnum);
1845 return operationResult;
1848 private ComponentInstance findComponentInstance(String componentInstanceId, Component containerComponent) {
1849 ComponentInstance foundInstance = null;
1850 if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
1851 foundInstance = containerComponent.getComponentInstances()
1853 .filter(i -> i.getUniqueId().equals(componentInstanceId))
1857 return foundInstance;
1860 private void validateDeploymentArtifact(final ArtifactDefinition artifactInfo, final Component component) {
1861 final ComponentTypeEnum componentType = component.getComponentType();
1862 if (componentType != ComponentTypeEnum.RESOURCE &&
1863 componentType != ComponentTypeEnum.SERVICE &&
1864 componentType != ComponentTypeEnum.RESOURCE_INSTANCE) {
1865 log.debug("Invalid component type '{}' for artifact. "
1866 + "Expected Resource, Component or Resource Instance", componentType.getValue());
1867 throw new ByActionStatusComponentException(MISMATCH_BETWEEN_ARTIFACT_TYPE_AND_COMPONENT_TYPE,
1868 componentType.getValue(), "Service, Resource or ResourceInstance", componentType.getValue());
1870 final String artifactType = artifactInfo.getArtifactType();
1871 final ArtifactConfiguration artifactConfiguration = loadArtifactTypeConfig(artifactType).orElse(null);
1872 if (artifactConfiguration == null) {
1873 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactType);
1875 validateArtifactType(componentType, artifactInfo.getArtifactGroupType(), artifactConfiguration);
1876 if (componentType == ComponentTypeEnum.RESOURCE || componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1877 final Resource resource = (Resource) component;
1878 final ResourceTypeEnum resourceType = resource.getResourceType();
1880 validateResourceType(resourceType, artifactInfo, artifactConfiguration.getResourceTypes());
1883 validateArtifactExtension(artifactConfiguration, artifactInfo);
1886 private void validateHeatArtifact(final Component parentComponent, final String componentId,
1887 final ArtifactDefinition artifactDefinition) {
1888 final String artifactType = artifactDefinition.getArtifactType();
1889 final ArtifactTypeEnum artifactTypeEnum = ArtifactTypeEnum.parse(artifactType);
1890 if (artifactTypeEnum == null) {
1894 switch (artifactTypeEnum) {
1898 validateHeatTimeoutValue(artifactDefinition);
1901 validateHeatEnvDeploymentArtifact(parentComponent, componentId, artifactDefinition);
1908 private void setArtifactTimeout(final ArtifactDefinition newArtifactInfo,
1909 final ArtifactDefinition existingArtifactInfo) {
1911 final String artifactType = newArtifactInfo.getArtifactType();
1912 final ArtifactTypeEnum artifactTypeEnum = ArtifactTypeEnum.parse(artifactType);
1913 if (artifactTypeEnum == null) {
1914 newArtifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
1917 switch (artifactTypeEnum) {
1921 if (newArtifactInfo.getTimeout() == null) {
1922 if (existingArtifactInfo == null) {
1923 newArtifactInfo.setTimeout(NodeTemplateOperation.getDefaultHeatTimeout());
1925 newArtifactInfo.setTimeout(existingArtifactInfo.getTimeout());
1930 newArtifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
1936 void validateDeploymentArtifactTypeIsLegalForParent(ArtifactDefinition artifactInfo, ArtifactTypeEnum artifactType, Map<String, ArtifactTypeConfig> resourceDeploymentArtifacts) {
1937 if ((resourceDeploymentArtifacts == null) || !resourceDeploymentArtifacts.containsKey(artifactType.name())) {
1938 log.debug("Artifact Type: {} Not found !", artifactInfo.getArtifactType());
1939 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
1943 Optional<ArtifactConfiguration> loadArtifactTypeConfig(final String artifactType) {
1944 if (artifactType == null) {
1945 return Optional.empty();
1947 final List<ArtifactConfiguration> artifactConfigurationList =
1948 ConfigurationManager.getConfigurationManager().getConfiguration().getArtifacts();
1949 if (CollectionUtils.isEmpty(artifactConfigurationList)) {
1950 return Optional.empty();
1953 return artifactConfigurationList.stream()
1954 .filter(artifactConfiguration -> artifactConfiguration.getType().equalsIgnoreCase(artifactType))
1958 private Either<Boolean, ResponseFormat> extractHeatParameters(ArtifactDefinition artifactInfo) {
1959 // extract heat parameters
1960 if (artifactInfo.getPayloadData() != null) {
1961 String heatDecodedPayload = new String(Base64.decodeBase64(artifactInfo.getPayloadData()));
1962 Either<List<HeatParameterDefinition>, ResultStatusEnum> heatParameters = ImportUtils.getHeatParamsWithoutImplicitTypes(heatDecodedPayload, artifactInfo
1963 .getArtifactType());
1964 if (heatParameters.isRight() && (heatParameters.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND)) {
1965 log.info("failed to parse heat parameters ");
1966 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, artifactInfo
1967 .getArtifactType());
1968 return Either.right(responseFormat);
1970 else if (heatParameters.isLeft() && heatParameters.left().value() != null) {
1971 artifactInfo.setListHeatParameters(heatParameters.left().value());
1974 return Either.left(true);
1979 void validateArtifactExtension(final ArtifactConfiguration artifactConfiguration,
1980 final ArtifactDefinition artifactDefinition) {
1981 final List<String> acceptedTypes = artifactConfiguration.getAcceptedTypes();
1983 * No need to check specific types. In case there are no acceptedTypes in configuration, then any type is accepted.
1985 if (CollectionUtils.isEmpty(acceptedTypes)) {
1988 final String artifactName = artifactDefinition.getArtifactName();
1989 final String fileExtension = FilenameUtils.getExtension(artifactName);
1991 if (fileExtension == null || !acceptedTypes.contains(fileExtension.toLowerCase())) {
1992 final String artifactType = artifactDefinition.getArtifactType();
1993 log.debug("File extension \"{}\" is not allowed for artifact type \"{}\"", fileExtension, artifactType);
1994 throw new ByActionStatusComponentException(ActionStatus.WRONG_ARTIFACT_FILE_EXTENSION, artifactType);
1999 void validateHeatEnvDeploymentArtifact(final Component parentComponent, final String parentId,
2000 final ArtifactDefinition artifactInfo) {
2001 final Wrapper<ArtifactDefinition> heatMDWrapper = new Wrapper<>();
2002 final Wrapper<byte[]> payloadWrapper = new Wrapper<>();
2004 validateYaml(artifactInfo);
2005 validateHeatExist(parentComponent.getUniqueId(), parentId, heatMDWrapper, artifactInfo, parentComponent.getComponentType());
2007 if (!heatMDWrapper.isEmpty()) {
2008 fillArtifactPayload(payloadWrapper, heatMDWrapper.getInnerElement());
2011 if (!heatMDWrapper.isEmpty()) {
2012 validateEnvVsHeat(artifactInfo, heatMDWrapper.getInnerElement(), payloadWrapper.getInnerElement());
2016 public void fillArtifactPayload(Wrapper<byte[]> payloadWrapper, ArtifactDefinition artifactDefinition) {
2017 if (ArrayUtils.isEmpty(artifactDefinition.getPayloadData())) {
2018 Either<DAOArtifactData, CassandraOperationStatus> eitherArtifactData = artifactCassandraDao.getArtifact(artifactDefinition.getEsId());
2019 if (eitherArtifactData.isLeft()) {
2020 byte[] data = eitherArtifactData.left().value().getDataAsArray();
2021 payloadWrapper.setInnerElement(Base64.encodeBase64(data));
2024 log.debug("Error getting payload for artifact:{}", artifactDefinition.getArtifactName());
2025 throw new StorageException(DaoStatusConverter.convertCassandraStatusToStorageStatus(eitherArtifactData.right().value()));
2029 payloadWrapper.setInnerElement(artifactDefinition.getPayloadData());
2033 private void validateEnvVsHeat(ArtifactDefinition envArtifact, ArtifactDefinition heatArtifact, byte[] heatPayloadData) {
2034 String envPayload = new String(Base64.decodeBase64(envArtifact.getPayloadData()));
2035 Map<String, Object> heatEnvToscaJson = (Map<String, Object>) new Yaml().load(envPayload);
2036 String heatDecodedPayload = new String(Base64.decodeBase64(heatPayloadData));
2037 Map<String, Object> heatToscaJson = (Map<String, Object>) new Yaml().load(heatDecodedPayload);
2039 Either<Map<String, Object>, ResultStatusEnum> eitherHeatEnvProperties = ImportUtils.findFirstToscaMapElement(heatEnvToscaJson, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
2040 if (eitherHeatEnvProperties.isRight()) {
2041 log.debug("Invalid heat env format for file:{}", envArtifact.getArtifactName());
2042 throw new ByActionStatusComponentException(ActionStatus.CORRUPTED_FORMAT, "Heat Env");
2044 Either<Map<String, Object>, ResultStatusEnum> eitherHeatProperties = ImportUtils.findFirstToscaMapElement(heatToscaJson, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
2045 if (eitherHeatProperties.isRight()) {
2046 log.debug("Invalid heat format for file:{}", heatArtifact.getArtifactName());
2047 throw new ByActionStatusComponentException(ActionStatus.CORRUPTED_FORMAT, "Heat");
2049 Set<String> heatPropertiesKeys = eitherHeatProperties.left().value().keySet();
2050 Set<String> heatEnvPropertiesKeys = eitherHeatEnvProperties.left().value().keySet();
2051 heatEnvPropertiesKeys.removeAll(heatPropertiesKeys);
2052 if (!heatEnvPropertiesKeys.isEmpty()) {
2053 log.debug("Validation of heat_env for artifact:{} vs heat artifact for artifact :{} failed", envArtifact.getArtifactName(), heatArtifact.getArtifactName());
2054 throw new ByActionStatusComponentException(ActionStatus.MISMATCH_HEAT_VS_HEAT_ENV, envArtifact.getArtifactName(), heatArtifact.getArtifactName());
2058 private void validateYaml(ArtifactDefinition artifactInfo) {
2059 YamlToObjectConverter yamlConverter = new YamlToObjectConverter();
2060 boolean isYamlValid = yamlConverter.isValidYamlEncoded64(artifactInfo.getPayloadData());
2062 log.debug("Yaml is not valid for artifact : {}", artifactInfo.getArtifactName());
2063 throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML, artifactInfo.getArtifactType());
2067 private boolean isValidXml(byte[] xmlToParse) {
2068 boolean isXmlValid = true;
2070 XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
2071 setFeatures(reader);
2072 reader.parse(new InputSource(new ByteArrayInputStream(xmlToParse)));
2074 catch (ParserConfigurationException | IOException | SAXException e) {
2075 log.debug("Xml is invalid : {}", e.getMessage(), e);
2081 private void setFeatures(XMLReader reader) throws SAXNotSupportedException {
2083 reader.setFeature("http://apache.org/xml/features/validation/schema", false);
2084 reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
2086 catch (SAXNotRecognizedException e) {
2087 log.debug("Xml parser couldn't set feature: \"http://apache.org/xml/features/validation/schema\", false", e.getMessage(), e);
2091 private void validateSingleDeploymentArtifactName(final String artifactName, final Component parentComponent) {
2092 boolean artifactNameFound = false;
2093 final Iterator<ArtifactDefinition> parentDeploymentArtifactsItr =
2094 getDeploymentArtifacts(parentComponent, null).iterator();
2096 while (!artifactNameFound && parentDeploymentArtifactsItr.hasNext()) {
2097 artifactNameFound = artifactName.equalsIgnoreCase(parentDeploymentArtifactsItr.next().getArtifactName());
2099 if (artifactNameFound) {
2100 final ComponentTypeEnum componentType = parentComponent.getComponentType();
2101 log.debug("Can't upload artifact: {}, because another artifact with this name already exist.", artifactName);
2102 throw new ByActionStatusComponentException(ActionStatus.DEPLOYMENT_ARTIFACT_NAME_ALREADY_EXISTS,
2103 componentType.getValue(), parentComponent.getName(), artifactName);
2107 private void validateHeatExist(String componentId, String parentRiId, Wrapper<ArtifactDefinition> heatArtifactMDWrapper, ArtifactDefinition heatEnvArtifact,
2108 ComponentTypeEnum componentType) {
2109 final Either<ArtifactDefinition, StorageOperationStatus> res = artifactToscaOperation
2110 .getHeatArtifactByHeatEnvId(parentRiId, heatEnvArtifact, componentId, componentType);
2111 if (res.isRight()) {
2112 throw new ByActionStatusComponentException(ActionStatus.MISSING_HEAT);
2115 heatArtifactMDWrapper.setInnerElement(res.left().value());
2119 void validateHeatTimeoutValue(final ArtifactDefinition artifactInfo) {
2120 log.trace("Started HEAT pre-payload validation for artifact {}", artifactInfo.getArtifactLabel());
2121 // timeout > 0 for HEAT artifacts
2122 if (artifactInfo.getTimeout() == null || artifactInfo.getTimeout() < 1) {
2123 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_TIMEOUT);
2125 // US649856 - Allow several HEAT files on Resource
2126 log.trace("Ended HEAT validation for artifact {}", artifactInfo.getArtifactLabel());
2130 void validateResourceType(final ResourceTypeEnum resourceType, final ArtifactDefinition artifactInfo,
2131 final List<String> typeList) {
2132 if (CollectionUtils.isEmpty(typeList) || typeList.contains(resourceType.getValue())) {
2135 final String listToString = typeList.stream().collect(Collectors.joining(", "));
2136 throw new ByActionStatusComponentException(MISMATCH_BETWEEN_ARTIFACT_TYPE_AND_COMPONENT_TYPE,
2137 artifactInfo.getArtifactGroupType().getType(), listToString, resourceType.getValue());
2141 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParameters(ArtifactDefinition artifactInfo, String artifactType) {
2142 if (artifactInfo.getHeatParameters() != null) {
2143 for (HeatParameterDefinition heatParam : artifactInfo.getListHeatParameters()) {
2144 String parameterType = heatParam.getType();
2145 HeatParameterType heatParameterType = HeatParameterType.isValidType(parameterType);
2146 String artifactTypeStr = artifactType != null ? artifactType : ArtifactTypeEnum.HEAT.getType();
2147 if (heatParameterType == null) {
2148 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_HEAT_PARAMETER_TYPE, artifactTypeStr, heatParam
2150 return Either.right(responseFormat);
2153 StorageOperationStatus validateAndUpdateProperty = heatParametersOperation.validateAndUpdateProperty(heatParam);
2154 if (validateAndUpdateProperty != StorageOperationStatus.OK) {
2155 log.debug("Heat parameter {} is invalid. Status is {}", heatParam.getName(), validateAndUpdateProperty);
2156 ActionStatus status = ActionStatus.INVALID_HEAT_PARAMETER_VALUE;
2157 ResponseFormat responseFormat = componentsUtils.getResponseFormat(status, artifactTypeStr, heatParam
2158 .getType(), heatParam.getName());
2159 return Either.right(responseFormat);
2163 return Either.left(artifactInfo);
2166 public List<ArtifactDefinition> getDeploymentArtifacts(final Component component, final String ciId) {
2167 final ComponentTypeEnum componentType = component.getComponentType();
2168 if (component.getDeploymentArtifacts() == null) {
2169 return Collections.emptyList();
2171 final List<ArtifactDefinition> deploymentArtifacts = new ArrayList<>();
2172 if (ComponentTypeEnum.RESOURCE == componentType && ciId != null) {
2173 final Either<ComponentInstance, ResponseFormat> getRI =
2174 getRIFromComponent(component, ciId, null, null, null);
2175 if (getRI.isRight()) {
2176 return Collections.emptyList();
2178 final ComponentInstance ri = getRI.left().value();
2179 if (ri.getDeploymentArtifacts() != null) {
2180 deploymentArtifacts.addAll(ri.getDeploymentArtifacts().values());
2183 deploymentArtifacts.addAll(component.getDeploymentArtifacts().values());
2185 return deploymentArtifacts;
2188 private void checkCreateFields(User user, ArtifactDefinition artifactInfo, ArtifactGroupTypeEnum type) {
2189 // on create if null add informational to current
2190 if (artifactInfo.getArtifactGroupType() == null) {
2191 artifactInfo.setArtifactGroupType(type);
2193 if (artifactInfo.getUniqueId() != null) {
2194 log.error("artifact uniqid cannot be set ignoring");
2196 artifactInfo.setUniqueId(null);
2198 if (artifactInfo.getArtifactRef() != null) {
2199 log.error("artifact ref cannot be set ignoring");
2201 artifactInfo.setArtifactRef(null);
2203 if (artifactInfo.getArtifactRepository() != null) {
2204 log.error("artifact repository cannot be set ignoring");
2206 artifactInfo.setArtifactRepository(null);
2208 if (artifactInfo.getUserIdCreator() != null) {
2209 log.error("creator uuid cannot be set ignoring");
2211 artifactInfo.setArtifactCreator(user.getUserId());
2213 if (artifactInfo.getUserIdLastUpdater() != null) {
2214 log.error("userId of last updater cannot be set ignoring");
2216 artifactInfo.setUserIdLastUpdater(user.getUserId());
2218 if (artifactInfo.getCreatorFullName() != null) {
2219 log.error("creator Full name cannot be set ignoring");
2221 String fullName = user.getFirstName() + " " + user.getLastName();
2222 artifactInfo.setUpdaterFullName(fullName);
2224 if (artifactInfo.getUpdaterFullName() != null) {
2225 log.error("updater Full name cannot be set ignoring");
2227 artifactInfo.setUpdaterFullName(fullName);
2229 if (artifactInfo.getCreationDate() != null) {
2230 log.error("Creation Date cannot be set ignoring");
2232 long time = System.currentTimeMillis();
2233 artifactInfo.setCreationDate(time);
2235 if (artifactInfo.getLastUpdateDate() != null) {
2236 log.error("Last Update Date cannot be set ignoring");
2238 artifactInfo.setLastUpdateDate(time);
2240 if (artifactInfo.getEsId() != null) {
2241 log.error("es id cannot be set ignoring");
2243 artifactInfo.setEsId(null);
2248 private String composeArtifactId(String resourceId, String artifactId, ArtifactDefinition artifactInfo, String interfaceName, String operationName) {
2249 String id = artifactId;
2250 if (artifactId == null || artifactId.isEmpty()) {
2251 String uniqueId = null;
2252 if (interfaceName != null && operationName != null) {
2253 uniqueId = UniqueIdBuilder.buildArtifactByInterfaceUniqueId(resourceId, interfaceName, operationName, artifactInfo
2254 .getArtifactLabel());
2257 uniqueId = UniqueIdBuilder.buildPropertyUniqueId(resourceId, artifactInfo.getArtifactLabel());
2259 artifactInfo.setUniqueId(uniqueId);
2260 artifactInfo.setEsId(uniqueId);
2264 artifactInfo.setUniqueId(artifactId);
2265 artifactInfo.setEsId(artifactId);
2270 private Either<Boolean, ResponseFormat> validateFirstUpdateHasPayload(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2271 if (currentArtifact.getEsId() == null && (artifactInfo.getPayloadData() == null || artifactInfo.getPayloadData().length == 0)) {
2272 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD));
2274 return Either.left(true);
2279 Either<Boolean, ResponseFormat> validateAndSetArtifactName(ArtifactDefinition artifactInfo) {
2280 if (artifactInfo.getArtifactName() == null || artifactInfo.getArtifactName().isEmpty()) {
2281 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_NAME));
2283 String normalizeFileName = ValidationUtils.normalizeFileName(artifactInfo.getArtifactName());
2284 if (normalizeFileName == null || normalizeFileName.isEmpty()) {
2285 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_NAME));
2287 artifactInfo.setArtifactName(normalizeFileName);
2289 if (!ValidationUtils.validateArtifactNameLength(artifactInfo.getArtifactName())) {
2290 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_NAME, String.valueOf(ValidationUtils.ARTIFACT_NAME_LENGTH)));
2293 return Either.left(true);
2296 private void validateArtifactTypeNotChanged(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2297 if (StringUtils.isEmpty(artifactInfo.getArtifactType())) {
2298 log.info("artifact type is missing operation ignored");
2299 throw new ByActionStatusComponentException(ActionStatus.MISSING_ARTIFACT_TYPE);
2302 if (!currentArtifact.getArtifactType().equalsIgnoreCase(artifactInfo.getArtifactType())) {
2303 log.info("artifact type cannot be changed operation ignored");
2304 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2308 private Either<ArtifactDefinition, ResponseFormat> validateOrSetArtifactGroupType(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2310 if (Objects.nonNull(artifactInfo) && Objects.nonNull(currentArtifact)) {
2311 if (artifactInfo.getArtifactGroupType() == null) {
2312 artifactInfo.setArtifactGroupType(currentArtifact.getArtifactGroupType());
2313 } else if (!currentArtifact.getArtifactGroupType()
2315 .equalsIgnoreCase(artifactInfo.getArtifactGroupType().getType())) {
2316 log.info("artifact group type cannot be changed. operation failed");
2317 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2320 return Either.left(artifactInfo);
2323 private void checkAndSetUnUpdatableFields(User user, ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact, ArtifactGroupTypeEnum type) {
2325 // on update if null add informational to current
2326 if (currentArtifact.getArtifactGroupType() == null && type != null) {
2327 currentArtifact.setArtifactGroupType(type);
2330 if (artifactInfo.getUniqueId() != null && !currentArtifact.getUniqueId().equals(artifactInfo.getUniqueId())) {
2331 log.error("artifact uniqid cannot be set ignoring");
2333 artifactInfo.setUniqueId(currentArtifact.getUniqueId());
2335 if (artifactInfo.getArtifactRef() != null && !currentArtifact.getArtifactRef()
2336 .equals(artifactInfo.getArtifactRef())) {
2337 log.error("artifact ref cannot be set ignoring");
2339 artifactInfo.setArtifactRef(currentArtifact.getArtifactRef());
2341 if (artifactInfo.getArtifactRepository() != null && !currentArtifact.getArtifactRepository()
2342 .equals(artifactInfo.getArtifactRepository())) {
2343 log.error("artifact repository cannot be set ignoring");
2345 artifactInfo.setArtifactRepository(currentArtifact.getArtifactRepository());
2347 if (artifactInfo.getUserIdCreator() != null && !currentArtifact.getUserIdCreator()
2348 .equals(artifactInfo.getUserIdCreator())) {
2349 log.error("creator uuid cannot be set ignoring");
2351 artifactInfo.setUserIdCreator(currentArtifact.getUserIdCreator());
2353 if (artifactInfo.getArtifactCreator() != null && !currentArtifact.getArtifactCreator()
2354 .equals(artifactInfo.getArtifactCreator())) {
2355 log.error("artifact creator cannot be set ignoring");
2357 artifactInfo.setArtifactCreator(currentArtifact.getArtifactCreator());
2359 if (artifactInfo.getUserIdLastUpdater() != null && !currentArtifact.getUserIdLastUpdater()
2360 .equals(artifactInfo.getUserIdLastUpdater())) {
2361 log.error("userId of last updater cannot be set ignoring");
2363 artifactInfo.setUserIdLastUpdater(user.getUserId());
2365 if (artifactInfo.getCreatorFullName() != null && !currentArtifact.getCreatorFullName()
2366 .equals(artifactInfo.getCreatorFullName())) {
2367 log.error("creator Full name cannot be set ignoring");
2369 artifactInfo.setCreatorFullName(currentArtifact.getCreatorFullName());
2371 if (artifactInfo.getUpdaterFullName() != null && !currentArtifact.getUpdaterFullName()
2372 .equals(artifactInfo.getUpdaterFullName())) {
2373 log.error("updater Full name cannot be set ignoring");
2375 String fullName = user.getFirstName() + " " + user.getLastName();
2376 artifactInfo.setUpdaterFullName(fullName);
2378 if (artifactInfo.getCreationDate() != null && !currentArtifact.getCreationDate()
2379 .equals(artifactInfo.getCreationDate())) {
2380 log.error("Creation Date cannot be set ignoring");
2382 artifactInfo.setCreationDate(currentArtifact.getCreationDate());
2384 if (artifactInfo.getLastUpdateDate() != null && !currentArtifact.getLastUpdateDate()
2385 .equals(artifactInfo.getLastUpdateDate())) {
2386 log.error("Last Update Date cannot be set ignoring");
2388 long time = System.currentTimeMillis();
2389 artifactInfo.setLastUpdateDate(time);
2391 if (artifactInfo.getEsId() != null && !currentArtifact.getEsId().equals(artifactInfo.getEsId())) {
2392 log.error("es id cannot be set ignoring");
2394 artifactInfo.setEsId(currentArtifact.getUniqueId());
2396 if (artifactInfo.getArtifactDisplayName() != null && !currentArtifact.getArtifactDisplayName()
2397 .equals(artifactInfo.getArtifactDisplayName())) {
2398 log.error(" Artifact Display Name cannot be set ignoring");
2400 artifactInfo.setArtifactDisplayName(currentArtifact.getArtifactDisplayName());
2402 if (artifactInfo.getServiceApi() != null && !currentArtifact.getServiceApi()
2403 .equals(artifactInfo.getServiceApi())) {
2404 log.debug("serviceApi cannot be set. ignoring.");
2406 artifactInfo.setServiceApi(currentArtifact.getServiceApi());
2408 if (artifactInfo.getArtifactGroupType() != null && currentArtifact.getArtifactGroupType() != artifactInfo.getArtifactGroupType()) {
2409 log.debug("artifact group cannot be set. ignoring.");
2411 artifactInfo.setArtifactGroupType(currentArtifact.getArtifactGroupType());
2413 artifactInfo.setArtifactVersion(currentArtifact.getArtifactVersion());
2415 if (artifactInfo.getArtifactUUID() != null && !artifactInfo.getArtifactUUID()
2416 .isEmpty() && !currentArtifact.getArtifactUUID()
2417 .equals(artifactInfo.getArtifactUUID())) {
2418 log.debug("artifact UUID cannot be set. ignoring.");
2420 artifactInfo.setArtifactUUID(currentArtifact.getArtifactUUID());
2422 if ((artifactInfo.getHeatParameters() != null) && (currentArtifact.getHeatParameters() != null) && !artifactInfo
2423 .getHeatParameters()
2424 .isEmpty() && !currentArtifact.getHeatParameters().isEmpty()) {
2425 checkAndSetUnupdatableHeatParams(artifactInfo.getListHeatParameters(), currentArtifact.getListHeatParameters());
2429 private void checkAndSetUnupdatableHeatParams(List<HeatParameterDefinition> heatParameters, List<HeatParameterDefinition> currentParameters) {
2431 Map<String, HeatParameterDefinition> currentParametersMap = getMapOfParameters(currentParameters);
2432 for (HeatParameterDefinition parameter : heatParameters) {
2433 HeatParameterDefinition currentParam = currentParametersMap.get(parameter.getUniqueId());
2435 if (currentParam != null) {
2437 if (parameter.getName() != null && !parameter.getName().equalsIgnoreCase(currentParam.getName())) {
2438 log.debug("heat parameter name cannot be updated ({}). ignoring.", parameter.getName());
2439 parameter.setName(currentParam.getName());
2441 if (parameter.getDefaultValue() != null && !parameter.getDefaultValue()
2442 .equalsIgnoreCase(currentParam.getDefaultValue())) {
2443 log.debug("heat parameter defaultValue cannot be updated ({}). ignoring.", parameter.getDefaultValue());
2444 parameter.setDefaultValue(currentParam.getDefaultValue());
2446 if (parameter.getType() != null && !parameter.getType().equalsIgnoreCase(currentParam.getType())) {
2447 log.debug("heat parameter type cannot be updated ({}). ignoring.", parameter.getType());
2448 parameter.setType(currentParam.getType());
2450 if (parameter.getDescription() != null && !parameter.getDescription()
2451 .equalsIgnoreCase(currentParam.getDescription())) {
2452 log.debug("heat parameter description cannot be updated ({}). ignoring.", parameter.getDescription());
2453 parameter.setDescription(currentParam.getDescription());
2456 // check and set current value
2457 if ((parameter.getCurrentValue() == null) && (currentParam.getDefaultValue() != null)) {
2458 log.debug("heat parameter current value is null. set it to default value {}). ignoring.", parameter.getDefaultValue());
2459 parameter.setCurrentValue(currentParam.getDefaultValue());
2465 private Map<String, HeatParameterDefinition> getMapOfParameters(List<HeatParameterDefinition> currentParameters) {
2467 Map<String, HeatParameterDefinition> currentParamsMap = new HashMap<>();
2468 for (HeatParameterDefinition param : currentParameters) {
2469 currentParamsMap.put(param.getUniqueId(), param);
2471 return currentParamsMap;
2474 private Either<Boolean, ResponseFormat> validateAndServiceApiUrl(ArtifactDefinition artifactInfo) {
2475 if (!ValidationUtils.validateStringNotEmpty(artifactInfo.getApiUrl())) {
2476 log.debug("Artifact url cannot be empty.");
2477 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_URL));
2479 artifactInfo.setApiUrl(artifactInfo.getApiUrl().toLowerCase());
2481 if (!ValidationUtils.validateUrl(artifactInfo.getApiUrl())) {
2482 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_SERVICE_API_URL));
2484 if (!ValidationUtils.validateUrlLength(artifactInfo.getApiUrl())) {
2485 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_URL, String.valueOf(ValidationUtils.API_URL_LENGTH)));
2488 return Either.left(true);
2491 private Either<Boolean, ResponseFormat> validateAndCleanDescription(ArtifactDefinition artifactInfo) {
2492 if (artifactInfo.getDescription() == null || artifactInfo.getDescription().isEmpty()) {
2493 log.debug("Artifact description cannot be empty.");
2494 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_DESCRIPTION));
2496 String description = artifactInfo.getDescription();
2497 description = ValidationUtils.removeNoneUtf8Chars(description);
2498 description = ValidationUtils.normaliseWhitespace(description);
2499 description = ValidationUtils.stripOctets(description);
2500 description = ValidationUtils.removeHtmlTagsOnly(description);
2501 if (!ValidationUtils.validateIsEnglish(description)) {
2502 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2504 if (!ValidationUtils.validateLength(description, ValidationUtils.ARTIFACT_DESCRIPTION_MAX_LENGTH)) {
2505 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_DESCRIPTION, String
2506 .valueOf(ValidationUtils.ARTIFACT_DESCRIPTION_MAX_LENGTH)));
2508 artifactInfo.setDescription(description);
2509 return Either.left(true);
2512 private <T> Either<ArtifactDefinition, T> updateArtifactFlow(Component parent, String parentId, String artifactId,
2513 ArtifactDefinition artifactInfo, byte[] decodedPayload,
2514 ComponentTypeEnum componentType, AuditingActionEnum auditingAction) {
2515 DAOArtifactData artifactData = createEsArtifactData(artifactInfo, decodedPayload);
2516 if (artifactData == null) {
2517 BeEcompErrorManager.getInstance().logBeDaoSystemError(UPDATE_ARTIFACT);
2518 log.debug("Failed to create artifact object for ES.");
2519 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
2521 log.debug("Entry on graph is updated. Update artifact in ES");
2522 // Changing previous and current artifactId for auditing
2523 String currArtifactId = artifactInfo.getUniqueId();
2525 NodeTypeEnum parentType = convertParentType(componentType);
2527 if (decodedPayload == null) {
2528 if (!artifactInfo.getMandatory() || artifactInfo.getEsId() != null) {
2529 Either<DAOArtifactData, CassandraOperationStatus> artifactFromCassandra = artifactCassandraDao.getArtifact(artifactInfo.getEsId());
2530 if (artifactFromCassandra.isRight()) {
2531 throw new StorageException(artifactFromCassandra.right().value());
2533 // clone data to new artifact
2534 artifactData.setData(artifactFromCassandra.left().value().getData());
2535 artifactData.setId(artifactFromCassandra.left().value().getId());
2537 } else if (artifactInfo.getEsId() == null) {
2538 artifactInfo.setEsId(artifactInfo.getUniqueId());
2539 artifactData.setId(artifactInfo.getUniqueId());
2542 Either<ArtifactDefinition, StorageOperationStatus> result = artifactToscaOperation.updateArtifactOnResource(artifactInfo,
2543 parent, artifactId, parentType, parentId, true);
2544 if (result.isRight()) {
2545 throw new StorageException(result.right().value());
2547 ArtifactDefinition artifactDefinition = result.left().value();
2548 updateGeneratedIdInHeatEnv(parent, parentId, artifactId, artifactInfo, artifactDefinition, parentType);
2550 StorageOperationStatus storageOperationStatus = generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentType);
2551 if (storageOperationStatus != StorageOperationStatus.OK) {
2552 throw new StorageException(storageOperationStatus);
2554 if (artifactData.getData() != null) {
2555 if (!artifactDefinition.getDuplicated() || artifactData.getId() == null) {
2556 artifactData.setId(artifactDefinition.getEsId());
2558 saveArtifactInCassandra(artifactData, parent, artifactInfo, currArtifactId, artifactId, auditingAction, componentType);
2560 return Either.left(artifactDefinition);
2563 private String updateGeneratedIdInHeatEnv(Component parent, String parentId, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType) {
2564 if (NodeTypeEnum.Resource == parentType) {
2565 return updateGeneratedIdInHeatEnv(parent.getDeploymentArtifacts(), parent, parentId, artifactId, artifactInfo, artifactDefinition, parentType, false);
2567 return artifactDefinition.getUniqueId();
2570 private String updateGeneratedIdInHeatEnv(Map<String, ArtifactDefinition> deploymentArtifacts, Component parentComponent, String parentId, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType, boolean isInstanceArtifact) {
2571 String artifactUniqueId;
2572 artifactUniqueId = artifactDefinition.getUniqueId();
2573 String artifactType = artifactInfo.getArtifactType();
2574 if ((ArtifactTypeEnum.HEAT.getType().equalsIgnoreCase(artifactType) ||
2575 ArtifactTypeEnum.HEAT_VOL.getType().equalsIgnoreCase(artifactType) ||
2576 ArtifactTypeEnum.HEAT_NET.getType().equalsIgnoreCase(artifactType))
2577 && !artifactUniqueId.equals(artifactId)) {
2578 // need to update the generated id in heat env
2579 Optional<Entry<String, ArtifactDefinition>> findFirst = deploymentArtifacts.entrySet()
2581 .filter(a -> artifactId.equals(a.getValue().getGeneratedFromId()))
2583 if (findFirst.isPresent()) {
2584 ArtifactDefinition artifactEnvInfo = findFirst.get().getValue();
2585 artifactEnvInfo.setIsFromCsar(artifactDefinition.getIsFromCsar());
2586 artifactEnvInfo.setArtifactChecksum(null);
2587 if (isInstanceArtifact) {
2588 artifactToscaOperation.updateHeatEnvArtifactOnInstance(parentComponent, artifactEnvInfo, artifactId, artifactUniqueId, parentType, parentId);
2590 artifactToscaOperation.updateHeatEnvArtifact(parentComponent, artifactEnvInfo, artifactId, artifactUniqueId, parentType, parentId);
2594 return artifactUniqueId;
2597 private String updateGeneratedIdInHeatEnvOnInstance(ComponentInstance parent, Component parentComponent, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType) {
2598 return updateGeneratedIdInHeatEnv(parent.getDeploymentArtifacts(), parentComponent, parent.getUniqueId(),artifactId, artifactInfo, artifactDefinition, parentType, true);
2602 private Either<byte[], ResponseFormat> handlePayload(ArtifactDefinition artifactInfo, boolean isArtifactMetadataUpdate) {
2603 log.trace("Starting payload handling");
2604 byte[] payload = artifactInfo.getPayloadData();
2605 byte[] decodedPayload = null;
2607 if (payload != null && payload.length != 0) {
2608 // the generated artifacts were already decoded by the handler
2609 decodedPayload = artifactInfo.getGenerated() ? payload : Base64.decodeBase64(payload);
2610 if (decodedPayload.length == 0) {
2611 log.debug("Failed to decode the payload.");
2612 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
2613 return Either.right(responseFormat);
2616 String checkSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload);
2617 artifactInfo.setArtifactChecksum(checkSum);
2618 log.trace("Calculated checksum, base64 payload: {}, checksum: {}", payload, checkSum);
2620 // Specific payload validations of different types
2621 Either<Boolean, ResponseFormat> result = Either.left(true);
2622 if (isDeploymentArtifact(artifactInfo)) {
2623 log.trace("Starting deployment artifacts payload validation");
2624 String artifactType = artifactInfo.getArtifactType();
2625 String fileExtension = GeneralUtility.getFilenameExtension(artifactInfo.getArtifactName());
2626 PayloadTypeEnum payloadType = ArtifactTypeToPayloadTypeSelector.getPayloadType(artifactType, fileExtension);
2627 Either<Boolean, ActionStatus> isPayloadValid = payloadType.isValid(decodedPayload);
2628 if (isPayloadValid.isRight()) {
2629 ResponseFormat responseFormat = componentsUtils.getResponseFormat(isPayloadValid.right().value(), artifactType);
2630 return Either.right(responseFormat);
2633 if (payloadType.isHeatRelated()) {
2634 log.trace("Payload is heat related so going to extract heat parameters for artifact type {}", artifactType);
2635 result = extractHeatParameters(artifactInfo);
2638 if (result.isRight()) {
2639 return Either.right(result.right().value());
2642 } // null/empty payload is normal if called from metadata update ONLY.
2643 // The validation of whether this is metadata/payload update case is
2644 // currently done separately
2646 if (!isArtifactMetadataUpdate) {
2647 log.debug("In artifact: {} Payload is missing.",artifactInfo.getArtifactName());
2648 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
2649 return Either.right(responseFormat);
2652 log.trace("Ended payload handling");
2653 return Either.left(decodedPayload);
2656 public Either<ArtifactDefinition, ResponseFormat> deleteArtifactByInterface(
2657 String resourceId, String userUserId, String artifactId, boolean inTransaction) {
2659 return toscaOperationFacade
2660 .getToscaElement(resourceId, JsonParseFlagEnum.ParseMetadata)
2661 .right().map(componentsUtils.toResponseFormat())
2662 .left().bind(parentComponent -> {
2663 User user = new User(userUserId);
2664 return handleDelete(resourceId, artifactId, user,
2666 false, inTransaction);
2670 private Operation convertToOperation(ArtifactDefinition artifactInfo, String operationName) {
2671 Operation op = new Operation();
2672 long time = System.currentTimeMillis();
2673 op.setCreationDate(time);
2675 String artifactName = artifactInfo.getArtifactName();
2676 artifactInfo.setArtifactName(createInterfaceArtifactNameFromOperation(operationName, artifactName));
2678 op.setImplementation(artifactInfo);
2679 op.setLastUpdateDate(time);
2683 private String createInterfaceArtifactNameFromOperation(String operationName, String artifactName) {
2684 String newArtifactName = operationName + "_" + artifactName;
2685 log.trace("converting artifact name {} to {}", artifactName, newArtifactName);
2686 return newArtifactName;
2690 public byte[] downloadRsrcArtifactByNames(String serviceName, String serviceVersion, String resourceName, String resourceVersion, String artifactName) {
2692 // General validation
2693 if (serviceName == null || serviceVersion == null || resourceName == null || resourceVersion == null || artifactName == null) {
2694 log.debug(NULL_PARAMETER);
2695 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2698 // Normalizing artifact name
2699 artifactName = ValidationUtils.normalizeFileName(artifactName);
2701 // Resource validation
2702 Resource resource = validateResourceNameAndVersion(resourceName, resourceVersion);
2703 String resourceId = resource.getUniqueId();
2705 // Service validation
2706 Service validateServiceNameAndVersion = validateServiceNameAndVersion(serviceName, serviceVersion);
2708 Map<String, ArtifactDefinition> artifacts = resource.getDeploymentArtifacts();
2709 if (artifacts == null || artifacts.isEmpty()) {
2710 log.debug("Deployment artifacts of resource {} are not found", resourceId);
2711 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactName);
2714 ArtifactDefinition deploymentArtifact = null;
2716 for (ArtifactDefinition artifactDefinition : artifacts.values()) {
2717 if (artifactDefinition.getArtifactName() != null && artifactDefinition.getArtifactName()
2718 .equals(artifactName)) {
2719 log.debug(FOUND_DEPLOYMENT_ARTIFACT, artifactName);
2720 deploymentArtifact = artifactDefinition;
2725 if (deploymentArtifact == null) {
2726 log.debug("No deployment artifact {} was found for resource {}", artifactName, resourceId);
2727 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactName);
2730 // Downloading the artifact
2731 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(deploymentArtifact);
2732 log.trace("Download of resource artifact succeeded, uniqueId {}", deploymentArtifact.getUniqueId());
2733 return downloadArtifactEither.getRight();
2737 public byte[] downloadRsrcInstArtifactByNames(String serviceName, String serviceVersion, String resourceInstanceName, String artifactName) {
2739 // General validation
2740 if (serviceName == null || serviceVersion == null || resourceInstanceName == null || artifactName == null) {
2741 log.debug(NULL_PARAMETER);
2742 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2745 // Normalizing artifact name
2746 artifactName = ValidationUtils.normalizeFileName(artifactName);
2748 // Service validation
2749 Service service = validateServiceNameAndVersion(serviceName, serviceVersion);
2751 // ResourceInstance validation
2752 ComponentInstance resourceInstance = validateResourceInstance(service, resourceInstanceName);
2754 Map<String, ArtifactDefinition> artifacts = resourceInstance.getDeploymentArtifacts();
2756 final String finalArtifactName = artifactName;
2757 Predicate<ArtifactDefinition> filterArtifactByName = p -> p.getArtifactName().equals(finalArtifactName);
2759 ArtifactDefinition deployableArtifact = artifacts==null ? null :
2760 artifacts.values().stream()
2761 .filter(filterArtifactByName)
2765 if (deployableArtifact == null) {
2766 log.debug("Deployment artifact with name {} not found", artifactName);
2767 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, artifactName));
2770 log.debug(FOUND_DEPLOYMENT_ARTIFACT, artifactName);
2771 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(deployableArtifact);
2773 log.trace("Download of resource artifact succeeded, uniqueId {}", deployableArtifact.getUniqueId());
2774 return downloadArtifactEither.getRight();
2777 private ComponentInstance validateResourceInstance(Service service, String resourceInstanceName) {
2779 List<ComponentInstance> riList = service.getComponentInstances();
2780 for (ComponentInstance ri : riList) {
2781 if (ri.getNormalizedName().equals(resourceInstanceName)) {
2785 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND, resourceInstanceName);
2788 private ComponentInstance validateResourceInstanceById(Component component, String resourceInstanceId) {
2790 List<ComponentInstance> riList = component.getComponentInstances();
2791 for (ComponentInstance ri : riList) {
2792 if (ri.getUniqueId().equals(resourceInstanceId)) {
2796 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceInstanceId);
2799 private Service validateServiceNameAndVersion(String serviceName, String serviceVersion) {
2801 Either<List<Service>, StorageOperationStatus> serviceListBySystemName = toscaOperationFacade.getBySystemName(ComponentTypeEnum.SERVICE, serviceName);
2802 if (serviceListBySystemName.isRight()) {
2803 log.debug("Couldn't fetch any service with name {}", serviceName);
2804 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceListBySystemName
2806 .value(), ComponentTypeEnum.SERVICE), serviceName);
2808 List<Service> serviceList = serviceListBySystemName.left().value();
2809 if (serviceList == null || serviceList.isEmpty()) {
2810 log.debug("Couldn't fetch any service with name {}", serviceName);
2811 throw new ByActionStatusComponentException(ActionStatus.SERVICE_NOT_FOUND, serviceName);
2814 Service foundService = null;
2815 for (Service service : serviceList) {
2816 if (service.getVersion().equals(serviceVersion)) {
2817 log.trace("Found service with version {}", serviceVersion);
2818 foundService = service;
2823 if (foundService == null) {
2824 log.debug("Couldn't find version {} for service {}", serviceVersion, serviceName);
2825 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_VERSION_NOT_FOUND, ComponentTypeEnum.SERVICE
2826 .getValue(), serviceVersion);
2828 return foundService;
2831 private Resource validateResourceNameAndVersion(String resourceName, String resourceVersion) {
2833 Either<Resource, StorageOperationStatus> resourceListBySystemName = toscaOperationFacade.getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion, JsonParseFlagEnum.ParseMetadata);
2834 if (resourceListBySystemName.isRight()) {
2835 log.debug("Couldn't fetch any resource with name {} and version {}. ", resourceName, resourceVersion);
2836 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(resourceListBySystemName
2838 .value()), resourceName);
2840 return resourceListBySystemName.left().value();
2843 public byte[] downloadServiceArtifactByNames(String serviceName, String serviceVersion, String artifactName) {
2845 log.trace("Starting download of service interface artifact, serviceName {}, serviceVersion {}, artifact name {}", serviceName, serviceVersion, artifactName);
2846 if (serviceName == null || serviceVersion == null || artifactName == null) {
2847 log.debug(NULL_PARAMETER);
2848 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2851 // Normalizing artifact name
2852 final String normalizedArtifactName = ValidationUtils.normalizeFileName(artifactName);
2854 // Service validation
2855 Service service = validateServiceNameAndVersion(serviceName, serviceVersion);
2856 // Looking for deployment or tosca artifacts
2857 String serviceId = service.getUniqueId();
2859 if (MapUtils.isEmpty(service.getDeploymentArtifacts()) && MapUtils.isEmpty(service.getToscaArtifacts())) {
2860 log.debug("Neither Deployment nor Tosca artifacts of service {} are found", serviceId);
2861 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, normalizedArtifactName);
2864 Optional<ArtifactDefinition> foundArtifactOptl = Optional.empty();
2866 if (!MapUtils.isEmpty(service.getDeploymentArtifacts())) {
2867 foundArtifactOptl = service.getDeploymentArtifacts().values().stream()
2868 // filters artifact by name
2869 .filter(a -> a.getArtifactName().equals(normalizedArtifactName)).findAny();
2871 if ((!foundArtifactOptl.isPresent()) && !MapUtils.isEmpty(service.getToscaArtifacts())) {
2872 foundArtifactOptl = service.getToscaArtifacts().values().stream()
2873 // filters TOSCA artifact by name
2874 .filter(a -> a.getArtifactName().equals(normalizedArtifactName)).findAny();
2876 if (!foundArtifactOptl.isPresent()) {
2877 log.debug("The artifact {} was not found for service {}", normalizedArtifactName, serviceId);
2878 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, normalizedArtifactName);
2880 log.debug(FOUND_DEPLOYMENT_ARTIFACT, normalizedArtifactName);
2881 // Downloading the artifact
2882 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(foundArtifactOptl
2884 log.trace("Download of service artifact succeeded, uniqueId {}", foundArtifactOptl.get().getUniqueId());
2885 return downloadArtifactEither.getRight();
2888 public ImmutablePair<String, byte[]> downloadArtifact(String parentId, String artifactUniqueId) {
2889 log.trace("Starting download of artifact, uniqueId {}", artifactUniqueId);
2890 Either<ArtifactDefinition, StorageOperationStatus> artifactById = artifactToscaOperation.getArtifactById(parentId, artifactUniqueId);
2891 if (artifactById.isRight()) {
2892 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(artifactById.right().value());
2893 log.debug("Error when getting artifact info by id{}, error: {}", artifactUniqueId, actionStatus);
2894 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatByArtifactId(actionStatus, ""));
2896 ArtifactDefinition artifactDefinition = artifactById.left().value();
2897 if (artifactDefinition == null) {
2898 log.debug("Empty artifact definition returned from DB by artifact id {}", artifactUniqueId);
2899 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, ""));
2902 return downloadArtifact(artifactDefinition);
2905 private boolean checkArtifactInComponent(Component component, String artifactId) {
2906 boolean found = false;
2907 Map<String, ArtifactDefinition> artifactsS = component.getArtifacts();
2908 if (artifactsS != null) {
2909 for (Map.Entry<String, ArtifactDefinition> entry : artifactsS.entrySet()) {
2910 if (entry.getValue().getUniqueId().equals(artifactId)) {
2916 Map<String, ArtifactDefinition> deploymentArtifactsS = component.getDeploymentArtifacts();
2917 if (!found && deploymentArtifactsS != null) {
2918 for (Map.Entry<String, ArtifactDefinition> entry : deploymentArtifactsS.entrySet()) {
2919 if (entry.getValue().getUniqueId().equals(artifactId)) {
2925 Map<String, ArtifactDefinition> toscaArtifactsS = component.getToscaArtifacts();
2926 if (!found && toscaArtifactsS != null) {
2927 for (Map.Entry<String, ArtifactDefinition> entry : toscaArtifactsS.entrySet()) {
2928 if (entry.getValue().getUniqueId().equals(artifactId)) {
2935 Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
2936 if (!found && interfaces != null) {
2937 for (Map.Entry<String, InterfaceDefinition> entry : interfaces.entrySet()) {
2938 Map<String, Operation> operations = entry.getValue().getOperationsMap();
2939 for (Map.Entry<String, Operation> entryOp : operations.entrySet()) {
2940 if (entryOp.getValue().getImplementation() != null && entryOp.getValue()
2941 .getImplementation()
2943 .equals(artifactId)) {
2950 switch (component.getComponentType()) {
2954 Map<String, ArtifactDefinition> apiArtifacts = ((Service) component).getServiceApiArtifacts();
2955 if (!found && apiArtifacts != null) {
2956 for (Map.Entry<String, ArtifactDefinition> entry : apiArtifacts.entrySet()) {
2957 if (entry.getValue().getUniqueId().equals(artifactId)) {
2971 private boolean checkArtifactInResourceInstance(Component component, String resourceInstanceId, String artifactId) {
2973 boolean found = false;
2974 List<ComponentInstance> resourceInstances = component.getComponentInstances();
2975 ComponentInstance resourceInstance = null;
2976 for (ComponentInstance ri : resourceInstances) {
2977 if (ri.getUniqueId().equals(resourceInstanceId)) {
2978 resourceInstance = ri;
2982 if (resourceInstance != null) {
2983 Map<String, ArtifactDefinition> artifacts = resourceInstance.getDeploymentArtifacts();
2984 if (artifacts != null) {
2985 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
2986 if (entry.getValue().getUniqueId().equals(artifactId)) {
2993 artifacts = resourceInstance.getArtifacts();
2994 if (artifacts != null) {
2995 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
2996 if (entry.getValue().getUniqueId().equals(artifactId)) {
3007 private Component validateComponentExists(String componentId, AuditingActionEnum auditingAction, User user, String artifactId, ComponentTypeEnum componentType,
3008 String containerComponentType) {
3010 ComponentTypeEnum componentForAudit = null == containerComponentType ? componentType : ComponentTypeEnum.findByParamName(containerComponentType);
3011 componentForAudit.getNodeType();
3013 Either<? extends Component, StorageOperationStatus> componentResult = toscaOperationFacade
3014 .getToscaFullElement(componentId);
3016 if (componentResult.isRight()) {
3017 ActionStatus status = componentForAudit == ComponentTypeEnum.RESOURCE ? ActionStatus.RESOURCE_NOT_FOUND : componentForAudit == ComponentTypeEnum.SERVICE ? ActionStatus.SERVICE_NOT_FOUND : ActionStatus.PRODUCT_NOT_FOUND;
3018 ResponseFormat responseFormat = componentsUtils.getResponseFormat(status, componentId);
3019 log.debug("Service not found, serviceId {}", componentId);
3020 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentForAudit, null);
3021 throw new ByActionStatusComponentException(status, componentId);
3023 return componentResult.left().value();
3026 private void validateWorkOnComponent(Component component, String userId, AuditingActionEnum auditingAction, User user, String artifactId, ArtifactOperationInfo operation) {
3027 if (operation.getArtifactOperationEnum() != ArtifactOperationEnum.DOWNLOAD && !operation.ignoreLifecycleState()) {
3029 validateCanWorkOnComponent(component, userId);
3030 }catch (ComponentException e) {
3031 String uniqueId = component.getUniqueId();
3032 log.debug("Service status isn't CHECKOUT or user isn't owner, serviceId {}", uniqueId);
3033 handleAuditing(auditingAction, component, uniqueId, user, null, null, artifactId, e.getResponseFormat(),
3034 component.getComponentType(), null);
3040 private void validateUserRole(User user, AuditingActionEnum auditingAction, String componentId, String artifactId, ComponentTypeEnum componentType, ArtifactOperationInfo operation) {
3042 if (operation.isNotDownload()) {
3043 String role = user.getRole();
3044 if (!role.equals(Role.ADMIN.name()) && !role.equals(Role.DESIGNER.name())) {
3045 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
3046 log.debug("addArtifact - user isn't permitted to perform operation, userId {}, role {}", user.getUserId(), role);
3047 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentType, null);
3048 throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
3053 private User validateUserExists(String userId, AuditingActionEnum auditingAction, String componentId, String artifactId, ComponentTypeEnum componentType, boolean inTransaction) {
3056 user = validateUserExists(userId);
3057 } catch(ByResponseFormatComponentException e){
3058 ResponseFormat responseFormat = e.getResponseFormat();
3059 handleComponentException(auditingAction, componentId, artifactId, responseFormat, componentType, userId);
3061 } catch(ByActionStatusComponentException e){
3062 ResponseFormat responseFormat = componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
3063 handleComponentException(auditingAction, componentId, artifactId, responseFormat, componentType, userId);
3069 private void handleComponentException(AuditingActionEnum auditingAction, String componentId, String artifactId,
3070 ResponseFormat responseFormat, ComponentTypeEnum componentType, String userId){
3071 User user = new User();
3072 user.setUserId(userId);
3073 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentType, null);
3076 protected AuditingActionEnum detectAuditingType(ArtifactOperationInfo operation, String origMd5) {
3077 AuditingActionEnum auditingAction = null;
3078 switch (operation.getArtifactOperationEnum()) {
3080 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_UPLOAD_BY_API : AuditingActionEnum.ARTIFACT_UPLOAD;
3083 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_UPLOAD_BY_API : origMd5 == null ? AuditingActionEnum.ARTIFACT_METADATA_UPDATE : AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE;
3086 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_DELETE_BY_API : AuditingActionEnum.ARTIFACT_DELETE;
3089 auditingAction = operation.isExternalApi() ? AuditingActionEnum.DOWNLOAD_ARTIFACT : AuditingActionEnum.ARTIFACT_DOWNLOAD;
3094 return auditingAction;
3097 private ImmutablePair<String, byte[]> downloadArtifact(ArtifactDefinition artifactDefinition) {
3098 String esArtifactId = artifactDefinition.getEsId();
3099 Either<DAOArtifactData, CassandraOperationStatus> artifactfromES = artifactCassandraDao.getArtifact(esArtifactId);
3100 if (artifactfromES.isRight()) {
3101 CassandraOperationStatus resourceUploadStatus = artifactfromES.right().value();
3102 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
3103 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageResponse);
3104 log.debug("Error when getting artifact from ES, error: {}", actionStatus);
3105 throw new ByActionStatusComponentException(actionStatus, artifactDefinition.getArtifactDisplayName());
3108 DAOArtifactData DAOArtifactData = artifactfromES.left().value();
3109 byte[] data = DAOArtifactData.getDataAsArray();
3111 log.debug("Artifact data from cassandra is null");
3112 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactDefinition.getArtifactDisplayName());
3114 String artifactName = artifactDefinition.getArtifactName();
3115 log.trace("Download of artifact succeeded, uniqueId {}, artifact file name {}", artifactDefinition.getUniqueId(), artifactName);
3116 return new ImmutablePair<>(artifactName, data);
3119 public DAOArtifactData createEsArtifactData(ArtifactDataDefinition artifactInfo, byte[] artifactPayload) {
3120 return new DAOArtifactData(artifactInfo.getEsId(), artifactPayload);
3123 private void saveArtifactInCassandra(DAOArtifactData artifactData, Component parent, ArtifactDefinition artifactInfo,
3124 String currArtifactId, String prevArtifactId, AuditingActionEnum auditingAction, ComponentTypeEnum componentType) {
3125 CassandraOperationStatus resourceUploadStatus = artifactCassandraDao.saveArtifact(artifactData);
3127 if (resourceUploadStatus == CassandraOperationStatus.OK) {
3128 log.debug("Artifact {} was saved in component {}.", artifactData.getId(), parent.getUniqueId());
3129 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
3130 handleAuditing(auditingAction, parent, parent.getUniqueId(), null, artifactInfo, prevArtifactId,
3131 currArtifactId, responseFormat, componentType, null);
3134 BeEcompErrorManager.getInstance().logBeDaoSystemError(UPDATE_ARTIFACT);
3135 log.info(FAILED_SAVE_ARTIFACT);
3136 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3137 handleAuditing(auditingAction, parent, parent.getUniqueId(), null, artifactInfo, prevArtifactId, currArtifactId, responseFormat, componentType, null);
3138 throw new StorageException(resourceUploadStatus);
3142 private boolean isArtifactMetadataUpdate(AuditingActionEnum auditingActionEnum) {
3143 return auditingActionEnum == AuditingActionEnum.ARTIFACT_METADATA_UPDATE;
3146 private boolean isDeploymentArtifact(ArtifactDefinition artifactInfo) {
3147 return ArtifactGroupTypeEnum.DEPLOYMENT == artifactInfo.getArtifactGroupType();
3150 private boolean isInformationalArtifact(final ArtifactDefinition artifactInfo) {
3151 return ArtifactGroupTypeEnum.INFORMATIONAL == artifactInfo.getArtifactGroupType();
3154 private boolean isHeatArtifact(final ArtifactDefinition artifactInfo) {
3155 final String artifactType = artifactInfo.getArtifactType();
3156 final ArtifactTypeEnum artifactTypeEnum = ArtifactTypeEnum.parse(artifactType);
3157 if (artifactTypeEnum == null) {
3158 artifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
3161 switch (artifactTypeEnum) {
3173 public ArtifactDefinition createArtifactPlaceHolderInfo(String resourceId, String logicalName, Map<String, Object> artifactInfoMap, String userUserId, ArtifactGroupTypeEnum groupType, boolean inTransaction) {
3174 User user = userBusinessLogic.getUser(userUserId, inTransaction);
3175 return createArtifactPlaceHolderInfo(resourceId, logicalName, artifactInfoMap, user, groupType);
3178 public ArtifactDefinition createArtifactPlaceHolderInfo(String resourceId, String logicalName, Map<String, Object> artifactInfoMap, User user, ArtifactGroupTypeEnum groupType) {
3179 ArtifactDefinition artifactInfo = new ArtifactDefinition();
3181 String artifactName = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_DISPLAY_NAME);
3182 String artifactType = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_TYPE);
3183 String artifactDescription = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_DESCRIPTION);
3185 artifactInfo.setArtifactDisplayName(artifactName);
3186 artifactInfo.setArtifactLabel(logicalName.toLowerCase());
3187 artifactInfo.setArtifactType(artifactType);
3188 artifactInfo.setDescription(artifactDescription);
3189 artifactInfo.setArtifactGroupType(groupType);
3190 nodeTemplateOperation.setDefaultArtifactTimeout(groupType, artifactInfo);
3192 setArtifactPlaceholderCommonFields(resourceId, user, artifactInfo);
3194 return artifactInfo;
3197 private void setArtifactPlaceholderCommonFields(String resourceId, User user, ArtifactDefinition artifactInfo) {
3198 String uniqueId = null;
3200 if (resourceId != null) {
3201 uniqueId = UniqueIdBuilder.buildPropertyUniqueId(resourceId.toLowerCase(), artifactInfo.getArtifactLabel()
3203 artifactInfo.setUniqueId(uniqueId);
3205 artifactInfo.setUserIdCreator(user.getUserId());
3206 String fullName = user.getFullName();
3207 artifactInfo.setUpdaterFullName(fullName);
3209 long time = System.currentTimeMillis();
3211 artifactInfo.setCreatorFullName(fullName);
3212 artifactInfo.setCreationDate(time);
3214 artifactInfo.setLastUpdateDate(time);
3215 artifactInfo.setUserIdLastUpdater(user.getUserId());
3217 artifactInfo.setMandatory(true);
3220 public Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifacts(String parentId, NodeTypeEnum parentType, ArtifactGroupTypeEnum groupType, String instanceId) {
3221 return artifactToscaOperation.getArtifacts(parentId, parentType, groupType, instanceId);
3224 public Either<ArtifactDefinition, StorageOperationStatus> addHeatEnvArtifact(ArtifactDefinition artifactHeatEnv, ArtifactDefinition artifact, Component component, NodeTypeEnum parentType, String instanceId) {
3225 return artifactToscaOperation.addHeatEnvArtifact(artifactHeatEnv, artifact, component, parentType, true, instanceId);
3228 private Either<DAOArtifactData, ResponseFormat> createEsHeatEnvArtifactDataFromString(ArtifactDefinition artifactDefinition, String payloadStr) {
3230 byte[] payload = payloadStr.getBytes();
3232 DAOArtifactData artifactData = createEsArtifactData(artifactDefinition, payload);
3233 return Either.left(artifactData);
3237 * @param artifactDefinition
3240 public Either<ArtifactDefinition, ResponseFormat> generateHeatEnvArtifact(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3241 String instanceId, boolean shouldLock, boolean inTransaction) {
3242 String payload = generateHeatEnvPayload(artifactDefinition);
3243 String prevUUID = artifactDefinition.getArtifactUUID();
3244 ArtifactDefinition clonedBeforeGenerate = new ArtifactDefinition(artifactDefinition);
3245 return generateAndSaveHeatEnvArtifact(artifactDefinition, payload, componentType, component, resourceInstanceName, modifier, instanceId, shouldLock, inTransaction)
3247 .bind(artifactDef -> updateArtifactOnGroupInstance(component, instanceId, prevUUID, clonedBeforeGenerate, artifactDef));
3250 public Either<ArtifactDefinition, ResponseFormat> forceGenerateHeatEnvArtifact(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3251 boolean shouldLock, boolean inTransaction, String instanceId) {
3252 String payload = generateHeatEnvPayload(artifactDefinition);
3253 String prevUUID = artifactDefinition.getArtifactUUID();
3254 ArtifactDefinition clonedBeforeGenerate = new ArtifactDefinition(artifactDefinition);
3255 return forceGenerateAndSaveHeatEnvArtifact(artifactDefinition, payload, componentType, component, resourceInstanceName, modifier, instanceId, shouldLock, inTransaction)
3257 .bind(artifactDef -> updateArtifactOnGroupInstance(component, instanceId, prevUUID, clonedBeforeGenerate, artifactDef));
3261 Either<ArtifactDefinition, ResponseFormat> updateArtifactOnGroupInstance(Component component, String instanceId, String prevUUID, ArtifactDefinition clonedBeforeGenerate, ArtifactDefinition updatedArtDef) {
3262 if (prevUUID == null || !prevUUID.equals(updatedArtDef.getArtifactUUID())) {
3263 List<ComponentInstance> componentInstances = component.getComponentInstances();
3264 if (componentInstances != null) {
3265 Optional<ComponentInstance> findFirst = componentInstances.stream()
3266 .filter(ci -> ci.getUniqueId()
3267 .equals(instanceId))
3269 if (findFirst.isPresent()) {
3270 ComponentInstance relevantInst = findFirst.get();
3271 List<GroupInstance> updatedGroupInstances = getUpdatedGroupInstances(updatedArtDef.getUniqueId(), clonedBeforeGenerate, relevantInst
3272 .getGroupInstances());
3274 if (CollectionUtils.isNotEmpty(updatedGroupInstances)) {
3275 updatedGroupInstances.forEach(gi -> {
3276 gi.getGroupInstanceArtifacts().add(updatedArtDef.getUniqueId());
3277 gi.getGroupInstanceArtifactsUuid().add(updatedArtDef.getArtifactUUID());
3279 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(component, instanceId, updatedGroupInstances);
3280 if (status.isRight()) {
3281 log.debug(FAILED_UPDATE_GROUPS, component.getUniqueId());
3282 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils
3283 .convertFromStorageResponse(status.right()
3284 .value()), clonedBeforeGenerate.getArtifactDisplayName());
3285 return Either.right(responseFormat);
3291 return Either.left(updatedArtDef);
3294 private String generateHeatEnvPayload(ArtifactDefinition artifactDefinition) {
3295 List<HeatParameterDefinition> heatParameters = artifactDefinition.getListHeatParameters();
3296 StringBuilder sb = new StringBuilder();
3297 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactHeader());
3298 sb.append("parameters:\n");
3299 if (heatParameters != null) {
3300 heatParameters.sort(Comparator.comparing(HeatParameterDataDefinition::getName));
3302 List<HeatParameterDefinition> empltyHeatValues = new ArrayList<>();
3304 for (HeatParameterDefinition heatParameterDefinition : heatParameters) {
3306 String heatValue = heatParameterDefinition.getCurrentValue();
3307 if (!ValidationUtils.validateStringNotEmpty(heatValue)) {
3308 heatValue = heatParameterDefinition.getDefaultValue();
3309 if (!ValidationUtils.validateStringNotEmpty(heatValue)) {
3310 empltyHeatValues.add(heatParameterDefinition);
3314 HeatParameterType type = HeatParameterType.isValidType(heatParameterDefinition.getType());
3319 .append(heatParameterDefinition.getName())
3322 .append(Boolean.parseBoolean(heatValue))
3327 .append(heatParameterDefinition.getName())
3330 .append(new BigDecimal(heatValue).toPlainString())
3333 case COMMA_DELIMITED_LIST:
3336 .append(heatParameterDefinition.getName())
3343 String value = heatValue;
3344 boolean starts = value.startsWith("\"");
3345 boolean ends = value.endsWith("\"");
3346 if (!(starts && ends)) {
3347 starts = value.startsWith("'");
3348 ends = value.endsWith("'");
3349 if (!(starts && ends)) {
3350 value = "\"" + value + "\"";
3354 .append(heatParameterDefinition.getName())
3364 if (!empltyHeatValues.isEmpty()) {
3365 empltyHeatValues.sort(Comparator.comparing(HeatParameterDataDefinition::getName));
3366 empltyHeatValues.forEach(hv -> {
3367 sb.append(" ").append(hv.getName()).append(":");
3368 HeatParameterType type = HeatParameterType.isValidType(hv.getType());
3369 if (type != null && type == HeatParameterType.STRING && (hv.getCurrentValue() != null && "".equals(hv
3370 .getCurrentValue()) || hv.getDefaultValue() != null && "".equals(hv.getDefaultValue()))) {
3371 sb.append(" \"\"").append("\n");
3374 sb.append(" ").append("\n");
3379 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter());
3382 return sb.toString().replaceAll("\\\\n", "\n");
3386 * @param artifactDefinition
3390 public Either<ArtifactDefinition, ResponseFormat> generateAndSaveHeatEnvArtifact(ArtifactDefinition artifactDefinition, String payload, ComponentTypeEnum componentType, Component component, String resourceInstanceName,
3391 User modifier, String instanceId, boolean shouldLock, boolean inTransaction) {
3392 return generateArtifactPayload(artifactDefinition, componentType, component, resourceInstanceName, modifier, shouldLock, inTransaction, artifactDefinition::getHeatParamsUpdateDate,
3393 () -> createEsHeatEnvArtifactDataFromString(artifactDefinition, payload), instanceId);
3397 public Either<ArtifactDefinition, ResponseFormat> forceGenerateAndSaveHeatEnvArtifact(ArtifactDefinition artifactDefinition, String payload, ComponentTypeEnum componentType, Component component, String resourceInstanceName,
3398 User modifier, String instanceId, boolean shouldLock, boolean inTransaction) {
3399 return generateArtifactPayload(artifactDefinition, componentType, component, resourceInstanceName, modifier, shouldLock, inTransaction, System::currentTimeMillis,
3400 () -> createEsHeatEnvArtifactDataFromString(artifactDefinition, payload), instanceId);
3404 protected Either<ArtifactDefinition, ResponseFormat> generateArtifactPayload(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3405 boolean shouldLock, boolean inTransaction, Supplier<Long> payloadUpdateDateGen, Supplier<Either<DAOArtifactData, ResponseFormat>> esDataCreator, String instanceId) {
3407 log.trace("Start generating payload for {} artifact {}", artifactDefinition.getArtifactType(), artifactDefinition
3409 if (artifactDefinition.getPayloadUpdateDate() == null || artifactDefinition.getPayloadUpdateDate() == 0 || artifactDefinition
3410 .getPayloadUpdateDate() <= payloadUpdateDateGen.get()) {
3412 log.trace("Generating payload for {} artifact {}", artifactDefinition.getArtifactType(), artifactDefinition.getEsId());
3413 Either<DAOArtifactData, ResponseFormat> artifactDataRes = esDataCreator.get();
3414 DAOArtifactData artifactData = null;
3416 if (artifactDataRes.isLeft()) {
3417 artifactData = artifactDataRes.left().value();
3420 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3421 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3422 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3423 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3425 return Either.right(artifactDataRes.right().value());
3427 String newCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(artifactData.getDataAsArray());
3429 String esArtifactId = artifactDefinition.getEsId();
3430 Either<DAOArtifactData, CassandraOperationStatus> artifactfromES;
3431 DAOArtifactData DAOArtifactData;
3432 if (esArtifactId != null && !esArtifactId.isEmpty() && artifactDefinition.getPayloadData() == null) {
3433 log.debug("Try to fetch artifact from cassandra with id : {}", esArtifactId);
3434 artifactfromES = artifactCassandraDao.getArtifact(esArtifactId);
3435 if (artifactfromES.isRight()) {
3436 CassandraOperationStatus resourceUploadStatus = artifactfromES.right().value();
3437 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
3438 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageResponse);
3439 log.debug("Error when getting artifact from ES, error: {} esid : {}", actionStatus, esArtifactId);
3440 return Either.right(componentsUtils.getResponseFormatByArtifactId(actionStatus, artifactDefinition.getArtifactDisplayName()));
3442 DAOArtifactData = artifactfromES.left().value();
3443 oldCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(DAOArtifactData.getDataAsArray());
3446 oldCheckSum = artifactDefinition.getArtifactChecksum();
3449 Either<ArtifactDefinition, StorageOperationStatus> updateArifactDefinitionStatus = null;
3453 lockComponent(component, "Update Artifact - lock resource: ");
3454 }catch (ComponentException e){
3455 handleAuditing(AuditingActionEnum.ARTIFACT_METADATA_UPDATE, component, component.getUniqueId(), modifier, null, null, artifactDefinition
3456 .getUniqueId(), e.getResponseFormat(), component.getComponentType(), null);
3461 if (oldCheckSum != null && oldCheckSum.equals(newCheckSum)) {
3463 artifactDefinition.setPayloadUpdateDate(payloadUpdateDateGen.get());
3464 updateArifactDefinitionStatus = artifactToscaOperation.updateArtifactOnResource(artifactDefinition, component
3465 ,artifactDefinition.getUniqueId(), componentType.getNodeType(), instanceId, true);
3466 log.trace("No real update done in payload for {} artifact, updating payloadUpdateDate {}", artifactDefinition
3467 .getArtifactType(), artifactDefinition.getEsId());
3468 if (updateArifactDefinitionStatus.isRight()) {
3469 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils.convertFromStorageResponse(updateArifactDefinitionStatus
3471 .value()), artifactDefinition.getArtifactDisplayName());
3472 log.trace("Failed to update payloadUpdateDate {}", artifactDefinition.getEsId());
3473 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3474 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3475 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3477 return Either.right(responseFormat);
3481 artifactDefinition.getArtifactChecksum();
3482 artifactDefinition.setArtifactChecksum(newCheckSum);
3483 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
3484 log.trace("No real update done in payload for {} artifact, updating payloadUpdateDate {}", artifactDefinition
3485 .getArtifactType(), artifactDefinition.getEsId());
3486 updateArifactDefinitionStatus = artifactToscaOperation.updateArtifactOnResource(artifactDefinition, component,
3487 artifactDefinition.getUniqueId(), componentType.getNodeType(), instanceId, true);
3489 log.trace("Update Payload {}", artifactDefinition.getEsId());
3491 if (updateArifactDefinitionStatus.isLeft()) {
3493 artifactDefinition = updateArifactDefinitionStatus.left().value();
3494 artifactData.setId(artifactDefinition.getUniqueId());
3495 CassandraOperationStatus saveArtifactStatus = artifactCassandraDao.saveArtifact(artifactData);
3497 if (saveArtifactStatus == CassandraOperationStatus.OK) {
3498 if (!inTransaction) {
3499 janusGraphDao.commit();
3501 log.debug("Artifact Saved In cassandra {}", artifactData.getId());
3502 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
3503 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3504 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3505 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3509 if (!inTransaction) {
3510 janusGraphDao.rollback();
3512 log.info("Failed to save artifact {}.", artifactData.getId());
3513 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3514 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3515 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3516 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3518 return Either.right(responseFormat);
3522 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils.convertFromStorageResponse(updateArifactDefinitionStatus
3524 .value()), artifactDefinition.getArtifactDisplayName());
3525 log.debug("Failed To update artifact {}", artifactData.getId());
3526 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3527 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3528 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3530 return Either.right(responseFormat);
3536 graphLockOperation.unlockComponent(component.getUniqueId(), component.getComponentType()
3542 return Either.left(artifactDefinition);
3546 public Map<String, Object> buildJsonForUpdateArtifact(ArtifactDefinition artifactDef, ArtifactGroupTypeEnum artifactGroupType, List<ArtifactTemplateInfo> updatedRequiredArtifacts) {
3547 return this.buildJsonForUpdateArtifact(artifactDef.getUniqueId(), artifactDef.getArtifactName(), artifactDef.getArtifactType(), artifactGroupType, artifactDef
3548 .getArtifactLabel(), artifactDef.getArtifactDisplayName(),
3549 artifactDef.getDescription(), artifactDef.getPayloadData(), updatedRequiredArtifacts, artifactDef.getListHeatParameters());
3553 public Map<String, Object> buildJsonForUpdateArtifact(String artifactId, String artifactName, String artifactType, ArtifactGroupTypeEnum artifactGroupType, String label, String displayName, String description, byte[] artifactContent,
3554 List<ArtifactTemplateInfo> updatedRequiredArtifacts, List<HeatParameterDefinition> heatParameters) {
3556 Map<String, Object> json = new HashMap<>();
3557 if (artifactId != null && !artifactId.isEmpty()) {
3558 json.put(Constants.ARTIFACT_ID, artifactId);
3561 json.put(Constants.ARTIFACT_NAME, artifactName);
3562 json.put(Constants.ARTIFACT_TYPE, artifactType);
3563 json.put(Constants.ARTIFACT_DESCRIPTION, description);
3565 if (artifactContent != null) {
3566 log.debug("payload is encoded. perform decode");
3567 String encodedPayload = Base64.encodeBase64String(artifactContent);
3568 json.put(Constants.ARTIFACT_PAYLOAD_DATA, encodedPayload);
3570 json.put(Constants.ARTIFACT_DISPLAY_NAME, displayName);
3571 json.put(Constants.ARTIFACT_LABEL, label);
3572 json.put(Constants.ARTIFACT_GROUP_TYPE, artifactGroupType.getType());
3573 json.put(Constants.REQUIRED_ARTIFACTS, (updatedRequiredArtifacts == null || updatedRequiredArtifacts.isEmpty()) ? new ArrayList<>()
3574 : updatedRequiredArtifacts.stream()
3575 .filter(e -> e.getType().equals(ArtifactTypeEnum.HEAT_ARTIFACT.getType()) || e
3577 .equals(ArtifactTypeEnum.HEAT_NESTED.getType()))
3578 .map(ArtifactTemplateInfo::getFileName)
3579 .collect(Collectors.toList()));
3580 json.put(Constants.ARTIFACT_HEAT_PARAMS, (heatParameters == null || heatParameters.isEmpty()) ? new ArrayList<>()
3585 public Either<ArtifactDefinition, Operation> updateResourceInstanceArtifactNoContent(String resourceId, Component containerComponent, User user, Map<String, Object> json, ArtifactOperationInfo operation, ArtifactDefinition artifactInfo) {
3587 String jsonStr = gson.toJson(json);
3588 ArtifactDefinition artifactDefinitionFromJson = artifactInfo == null ? RepresentationUtils.convertJsonToArtifactDefinition(jsonStr, ArtifactDefinition.class, false) : artifactInfo;
3589 String artifactUniqueId = artifactDefinitionFromJson == null ? null : artifactDefinitionFromJson.getUniqueId();
3590 Either<ArtifactDefinition, Operation> uploadArtifactToService = validateAndHandleArtifact(resourceId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactUniqueId,
3591 artifactDefinitionFromJson, null, jsonStr, null, null, user, containerComponent, false, false, true);
3593 return Either.left(uploadArtifactToService.left().value());
3596 private Either<ArtifactDefinition, Operation> handleUpdateHeatEnvAndHeatMeta(String componentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, String artifactId, User user, ComponentTypeEnum componentType,
3597 Component parent, String originData, String origMd5, ArtifactOperationInfo operation) {
3598 if (origMd5 != null) {
3599 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
3600 if (ArrayUtils.isNotEmpty(artifactInfo.getPayloadData())) {
3601 validateDeploymentArtifact(artifactInfo, parent);
3602 handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
3603 } else { // duplicate
3604 throw new ByActionStatusComponentException(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
3607 return updateHeatEnvParamsAndMetadata(componentId, artifactId, artifactInfo, user, auditingAction, parent, componentType, origMd5);
3610 private Either<ArtifactDefinition, Operation> updateHeatEnvParamsAndMetadata(String componentId, String artifactId, ArtifactDefinition artifactInfo, User user, AuditingActionEnum auditingAction, Component parent,
3611 ComponentTypeEnum componentType, String origMd5) {
3612 Either<ComponentInstance, ResponseFormat> getRI = getRIFromComponent(parent, componentId, artifactId, auditingAction, user);
3613 if (getRI.isRight()) {
3614 throw new ByResponseFormatComponentException(getRI.right().value());
3616 ComponentInstance ri = getRI.left().value();
3617 Either<ArtifactDefinition, ResponseFormat> getArtifactRes = getArtifactFromRI(parent, ri, componentId, artifactId, auditingAction, user);
3618 if (getArtifactRes.isRight()) {
3619 throw new ByResponseFormatComponentException(getArtifactRes.right().value());
3621 ArtifactDefinition currArtifact = getArtifactRes.left().value();
3623 if (currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT.getType()) ||
3624 currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT_VOL.getType()) ||
3625 currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT_NET.getType())) {
3626 throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
3628 List<HeatParameterDefinition> currentHeatEnvParams = currArtifact.getListHeatParameters();
3629 List<HeatParameterDefinition> updatedHeatEnvParams = artifactInfo.getListHeatParameters();
3632 if (origMd5 != null) {
3633 Either<List<HeatParameterDefinition>, ResponseFormat> uploadParamsValidationResult = validateUploadParamsFromEnvFile(auditingAction, parent, user, artifactInfo,
3634 artifactId, componentType, ri.getName(), currentHeatEnvParams, updatedHeatEnvParams, currArtifact.getArtifactName());
3635 if (uploadParamsValidationResult.isRight()) {
3636 throw new ByResponseFormatComponentException(uploadParamsValidationResult.right().value());
3638 artifactInfo.setListHeatParameters(updatedHeatEnvParams);
3641 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParamers = validateAndConvertHeatParameters(artifactInfo, ArtifactTypeEnum.HEAT_ENV.getType());
3642 if (validateAndConvertHeatParamers.isRight()) {
3643 throw new ByResponseFormatComponentException(validateAndConvertHeatParamers.right().value());
3646 if (updatedHeatEnvParams != null && !updatedHeatEnvParams.isEmpty()) {
3647 // fill reduced heat env parameters List for updating
3648 boolean updateRequired = replaceCurrHeatValueWithUpdatedValue(currentHeatEnvParams, updatedHeatEnvParams);
3649 if (updateRequired) {
3650 currArtifact.setHeatParamsUpdateDate(System.currentTimeMillis());
3651 currArtifact.setListHeatParameters(currentHeatEnvParams);
3652 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactRes = artifactToscaOperation.updateArtifactOnResource(
3653 currArtifact, parent, currArtifact.getUniqueId(), componentType.getNodeType(), componentId, true);
3654 if (updateArtifactRes.isRight()) {
3655 log.debug("Failed to update artifact on graph - {}", artifactId);
3656 throw new StorageException(updateArtifactRes.right().value());
3658 StorageOperationStatus error = generateCustomizationUUIDOnGroupInstance(ri, updateArtifactRes.left().value().getUniqueId(), parent.getUniqueId());
3659 if (error != StorageOperationStatus.OK) {
3660 throw new StorageException(error);
3664 updateHeatMetaDataIfNeeded(componentId,user,auditingAction,componentType, parent,ri,artifactInfo);
3665 StorageOperationStatus error = generateCustomizationUUIDOnInstance(parent.getUniqueId(), ri.getUniqueId(), componentType);
3666 if (error != StorageOperationStatus.OK) {
3667 throw new StorageException(error);
3670 return Either.left(currArtifact);
3674 updateHeatMetaDataIfNeeded(String componentId, User user, AuditingActionEnum auditingAction, ComponentTypeEnum componentType, Component parent, ComponentInstance resourceInstance, ArtifactDefinition updatedHeatEnvArtifact) {
3675 String heatArtifactId = updatedHeatEnvArtifact.getGeneratedFromId();
3676 Either<ArtifactDefinition, ResponseFormat> getArtifactRes = getArtifactFromRI(parent, resourceInstance, componentId, heatArtifactId, auditingAction, user);
3677 if (getArtifactRes.isRight()) {
3678 throw new ByResponseFormatComponentException(getArtifactRes.right().value());
3680 ArtifactDefinition heatArtifactToUpdate = getArtifactRes.left().value();
3681 if (isUpdateHeatMetaDataNeeded(updatedHeatEnvArtifact, heatArtifactToUpdate)) {
3682 validateHeatMetaData(updatedHeatEnvArtifact);
3683 updateHeatMetadataFromHeatEnv(updatedHeatEnvArtifact, heatArtifactToUpdate);
3684 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactRes = artifactToscaOperation.updateArtifactOnResource(heatArtifactToUpdate, parent,
3685 heatArtifactToUpdate.getUniqueId(), componentType.getNodeType(), componentId, false);
3687 if (updateArtifactRes.isRight()) {
3688 log.debug("Failed to update artifact on graph - {}", heatArtifactId);
3689 throw new StorageException(updateArtifactRes.right().value());
3691 ArtifactDefinition artifactDefinition = updateArtifactRes.left().value();
3692 updateGeneratedIdInHeatEnvOnInstance(resourceInstance, parent, heatArtifactId, heatArtifactToUpdate, artifactDefinition, componentType.getNodeType());
3693 StorageOperationStatus error = generateCustomizationUUIDOnGroupInstance(resourceInstance, artifactDefinition.getUniqueId(), parent.getUniqueId());
3694 if (error != StorageOperationStatus.OK) {
3695 throw new StorageException(error);
3700 private void validateHeatMetaData(ArtifactDefinition updatedHeatEnv) {
3701 Integer maxMinutes = ConfigurationManager.getConfigurationManager().getConfiguration().getHeatArtifactDeploymentTimeout().getMaxMinutes();
3702 Integer minMinutes = ConfigurationManager.getConfigurationManager().getConfiguration().getHeatArtifactDeploymentTimeout().getMinMinutes();
3703 Integer updateTimeout = updatedHeatEnv.getTimeout();
3704 if (updateTimeout > maxMinutes || updateTimeout < minMinutes) {
3705 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_TIMEOUT);
3709 private boolean isUpdateHeatMetaDataNeeded(ArtifactDefinition updatedHeatEnv, ArtifactDefinition origHeat) {
3710 // currently only timeout metadata can be updated
3711 return !origHeat.getTimeout().equals(updatedHeatEnv.getTimeout());
3714 private void updateHeatMetadataFromHeatEnv(ArtifactDefinition updatedHeatEnv, ArtifactDefinition origHeat) {
3715 // currently only timeout metadata can be updated
3716 origHeat.setTimeout(updatedHeatEnv.getTimeout());
3719 private boolean replaceCurrHeatValueWithUpdatedValue(List<HeatParameterDefinition> currentHeatEnvParams, List<HeatParameterDefinition> updatedHeatEnvParams) {
3720 boolean isUpdate = false;
3721 List<String> currentParamsNames = currentHeatEnvParams.stream().map(x -> x.getName()).collect(Collectors.toList());
3722 for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) {
3723 String paramName = heatEnvParam.getName();
3724 validateParamName(paramName, currentParamsNames);
3725 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3726 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3727 String updatedParamValue = heatEnvParam.getCurrentValue();
3728 if (!Objects.equals(updatedParamValue, currHeatParam.getCurrentValue())) {
3729 currHeatParam.setCurrentValue(updatedParamValue);
3738 private void validateParamName(String paramName, List<String> heatParamsNames) {
3739 if (!heatParamsNames.contains(paramName)) {
3740 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, paramName);
3744 private Either<ArtifactDefinition, Operation> updateHeatParams(String componentId, ArtifactDefinition artifactEnvInfo, AuditingActionEnum auditingAction, Component parent,
3745 ComponentTypeEnum componentType, ArtifactDefinition currHeatArtifact, boolean needToUpdateGroup) {
3746 Either<ArtifactDefinition, Operation> insideEither = null;
3747 String currentHeatId = currHeatArtifact.getUniqueId();
3749 String esArtifactId = currHeatArtifact.getEsId();
3750 Either<DAOArtifactData, CassandraOperationStatus> artifactFromES = artifactCassandraDao.getArtifact(esArtifactId);
3751 if (artifactFromES.isRight()) {
3752 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(artifactFromES.right().value());
3753 throw new StorageException(storageResponse, currHeatArtifact.getArtifactDisplayName());
3756 DAOArtifactData DAOArtifactData = artifactFromES.left().value();
3757 ArtifactDefinition updatedHeatArt = currHeatArtifact;
3758 List<HeatParameterDefinition> updatedHeatEnvParams = artifactEnvInfo.getListHeatParameters();
3759 List<HeatParameterDefinition> currentHeatEnvParams = currHeatArtifact.getListHeatParameters();
3760 List<HeatParameterDefinition> newHeatEnvParams = new ArrayList<>();
3762 if (CollectionUtils.isNotEmpty(updatedHeatEnvParams) && CollectionUtils.isNotEmpty(currentHeatEnvParams)) {
3763 //TODO: improve complexity - currently N^2
3765 for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) {
3766 paramName = heatEnvParam.getName();
3767 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3768 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3769 String updatedParamValue = heatEnvParam.getCurrentValue();
3770 if (updatedParamValue == null) {
3771 updatedParamValue = heatEnvParam.getDefaultValue();
3773 HeatParameterType paramType = HeatParameterType.isValidType(currHeatParam.getType());
3774 if (!paramType.getValidator().isValid(updatedParamValue, null)) {
3775 throw new ByActionStatusComponentException(ActionStatus.INVALID_HEAT_PARAMETER_VALUE,
3776 ArtifactTypeEnum.HEAT_ENV.getType(), paramType.getType(), paramName);
3778 currHeatParam.setCurrentValue(paramType.getConverter().convert(updatedParamValue, null, null));
3779 newHeatEnvParams.add(currHeatParam);
3784 if (!newHeatEnvParams.isEmpty()) {
3785 currHeatArtifact.setListHeatParameters(currentHeatEnvParams);
3786 Either<ArtifactDefinition, StorageOperationStatus> operationStatus = artifactToscaOperation.updateArtifactOnResource(
3787 currHeatArtifact, parent, currHeatArtifact.getUniqueId(), componentType.getNodeType(), componentId, true);
3789 if (operationStatus.isRight()) {
3790 log.debug("Failed to update artifact on graph - {}", currHeatArtifact.getUniqueId());
3791 throw new StorageException(operationStatus.right().value());
3793 updatedHeatArt = operationStatus.left().value();
3794 if (!updatedHeatArt.getDuplicated() || DAOArtifactData.getId() == null) {
3795 DAOArtifactData.setId(updatedHeatArt.getEsId());
3797 saveArtifactInCassandra(DAOArtifactData, parent, artifactEnvInfo, currentHeatId, updatedHeatArt
3798 .getUniqueId(), auditingAction, componentType);
3799 insideEither = Either.left(updatedHeatArt);
3802 Either<ArtifactDefinition, StorageOperationStatus> updateHeatEnvArtifact;
3803 if (!currentHeatId.equals(updatedHeatArt.getUniqueId())) {
3804 artifactEnvInfo.setArtifactChecksum(null);
3805 updateHeatEnvArtifact = artifactToscaOperation.updateHeatEnvArtifact(parent, artifactEnvInfo, currentHeatId, updatedHeatArt
3806 .getUniqueId(), componentType.getNodeType(), componentId);
3809 //TODO Andrey check if componentId = parent.getUniqeId
3810 updateHeatEnvArtifact = artifactToscaOperation.updateHeatEnvPlaceholder(artifactEnvInfo, parent, componentType
3814 if (needToUpdateGroup && updateHeatEnvArtifact.isLeft()) {
3815 ActionStatus result = updateGroupForHeat(currHeatArtifact, updatedHeatArt, artifactEnvInfo,
3816 updateHeatEnvArtifact.left().value(), parent);
3817 if (result != ActionStatus.OK) {
3818 throw new ByActionStatusComponentException(result);
3821 if (updatedHeatEnvParams.isEmpty()) {
3822 throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML, currHeatArtifact.getArtifactName());
3824 return insideEither;
3828 private StorageOperationStatus generateCustomizationUUIDOnGroupInstance(ComponentInstance ri, String artifactId, String componentId) {
3829 StorageOperationStatus error = StorageOperationStatus.OK;
3830 log.debug("Need to re-generate customization UUID for group instance on component instance {}", ri.getUniqueId());
3831 List<GroupInstance> groupsInstances = ri.getGroupInstances();
3832 List<String> groupInstancesId = null;
3833 if (groupsInstances != null && !groupsInstances.isEmpty()) {
3834 groupInstancesId = groupsInstances.stream()
3835 .filter(p -> p.getGroupInstanceArtifacts() != null && p.getGroupInstanceArtifacts()
3836 .contains(artifactId))
3837 .map(GroupInstanceDataDefinition::getUniqueId)
3838 .collect(Collectors.toList());
3840 if (groupInstancesId != null && !groupInstancesId.isEmpty()) {
3841 toscaOperationFacade.generateCustomizationUUIDOnInstanceGroup(componentId, ri.getUniqueId(), groupInstancesId);
3847 public Either<List<HeatParameterDefinition>, ResponseFormat> validateUploadParamsFromEnvFile(AuditingActionEnum auditingAction, Component parent, User user, ArtifactDefinition artifactInfo, String artifactId, ComponentTypeEnum componentType,
3848 String riName, List<HeatParameterDefinition> currentHeatEnvParams, List<HeatParameterDefinition> updatedHeatEnvParams, String currArtifactName) {
3850 if (updatedHeatEnvParams == null || updatedHeatEnvParams.isEmpty()) {
3851 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, artifactInfo
3852 .getArtifactName(), currArtifactName);
3853 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, artifactInfo, null, artifactId, responseFormat, componentType, riName);
3854 return Either.right(responseFormat);
3857 for (HeatParameterDefinition uploadedHeatParam : updatedHeatEnvParams) {
3858 String paramName = uploadedHeatParam.getName();
3859 boolean isExistsInHeat = false;
3860 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3861 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3863 isExistsInHeat = true;
3864 uploadedHeatParam.setType(currHeatParam.getType());
3865 uploadedHeatParam.setCurrentValue(uploadedHeatParam.getDefaultValue());
3866 uploadedHeatParam.setDefaultValue(currHeatParam.getDefaultValue());
3867 uploadedHeatParam.setUniqueId(currHeatParam.getUniqueId());
3871 if (!isExistsInHeat) {
3872 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISMATCH_HEAT_VS_HEAT_ENV, currArtifactName);
3873 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, artifactInfo, null, artifactId, responseFormat, componentType, riName);
3874 return Either.right(responseFormat);
3877 return Either.left(updatedHeatEnvParams);
3880 private Either<ComponentInstance, ResponseFormat> getRIFromComponent(Component component, String riID, String artifactId, AuditingActionEnum auditingAction, User user) {
3881 ResponseFormat responseFormat = null;
3882 List<ComponentInstance> ris = component.getComponentInstances();
3883 for (ComponentInstance ri : ris) {
3884 if (riID.equals(ri.getUniqueId())) {
3885 return Either.left(ri);
3888 responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, riID);
3889 log.debug("Resource Instance not found, resourceInstanceId {}", riID);
3890 handleAuditing(auditingAction, null, riID, user, null, null, artifactId, responseFormat, ComponentTypeEnum.RESOURCE_INSTANCE, null);
3891 return Either.right(responseFormat);
3894 private Either<ArtifactDefinition, ResponseFormat> getArtifactFromRI(Component component, ComponentInstance ri, String riID, String artifactId, AuditingActionEnum auditingAction, User user) {
3895 ResponseFormat responseFormat = null;
3896 Map<String, ArtifactDefinition> rtifactsMap = ri.getDeploymentArtifacts();
3897 for (ArtifactDefinition artifact : rtifactsMap.values()) {
3898 if (artifactId.equals(artifact.getUniqueId())) {
3899 return Either.left(artifact);
3902 responseFormat = componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, riID, component.getUniqueId());
3903 handleAuditing(auditingAction, component, riID, user, null, null, artifactId, responseFormat, ComponentTypeEnum.RESOURCE_INSTANCE, ri
3905 return Either.right(responseFormat);
3908 public ArtifactDefinition extractArtifactDefinition(Either<ArtifactDefinition, Operation> eitherArtifact) {
3909 ArtifactDefinition ret;
3910 if (eitherArtifact.isLeft()) {
3911 ret = eitherArtifact.left().value();
3914 ret = eitherArtifact.right().value().getImplementationArtifact();
3919 public byte[] downloadComponentArtifactByUUIDs(ComponentTypeEnum componentType, String componentUuid, String artifactUUID, ResourceCommonInfo resourceCommonInfo) {
3920 Component component = getComponentByUuid(componentType, componentUuid);
3921 resourceCommonInfo.setResourceName(component.getName());
3922 return downloadArtifact(component.getAllArtifacts(), artifactUUID, component.getName());
3926 * downloads an artifact of resource instance of component by UUIDs
3928 * @param componentType
3929 * @param componentUuid
3930 * @param resourceInstanceName
3931 * @param artifactUUID
3934 public byte[] downloadResourceInstanceArtifactByUUIDs(ComponentTypeEnum componentType, String componentUuid,
3935 String resourceInstanceName, String artifactUUID) {
3936 ComponentInstance resourceInstance = getRelatedComponentInstance(componentType, componentUuid, resourceInstanceName);
3937 return downloadArtifact(resourceInstance == null ? null : resourceInstance.getDeploymentArtifacts(),
3938 artifactUUID, resourceInstance.getName());
3942 * uploads an artifact to a component by UUID
3946 * @param componentType
3947 * @param componentUuid
3948 * @param resourceCommonInfo
3952 public ArtifactDefinition uploadArtifactToComponentByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType,
3953 String componentUuid, ResourceCommonInfo resourceCommonInfo, ArtifactOperationInfo operation) {
3954 Either<ArtifactDefinition, Operation> actionResult;
3955 Component component;
3957 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
3958 String origMd5 = request.getHeader(Constants.MD5_HEADER);
3959 String userId = request.getHeader(Constants.USER_ID_HEADER);
3961 Either<ComponentMetadataData, ActionStatus> getComponentRes =
3962 fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
3964 ComponentMetadataDataDefinition componentMetadataDataDefinition = getComponentRes.left().value().getMetadataDataDefinition();
3965 componentId = componentMetadataDataDefinition.getUniqueId();
3966 String componentName = componentMetadataDataDefinition.getName();
3968 if (!componentMetadataDataDefinition
3970 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
3971 component = checkoutParentComponent(componentType, componentId, userId);
3972 if (component != null) {
3973 componentId = component.getUniqueId();
3974 componentName = component.getName();
3977 resourceCommonInfo.setResourceName(componentName);
3979 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, null, artifactInfo,
3980 origMd5, data, null, null, null, null);
3981 return actionResult.left().value();
3985 * upload an artifact to a resource instance by UUID
3989 * @param componentType
3990 * @param componentUuid
3991 * @param resourceInstanceName
3995 public ArtifactDefinition uploadArtifactToRiByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName,
3996 ArtifactOperationInfo operation) {
3997 Either<ArtifactDefinition, Operation> actionResult;
3998 Component component = null;
3999 String componentInstanceId;
4001 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4002 String userId = request.getHeader(Constants.USER_ID_HEADER);
4004 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4006 Either<ComponentMetadataData, ActionStatus> getComponentRes =
4007 fetchLatestComponentMetadataOrThrow(componentType, componentUuid, resourceInstanceName);
4008 if (!getComponentRes.left()
4010 .getMetadataDataDefinition()
4012 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4013 component = checkoutParentComponent(componentType, getComponentRes.left()
4015 .getMetadataDataDefinition()
4016 .getUniqueId(), userId);
4018 if (component == null) {
4019 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4022 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4024 componentInstanceId = componentRiPair.getRight().getUniqueId();
4025 componentId = componentRiPair.getLeft().getUniqueId();
4026 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
4028 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE,
4029 operation, null, artifactInfo, origMd5, data, null, null,
4030 componentId, ComponentTypeEnum.findParamByType(componentType));
4032 return actionResult.left().value();
4036 * updates an artifact on a component by UUID
4040 * @param componentType
4041 * @param componentUuid
4042 * @param artifactUUID
4043 * @param resourceCommonInfo
4044 * @param operation TODO
4047 public ArtifactDefinition updateArtifactOnComponentByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String artifactUUID,
4048 ResourceCommonInfo resourceCommonInfo, ArtifactOperationInfo operation) {
4049 Either<ArtifactDefinition, Operation> actionResult;
4050 Component component;
4053 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinitionForUpdate(data, ArtifactDefinition.class);
4054 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4055 String userId = request.getHeader(Constants.USER_ID_HEADER);
4057 Either<ComponentMetadataData, ActionStatus> getComponentRes =
4058 fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
4059 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4060 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4062 if (!getComponentRes.left()
4064 .getMetadataDataDefinition()
4066 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4067 component = checkoutParentComponent(componentType, componentId, userId);
4068 if (component != null) {
4069 componentId = component.getUniqueId();
4070 componentName = component.getName();
4073 resourceCommonInfo.setResourceName(componentName);
4074 artifactId = getLatestParentArtifactDataIdByArtifactUUID(artifactUUID, componentId, componentType);
4075 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, artifactId, artifactInfo,
4076 origMd5, data, null, null, null, null);
4077 if (actionResult.isRight()) {
4078 log.debug(FAILED_UPLOAD_ARTIFACT_TO_COMPONENT, componentType, componentUuid, actionResult
4083 return actionResult.left().value();
4087 * updates an artifact on a resource instance by UUID
4091 * @param componentType
4092 * @param componentUuid
4093 * @param resourceInstanceName
4094 * @param artifactUUID
4095 * @param operation TODO
4098 public ArtifactDefinition updateArtifactOnRiByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName, String artifactUUID,
4099 ArtifactOperationInfo operation) {
4101 Either<ArtifactDefinition, Operation> actionResult;
4102 Component component = null;
4103 String componentInstanceId;
4106 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4107 String userId = request.getHeader(Constants.USER_ID_HEADER);
4109 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4110 Either<ComponentMetadataData, ActionStatus> getComponentRes =
4111 fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
4112 if (!getComponentRes.left()
4114 .getMetadataDataDefinition()
4116 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4117 component = checkoutParentComponent(componentType, getComponentRes.left()
4119 .getMetadataDataDefinition()
4120 .getUniqueId(), userId);
4122 if (component == null) {
4123 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4126 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4128 componentInstanceId = componentRiPair.getRight().getUniqueId();
4129 componentId = componentRiPair.getLeft().getUniqueId();
4130 artifactId = findArtifactId(componentRiPair.getRight(), artifactUUID);
4131 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
4133 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactId, artifactInfo, origMd5, data, null, null, componentId, ComponentTypeEnum
4134 .findParamByType(componentType));
4135 return actionResult.left().value();
4138 private Either<ArtifactDefinition, ResponseFormat> updateOperationArtifact(String componentId, String interfaceType, String operationUuid, ArtifactDefinition artifactInfo){
4139 Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId);
4140 if (componentStorageOperationStatusEither.isRight()) {
4141 StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value();
4142 log.debug("Failed to fetch resource information by resource id, error {}", errorStatus);
4143 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus)));
4145 Component storedComponent = componentStorageOperationStatusEither.left().value();
4147 Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils.getInterfaceDefinitionFromComponentByInterfaceType(storedComponent, interfaceType);
4148 if(!optionalInterface.isPresent()) {
4149 log.debug("Failed to get resource interface for resource Id {}", componentId);
4150 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceType));
4153 //fetch the operation from storage
4154 InterfaceDefinition gotInterface = optionalInterface.get();
4155 Map<String, Operation> operationsMap = gotInterface.getOperationsMap();
4156 Optional<Operation> optionalOperation = operationsMap.values()
4158 .filter(o -> o.getUniqueId().equals(operationUuid))
4160 if (!optionalOperation.isPresent()) {
4161 log.debug("Failed to get resource interface operation for resource Id {} and operationId {}", componentId, operationUuid);
4162 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, componentId);
4163 return Either.right(responseFormat);
4166 Operation operation = optionalOperation.get();
4167 ArtifactDefinition implementationArtifact = operation.getImplementationArtifact();
4168 implementationArtifact.setArtifactUUID(artifactInfo.getArtifactUUID());
4169 implementationArtifact.setUniqueId(artifactInfo.getUniqueId());
4170 implementationArtifact.setArtifactName(artifactInfo.getArtifactName());
4171 implementationArtifact.setDescription(artifactInfo.getDescription());
4172 implementationArtifact.setArtifactType(artifactInfo.getArtifactType());
4173 implementationArtifact.setArtifactLabel(artifactInfo.getArtifactLabel());
4174 implementationArtifact.setArtifactDisplayName(artifactInfo.getArtifactDisplayName());
4175 implementationArtifact.setEsId(artifactInfo.getEsId());
4176 operation.setImplementation(implementationArtifact);
4177 gotInterface.setOperationsMap(operationsMap);
4178 Either<List<InterfaceDefinition>, StorageOperationStatus> interfaceDefinitionStorageOperationStatusEither =
4179 interfaceOperation.updateInterfaces(storedComponent.getUniqueId(), Collections.singletonList(gotInterface));
4180 if (interfaceDefinitionStorageOperationStatusEither.isRight()){
4181 StorageOperationStatus storageOperationStatus = interfaceDefinitionStorageOperationStatusEither.right().value();
4182 ActionStatus actionStatus =
4183 componentsUtils.convertFromStorageResponseForDataType(storageOperationStatus);
4184 return Either.right(componentsUtils.getResponseFormat(actionStatus));
4187 return Either.left(artifactInfo);
4191 * updates an artifact on a component by UUID
4195 * @param componentType
4196 * @param componentUuid
4197 * @param artifactUUID
4201 public Either<ArtifactDefinition, ResponseFormat> updateArtifactOnInterfaceOperationByResourceUUID(
4202 String data, HttpServletRequest request, ComponentTypeEnum componentType,
4203 String componentUuid, String interfaceUUID, String operationUUID, String artifactUUID,
4204 ResourceCommonInfo resourceCommonInfo,ArtifactOperationInfo operation) {
4205 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
4206 Either<ArtifactDefinition, ResponseFormat> updateArtifactResult;
4207 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> actionResult = null;
4208 ArtifactDefinition updateArtifact = null;
4209 String componentId = null;
4210 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinitionForUpdate(data, ArtifactDefinition.class);
4211 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4212 String userId = request.getHeader(Constants.USER_ID_HEADER);
4213 ArtifactDefinition existingArtifactInfo = null;
4214 String interfaceName = null;
4216 Either<ComponentMetadataData, ActionStatus> getComponentRes =
4217 fetchLatestComponentMetadata(componentType, componentUuid).right().map(as -> {
4218 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(as));
4222 if(getComponentRes.isRight()) {
4223 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(getComponentRes.right().value()));
4226 if (errorWrapper.isEmpty()) {
4227 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4228 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4229 if (!getComponentRes.left()
4231 .getMetadataDataDefinition()
4233 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4234 Component component = checkoutParentComponent(componentType, componentId, userId);
4235 if (component != null) {
4236 componentId = component.getUniqueId();
4237 componentName = component.getName();
4241 resourceCommonInfo.setResourceName(componentName);
4244 if(errorWrapper.isEmpty()){
4245 Either<String, ResponseFormat> interfaceNameEither = fetchInterfaceName(componentId, interfaceUUID);
4246 if (interfaceNameEither.isRight()) {
4247 errorWrapper.setInnerElement(interfaceNameEither.right().value());
4250 interfaceName = interfaceNameEither.left().value();
4253 if(errorWrapper.isEmpty()){
4254 Either<Component, StorageOperationStatus> toscaComponentEither = toscaOperationFacade.getToscaElement(componentId);
4255 if (toscaComponentEither.isRight()) {
4256 StorageOperationStatus status = toscaComponentEither.right().value();
4257 log.debug("Could not fetch component with type {} and id {}. Status is {}. ", componentType, componentId, status);
4258 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status)));
4261 if (errorWrapper.isEmpty()) {
4262 NodeTypeEnum parentType = convertParentType(componentType);
4263 final List<ArtifactDefinition> existingDeploymentArtifacts =
4264 getDeploymentArtifacts(toscaComponentEither.left().value(),null);
4265 for (ArtifactDefinition artifactDefinition: existingDeploymentArtifacts){
4266 if(artifactInfo.getArtifactName().equalsIgnoreCase(artifactDefinition.getArtifactName())){
4267 existingArtifactInfo = artifactDefinition;
4271 if(existingArtifactInfo != null){
4272 return updateOperationArtifact(componentId, interfaceName, operationUUID, existingArtifactInfo);
4278 if (errorWrapper.isEmpty()) {
4280 actionResult = Either.left(handleArtifactRequest(componentId, userId, componentType, operation,
4281 artifactUUID, artifactInfo, origMd5, data, interfaceName,
4282 operationUUID, null, null));
4283 }catch (ComponentException e){
4284 errorWrapper.setInnerElement(e.getResponseFormat());
4288 if (errorWrapper.isEmpty()) {
4289 updateArtifact = actionResult.left().value().left().value();
4290 updateArtifactResult = Either.left(updateArtifact);
4294 updateArtifactResult = Either.right(errorWrapper.getInnerElement());
4296 return updateArtifactResult;
4300 private Either<ComponentMetadataData, ActionStatus> fetchLatestComponentMetadataOrThrow(
4301 ComponentTypeEnum componentType, String componentUuid
4303 return fetchLatestComponentMetadataOrThrow(componentType, componentUuid, componentUuid);
4306 private Either<ComponentMetadataData, ActionStatus> fetchLatestComponentMetadataOrThrow(
4307 ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName
4309 return fetchLatestComponentMetadata(componentType, componentUuid).right().map(as -> {
4310 throw new ByActionStatusComponentException(as, resourceInstanceName);
4314 private Either<ComponentMetadataData, ActionStatus> fetchLatestComponentMetadata(
4315 ComponentTypeEnum componentType, String componentUuid
4317 return toscaOperationFacade
4318 .getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true)
4319 .right().map(sos -> {
4320 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, sos);
4321 return componentsUtils.convertFromStorageResponse(sos, componentType);
4325 private Either<String, ResponseFormat> fetchInterfaceName(String componentId, String interfaceUUID) {
4326 Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId);
4327 if (componentStorageOperationStatusEither.isRight()) {
4328 StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value();
4329 log.debug("Failed to fetch component information by component id, error {}", errorStatus);
4330 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus)));
4332 Component storedComponent = componentStorageOperationStatusEither.left().value();
4334 Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils
4335 .getInterfaceDefinitionFromComponentByInterfaceId(storedComponent, interfaceUUID);
4336 if(!optionalInterface.isPresent()) {
4337 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceUUID));
4339 return Either.left(optionalInterface.get().getType());
4343 * deletes an artifact on a component by UUID
4346 * @param componentType
4347 * @param componentUuid
4348 * @param artifactUUID
4349 * @param resourceCommonInfo
4350 * @param operation TODO
4353 public ArtifactDefinition deleteArtifactOnComponentByUUID(HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String artifactUUID, ResourceCommonInfo resourceCommonInfo,
4354 ArtifactOperationInfo operation) {
4356 Either<ArtifactDefinition, Operation> actionResult;
4357 Component component;
4358 String componentId ;
4360 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4361 String userId = request.getHeader(Constants.USER_ID_HEADER);
4363 Either<ComponentMetadataData, ActionStatus> getComponentRes =
4364 fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
4365 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4366 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4367 if (!getComponentRes.left()
4369 .getMetadataDataDefinition()
4371 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4372 component = checkoutParentComponent(componentType, componentId, userId);
4373 if (component != null) {
4374 componentId = component.getUniqueId();
4375 componentName = component.getName();
4378 resourceCommonInfo.setResourceName(componentName);
4379 artifactId = getLatestParentArtifactDataIdByArtifactUUID(artifactUUID, componentId, componentType);
4380 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, artifactId, null, origMd5, null, null, null, null, null);
4381 return actionResult.left().value();
4385 * deletes an artifact from a resource instance by UUID
4388 * @param componentType
4389 * @param componentUuid
4390 * @param resourceInstanceName
4391 * @param artifactUUID
4392 * @param operation TODO
4395 public ArtifactDefinition deleteArtifactOnRiByUUID(HttpServletRequest request, ComponentTypeEnum componentType,
4396 String componentUuid, String resourceInstanceName,
4397 String artifactUUID, ArtifactOperationInfo operation) {
4399 Either<ArtifactDefinition, Operation> actionResult;
4400 Component component = null;
4401 String componentInstanceId;
4404 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4405 String userId = request.getHeader(Constants.USER_ID_HEADER);
4406 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4407 Either<ComponentMetadataData, ActionStatus> getComponentRes =
4408 fetchLatestComponentMetadataOrThrow(componentType, componentUuid);
4409 if (!getComponentRes.left()
4411 .getMetadataDataDefinition()
4413 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4414 component = checkoutParentComponent(componentType, getComponentRes.left()
4416 .getMetadataDataDefinition()
4417 .getUniqueId(), userId);
4419 if (component == null) {
4420 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4423 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4425 componentInstanceId = componentRiPair.getRight().getUniqueId();
4426 componentId = componentRiPair.getLeft().getUniqueId();
4427 artifactId = findArtifactId(componentRiPair.getRight(), artifactUUID);
4429 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactId, null, origMd5, null, null, null, componentId, ComponentTypeEnum
4430 .findParamByType(componentType));
4431 return actionResult.left().value();
4434 private String findArtifactId(ComponentInstance instance, String artifactUUID) {
4435 String artifactId = null;
4436 ArtifactDefinition foundArtifact = null;
4437 if (instance.getDeploymentArtifacts() != null) {
4438 foundArtifact = instance.getDeploymentArtifacts()
4441 .filter(e -> e.getArtifactUUID() != null && e.getArtifactUUID()
4442 .equals(artifactUUID))
4446 if (foundArtifact == null && instance.getArtifacts() != null) {
4447 foundArtifact = instance.getArtifacts()
4450 .filter(e -> e.getArtifactUUID() != null && e.getArtifactUUID()
4451 .equals(artifactUUID))
4455 if (foundArtifact == null) {
4456 log.debug("The artifact {} was not found on instance {}. ", artifactUUID, instance.getUniqueId());
4457 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactUUID);
4460 artifactId = foundArtifact.getUniqueId();
4465 @SuppressWarnings("unchecked")
4466 public ArtifactDefinition createHeatEnvPlaceHolder(List<ArtifactDefinition> createdArtifacts, ArtifactDefinition heatArtifact,
4467 String envType, String parentId, NodeTypeEnum parentType,
4468 String parentName, User user, Component component,
4469 Map<String, String> existingEnvVersions) {
4470 Map<String, Object> deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager()
4472 .getDeploymentResourceInstanceArtifacts();
4473 if (deploymentResourceArtifacts == null) {
4474 log.debug("no deployment artifacts are configured for generated artifacts");
4475 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
4477 Map<String, Object> placeHolderData = (Map<String, Object>) deploymentResourceArtifacts.get(envType);
4478 if (placeHolderData == null) {
4479 log.debug("no env type {} are configured for generated artifacts", envType);
4480 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
4483 String envLabel = (heatArtifact.getArtifactLabel() + HEAT_ENV_SUFFIX).toLowerCase();
4484 ArtifactDefinition createArtifactPlaceHolder = createArtifactPlaceHolderInfo(parentId, envLabel, placeHolderData, user
4485 .getUserId(), ArtifactGroupTypeEnum.DEPLOYMENT, true);
4486 ArtifactDefinition artifactHeatEnv = createArtifactPlaceHolder;
4487 artifactHeatEnv.setGeneratedFromId(heatArtifact.getUniqueId());
4488 artifactHeatEnv.setHeatParamsUpdateDate(System.currentTimeMillis());
4489 artifactHeatEnv.setTimeout(0);
4490 artifactHeatEnv.setIsFromCsar(heatArtifact.getIsFromCsar());
4491 buildHeatEnvFileName(heatArtifact, artifactHeatEnv, placeHolderData);
4492 // rbetzer - keep env artifactVersion - changeComponentInstanceVersion flow
4493 handleEnvArtifactVersion(artifactHeatEnv, existingEnvVersions);
4494 ArtifactDefinition heatEnvPlaceholder;
4495 // Evg : for resource instance artifact will be added later as block with other env artifacts from BL
4496 if (parentType != NodeTypeEnum.ResourceInstance) {
4497 String checkSum = artifactToscaOperation.sortAndCalculateChecksumForHeatParameters(heatArtifact.getHeatParameters());
4498 artifactHeatEnv.setArtifactChecksum(checkSum);
4499 Either<ArtifactDefinition, StorageOperationStatus> addHeatEnvArtifact = addHeatEnvArtifact(artifactHeatEnv, heatArtifact, component, parentType, parentId);
4500 if (addHeatEnvArtifact.isRight()) {
4501 log.debug("failed to create heat env artifact on resource instance");
4502 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(addHeatEnvArtifact
4504 .value(), false), "", null));
4506 heatEnvPlaceholder = createArtifactPlaceHolder;
4509 heatEnvPlaceholder = artifactHeatEnv;
4510 artifactToscaOperation.generateUUID(heatEnvPlaceholder, heatEnvPlaceholder.getArtifactVersion());
4511 setHeatCurrentValuesOnHeatEnvDefaultValues(heatArtifact, heatEnvPlaceholder);
4513 ComponentTypeEnum componentType = component.getComponentType();
4514 if (parentType == NodeTypeEnum.ResourceInstance) {
4515 componentType = ComponentTypeEnum.RESOURCE_INSTANCE;
4517 createdArtifacts.add(heatEnvPlaceholder);
4518 componentsUtils.auditComponent(componentsUtils.getResponseFormat(ActionStatus.OK), user, component, AuditingActionEnum.ARTIFACT_UPLOAD,
4519 new ResourceCommonInfo(parentName, componentType.getValue()),
4520 ResourceVersionInfo.newBuilder().build(),
4521 ResourceVersionInfo.newBuilder().artifactUuid(heatEnvPlaceholder.getUniqueId()).build(),
4522 null, heatEnvPlaceholder, null);
4523 return heatEnvPlaceholder;
4526 private void setHeatCurrentValuesOnHeatEnvDefaultValues(ArtifactDefinition artifact, ArtifactDefinition artifactDefinition) {
4527 if (artifact.getListHeatParameters() == null) {
4530 List<HeatParameterDefinition> heatEnvParameters = new ArrayList<>();
4531 for (HeatParameterDefinition parameter : artifact.getListHeatParameters()) {
4532 HeatParameterDefinition heatEnvParameter = new HeatParameterDefinition(parameter);
4533 heatEnvParameter.setDefaultValue(parameter.getCurrentValue());
4534 heatEnvParameter.setCurrentValue(null);
4535 heatEnvParameters.add(heatEnvParameter);
4537 artifactDefinition.setListHeatParameters(heatEnvParameters);
4540 private void buildHeatEnvFileName(ArtifactDefinition heatArtifact, ArtifactDefinition heatEnvArtifact, Map<String, Object> placeHolderData) {
4541 String heatExtension = GeneralUtility.getFilenameExtension(heatArtifact.getArtifactName());
4542 String envExtension = (String) placeHolderData.get(ARTIFACT_PLACEHOLDER_FILE_EXTENSION);
4543 String name = heatArtifact.getArtifactName();
4546 name = heatArtifact.getArtifactLabel();
4547 fileName = name + "." + envExtension;
4550 fileName = name.replaceAll("." + heatExtension, "." + envExtension);
4552 heatEnvArtifact.setArtifactName(fileName);
4555 private void handleEnvArtifactVersion(ArtifactDefinition heatEnvArtifact, Map<String, String> existingEnvVersions) {
4556 if (null != existingEnvVersions) {
4557 String prevVersion = existingEnvVersions.get(heatEnvArtifact.getArtifactName());
4558 if (null != prevVersion) {
4559 heatEnvArtifact.setArtifactVersion(prevVersion);
4564 public List<ArtifactDefinition> handleArtifactsForInnerVfcComponent(List<ArtifactDefinition> artifactsToHandle, Resource component, User user, List<ArtifactDefinition> vfcsNewCreatedArtifacts,
4565 ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction) {
4566 ComponentTypeEnum componentType = component.getComponentType();
4567 List<ArtifactDefinition> uploadedArtifacts = new ArrayList<>();
4568 Either<ArtifactDefinition, Operation> result;
4570 for (ArtifactDefinition artifactDefinition : artifactsToHandle) {
4571 result = handleLoadedArtifact(component, user, operation, shouldLock, inTransaction, componentType, artifactDefinition);
4572 uploadedArtifacts.add(result.left().value());
4574 } catch (ComponentException e) {
4575 log.debug(FAILED_UPLOAD_ARTIFACT_TO_COMPONENT, componentType, component
4576 .getName(), e.getResponseFormat());
4577 if (operation.isCreateOrLink()) {
4578 vfcsNewCreatedArtifacts.addAll(uploadedArtifacts);
4582 return uploadedArtifacts;
4585 public Either<ArtifactDefinition, Operation> handleLoadedArtifact(Resource component, User user, ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction,
4586 ComponentTypeEnum componentType, ArtifactDefinition artifactDefinition) {
4587 AuditingActionEnum auditingAction = detectAuditingType(operation, "");
4588 String componentId = component.getUniqueId();
4589 String artifactId = artifactDefinition.getUniqueId();
4590 Either<ArtifactDefinition, Operation> result;
4591 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
4592 //artifact validation
4593 artifactDefinition = validateArtifact(componentId, componentType, operation,
4594 artifactId, artifactDefinition, auditingAction, user,
4595 component, shouldLock, inTransaction);
4596 switch (operation.getArtifactOperationEnum()) {
4598 byte[] validPayload = getValidPayload(componentId, artifactDefinition, operation, auditingAction, artifactId, user, componentType, component, null, null);
4599 result = createArtifact(component, componentId, artifactDefinition, validPayload,
4600 componentType, auditingAction, null, null);
4603 validPayload = getValidPayload(componentId, artifactDefinition, operation, auditingAction, artifactId, user, componentType, component, null, null);
4604 result = handleUpdate(componentId, componentType, operation, artifactId, artifactDefinition, validPayload, null, null, null, null,
4605 auditingAction, user, component, true);
4608 result = Either.left(handleDeleteInternal(componentId, artifactId, componentType, component));
4611 if (artifactGenerationRequired(component, artifactDefinition)) {
4612 result = Either.left(generateNotSavedArtifact(component, artifactDefinition));
4614 result = Either.left(handleDownload(componentId, artifactId, componentType, component));
4618 result = Either.left(handleLink(componentId, artifactDefinition, componentType, component));
4621 throw new UnsupportedOperationException("In ArtifactsBusinessLogic received illegal operation: " + operation.getArtifactOperationEnum());
4626 public List<ArtifactDefinition> handleArtifactsRequestForInnerVfcComponent(List<ArtifactDefinition> artifactsToHandle, Resource component, User user, List<ArtifactDefinition> vfcsNewCreatedArtifacts,
4627 ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction) {
4629 List<ArtifactDefinition> handleArtifactsResult;
4630 ComponentTypeEnum componentType = component.getComponentType();
4631 List<ArtifactDefinition> uploadedArtifacts = new ArrayList<>();
4632 Either<ArtifactDefinition, Operation> actionResult;
4636 for (ArtifactDefinition artifact : artifactsToHandle) {
4637 originData = ArtifactUtils.buildJsonStringForCsarVfcArtifact(artifact);
4638 origMd5 = GeneralUtility.calculateMD5Base64EncodedByString(originData);
4639 actionResult = handleArtifactRequest(component.getUniqueId(), user.getUserId(), componentType, operation, artifact
4640 .getUniqueId(), artifact, origMd5, originData, null, null, null, null, shouldLock, inTransaction);
4641 uploadedArtifacts.add(actionResult.left().value());
4643 handleArtifactsResult = uploadedArtifacts;
4644 }catch (ComponentException e){
4645 if (operation.isCreateOrLink()) {
4646 vfcsNewCreatedArtifacts.addAll(uploadedArtifacts);
4650 return handleArtifactsResult;
4653 private ComponentInstance getRelatedComponentInstance(
4654 ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName
4656 String normalizedName = ValidationUtils.normalizeComponentInstanceName(resourceInstanceName);
4657 Option<Component> oComponent = Option.of(getComponentByUuid(componentType, componentUuid));
4659 .toTry(componentNotFound(componentType, componentUuid))
4660 .flatMap(component -> findFirstMatching(component,
4661 ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName()).equals(normalizedName)
4662 ).toTry(componentInstanceNotFound(componentType, resourceInstanceName, component))
4666 private ImmutablePair<Component, ComponentInstance> getRelatedComponentComponentInstance(
4667 Component component, String resourceInstanceName
4669 String normalizedName = ValidationUtils.normalizeComponentInstanceName(resourceInstanceName);
4670 ComponentInstance componentInstance = findFirstMatching(component,
4671 ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName()).equals(normalizedName)
4672 ).toTry(componentInstanceNotFound(component.getComponentType(), resourceInstanceName, component)).get();
4673 return new ImmutablePair<>(component, componentInstance);
4676 private ImmutablePair<Component, ComponentInstance> getRelatedComponentComponentInstance(
4677 ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName
4679 Component component = getLatestComponentByUuid(componentType, componentUuid);
4680 ComponentInstance componentInstance = findFirstMatching(component,
4681 ci -> ci.getNormalizedName().equals(resourceInstanceName)
4682 ).toTry(componentInstanceNotFound(component.getComponentType(), resourceInstanceName, component)).get();
4683 return new ImmutablePair<>(component, componentInstance);
4686 private Supplier<Throwable> componentNotFound(ComponentTypeEnum componentType, String componentUuid) {
4688 log.debug(FAILED_FETCH_COMPONENT, componentType.getValue(), componentUuid);
4689 return new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, componentUuid);
4693 private Supplier<Throwable> componentInstanceNotFound(
4694 ComponentTypeEnum componentType, String resourceInstanceName, Component component
4697 log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName());
4698 return new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
4699 resourceInstanceName,
4700 RESOURCE_INSTANCE, componentType.getValue(), component.getName());
4704 private static Option<ComponentInstance> findFirstMatching(Component component, Predicate<ComponentInstance> filter) {
4705 return Option.ofOptional(component.getComponentInstances()
4711 private byte[] downloadArtifact(Map<String, ArtifactDefinition> artifacts, String artifactUUID, String componentName) {
4713 ImmutablePair<String, byte[]> downloadArtifact;
4714 List<ArtifactDefinition> artifactsList = null;
4715 ArtifactDefinition deploymentArtifact;
4716 if (artifacts != null && !artifacts.isEmpty()) {
4717 artifactsList = artifacts.values()
4719 .filter(art -> art.getArtifactUUID() != null && art.getArtifactUUID()
4720 .equals(artifactUUID))
4721 .collect(Collectors.toList());
4723 if (artifactsList == null || artifactsList.isEmpty()) {
4724 log.debug("Deployment artifact with uuid {} was not found for component {}", artifactUUID, componentName);
4725 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactUUID);
4727 deploymentArtifact = artifactsList.get(0);
4728 downloadArtifact = downloadArtifact(deploymentArtifact);
4729 log.trace("Succeeded to download artifact with uniqueId {}", deploymentArtifact.getUniqueId());
4730 return downloadArtifact.getRight();
4733 private Component getLatestComponentByUuid(ComponentTypeEnum componentType, String componentUuid) {
4734 Component component;
4735 Either<Component, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentByUuid(componentUuid);
4736 if (getComponentRes.isRight()) {
4737 StorageOperationStatus status = getComponentRes.right().value();
4738 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4739 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4742 component = getComponentRes.left().value();
4747 private Component getComponentByUuid(ComponentTypeEnum componentType, String componentUuid) {
4748 Component component;
4749 Either<List<Component>, StorageOperationStatus> getComponentRes = toscaOperationFacade.getComponentListByUuid(componentUuid, null);
4750 if (getComponentRes.isRight()) {
4751 StorageOperationStatus status = getComponentRes.right().value();
4752 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4753 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4756 List<Component> value = getComponentRes.left().value();
4757 if (value.isEmpty()) {
4758 log.debug("Could not fetch component with type {} and uuid {}.", componentType, componentUuid);
4759 ActionStatus status = componentType == ComponentTypeEnum.RESOURCE ? ActionStatus.RESOURCE_NOT_FOUND : ActionStatus.SERVICE_NOT_FOUND;
4760 throw new ByActionStatusComponentException(status);
4763 component = value.get(0);
4769 private String getLatestParentArtifactDataIdByArtifactUUID(String artifactUUID, String parentId, ComponentTypeEnum componentType) {
4770 ActionStatus actionStatus = ActionStatus.ARTIFACT_NOT_FOUND;
4771 StorageOperationStatus storageStatus;
4772 ArtifactDefinition latestArtifact;
4773 List<ArtifactDefinition> artifacts;
4774 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifactsRes = artifactToscaOperation.getArtifacts(parentId);
4775 if (getArtifactsRes.isRight()) {
4776 storageStatus = getArtifactsRes.right().value();
4777 log.debug("Couldn't fetch artifacts data for parent component {} with uid {}, error: {}", componentType, parentId, storageStatus);
4778 if (storageStatus != StorageOperationStatus.NOT_FOUND) {
4779 actionStatus = componentsUtils.convertFromStorageResponse(storageStatus);
4781 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4783 artifacts = getArtifactsRes.left()
4787 .filter(a -> a.getArtifactUUID() != null && a.getArtifactUUID()
4788 .equals(artifactUUID))
4789 .collect(Collectors.toList());
4790 if (artifacts == null || artifacts.isEmpty()) {
4791 log.debug("Couldn't fetch artifact with UUID {} data for parent component {} with uid {}, error: {}", artifactUUID, componentType, parentId, actionStatus);
4792 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4794 latestArtifact = artifacts.stream().max((a1, a2) -> {
4795 int compareRes = Double.compare(Double.parseDouble(a1.getArtifactVersion()), Double.parseDouble(a2.getArtifactVersion()));
4796 if (compareRes == 0) {
4797 compareRes = Long.compare(a1.getLastUpdateDate() == null ? 0 : a1.getLastUpdateDate(), a2.getLastUpdateDate() == null ? 0 : a2
4798 .getLastUpdateDate());
4802 if (latestArtifact == null) {
4803 log.debug("Couldn't fetch latest artifact with UUID {} data for parent component {} with uid {}, error: {}", artifactUUID, componentType, parentId, actionStatus);
4804 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4806 return latestArtifact.getUniqueId();
4809 private Component checkoutParentComponent(ComponentTypeEnum componentType, String parentId, String userId) {
4811 Component component = null;
4812 User modifier = userBusinessLogic.getUser(userId, false);
4813 LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction("External API checkout", LifecycleChanceActionEnum.UPDATE_FROM_EXTERNAL_API);
4814 Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic.changeComponentState(componentType, parentId, modifier, LifeCycleTransitionEnum.CHECKOUT, changeInfo, false, true);
4815 if (checkoutRes.isRight()) {
4816 log.debug("Could not change state of component {} with uid {} to checked out. Status is {}. ", componentType
4817 .getNodeType(), parentId, checkoutRes.right().value().getStatus());
4818 throw new ByResponseFormatComponentException(checkoutRes.right().value());
4820 return checkoutRes.left().value();
4824 void setNodeTemplateOperation(NodeTemplateOperation nodeTemplateOperation) {
4825 this.nodeTemplateOperation = nodeTemplateOperation;
4828 public List<ArtifactConfiguration> getConfiguration() {
4829 return ConfigurationManager.getConfigurationManager().getConfiguration().getArtifacts();