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 com.google.common.annotations.VisibleForTesting;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import fj.data.Either;
29 import org.apache.commons.codec.binary.Base64;
30 import org.apache.commons.collections.CollectionUtils;
31 import org.apache.commons.collections.MapUtils;
32 import org.apache.commons.lang.ArrayUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.commons.lang3.tuple.ImmutablePair;
35 import org.openecomp.sdc.be.components.ArtifactsResolver;
36 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
37 import org.openecomp.sdc.be.components.impl.artifact.ArtifactTypeToPayloadTypeSelector;
38 import org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum;
39 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
40 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
41 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
42 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
43 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
44 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
45 import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils;
46 import org.openecomp.sdc.be.config.BeEcompErrorManager;
47 import org.openecomp.sdc.be.config.Configuration.ArtifactTypeConfig;
48 import org.openecomp.sdc.be.config.ConfigurationManager;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
51 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
52 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
53 import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
55 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
56 import org.openecomp.sdc.be.datatypes.elements.GroupInstanceDataDefinition;
57 import org.openecomp.sdc.be.datatypes.elements.HeatParameterDataDefinition;
58 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
59 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
60 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
61 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
62 import org.openecomp.sdc.be.info.ArtifactTemplateInfo;
63 import org.openecomp.sdc.be.model.ArtifactDefinition;
64 import org.openecomp.sdc.be.model.ArtifactType;
65 import org.openecomp.sdc.be.model.Component;
66 import org.openecomp.sdc.be.model.ComponentInstance;
67 import org.openecomp.sdc.be.model.ComponentParametersView;
68 import org.openecomp.sdc.be.model.GroupDefinition;
69 import org.openecomp.sdc.be.model.GroupInstance;
70 import org.openecomp.sdc.be.model.HeatParameterDefinition;
71 import org.openecomp.sdc.be.model.InterfaceDefinition;
72 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
73 import org.openecomp.sdc.be.model.LifecycleStateEnum;
74 import org.openecomp.sdc.be.model.Operation;
75 import org.openecomp.sdc.be.model.Resource;
76 import org.openecomp.sdc.be.model.Service;
77 import org.openecomp.sdc.be.model.User;
78 import org.openecomp.sdc.be.model.heat.HeatParameterType;
79 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
80 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
81 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
82 import org.openecomp.sdc.be.model.operations.StorageException;
83 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
84 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
85 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
86 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
87 import org.openecomp.sdc.be.model.operations.api.IHeatParametersOperation;
88 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
89 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
90 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
91 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
92 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
93 import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation;
94 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
95 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
96 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
97 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
98 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
99 import org.openecomp.sdc.be.servlets.RepresentationUtils;
100 import org.openecomp.sdc.be.tosca.CsarUtils;
101 import org.openecomp.sdc.be.tosca.ToscaError;
102 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
103 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
104 import org.openecomp.sdc.be.user.Role;
105 import org.openecomp.sdc.be.user.UserBusinessLogic;
106 import org.openecomp.sdc.be.utils.TypeUtils;
107 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
108 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
109 import org.openecomp.sdc.common.api.Constants;
110 import org.openecomp.sdc.common.datastructure.Wrapper;
111 import org.openecomp.sdc.common.log.wrappers.Logger;
112 import org.openecomp.sdc.common.util.GeneralUtility;
113 import org.openecomp.sdc.common.util.ValidationUtils;
114 import org.openecomp.sdc.common.util.YamlToObjectConverter;
115 import org.openecomp.sdc.exception.ResponseFormat;
116 import org.springframework.beans.factory.annotation.Autowired;
117 import org.xml.sax.InputSource;
118 import org.xml.sax.SAXException;
119 import org.xml.sax.SAXNotRecognizedException;
120 import org.xml.sax.SAXNotSupportedException;
121 import org.xml.sax.XMLReader;
122 import org.yaml.snakeyaml.Yaml;
124 import javax.servlet.http.HttpServletRequest;
125 import javax.xml.XMLConstants;
126 import javax.xml.parsers.ParserConfigurationException;
127 import javax.xml.parsers.SAXParserFactory;
128 import java.io.ByteArrayInputStream;
129 import java.io.IOException;
130 import java.math.BigDecimal;
131 import java.util.ArrayList;
132 import java.util.Collections;
133 import java.util.Comparator;
134 import java.util.HashMap;
135 import java.util.Iterator;
136 import java.util.List;
137 import java.util.Map;
138 import java.util.Map.Entry;
139 import java.util.Objects;
140 import java.util.Optional;
141 import java.util.Set;
142 import java.util.function.Function;
143 import java.util.function.Predicate;
144 import java.util.function.Supplier;
145 import java.util.stream.Collectors;
147 import static org.openecomp.sdc.be.dao.api.ActionStatus.MISMATCH_BETWEEN_ARTIFACT_TYPE_AND_COMPONENT_TYPE;
149 @org.springframework.stereotype.Component("artifactBusinessLogic")
150 public class ArtifactsBusinessLogic extends BaseBusinessLogic {
151 private static final String RESOURCE_INSTANCE = "resource instance";
152 private static final String ARTIFACT_TYPE_OTHER = "OTHER";
153 private static final String ARTIFACT_DESCRIPTION = "artifact description";
154 private static final String ARTIFACT_LABEL = "artifact label";
155 private static final String ARTIFACT_URL = "artifact url";
156 private static final String ARTIFACT_NAME = "artifact name";
157 private static final String ARTIFACT_PAYLOAD = "artifact payload";
159 private static final String ARTIFACT_PLACEHOLDER_TYPE = "type";
160 private static final String ARTIFACT_PLACEHOLDER_DISPLAY_NAME = "displayName";
161 private static final Object ARTIFACT_PLACEHOLDER_DESCRIPTION = "description";
163 public static final String HEAT_ENV_NAME = "heatEnv";
164 public static final String HEAT_VF_ENV_NAME = "VfHeatEnv";
165 public static final String HEAT_ENV_SUFFIX = "env";
166 private static final String ARTIFACT_PLACEHOLDER_FILE_EXTENSION = "fileExtension";
168 private static final Logger log = Logger.getLogger(ArtifactsBusinessLogic.class.getName());
169 private static final String FAILED_UPDATE_GROUPS = "Failed to update groups of the component {}. ";
170 private static final String FAILED_UPDATE_ARTIFACT = "Failed to delete or update the artifact {}. Parent uniqueId is {}";
171 private static final String FAILED_SAVE_ARTIFACT = "Failed to save the artifact.";
172 public static final String ARTIFACT_ACTION_LOCK = "Artifact action - lock ";
173 private static final String UPDATE_ARTIFACT_LOCK = "Update Artifact - lock ";
174 private static final String FAILED_DOWNLOAD_ARTIFACT = "Download artifact {} failed";
175 public static final String FAILED_UPLOAD_ARTIFACT_TO_COMPONENT = "Failed to upload artifact to component with type {} and uuid {}. Status is {}. ";
176 private static final String FAILED_UPLOAD_ARTIFACT_TO_INSTANCE = "Failed to upload artifact to component instance {} of 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 ARTIFACT_SAVED = "Artifact saved into ES - {}";
183 private static final String UPDATE_ARTIFACT = "Update Artifact";
184 private static final String FOUND_DEPLOYMENT_ARTIFACT = "Found deployment artifact {}";
185 private Gson gson = new GsonBuilder().setPrettyPrinting().create();
187 @javax.annotation.Resource
188 private IInterfaceLifecycleOperation interfaceLifecycleOperation;
189 @javax.annotation.Resource
190 private UserAdminOperation userOperaton;
192 @javax.annotation.Resource
193 private IElementOperation elementOperation;
195 @javax.annotation.Resource
196 private IHeatParametersOperation heatParametersOperation;
198 private ArtifactCassandraDao artifactCassandraDao;
199 private ToscaExportHandler toscaExportUtils;
200 private CsarUtils csarUtils;
201 private LifecycleBusinessLogic lifecycleBusinessLogic;
202 private UserBusinessLogic userBusinessLogic;
203 private ArtifactsResolver artifactsResolver;
204 private NodeTemplateOperation nodeTemplateOperation;
207 public ArtifactsBusinessLogic(ArtifactCassandraDao artifactCassandraDao, ToscaExportHandler toscaExportUtils,
208 CsarUtils csarUtils, LifecycleBusinessLogic lifecycleBusinessLogic, UserBusinessLogic userBusinessLogic,
209 ArtifactsResolver artifactsResolver, IElementOperation elementDao, IGroupOperation groupOperation,
210 IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation, InterfaceOperation interfaceOperation,
211 InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsOperations artifactToscaOperation) {
212 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
213 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
214 this.artifactCassandraDao = artifactCassandraDao;
215 this.toscaExportUtils = toscaExportUtils;
216 this.csarUtils = csarUtils;
217 this.lifecycleBusinessLogic = lifecycleBusinessLogic;
218 this.userBusinessLogic = userBusinessLogic;
219 this.artifactsResolver = artifactsResolver;
222 public enum ArtifactOperationEnum {
223 CREATE, UPDATE, DELETE, DOWNLOAD, LINK;
225 public static boolean isCreateOrLink(ArtifactOperationEnum operation) {
226 return operation == CREATE || operation == LINK;
230 public class ArtifactOperationInfo {
232 private ArtifactOperationEnum artifactOperationEnum;
233 private boolean isExternalApi;
234 private boolean ignoreLifecycleState;
236 public ArtifactOperationInfo(boolean isExternalApi, boolean ignoreLifecycleState, ArtifactOperationEnum artifactOperationEnum) {
237 this.artifactOperationEnum = artifactOperationEnum;
238 this.isExternalApi = isExternalApi;
239 this.ignoreLifecycleState = ignoreLifecycleState;
242 public boolean isExternalApi() {
243 return isExternalApi;
246 public boolean ignoreLifecycleState() {
247 return ignoreLifecycleState;
250 public ArtifactOperationEnum getArtifactOperationEnum() {
251 return artifactOperationEnum;
257 public Either<ArtifactDefinition, Operation> handleArtifactRequest(String componentId, String userId, ComponentTypeEnum componentType,
258 ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo,
259 String origMd5, String originData, String interfaceName, String operationName,
260 String parentId, String containerComponentType, boolean shouldLock, boolean inTransaction) {
262 // step 1 - detect auditing type
263 AuditingActionEnum auditingAction = detectAuditingType(operation, origMd5);
264 // step 2 - check header
265 if (userId == null) {
266 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
267 log.debug("handleArtifactRequest - no HTTP_CSP_HEADER , component id {}", componentId);
268 handleAuditing(auditingAction, null, componentId, null, null, null, artifactId, responseFormat, componentType, null);
269 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
271 // step 3 - check user existence
272 // step 4 - check user's role
273 User user = validateUserExists(userId, auditingAction, componentId, artifactId, componentType, inTransaction);
274 validateUserRole(user, auditingAction, componentId, artifactId, componentType, operation);
277 // 5. check service/resource existence
278 // 6. check service/resource check out
279 // 7. user is owner of checkout state
280 Component component = null;
281 String realComponentId = componentType == ComponentTypeEnum.RESOURCE_INSTANCE ? parentId : componentId;
282 component = validateComponentExists(realComponentId, auditingAction, user, artifactId, componentType, containerComponentType);
283 validateWorkOnComponent(component, userId, auditingAction, user, artifactId, operation);
284 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
285 validateResourceInstanceById(component, componentId);
289 return validateAndHandleArtifact(componentId, componentType, operation, artifactId, artifactInfo, origMd5,
290 originData, interfaceName, operationName, user, component, shouldLock, inTransaction, true);
293 public Either<ArtifactDefinition, Operation> handleArtifactRequest(String componentId, String userId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo,
294 String origMd5, String originData, String interfaceName, String operationName, String parentId, String containerComponentType) {
295 return handleArtifactRequest(componentId, userId, componentType, operation, artifactId, artifactInfo, origMd5, originData, interfaceName, operationName, parentId, containerComponentType, true, false);
299 * This Method validates only the Artifact and does not validate user / role / component ect...<br>
300 * For regular usage use <br>
301 * {@link #handleArtifactRequest(String, String, ComponentTypeEnum, ArtifactOperationInfo, String, ArtifactDefinition, String, String, String, String, String, String)}
305 public Either<ArtifactDefinition, Operation> validateAndHandleArtifact(
306 String componentUniqueId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactUniqueId,
307 ArtifactDefinition artifactDefinition, String origMd5, String originData, String interfaceName,
308 String operationName, User user, Component component, boolean shouldLock, boolean inTransaction, boolean needUpdateGroup) {
309 AuditingActionEnum auditingAction = detectAuditingType(operation, origMd5);
310 artifactDefinition = validateArtifact(componentUniqueId, componentType, operation,
311 artifactUniqueId, artifactDefinition, auditingAction, user,
312 component, shouldLock, inTransaction);
315 Either<ArtifactDefinition, Operation> result = doAction(componentUniqueId, componentType, operation, artifactUniqueId, artifactDefinition,
316 origMd5, originData, interfaceName, operationName, auditingAction, user, component, shouldLock, inTransaction, needUpdateGroup);
317 //TODO: audit positive action
322 ArtifactDefinition validateArtifact(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, User user,
323 Component component, boolean shouldLock, boolean inTransaction) {
324 ArtifactDefinition artifactInfoToReturn = artifactInfo;
325 ArtifactOperationEnum operationEnum = operation.getArtifactOperationEnum();
326 if (operationEnum == ArtifactOperationEnum.UPDATE || operationEnum == ArtifactOperationEnum.DELETE || operationEnum == ArtifactOperationEnum.DOWNLOAD) {
327 ArtifactDefinition dbArtifact = getArtifactIfBelongsToComponent(componentId, componentType, artifactId, component);
328 if (operationEnum == ArtifactOperationEnum.DOWNLOAD) {
329 artifactInfoToReturn = dbArtifact;
330 handleHeatEnvDownload(componentId, componentType, user, component, dbArtifact, shouldLock, inTransaction);
333 return artifactInfoToReturn;
337 void handleHeatEnvDownload(String componentId, ComponentTypeEnum componentType, User user, Component component,
338 ArtifactDefinition artifactDefinition, boolean shouldLock, boolean inTransaction) {
340 if (artifactDefinition.getArtifactType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV.getType())
341 && ComponentTypeEnum.SERVICE == component.getComponentType()) {
342 ComponentInstance componentInstance = component.getComponentInstances()
344 .filter(p -> p.getUniqueId().equals(componentId))
347 if (componentInstance == null) {
348 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentId,
349 "instance", "Service", component.getName());
351 Map<String, ArtifactDefinition> deploymentArtifacts = componentInstance.getDeploymentArtifacts();
353 ArtifactDefinition heatEnvWithHeatParams = deploymentArtifacts.values()
355 .filter(p -> p.getUniqueId()
356 .equals(artifactDefinition.getUniqueId()))
359 Either<ArtifactDefinition, ResponseFormat> eitherGenerated = generateHeatEnvArtifact(heatEnvWithHeatParams,
360 componentType, component, componentInstance.getName(), user, componentId, shouldLock, inTransaction);
361 if (eitherGenerated.isRight()) {
362 throw new ByResponseFormatComponentException((eitherGenerated.right().value()));
367 private boolean artifactGenerationRequired(Component component, ArtifactDefinition artifactInfo) {
368 boolean needGenerate;
369 needGenerate = artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA && (component.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || component
370 .getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
371 needGenerate = needGenerate || (ComponentTypeEnum.RESOURCE == component.getComponentType() && (artifactInfo.getArtifactType()
372 .equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV
373 .getType()) || isAbstractVfcEmptyCsar((Resource) component, artifactInfo)));
377 private boolean isAbstractVfcEmptyCsar(Resource resource, ArtifactDefinition artifactInfo) {
378 return resource.isAbstract() && artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA && artifactInfo
380 .equals(ArtifactTypeEnum.TOSCA_CSAR.getType()) && StringUtils.isEmpty(artifactInfo.getArtifactChecksum());
383 public Either<ArtifactDefinition, Operation> generateAndSaveToscaArtifact(
384 ArtifactDefinition artifactDefinition, Component component,
385 User user, boolean isInCertificationRequest, boolean shouldLock, boolean inTransaction,
386 boolean fetchTemplatesFromDB) {
387 generateToscaArtifact(component, artifactDefinition, isInCertificationRequest, fetchTemplatesFromDB);
388 byte[] decodedPayload = artifactDefinition.getPayloadData();
389 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
390 artifactDefinition.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
391 return lockComponentAndUpdateArtifact(component.getUniqueId(), artifactDefinition, AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, artifactDefinition.getUniqueId(),
392 user, component.getComponentType(), component, decodedPayload, null, null, shouldLock, inTransaction);
395 private ArtifactDefinition generateToscaArtifact(Component parent, ArtifactDefinition artifactInfo, boolean isInCertificationRequest, boolean fetchTemplatesFromDB) {
396 log.debug("tosca artifact generation");
397 if (ArtifactTypeEnum.TOSCA_CSAR.getType().equals(artifactInfo.getArtifactType())) {
398 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(parent, fetchTemplatesFromDB, isInCertificationRequest);
399 if (generated.isRight()) {
400 ResponseFormat error = generated.right().value();
401 log.debug("Failed to generate tosca csar for component {} error {}", parent.getUniqueId(), error);
402 throw new ByResponseFormatComponentException(error);
404 artifactInfo.setPayload(generated.left().value());
408 Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
409 if (exportComponent.isRight()) {
410 ToscaError toscaError = exportComponent.right().value();
411 log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), toscaError);
412 ActionStatus status = componentsUtils.convertFromToscaError(toscaError);
413 throw new ByActionStatusComponentException(status);
415 log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
416 artifactInfo.setPayloadData(exportComponent.left().value().getMainYaml());
421 private Either<ArtifactDefinition, Operation> doAction(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo, String origMd5,
422 String originData, String interfaceName, String operationName, AuditingActionEnum auditingAction, User user, Component parent, boolean shouldLock, boolean inTransaction, boolean needUpdateGroup) {
423 if (interfaceName != null && operationName != null) {
424 interfaceName = interfaceName.toLowerCase();
425 operationName = operationName.toLowerCase();
428 lockComponent(componentType, artifactId, auditingAction, user, parent);
430 Either<ArtifactDefinition, Operation> result;
431 boolean operationSucceeded = false;
433 switch (operation.getArtifactOperationEnum()) {
435 if (artifactGenerationRequired(parent, artifactInfo)) {
436 result = Either.left(generateNotSavedArtifact(parent, artifactInfo));
438 result = Either.left(handleDownload(componentId, artifactId, componentType, parent));
442 result = Either.left(handleDeleteInternal(componentId, artifactId, componentType, parent));
445 result = handleUpdate(componentId, componentType, operation, artifactId, artifactInfo, null, origMd5, originData, interfaceName, operationName,
446 auditingAction, user, parent, needUpdateGroup);
449 result = handleCreate(componentId, artifactInfo, operation, auditingAction, user, componentType, parent, origMd5, originData, interfaceName,
453 result = Either.left(handleLink(componentId, artifactInfo, componentType, parent));
456 throw new UnsupportedOperationException("In ArtifactsBusinessLogic received illegal operation: " + operation.getArtifactOperationEnum());
458 operationSucceeded = true;
462 handleLockingAndCommit(parent, shouldLock, inTransaction, operationSucceeded);
466 private void lockComponent(ComponentTypeEnum componentType, String artifactId, AuditingActionEnum auditingAction, User user, Component parent) {
468 lockComponent(parent, ARTIFACT_ACTION_LOCK);
469 }catch (ComponentException e){
470 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, null, null, artifactId, e.getResponseFormat(),
471 componentType, null);
477 public Either<ArtifactDefinition, Operation> handleUpdate(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId,
478 ArtifactDefinition artifactInfo, byte[] decodedPayload, String origMd5, String originData, String interfaceName,
479 String operationName, AuditingActionEnum auditingAction, User user, Component parent,
480 boolean needUpdateGroup) {
481 Either<ArtifactDefinition, Operation> result;
482 ArtifactTypeEnum artifactType = validateAndReturnArtifactType(artifactInfo);
483 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE
484 && (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_VOL || artifactType == ArtifactTypeEnum.HEAT_NET || artifactType == ArtifactTypeEnum.HEAT_ENV)) {
485 result = handleUpdateHeatEnvAndHeatMeta(componentId, artifactInfo, auditingAction, artifactId, user, componentType, parent, originData, origMd5, operation);
486 if (needUpdateGroup) {
487 ActionStatus error = updateGroupInstance(artifactInfo, result.left().value(), parent, componentId);
488 if (error != ActionStatus.OK) {
489 throw new ByActionStatusComponentException(error);
493 else if (componentType == ComponentTypeEnum.RESOURCE && artifactType == ArtifactTypeEnum.HEAT_ENV) {
494 result = handleUpdateHeatWithHeatEnvParams(componentId, artifactInfo, auditingAction, componentType, parent, originData, origMd5, operation, needUpdateGroup);
497 if (decodedPayload == null) {
498 decodedPayload = validateInput(componentId, artifactInfo, operation, auditingAction, artifactId, user,
499 componentType, parent, origMd5, originData, interfaceName, operationName);
501 result = updateArtifactFlow(parent, componentId, artifactId, artifactInfo, decodedPayload, componentType, auditingAction, interfaceName, operationName);
502 if (needUpdateGroup && result.isLeft()) {
503 ArtifactDefinition updatedArtifact = result.left().value();
504 updateGroupForHeat(artifactInfo, updatedArtifact, parent);
510 private ArtifactTypeEnum validateAndReturnArtifactType(ArtifactDefinition artifactInfo) {
511 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactInfo.getArtifactType());
512 if (artifactType == null) {
513 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
518 public ActionStatus updateGroupForHeatEnv(ArtifactDefinition artifactInfo, ArtifactDefinition artAfterUpdate, Component parent) {
519 return ActionStatus.OK;
523 public ActionStatus updateGroupForHeat(ArtifactDefinition artifactInfo, ArtifactDefinition artAfterUpdate, Component parent) {
524 List<GroupDefinition> groups = parent.getGroups();
525 if (groups != null && !groups.isEmpty()) {
526 List<GroupDataDefinition> groupToUpdate = groups.stream()
527 .filter(g -> g.getArtifacts() != null && g.getArtifacts()
528 .contains(artifactInfo
530 .collect(Collectors.toList());
531 if (groupToUpdate != null && !groupToUpdate.isEmpty()) {
532 groupToUpdate.forEach(g -> {
533 g.getArtifacts().remove(artifactInfo.getUniqueId());
534 g.getArtifactsUuid().remove(artifactInfo.getArtifactUUID());
535 g.getArtifacts().add(artAfterUpdate.getUniqueId());
536 g.getArtifactsUuid().add(artAfterUpdate.getArtifactUUID());
537 if(!artifactInfo.getArtifactUUID().equals(artAfterUpdate.getArtifactUUID())){
538 g.setGroupUUID(UniqueIdBuilder.generateUUID());
541 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, groupToUpdate);
542 if (status.isRight()) {
543 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
544 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status.right().value()));
548 return ActionStatus.OK;
552 ActionStatus updateGroupForHeat(ArtifactDefinition artifactInfoHeat, ArtifactDefinition artHeatAfterUpdate, ArtifactDefinition artifactInfoHeatE, ArtifactDefinition artHEAfterUpdate, Component parent) {
553 List<GroupDefinition> groups = parent.getGroups();
554 if (groups != null && !groups.isEmpty()) {
555 List<GroupDataDefinition> groupToUpdate = groups.stream()
556 .filter(g -> g.getArtifacts() != null && g.getArtifacts()
557 .contains(artifactInfoHeat
559 .collect(Collectors.toList());
560 if (groupToUpdate != null && !groupToUpdate.isEmpty()) {
561 groupToUpdate.forEach(g -> {
562 g.getArtifacts().remove(artifactInfoHeat.getUniqueId());
563 g.getArtifactsUuid().remove(artifactInfoHeat.getArtifactUUID());
564 g.getArtifacts().remove(artifactInfoHeatE.getUniqueId());
565 g.getArtifacts().add(artHeatAfterUpdate.getUniqueId());
566 g.getArtifactsUuid().add(artHeatAfterUpdate.getArtifactUUID());
567 g.getArtifacts().add(artHEAfterUpdate.getUniqueId());
569 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, groupToUpdate);
570 if (status.isRight()) {
571 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
572 return componentsUtils.convertFromStorageResponse(status.right().value());
576 return ActionStatus.OK;
579 private ActionStatus updateGroupInstance(ArtifactDefinition artifactInfo, ArtifactDefinition artAfterUpdate, Component parent, String parentId) {
580 List<GroupInstance> updatedGroupInstances = new ArrayList<>();
581 List<GroupInstance> groupInstances = null;
582 Optional<ComponentInstance> componentInstOp = parent.getComponentInstances()
584 .filter(ci -> ci.getUniqueId().equals(parentId))
586 if (componentInstOp.isPresent()) {
587 groupInstances = componentInstOp.get().getGroupInstances();
589 if (CollectionUtils.isNotEmpty(groupInstances)) {
590 boolean isUpdated = false;
591 for (GroupInstance groupInstance : groupInstances) {
593 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifacts()) && groupInstance.getGroupInstanceArtifacts()
594 .contains(artifactInfo
596 groupInstance.getGroupInstanceArtifacts().remove(artifactInfo.getUniqueId());
597 groupInstance.getGroupInstanceArtifacts().add(artAfterUpdate.getUniqueId());
600 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifactsUuid()) && groupInstance.getGroupInstanceArtifactsUuid()
601 .contains(artifactInfo
602 .getArtifactUUID())) {
603 groupInstance.getGroupInstanceArtifactsUuid().remove(artifactInfo.getArtifactUUID());
604 groupInstance.getGroupInstanceArtifacts().add(artAfterUpdate.getArtifactUUID());
608 updatedGroupInstances.add(groupInstance);
612 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(parent, parentId, updatedGroupInstances);
613 if (status.isRight()) {
614 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
615 return componentsUtils.convertFromStorageResponse(status.right().value());
617 return ActionStatus.OK;
620 ArtifactDefinition generateNotSavedArtifact(Component parent, ArtifactDefinition artifactInfo) {
621 if (artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA) {
622 return generateToscaArtifact(parent, artifactInfo, false, false);
625 String heatArtifactId = artifactInfo.getGeneratedFromId();
626 Either<ArtifactDefinition, StorageOperationStatus> heatRes = artifactToscaOperation.getArtifactById(parent.getUniqueId(), heatArtifactId);
627 if (heatRes.isRight()) {
628 log.debug("Failed to fetch heat artifact by generated id {} for heat env {}", heatArtifactId, artifactInfo.getUniqueId());
629 throw new StorageException(heatRes.right().value());
631 String generatedPayload = generateHeatEnvPayload(heatRes.left().value());
632 artifactInfo.setPayloadData(generatedPayload);
637 private Either<ArtifactDefinition, Operation> handleUpdateHeatWithHeatEnvParams(String componentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction,
638 ComponentTypeEnum componentType, Component parent, String originData, String origMd5, ArtifactOperationInfo operation,
639 boolean needToUpdateGroup) {
640 Either<ArtifactDefinition, StorageOperationStatus> artifactHeatRes = artifactToscaOperation.getArtifactById(componentId, artifactInfo
641 .getGeneratedFromId());
642 ArtifactDefinition currHeatArtifact = artifactHeatRes.left().value();
643 if (origMd5 != null) {
644 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
645 if (ArrayUtils.isNotEmpty(artifactInfo.getPayloadData())) {
646 handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
647 } else { // duplicate
648 throw new ByActionStatusComponentException(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
651 return updateHeatParams(componentId, artifactInfo, auditingAction, parent, componentType, currHeatArtifact, needToUpdateGroup);
654 private void handleLockingAndCommit(Component parent, boolean shouldLock, boolean inTransaction, boolean actionSucceeded) {
655 if (actionSucceeded) {
657 if (!inTransaction) {
658 janusGraphDao.commit();
662 if (!inTransaction) {
663 janusGraphDao.rollback();
667 graphLockOperation.unlockComponent(parent.getUniqueId(), parent.getComponentType().getNodeType());
671 public ImmutablePair<String, byte[]> handleDownloadToscaModelRequest(Component component, ArtifactDefinition csarArtifact) {
672 if (artifactGenerationRequired(component, csarArtifact)) {
673 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(component, false, false);
675 if (generated.isRight()) {
676 log.debug("Failed to export tosca csar for component {} error {}", component.getUniqueId(), generated.right()
678 throw new ByResponseFormatComponentException(generated.right().value());
680 return new ImmutablePair<>(csarArtifact.getArtifactName(), generated.left().value());
682 return downloadArtifact(csarArtifact);
685 public ImmutablePair<String, byte[]> handleDownloadRequestById(String componentId, String artifactId, String userId, ComponentTypeEnum componentType, String parentId, String containerComponentType) {
686 // perform all validation in common flow
687 Either<ArtifactDefinition, Operation> result = handleArtifactRequest(componentId, userId, componentType, new ArtifactOperationInfo(false, false, ArtifactOperationEnum.DOWNLOAD), artifactId, null, null, null, null,
688 null, parentId, containerComponentType);
689 ArtifactDefinition artifactDefinition;
690 Either<ArtifactDefinition, Operation> insideValue = result;
691 if (insideValue.isLeft()) {
692 artifactDefinition = insideValue.left().value();
695 artifactDefinition = insideValue.right().value().getImplementationArtifact();
697 // for tosca artifacts and heat env on VF level generated on download without saving
698 if (artifactDefinition.getPayloadData() != null) {
699 return (new ImmutablePair<>(artifactDefinition.getArtifactName(), artifactDefinition
702 return downloadArtifact(artifactDefinition);
705 public Map<String, ArtifactDefinition> handleGetArtifactsByType(String containerComponentType, String parentId, ComponentTypeEnum componentType, String componentId, String artifactGroupType, String userId) {
707 // detect auditing type
708 Map<String, ArtifactDefinition> resMap = null;
709 // Either<Map<String, ArtifactDefinition>, ResponseFormat> resultOp = null;
714 if (userId == null) {
715 log.debug("handleGetArtifactsByType - no HTTP_CSP_HEADER , component id {}", componentId);
716 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
719 // check user existence
723 validateUserExists(userId);
725 // 5. check service/resource existence
726 // 6. check service/resource check out
727 // 7. user is owner of checkout state
728 String realComponentId = componentType == ComponentTypeEnum.RESOURCE_INSTANCE ? parentId : componentId;
729 ComponentParametersView componentFilter = new ComponentParametersView();
730 componentFilter.disableAll();
731 componentFilter.setIgnoreArtifacts(false);
732 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
733 componentFilter.setIgnoreComponentInstances(false);
736 Component component = validateComponentExistsByFilter(realComponentId, ComponentTypeEnum
737 .findByParamName(containerComponentType), componentFilter);
738 lockComponent(component, ARTIFACT_ACTION_LOCK);
739 boolean failed = false;
741 ArtifactGroupTypeEnum groupType = ArtifactGroupTypeEnum.findType(artifactGroupType);
743 if (groupType == null) {
744 log.debug("handleGetArtifactsByType - not failed groupType {} , component id {}", artifactGroupType, componentId);
745 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
747 if (groupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
748 List<ArtifactDefinition> list = getDeploymentArtifacts(component, componentType.getNodeType(), componentId);
749 if (list != null && !list.isEmpty()) {
750 resMap = list.stream().collect(Collectors.toMap(ArtifactDataDefinition::getArtifactLabel, Function.identity()));
753 resMap = new HashMap<>();
758 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsMapStatus = getArtifacts(realComponentId, componentType
759 .getNodeType(), groupType, componentId);
760 if (artifactsMapStatus.isRight()) {
761 if (artifactsMapStatus.right().value() != StorageOperationStatus.NOT_FOUND) {
762 log.debug("handleGetArtifactsByType - not failed groupType {} , component id {}", artifactGroupType, componentId);
763 throw new ByActionStatusComponentException(ActionStatus.MISSING_INFORMATION);
766 resMap = new HashMap<>();
770 resMap = artifactsMapStatus.left().value();
774 }catch (ComponentException e){
781 janusGraphDao.rollback();
785 janusGraphDao.commit();
788 componentType = component.getComponentType();
789 NodeTypeEnum nodeType = componentType.getNodeType();
790 graphLockOperation.unlockComponent(component.getUniqueId(), nodeType);
795 private ArtifactDefinition getArtifactIfBelongsToComponent(String componentId, ComponentTypeEnum componentType, String artifactId, Component component) {
796 // check artifact existence
797 Either<ArtifactDefinition, StorageOperationStatus> artifactResult = artifactToscaOperation.getArtifactById(componentId, artifactId,
798 componentType, component.getUniqueId());
799 if (artifactResult.isRight()) {
800 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_ARTIFACT_NOT_FOUND, artifactId, componentId);
802 // verify artifact belongs to component
804 switch (componentType) {
807 found = checkArtifactInComponent(component, artifactId);
809 case RESOURCE_INSTANCE:
810 found = checkArtifactInResourceInstance(component, componentId, artifactId);
816 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_ARTIFACT_NOT_FOUND, artifactId, componentType.name().toLowerCase());
818 return artifactResult.left().value();
821 private Either<ArtifactDefinition, Operation> handleCreate(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction, User user, ComponentTypeEnum componentType,
822 Component parent, String origMd5, String originData, String interfaceType, String operationName) {
823 byte[] decodedPayload = validateInput(componentId, artifactInfo, operation, auditingAction, null, user, componentType, parent, origMd5, originData, interfaceType, operationName);
824 return createArtifact(parent, componentId, artifactInfo, decodedPayload, componentType, auditingAction, interfaceType, operationName);
827 private ArtifactDefinition handleLink(String componentId, ArtifactDefinition artifactInfo, ComponentTypeEnum componentType,
829 ComponentInstance foundInstance = findComponentInstance(componentId, parent);
830 String instanceId = null;
831 if (foundInstance != null) {
832 instanceId = foundInstance.getUniqueId();
834 NodeTypeEnum nodeType = convertParentType(componentType);
835 Either<ArtifactDefinition, StorageOperationStatus> artifactDefinitionEither = artifactToscaOperation.addArtifactToComponent(artifactInfo, parent,
836 nodeType, true, instanceId);
837 if (artifactDefinitionEither.isRight()) {
838 throw new StorageException(artifactDefinitionEither.right().value(), artifactInfo.getArtifactDisplayName());
840 if (generateCustomizationUUIDOnInstance(parent.getUniqueId(), componentId, componentType) != StorageOperationStatus.OK) {
841 throw new StorageException(artifactDefinitionEither.right().value(), artifactInfo.getArtifactDisplayName());
843 return artifactDefinitionEither.left().value();
846 private Either<ArtifactDefinition, Operation> lockComponentAndUpdateArtifact(
847 String parentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, String artifactId,
848 User user, ComponentTypeEnum componentType, Component parent, byte[] decodedPayload, String interfaceType,
849 String operationName, boolean shouldLock, boolean inTransaction) {
851 Either<ArtifactDefinition, Operation> resultOp = null;
852 boolean failed = false;
853 boolean writeAudit = true;
855 lockComponent(parent, shouldLock, ARTIFACT_ACTION_LOCK);
857 resultOp = updateArtifactFlow(parent, parentId, artifactId, artifactInfo, decodedPayload, componentType, auditingAction, interfaceType, operationName);
860 catch (ComponentException ce) {
862 handleAuditing(auditingAction, parent, parentId, user, null, null, artifactId, ce.getResponseFormat(), componentType, null);
867 catch (StorageException se) {
874 unlockComponent(failed, parent, inTransaction);
879 private byte[] validateInput(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction, String artifactId, User user, ComponentTypeEnum componentType,
880 Component parent, String origMd5, String originData, String interfaceType, String operationName) {
881 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
882 return getValidPayload(componentId, artifactInfo, operation, auditingAction, artifactId, user, componentType, parent, interfaceType, operationName);
885 private byte[] getValidPayload(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, AuditingActionEnum auditingAction,
886 String artifactId, User user, ComponentTypeEnum componentType, Component parent, String interfaceType, String operationName) {
888 Either<ArtifactDefinition, ResponseFormat> validateResult = validateInput(componentId, artifactInfo, operation, artifactId, user, interfaceType, operationName, componentType, parent);
889 if (validateResult.isRight()) {
890 ResponseFormat responseFormat = validateResult.right().value();
891 handleAuditing(auditingAction, parent, componentId, user, null, null, artifactId, responseFormat, componentType, null);
892 throw new ByResponseFormatComponentException(responseFormat);
895 Either<byte[], ResponseFormat> payloadEither = handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
896 if (payloadEither.isRight()) {
897 ResponseFormat responseFormat = payloadEither.right().value();
898 handleAuditing(auditingAction, parent, componentId, user, null, null, artifactId, responseFormat, componentType, null);
899 log.debug("Error during handle payload");
900 throw new ByResponseFormatComponentException(responseFormat);
902 // validate heat parameters. this part must be after the parameters are
903 // extracted in "handlePayload"
904 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParameters = validateAndConvertHeatParameters(artifactInfo, artifactInfo
906 if (validateAndConvertHeatParameters.isRight()) {
907 ResponseFormat responseFormat = validateAndConvertHeatParameters.right().value();
908 handleAuditing(auditingAction, parent, componentId, user, artifactInfo, null, artifactId, responseFormat, componentType, null);
909 log.debug("Error during handle payload");
910 throw new ByResponseFormatComponentException(responseFormat);
912 return payloadEither.left().value();
915 public void handleAuditing(AuditingActionEnum auditingActionEnum, Component component, String componentId, User user,
916 ArtifactDefinition artifactDefinition, String prevArtifactUuid, String currentArtifactUuid,
917 ResponseFormat responseFormat, ComponentTypeEnum componentTypeEnum, String resourceInstanceName) {
919 if (componentsUtils.isExternalApiEvent(auditingActionEnum)) {
925 user.setUserId("UNKNOWN");
927 handleInternalAuditEvent(auditingActionEnum, component, componentId, user, artifactDefinition, prevArtifactUuid, currentArtifactUuid, responseFormat, componentTypeEnum, resourceInstanceName);
930 private void handleInternalAuditEvent(AuditingActionEnum auditingActionEnum, Component component, String componentId, User user, ArtifactDefinition artifactDefinition, String prevArtifactUuid, String currentArtifactUuid, ResponseFormat responseFormat, ComponentTypeEnum componentTypeEnum, String resourceInstanceName) {
931 switch (componentTypeEnum) {
933 Resource resource = (Resource) component;
934 if (resource == null) {
935 // In that case, component ID should be instead of name
936 resource = new Resource();
937 resource.setName(componentId);
939 componentsUtils.auditResource(responseFormat, user, resource, resource.getName(), auditingActionEnum,
940 ResourceVersionInfo.newBuilder()
941 .artifactUuid(prevArtifactUuid)
942 .build(), currentArtifactUuid, artifactDefinition);
946 Service service = (Service) component;
947 if (service == null) {
948 // In that case, component ID should be instead of name
949 service = new Service();
950 service.setName(componentId);
952 componentsUtils.auditComponent(responseFormat, user, service, auditingActionEnum, new ResourceCommonInfo(ComponentTypeEnum.SERVICE.getValue()),
953 ResourceVersionInfo.newBuilder()
954 .artifactUuid(prevArtifactUuid)
956 ResourceVersionInfo.newBuilder()
957 .artifactUuid(currentArtifactUuid)
959 null, artifactDefinition, null);
962 case RESOURCE_INSTANCE:
963 if (resourceInstanceName == null) {
964 resourceInstanceName = getResourceInstanceNameFromComponent(component, componentId);
966 componentsUtils.auditComponent(responseFormat, user, component, auditingActionEnum,
967 new ResourceCommonInfo(resourceInstanceName, ComponentTypeEnum.RESOURCE_INSTANCE.getValue()),
968 ResourceVersionInfo.newBuilder()
969 .artifactUuid(prevArtifactUuid)
971 ResourceVersionInfo.newBuilder()
972 .artifactUuid(currentArtifactUuid)
974 null, artifactDefinition, null);
981 private String getResourceInstanceNameFromComponent(Component component, String componentId) {
982 ComponentInstance resourceInstance = component.getComponentInstances()
984 .filter(p -> p.getUniqueId().equals(componentId))
987 String resourceInstanceName = null;
988 if (resourceInstance != null) {
989 resourceInstanceName = resourceInstance.getName();
991 return resourceInstanceName;
994 private void validateMd5(String origMd5, String originData, byte[] payload, ArtifactOperationInfo operation) {
995 if (origMd5 == null) {
996 if (ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum()) && ArrayUtils.isNotEmpty(payload)) {
997 log.debug("Missing md5 header during artifact create");
998 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_MD5);
1001 if (ArrayUtils.isNotEmpty(payload)) {
1002 log.debug("Cannot have payload while md5 header is missing");
1003 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
1006 String encodeBase64Str = GeneralUtility.calculateMD5Base64EncodedByString(originData);
1007 if (!encodeBase64Str.equals(origMd5)) {
1008 log.debug("The calculated md5 is different then the received one");
1009 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_MD5);
1014 private Either<ArtifactDefinition, ResponseFormat> validateInput(String componentId, ArtifactDefinition artifactInfo, ArtifactOperationInfo operation, String artifactId, User user, String interfaceName, String operationName,
1015 ComponentTypeEnum componentType, Component parentComponent) {
1017 ArtifactDefinition currentArtifactInfo = findArtifactOnParentComponent(parentComponent, componentType, componentId, operation, artifactId);
1018 ignoreUnupdateableFieldsInUpdate(operation, artifactInfo, currentArtifactInfo);
1019 validateInformationalArtifact(artifactInfo, parentComponent);
1020 Either<Boolean, ResponseFormat> validateAndSetArtifactname = validateAndSetArtifactName(
1022 if (validateAndSetArtifactname.isRight()) {
1023 return Either.right(validateAndSetArtifactname.right().value());
1025 if (!validateArtifactNameUniqueness(componentId, parentComponent, artifactInfo, componentType)) {
1026 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_EXIST));
1028 if (operationName != null && interfaceName != null) {
1029 operationName = operationName.toLowerCase();
1030 interfaceName = interfaceName.toLowerCase();
1032 Either<ActionStatus, ResponseFormat> logicalNameStatus = handleArtifactLabel(componentId, parentComponent, operation, artifactInfo, operationName, componentType);
1033 if (logicalNameStatus.isRight()) {
1034 return Either.right(logicalNameStatus.right().value());
1036 // This is a patch to block possibility of updating service api fields
1037 // through other artifacts flow
1039 ArtifactGroupTypeEnum artifactGroupType = operationName != null ? ArtifactGroupTypeEnum.LIFE_CYCLE : ArtifactGroupTypeEnum.INFORMATIONAL;
1040 if (!ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum())) {
1041 checkAndSetUnUpdatableFields(user, artifactInfo, currentArtifactInfo, artifactGroupType);
1044 checkCreateFields(user, artifactInfo, artifactGroupType);
1047 composeArtifactId(componentId, artifactId, artifactInfo, interfaceName, operationName);
1048 if (currentArtifactInfo != null) {
1049 artifactInfo.setMandatory(currentArtifactInfo.getMandatory());
1052 // artifactGroupType is not allowed to be updated
1053 if (!ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum())) {
1054 Either<ArtifactDefinition, ResponseFormat> validateGroupType = validateOrSetArtifactGroupType(artifactInfo, currentArtifactInfo);
1055 if (validateGroupType.isRight()) {
1056 return Either.right(validateGroupType.right().value());
1060 NodeTypeEnum parentType = convertParentType(componentType);
1063 boolean isCreate = ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum());
1065 if (isDeploymentArtifact(artifactInfo)) {
1066 validateDeploymentArtifact(parentComponent, componentId, isCreate, artifactInfo, currentArtifactInfo, parentType);
1069 artifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
1072 Either<Boolean, ResponseFormat> descriptionResult = validateAndCleanDescription(artifactInfo);
1073 if (descriptionResult.isRight()) {
1074 return Either.right(descriptionResult.right().value());
1077 if (currentArtifactInfo != null && currentArtifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.SERVICE_API) {
1078 Either<ActionStatus, ResponseFormat> validateServiceApiType = validateArtifactType(user.getUserId(), artifactInfo, parentType);
1079 if (validateServiceApiType.isRight()) {
1080 return Either.right(validateServiceApiType.right().value());
1082 // Change of type is not allowed and should be ignored
1084 artifactInfo.setArtifactType(ARTIFACT_TYPE_OTHER);
1086 Either<Boolean, ResponseFormat> validateUrl = validateAndServiceApiUrl(artifactInfo);
1087 if (validateUrl.isRight()) {
1088 return Either.right(validateUrl.right().value());
1091 Either<Boolean, ResponseFormat> validateUpdate = validateFirstUpdateHasPayload(artifactInfo, currentArtifactInfo);
1092 if (validateUpdate.isRight()) {
1093 log.debug("serviceApi first update cnnot be without payload.");
1094 return Either.right(validateUpdate.right().value());
1098 Either<ActionStatus, ResponseFormat> validateArtifactType = validateArtifactType(user.getUserId(), artifactInfo, parentType);
1099 if (validateArtifactType.isRight()) {
1100 return Either.right(validateArtifactType.right().value());
1102 if (artifactInfo.getApiUrl() != null) {
1103 artifactInfo.setApiUrl(null);
1104 log.error("Artifact URL cannot be set through this API - ignoring");
1107 if (artifactInfo.getServiceApi() != null && artifactInfo.getServiceApi()) {
1108 artifactInfo.setServiceApi(false);
1109 log.error("Artifact service API flag cannot be changed - ignoring");
1113 return Either.left(artifactInfo);
1116 private void ignoreUnupdateableFieldsInUpdate(ArtifactOperationInfo operation, ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifactInfo) {
1117 if (operation.getArtifactOperationEnum() == ArtifactOperationEnum.UPDATE) {
1118 artifactInfo.setArtifactType(currentArtifactInfo.getArtifactType());
1119 artifactInfo.setArtifactGroupType(currentArtifactInfo.getArtifactGroupType());
1120 artifactInfo.setArtifactLabel(currentArtifactInfo.getArtifactLabel());
1124 private ArtifactDefinition findArtifactOnParentComponent(Component parentComponent, ComponentTypeEnum componentType, String parentId, ArtifactOperationInfo operation, String artifactId) {
1126 ArtifactDefinition foundArtifact = null;
1127 if (StringUtils.isNotEmpty(artifactId)) {
1128 foundArtifact = findArtifact(parentComponent, componentType, parentId, artifactId);
1130 if (foundArtifact != null && ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum())) {
1131 log.debug("Artifact {} already exist", artifactId);
1132 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_EXIST, foundArtifact.getArtifactLabel());
1134 if (foundArtifact == null && !ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum())) {
1135 log.debug("The artifact {} was not found on parent {}. ", artifactId, parentId);
1136 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, "");
1138 return foundArtifact;
1141 private ArtifactDefinition findArtifact(Component parentComponent, ComponentTypeEnum componentType, String parentId, String artifactId) {
1142 ArtifactDefinition foundArtifact;
1143 if (parentComponent.getUniqueId().equals(parentId)) {
1144 foundArtifact = artifactsResolver.findArtifactOnComponent(parentComponent, componentType, artifactId);
1147 ComponentInstance instance = findComponentInstance(parentId, parentComponent);
1148 foundArtifact = artifactsResolver.findArtifactOnComponentInstance(instance, artifactId);
1150 return foundArtifact;
1153 private void validateInformationalArtifact(ArtifactDefinition artifactInfo, Component parentComponent) {
1154 ComponentTypeEnum parentComponentType = parentComponent.getComponentType();
1155 ArtifactGroupTypeEnum groupType = artifactInfo.getArtifactGroupType();
1156 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactInfo.getArtifactType());
1157 if (artifactType == null) {
1158 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
1160 else if (parentComponentType == ComponentTypeEnum.RESOURCE && groupType == ArtifactGroupTypeEnum.INFORMATIONAL) {
1161 String artifactTypeName = artifactType.getType();
1162 ResourceTypeEnum parentResourceType = ((Resource) parentComponent).getResourceType();
1163 Map<String, ArtifactTypeConfig> resourceInformationalArtifacts = ConfigurationManager.getConfigurationManager()
1165 .getResourceInformationalArtifacts();
1166 Set<String> validArtifactTypes = resourceInformationalArtifacts.keySet();
1167 if (!validArtifactTypes.contains(artifactTypeName)) {
1168 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactTypeName);
1171 List<String> validResourceType = resourceInformationalArtifacts.get(artifactTypeName)
1172 .getValidForResourceTypes();
1173 if (!validResourceType.contains(parentResourceType.name())) {
1174 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactTypeName);
1180 private NodeTypeEnum convertParentType(ComponentTypeEnum componentType) {
1181 if (componentType == ComponentTypeEnum.RESOURCE) {
1182 return NodeTypeEnum.Resource;
1184 else if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1185 return NodeTypeEnum.ResourceInstance;
1188 return NodeTypeEnum.Service;
1192 // This method is here for backward compatibility - when other parts of the code are cleaned can change to use the internal version
1193 public Either<Either<ArtifactDefinition, Operation>, ResponseFormat> handleDelete(String parentId, String artifactId, User user, AuditingActionEnum auditingAction, ComponentTypeEnum componentType, Component parent,
1194 boolean shouldLock, boolean inTransaction) {
1195 ResponseFormat responseFormat;
1196 boolean operationSucceeded = false;
1198 lockComponent(componentType, artifactId, auditingAction, user, parent);
1201 ArtifactDefinition artifactDefinition = handleDeleteInternal(parentId, artifactId, componentType, parent);
1202 operationSucceeded = true;
1203 return Either.left(Either.left(artifactDefinition));
1205 catch (ComponentException ce) {
1206 responseFormat = componentsUtils.getResponseFormat(ce);
1207 handleAuditing(auditingAction, parent, parentId, user, null, null, artifactId, responseFormat, componentType, null);
1208 return Either.right(responseFormat);
1210 catch (StorageException se) {
1211 responseFormat = componentsUtils.getResponseFormat(se);
1212 handleAuditing(auditingAction, parent, parentId, user, null, null, artifactId, responseFormat, componentType, null);
1213 return Either.right(responseFormat);
1215 handleLockingAndCommit(parent, shouldLock, inTransaction, operationSucceeded);
1219 private ArtifactDefinition handleDeleteInternal(String parentId, String artifactId, ComponentTypeEnum componentType, Component parent) {
1220 NodeTypeEnum parentType = convertParentType(componentType);
1221 log.debug("Going to find the artifact {} on the component {}", artifactId, parent.getUniqueId());
1222 Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> getArtifactRes = findArtifact(artifactId, parent, parentId, componentType);
1223 if (getArtifactRes.isRight()) {
1224 log.debug("Failed to find the artifact {} belonging to {} on the component {}", artifactId, parentId, parent.getUniqueId());
1225 throw new ByActionStatusComponentException(getArtifactRes.right().value(), artifactId);
1227 ArtifactDefinition foundArtifact = getArtifactRes.left().value().getLeft();
1228 ComponentInstance foundInstance = getArtifactRes.left().value().getRight();
1229 String esId = foundArtifact.getEsId();
1230 boolean needToClone = false;
1231 if (StringUtils.isNotEmpty(esId)) {
1232 Either<Boolean, StorageOperationStatus> needCloneRes = null;
1233 needCloneRes = artifactToscaOperation.isCloneNeeded(parent.getUniqueId(), foundArtifact, parentType);
1234 if (needCloneRes.isRight()) {
1235 throw new StorageException(needCloneRes.right().value(), foundArtifact.getArtifactDisplayName());
1236 } else if (log.isDebugEnabled()) {
1237 needToClone = needCloneRes.left().value();
1238 log.debug("handleDelete: clone is needed for deleting {} held by {} in component {} ? {}",
1239 foundArtifact.getArtifactName(), parentType, parent.getUniqueId(), parent.getName(), needCloneRes.left().value());
1242 boolean isNeedToDeleteArtifactFromDB = true;
1243 boolean isDuplicated = false;
1244 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1245 isNeedToDeleteArtifactFromDB = isArtifactOnlyResourceInstanceArtifact(foundArtifact, parent, parentId);
1247 ArtifactDataDefinition updatedArtifact = deleteOrUpdateArtifactOnGraph(parent, parentId, artifactId, parentType, foundArtifact, needToClone);
1248 isDuplicated = updatedArtifact.getDuplicated();
1250 if (!needToClone && !isDuplicated && isNeedToDeleteArtifactFromDB) {
1251 log.debug("Going to delete the artifact {} from the database. ", artifactId);
1252 CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(esId);
1253 if (cassandraStatus != CassandraOperationStatus.OK) {
1254 log.debug("Failed to delete the artifact {} from the database. ", artifactId);
1255 throw new StorageException(convertToStorageOperationStatus(cassandraStatus), foundArtifact.getArtifactDisplayName());
1258 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1259 List<GroupInstance> updatedGroupInstances = getUpdatedGroupInstances(artifactId, foundArtifact, foundInstance.getGroupInstances());
1260 if (CollectionUtils.isNotEmpty(updatedGroupInstances)) {
1261 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(parent, parentId, updatedGroupInstances);
1262 if (status.isRight()) {
1263 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
1264 throw new StorageException(status.right().value(), foundArtifact.getArtifactDisplayName());
1267 StorageOperationStatus status = generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentType);
1268 if (status != StorageOperationStatus.OK) {
1269 log.debug("Failed to generate new customization UUID for the component instance {}. ", parentId);
1270 throw new StorageException(status, foundArtifact.getArtifactDisplayName());
1273 List<GroupDataDefinition> updatedGroups = getUpdatedGroups(artifactId, foundArtifact, parent.getGroups());
1274 if (CollectionUtils.isNotEmpty(updatedGroups)) {
1275 Either<List<GroupDefinition>, StorageOperationStatus> status = toscaOperationFacade.updateGroupsOnComponent(parent, updatedGroups);
1276 if (status.isRight()) {
1277 log.debug(FAILED_UPDATE_GROUPS, parent.getUniqueId());
1278 throw new StorageException(status.right().value(), foundArtifact.getArtifactDisplayName());
1282 return foundArtifact;
1285 private boolean isArtifactOnlyResourceInstanceArtifact(ArtifactDefinition foundArtifact, Component parent, String instanceId) {
1286 Optional<ComponentInstance> componentInstanceOpt = parent.getComponentInstanceById(instanceId);
1287 if (!componentInstanceOpt.isPresent()) {
1288 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "", "", parent.getName());
1290 ComponentInstance foundInstance = componentInstanceOpt.get();
1291 String componentUid = foundInstance.getComponentUid();
1292 Either<Component, StorageOperationStatus> getContainerRes = toscaOperationFacade.getToscaElement(componentUid);
1293 if (getContainerRes.isRight()) {
1294 log.debug("Failed to fetch the container component {}. ", componentUid);
1295 throw new StorageException(getContainerRes.right().value());
1297 Component origComponent = getContainerRes.left().value();
1298 Map<String, ArtifactDefinition> deploymentArtifacts = origComponent.getDeploymentArtifacts();
1299 if (MapUtils.isNotEmpty(deploymentArtifacts)) {
1300 Optional<String> op = deploymentArtifacts.keySet()
1302 .filter(a -> a.equals(foundArtifact.getArtifactLabel()))
1304 if (op.isPresent()) {
1308 Map<String, ArtifactDefinition> artifacts = origComponent.getArtifacts();
1309 if (MapUtils.isNotEmpty(artifacts)) {
1310 Optional<String> op = artifacts.keySet()
1312 .filter(a -> a.equals(foundArtifact.getArtifactLabel()))
1314 if (op.isPresent()) {
1321 private List<GroupDataDefinition> getUpdatedGroups(String artifactId, ArtifactDefinition foundArtifact, List<GroupDefinition> groups) {
1322 List<GroupDataDefinition> updatedGroups = new ArrayList<>();
1323 boolean isUpdated = false;
1324 if (groups != null) {
1325 for (GroupDefinition group : groups) {
1327 if (CollectionUtils.isNotEmpty(group.getArtifacts()) && group.getArtifacts().contains(artifactId)) {
1328 group.getArtifacts().remove(artifactId);
1331 if (CollectionUtils.isNotEmpty(group.getArtifactsUuid()) && group.getArtifactsUuid()
1332 .contains(foundArtifact.getArtifactUUID())) {
1333 group.getArtifactsUuid().remove(foundArtifact.getArtifactUUID());
1337 updatedGroups.add(group);
1341 return updatedGroups;
1344 private List<GroupInstance> getUpdatedGroupInstances(String artifactId, ArtifactDefinition foundArtifact, List<GroupInstance> groupInstances) {
1345 List<GroupInstance> updatedGroupInstances = new ArrayList<>();
1346 if (CollectionUtils.isNotEmpty(groupInstances)) {
1347 boolean isUpdated = false;
1348 for (GroupInstance groupInstance : groupInstances) {
1350 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifacts()) && groupInstance.getGroupInstanceArtifacts().contains(artifactId)) {
1351 groupInstance.getGroupInstanceArtifacts().remove(artifactId);
1354 if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifactsUuid()) && groupInstance.getGroupInstanceArtifactsUuid()
1355 .contains(foundArtifact.getArtifactUUID())) {
1356 groupInstance.getGroupInstanceArtifactsUuid().remove(foundArtifact.getArtifactUUID());
1360 updatedGroupInstances.add(groupInstance);
1364 return updatedGroupInstances;
1367 private ArtifactDataDefinition deleteOrUpdateArtifactOnGraph(Component component, String parentId, String artifactId, NodeTypeEnum parentType, ArtifactDefinition foundArtifact, Boolean cloneIsNeeded) {
1369 Either<ArtifactDataDefinition, StorageOperationStatus> result;
1370 boolean isMandatory = foundArtifact.getMandatory() || foundArtifact.getServiceApi();
1371 String componentId = component.getUniqueId();
1372 String instanceId = componentId.equals(parentId) ? null : parentId;
1374 log.debug("Going to update mandatory artifact {} from the component {}", artifactId, parentId);
1375 resetMandatoryArtifactFields(foundArtifact);
1376 result = artifactToscaOperation.updateArtifactOnGraph(component, foundArtifact, parentType, artifactId, instanceId, true, true);
1378 else if (cloneIsNeeded) {
1379 log.debug("Going to clone artifacts and to delete the artifact {} from the component {}", artifactId, parentId);
1380 result = artifactToscaOperation.deleteArtifactWithCloningOnGraph(componentId, foundArtifact, parentType, instanceId, false);
1383 log.debug("Going to delete the artifact {} from the component {}", artifactId, parentId);
1384 result = artifactToscaOperation.removeArtifactOnGraph(foundArtifact, componentId, instanceId, parentType, false);
1386 if (result.isRight()) {
1387 throw new StorageException(result.right().value(), foundArtifact.getArtifactDisplayName());
1389 return result.left().value();
1392 private Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> findArtifact(String artifactId, Component fetchedContainerComponent, String parentId, ComponentTypeEnum componentType) {
1394 Either<ImmutablePair<ArtifactDefinition, ComponentInstance>, ActionStatus> result = null;
1395 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
1396 ComponentInstance foundInstance = null;
1397 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE && StringUtils.isNotEmpty(parentId)) {
1398 Optional<ComponentInstance> componentInstanceOpt = fetchedContainerComponent.getComponentInstances()
1400 .filter(i -> i.getUniqueId()
1403 if (!componentInstanceOpt.isPresent()) {
1404 result = Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER);
1407 foundInstance = componentInstanceOpt.get();
1408 fetchArtifactsFromInstance(artifactId, artifacts, foundInstance);
1412 fetchArtifactsFromComponent(artifactId, fetchedContainerComponent, artifacts);
1414 if (result == null) {
1415 if (artifacts.containsKey(artifactId)) {
1416 result = Either.left(new ImmutablePair<>(artifacts.get(artifactId), foundInstance));
1419 result = Either.right(ActionStatus.ARTIFACT_NOT_FOUND);
1425 private void fetchArtifactsFromComponent(String artifactId, Component component, Map<String, ArtifactDefinition> artifacts) {
1426 Map<String, ArtifactDefinition> currArtifacts;
1427 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getDeploymentArtifacts())) {
1428 currArtifacts = component.getDeploymentArtifacts()
1431 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, i -> i));
1432 if (MapUtils.isNotEmpty(currArtifacts)) {
1433 artifacts.putAll(currArtifacts);
1436 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getArtifacts())) {
1437 currArtifacts = component.getArtifacts()
1440 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1441 if (MapUtils.isNotEmpty(currArtifacts)) {
1442 artifacts.putAll(currArtifacts);
1445 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(component.getArtifacts())) {
1446 currArtifacts = component.getToscaArtifacts()
1449 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1450 if (MapUtils.isNotEmpty(currArtifacts)) {
1451 artifacts.putAll(currArtifacts);
1456 private void fetchArtifactsFromInstance(String artifactId, Map<String, ArtifactDefinition> artifacts, ComponentInstance instance) {
1457 Map<String, ArtifactDefinition> currArtifacts;
1458 if (MapUtils.isNotEmpty(instance.getDeploymentArtifacts())) {
1459 currArtifacts = instance.getDeploymentArtifacts()
1462 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1463 if (MapUtils.isNotEmpty(currArtifacts)) {
1464 artifacts.putAll(currArtifacts);
1467 if (!artifacts.containsKey(artifactId) && MapUtils.isNotEmpty(instance.getArtifacts())) {
1468 currArtifacts = instance.getArtifacts()
1471 .collect(Collectors.toMap(ArtifactDataDefinition::getUniqueId, Function.identity()));
1472 if (MapUtils.isNotEmpty(currArtifacts)) {
1473 artifacts.putAll(currArtifacts);
1478 private StorageOperationStatus convertToStorageOperationStatus(CassandraOperationStatus cassandraStatus) {
1479 StorageOperationStatus result;
1480 switch (cassandraStatus) {
1482 result = StorageOperationStatus.OK;
1485 result = StorageOperationStatus.NOT_FOUND;
1487 case CLUSTER_NOT_CONNECTED:
1488 case KEYSPACE_NOT_CONNECTED:
1489 result = StorageOperationStatus.CONNECTION_FAILURE;
1492 result = StorageOperationStatus.GENERAL_ERROR;
1498 private void resetMandatoryArtifactFields(ArtifactDefinition fetchedArtifact) {
1499 if (fetchedArtifact != null) {
1500 log.debug("Going to reset mandatory artifact {} fields. ", fetchedArtifact.getUniqueId());
1501 fetchedArtifact.setEsId(null);
1502 fetchedArtifact.setArtifactName(null);
1503 fetchedArtifact.setDescription(null);
1504 fetchedArtifact.setApiUrl(null);
1505 fetchedArtifact.setArtifactChecksum(null);
1506 nodeTemplateOperation.setDefaultArtifactTimeout(fetchedArtifact.getArtifactGroupType(), fetchedArtifact);
1507 fetchedArtifact.setArtifactUUID(null);
1508 long time = System.currentTimeMillis();
1509 fetchedArtifact.setPayloadUpdateDate(time);
1510 fetchedArtifact.setHeatParameters(null);
1511 fetchedArtifact.setHeatParamsUpdateDate(null);
1515 private StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId, ComponentTypeEnum componentType) {
1516 StorageOperationStatus error = StorageOperationStatus.OK;
1517 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1518 log.debug("Need to re-generate customization UUID for instance {}", instanceId);
1519 error = toscaOperationFacade.generateCustomizationUUIDOnInstance(componentId, instanceId);
1524 private ArtifactDefinition handleDownload(String componentId, String artifactId, ComponentTypeEnum componentType,
1526 Either<ArtifactDefinition, StorageOperationStatus> artifactById = artifactToscaOperation.getArtifactById(componentId, artifactId, componentType,
1527 parent.getUniqueId());
1528 if (artifactById.isRight()) {
1529 throw new StorageException(artifactById.right().value());
1531 ArtifactDefinition artifactDefinition = artifactById.left().value();
1532 if (artifactDefinition == null) {
1533 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactId);
1535 return artifactDefinition;
1538 private Either<ActionStatus, ResponseFormat> handleArtifactLabel(String componentId, Component parentComponent, ArtifactOperationInfo operation, ArtifactDefinition artifactInfo, String operationName,
1539 ComponentTypeEnum componentType) {
1541 String artifactLabel = artifactInfo.getArtifactLabel();
1542 if (operationName == null && (artifactInfo.getArtifactLabel() == null || artifactInfo.getArtifactLabel()
1544 BeEcompErrorManager.getInstance()
1545 .logBeMissingArtifactInformationError("Artifact Update / Upload", "artifactLabel");
1546 log.debug("missing artifact logical name for component {}", componentId);
1547 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_LABEL));
1549 if (ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum()) && !artifactInfo.getMandatory()) {
1551 if (operationName != null) {
1552 if (artifactInfo.getArtifactLabel() != null && !operationName.equals(artifactInfo.getArtifactLabel())) {
1553 log.debug("artifact label cannot be set {}", artifactLabel);
1554 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_LOGICAL_NAME_CANNOT_BE_CHANGED));
1557 artifactLabel = operationName;
1560 String displayName = artifactInfo.getArtifactDisplayName();
1561 if (displayName == null || displayName.isEmpty()) {
1562 displayName = artifactLabel;
1564 displayName = ValidationUtils.cleanArtifactDisplayName(displayName);
1565 artifactInfo.setArtifactDisplayName(displayName);
1567 if (!ValidationUtils.validateArtifactLabel(artifactLabel)) {
1568 log.debug("Invalid format form Artifact label : {}", artifactLabel);
1569 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1571 artifactLabel = ValidationUtils.normalizeArtifactLabel(artifactLabel);
1573 if (artifactLabel.isEmpty()) {
1574 log.debug("missing normalized artifact logical name for component {}", componentId);
1575 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_LABEL));
1578 if (!ValidationUtils.validateArtifactLabelLength(artifactLabel)) {
1579 log.debug("Invalid lenght form Artifact label : {}", artifactLabel);
1580 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_LABEL, String
1581 .valueOf(ValidationUtils.ARTIFACT_LABEL_LENGTH)));
1583 if (!validateLabelUniqueness(componentId, parentComponent, artifactLabel, componentType)) {
1584 log.debug("Non unique Artifact label : {}", artifactLabel);
1585 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_EXIST, artifactLabel));
1588 artifactInfo.setArtifactLabel(artifactLabel);
1590 return Either.left(ActionStatus.OK);
1593 private boolean validateLabelUniqueness(String componentId, Component parentComponent, String artifactLabel, ComponentTypeEnum componentType) {
1594 boolean isUnique = true;
1595 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifacts;
1596 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1597 artifacts = artifactToscaOperation.getAllInstanceArtifacts(parentComponent.getUniqueId(), componentId);
1600 artifacts = artifactToscaOperation.getArtifacts(componentId);
1603 if (artifacts.isLeft()) {
1604 for (String label : artifacts.left().value().keySet()) {
1605 if (label.equals(artifactLabel)) {
1611 if (componentType == ComponentTypeEnum.RESOURCE && isUnique) {
1612 isUnique = isUniqueLabelInResourceInterfaces(componentId, artifactLabel);
1617 boolean validateArtifactNameUniqueness(String componentId, Component parentComponent, ArtifactDefinition artifactInfo,
1618 ComponentTypeEnum componentType) {
1619 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifacts = getArtifacts(componentType,
1620 parentComponent, componentId, artifactInfo.getArtifactGroupType());
1621 String artifactName = artifactInfo.getArtifactName();
1622 if (artifacts.isLeft() && Objects.nonNull(artifacts.left().value())){
1623 if (artifacts.left().value().values().stream()
1624 .anyMatch(ad -> artifactName.equals(ad.getArtifactName())
1625 //check whether it is the same artifact we hold (by label)
1626 && !artifactInfo.getArtifactLabel().equals(ad.getArtifactLabel()))){
1630 if (ComponentTypeEnum.RESOURCE == componentType) {
1631 return isUniqueArtifactNameInResourceInterfaces(componentId, artifactName, artifactInfo.getArtifactLabel());
1636 private boolean isUniqueArtifactNameInResourceInterfaces(String componentId, String artifactName, String artifactLabel) {
1637 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> allInterfacesOfResource = interfaceLifecycleOperation
1638 .getAllInterfacesOfResource(componentId, true, true);
1640 if (allInterfacesOfResource.isLeft()){
1641 return allInterfacesOfResource.left().value()
1643 .stream().map(InterfaceDefinition :: getOperationsMap)
1644 .flatMap(map -> map.values().stream())
1645 .map(OperationDataDefinition::getImplementation)
1646 .filter(Objects::nonNull)
1647 .noneMatch(add -> artifactName.equals(add.getArtifactName())
1648 && !artifactLabel.equals(add.getArtifactLabel()));
1653 private boolean isUniqueLabelInResourceInterfaces(String componentId, String artifactLabel) {
1654 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> allInterfacesOfResource = interfaceLifecycleOperation
1655 .getAllInterfacesOfResource(componentId, true, true);
1657 if (allInterfacesOfResource.isLeft()){
1658 return allInterfacesOfResource.left().value()
1660 .stream().map(InterfaceDefinition :: getOperationsMap)
1661 .flatMap(map -> map.values().stream())
1662 .map(OperationDataDefinition::getImplementation)
1663 .filter(Objects::nonNull)
1664 .noneMatch(add -> artifactLabel.equals(add.getArtifactLabel()));
1669 private Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifacts(ComponentTypeEnum componentType, Component parentComponent,
1670 String componentId, ArtifactGroupTypeEnum artifactGroupType) {
1671 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsResponse;
1672 if (componentType == ComponentTypeEnum.RESOURCE_INSTANCE) {
1673 artifactsResponse = artifactToscaOperation.getAllInstanceArtifacts(parentComponent.getUniqueId(), componentId);
1676 artifactsResponse = artifactToscaOperation.getArtifacts(componentId);
1678 if (artifactsResponse.isRight() && artifactsResponse.right().value() == StorageOperationStatus.NOT_FOUND) {
1679 log.debug("failed to retrieve artifacts for {} ", componentId);
1680 return Either.right(artifactsResponse.right().value());
1682 return Either.left(artifactsResponse.left().value().entrySet()
1684 .filter(x -> artifactGroupType == x.getValue().getArtifactGroupType())
1685 .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
1688 // ***************************************************************
1690 private Either<ArtifactDefinition, Operation> createArtifact(Component parent, String parentId, ArtifactDefinition artifactInfo, byte[] decodedPayload,
1691 ComponentTypeEnum componentTypeEnum, AuditingActionEnum auditingActionEnum, String interfaceType, String operationName) {
1693 DAOArtifactData artifactData = createEsArtifactData(artifactInfo, decodedPayload);
1694 if (artifactData == null) {
1695 BeEcompErrorManager.getInstance().logBeDaoSystemError("Upload Artifact");
1696 log.debug("Failed to create artifact object for ES.");
1697 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
1699 ComponentInstance foundInstance = findComponentInstance(parentId, parent);
1700 String instanceId = null;
1701 if (foundInstance != null) {
1702 if (foundInstance.isArtifactExists(artifactInfo.getArtifactGroupType(), artifactInfo.getArtifactLabel())) {
1703 log.debug("Failed to create artifact, already exists");
1704 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_EXIST, artifactInfo.getArtifactLabel());
1706 instanceId = foundInstance.getUniqueId();
1708 // set on graph object id of artifact in ES!
1709 artifactInfo.setEsId(artifactData.getId());
1711 Either<ArtifactDefinition, Operation> operationResult;
1712 if (interfaceType != null && operationName != null) {
1713 // lifecycle artifact
1714 Operation operation = convertToOperation(artifactInfo, operationName);
1715 Either<Operation, StorageOperationStatus> result = interfaceLifecycleOperation.updateInterfaceOperation(parentId, interfaceType, operationName, operation);
1716 if (result.isRight()) {
1717 throw new StorageException(result.right().value());
1719 operationResult = Either.right(result.left().value());
1722 // information/deployment/api artifacts
1723 NodeTypeEnum nodeType = convertParentType(componentTypeEnum);
1724 Either<ArtifactDefinition, StorageOperationStatus> result = artifactToscaOperation.addArtifactToComponent(
1725 artifactInfo, parent, nodeType, true, instanceId);
1726 if (result.isRight()) {
1727 throw new StorageException(result.right().value());
1729 ArtifactDefinition artifactDefinition = result.left().value();
1730 artifactData.setId(artifactDefinition.getEsId());
1731 operationResult = Either.left(artifactDefinition);
1733 if (generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentTypeEnum) != StorageOperationStatus.OK) {
1734 throw new StorageException(generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentTypeEnum));
1737 saveArtifactInCassandra(artifactData, parent, artifactInfo, "", "", auditingActionEnum, componentTypeEnum);
1738 return operationResult;
1741 private ComponentInstance findComponentInstance(String componentInstanceId, Component containerComponent) {
1742 ComponentInstance foundInstance = null;
1743 if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
1744 foundInstance = containerComponent.getComponentInstances()
1746 .filter(i -> i.getUniqueId().equals(componentInstanceId))
1750 return foundInstance;
1753 private void validateDeploymentArtifact(Component parentComponent, String parentId, boolean isCreate, ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact, NodeTypeEnum parentType) {
1755 ArtifactTypeEnum artifactType = getValidArtifactType(artifactInfo);
1756 Map<String, ArtifactTypeConfig> resourceDeploymentArtifacts = fillDeploymentArtifactTypeConf(parentType);
1757 validateDeploymentArtifactTypeIsLegalForParent(artifactInfo, artifactType, resourceDeploymentArtifacts);
1759 validateArtifactTypeNotChanged(artifactInfo, currentArtifact);
1761 if (parentType == NodeTypeEnum.Resource) {
1762 Resource resource = (Resource) parentComponent;
1763 ResourceTypeEnum resourceType = resource.getResourceType();
1764 ArtifactTypeConfig config = resourceDeploymentArtifacts.get(artifactType.getType());
1765 if (config == null) {
1766 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
1768 List<String> myList = config.getValidForResourceTypes();
1769 validateResourceType(resourceType, artifactInfo, myList);
1772 validateFileExtension(() -> getDeploymentArtifactTypeConfig(parentType, artifactType), artifactInfo, parentType, artifactType);
1774 if (NodeTypeEnum.ResourceInstance != parentType) {
1775 String artifactName = artifactInfo.getArtifactName();
1776 if (isCreate || !artifactName.equalsIgnoreCase(currentArtifact.getArtifactName())) {
1777 validateSingleDeploymentArtifactName(artifactName, parentComponent, parentType);
1781 switch (artifactType) {
1785 validateHeatTimeoutValue(isCreate, artifactInfo, currentArtifact);
1788 validateHeatEnvDeploymentArtifact(parentComponent, parentId, artifactInfo, parentType);
1789 artifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
1791 case DCAE_INVENTORY_TOSCA:
1792 case DCAE_INVENTORY_JSON:
1793 case DCAE_INVENTORY_POLICY:
1794 // Validation is done in handle payload.
1795 case DCAE_INVENTORY_DOC:
1796 case DCAE_INVENTORY_BLUEPRINT:
1797 case DCAE_INVENTORY_EVENT:
1798 // No specific validation
1800 artifactInfo.setTimeout(NodeTemplateOperation.NON_HEAT_TIMEOUT);
1806 void validateDeploymentArtifactTypeIsLegalForParent(ArtifactDefinition artifactInfo, ArtifactTypeEnum artifactType, Map<String, ArtifactTypeConfig> resourceDeploymentArtifacts) {
1807 if ((resourceDeploymentArtifacts == null) || !resourceDeploymentArtifacts.containsKey(artifactType.name())) {
1808 log.debug("Artifact Type: {} Not found !", artifactInfo.getArtifactType());
1809 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
1813 private Map<String, ArtifactTypeConfig> fillDeploymentArtifactTypeConf(NodeTypeEnum parentType) {
1814 Map<String, ArtifactTypeConfig> resourceDeploymentArtifacts;
1815 if (parentType == NodeTypeEnum.Resource) {
1816 resourceDeploymentArtifacts = ConfigurationManager.getConfigurationManager()
1818 .getResourceDeploymentArtifacts();
1820 else if (parentType == NodeTypeEnum.ResourceInstance) {
1821 resourceDeploymentArtifacts = ConfigurationManager.getConfigurationManager()
1823 .getResourceInstanceDeploymentArtifacts();
1826 resourceDeploymentArtifacts = ConfigurationManager.getConfigurationManager()
1828 .getServiceDeploymentArtifacts();
1830 return resourceDeploymentArtifacts;
1833 public ArtifactTypeEnum getValidArtifactType(ArtifactDefinition artifactInfo) {
1834 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactInfo.getArtifactType());
1835 if (artifactType == null) {
1836 log.debug("Artifact Type: {} Not found !", artifactInfo.getArtifactType());
1837 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo.getArtifactType());
1839 return artifactType;
1842 private ArtifactTypeConfig getDeploymentArtifactTypeConfig(NodeTypeEnum parentType, ArtifactTypeEnum artifactType) {
1843 ArtifactTypeConfig retConfig = null;
1844 String fileType = artifactType.getType();
1845 if (parentType == NodeTypeEnum.Resource) {
1846 retConfig = ConfigurationManager.getConfigurationManager()
1848 .getResourceDeploymentArtifacts()
1851 else if (parentType == NodeTypeEnum.Service) {
1852 retConfig = ConfigurationManager.getConfigurationManager()
1854 .getServiceDeploymentArtifacts()
1857 else if (parentType == NodeTypeEnum.ResourceInstance) {
1858 retConfig = ConfigurationManager.getConfigurationManager()
1860 .getResourceInstanceDeploymentArtifacts()
1866 private Either<Boolean, ResponseFormat> extractHeatParameters(ArtifactDefinition artifactInfo) {
1867 // extract heat parameters
1868 if (artifactInfo.getPayloadData() != null) {
1869 String heatDecodedPayload = new String(Base64.decodeBase64(artifactInfo.getPayloadData()));
1870 Either<List<HeatParameterDefinition>, ResultStatusEnum> heatParameters = ImportUtils.getHeatParamsWithoutImplicitTypes(heatDecodedPayload, artifactInfo
1871 .getArtifactType());
1872 if (heatParameters.isRight() && (heatParameters.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND)) {
1873 log.info("failed to parse heat parameters ");
1874 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, artifactInfo
1875 .getArtifactType());
1876 return Either.right(responseFormat);
1878 else if (heatParameters.isLeft() && heatParameters.left().value() != null) {
1879 artifactInfo.setListHeatParameters(heatParameters.left().value());
1882 return Either.left(true);
1887 public void validateFileExtension(IDeploymentArtifactTypeConfigGetter deploymentConfigGetter, ArtifactDefinition artifactInfo, NodeTypeEnum parentType, ArtifactTypeEnum artifactType) {
1888 if (parentType != NodeTypeEnum.Resource && parentType != NodeTypeEnum.Service && parentType != NodeTypeEnum.ResourceInstance) {
1889 log.debug("parent type of artifact can be either resource or service");
1890 throw new ByActionStatusComponentException(MISMATCH_BETWEEN_ARTIFACT_TYPE_AND_COMPONENT_TYPE, artifactType.name(), "Service, Resource or ResourceInstance", parentType.getName());
1893 String fileType = artifactType.getType();
1894 ArtifactTypeConfig deploymentAcceptedTypes = deploymentConfigGetter.getDeploymentArtifactConfig();
1895 if (deploymentAcceptedTypes == null) {
1896 log.debug("invalid artifact type {}", fileType);
1897 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, fileType);
1900 * No need to check specific types. In case there are no acceptedTypes in configuration, then any type is accepted.
1903 List<String> acceptedTypes = deploymentAcceptedTypes.getAcceptedTypes();
1904 String artifactName = artifactInfo.getArtifactName();
1905 String fileExtension = GeneralUtility.getFilenameExtension(artifactName);
1906 // Pavel - File extension validation is case-insensitive - Ella,
1908 if (CollectionUtils.isNotEmpty(acceptedTypes) && !acceptedTypes.contains(fileExtension.toLowerCase())) {
1909 log.debug("File extension \"{}\" is not allowed for {} which is of type:{}", fileExtension, artifactName, fileType);
1910 throw new ByActionStatusComponentException(ActionStatus.WRONG_ARTIFACT_FILE_EXTENSION, fileType);
1915 void validateHeatEnvDeploymentArtifact(Component parentComponent, String parentId, ArtifactDefinition artifactInfo, NodeTypeEnum parentType) {
1917 Wrapper<ArtifactDefinition> heatMDWrapper = new Wrapper<>();
1918 Wrapper<byte[]> payloadWrapper = new Wrapper<>();
1920 validateYaml(artifactInfo);
1921 validateHeatExist(parentComponent.getUniqueId(), parentId, heatMDWrapper, artifactInfo,
1922 parentType, parentComponent.getComponentType());
1924 if (!heatMDWrapper.isEmpty()) {
1925 fillArtifactPayload(payloadWrapper, heatMDWrapper.getInnerElement());
1928 if (!heatMDWrapper.isEmpty()) {
1929 validateEnvVsHeat(artifactInfo, heatMDWrapper.getInnerElement(), payloadWrapper.getInnerElement());
1933 public void fillArtifactPayload(Wrapper<byte[]> payloadWrapper, ArtifactDefinition artifactDefinition) {
1934 if (ArrayUtils.isEmpty(artifactDefinition.getPayloadData())) {
1935 Either<DAOArtifactData, CassandraOperationStatus> eitherArtifactData = artifactCassandraDao.getArtifact(artifactDefinition.getEsId());
1936 if (eitherArtifactData.isLeft()) {
1937 byte[] data = eitherArtifactData.left().value().getDataAsArray();
1938 payloadWrapper.setInnerElement(Base64.encodeBase64(data));
1941 log.debug("Error getting payload for artifact:{}", artifactDefinition.getArtifactName());
1942 throw new StorageException(DaoStatusConverter.convertCassandraStatusToStorageStatus(eitherArtifactData.right().value()));
1946 payloadWrapper.setInnerElement(artifactDefinition.getPayloadData());
1950 private void validateEnvVsHeat(ArtifactDefinition envArtifact, ArtifactDefinition heatArtifact, byte[] heatPayloadData) {
1951 String envPayload = new String(Base64.decodeBase64(envArtifact.getPayloadData()));
1952 Map<String, Object> heatEnvToscaJson = (Map<String, Object>) new Yaml().load(envPayload);
1953 String heatDecodedPayload = new String(Base64.decodeBase64(heatPayloadData));
1954 Map<String, Object> heatToscaJson = (Map<String, Object>) new Yaml().load(heatDecodedPayload);
1956 Either<Map<String, Object>, ResultStatusEnum> eitherHeatEnvProperties = ImportUtils.findFirstToscaMapElement(heatEnvToscaJson, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
1957 if (eitherHeatEnvProperties.isRight()) {
1958 log.debug("Invalid heat env format for file:{}", envArtifact.getArtifactName());
1959 throw new ByActionStatusComponentException(ActionStatus.CORRUPTED_FORMAT, "Heat Env");
1961 Either<Map<String, Object>, ResultStatusEnum> eitherHeatProperties = ImportUtils.findFirstToscaMapElement(heatToscaJson, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
1962 if (eitherHeatProperties.isRight()) {
1963 log.debug("Invalid heat format for file:{}", heatArtifact.getArtifactName());
1964 throw new ByActionStatusComponentException(ActionStatus.CORRUPTED_FORMAT, "Heat");
1966 Set<String> heatPropertiesKeys = eitherHeatProperties.left().value().keySet();
1967 Set<String> heatEnvPropertiesKeys = eitherHeatEnvProperties.left().value().keySet();
1968 heatEnvPropertiesKeys.removeAll(heatPropertiesKeys);
1969 if (!heatEnvPropertiesKeys.isEmpty()) {
1970 log.debug("Validation of heat_env for artifact:{} vs heat artifact for artifact :{} failed", envArtifact.getArtifactName(), heatArtifact.getArtifactName());
1971 throw new ByActionStatusComponentException(ActionStatus.MISMATCH_HEAT_VS_HEAT_ENV, envArtifact.getArtifactName(), heatArtifact.getArtifactName());
1975 private void validateYaml(ArtifactDefinition artifactInfo) {
1976 YamlToObjectConverter yamlConverter = new YamlToObjectConverter();
1977 boolean isYamlValid = yamlConverter.isValidYamlEncoded64(artifactInfo.getPayloadData());
1979 log.debug("Yaml is not valid for artifact : {}", artifactInfo.getArtifactName());
1980 throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML, artifactInfo.getArtifactType());
1984 private boolean isValidXml(byte[] xmlToParse) {
1985 boolean isXmlValid = true;
1987 XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
1988 setFeatures(reader);
1989 reader.parse(new InputSource(new ByteArrayInputStream(xmlToParse)));
1991 catch (ParserConfigurationException | IOException | SAXException e) {
1992 log.debug("Xml is invalid : {}", e.getMessage(), e);
1998 private void setFeatures(XMLReader reader) throws SAXNotSupportedException {
2000 reader.setFeature("http://apache.org/xml/features/validation/schema", false);
2001 reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
2003 catch (SAXNotRecognizedException e) {
2004 log.debug("Xml parser couldn't set feature: \"http://apache.org/xml/features/validation/schema\", false", e.getMessage(), e);
2008 private void validateSingleDeploymentArtifactName(String artifactName, Component parentComponent, NodeTypeEnum parentType) {
2009 boolean artifactNameFound = false;
2010 Iterator<ArtifactDefinition> parentDeploymentArtifactsItr = getDeploymentArtifacts(parentComponent, parentType, null)
2013 while (!artifactNameFound && parentDeploymentArtifactsItr.hasNext()) {
2014 artifactNameFound = artifactName.equalsIgnoreCase(parentDeploymentArtifactsItr.next().getArtifactName());
2016 if (artifactNameFound) {
2017 log.debug("Can't upload artifact: {}, because another artifact with this name already exist.", artifactName);
2018 throw new ByActionStatusComponentException(ActionStatus.DEPLOYMENT_ARTIFACT_NAME_ALREADY_EXISTS, parentType.name(),
2019 parentComponent.getName(), artifactName);
2023 private void validateHeatExist(String componentId, String parentRiId, Wrapper<ArtifactDefinition> heatArtifactMDWrapper, ArtifactDefinition heatEnvArtifact, NodeTypeEnum parentType,
2024 ComponentTypeEnum componentType) {
2025 Either<ArtifactDefinition, StorageOperationStatus> res = artifactToscaOperation.getHeatArtifactByHeatEnvId(parentRiId, heatEnvArtifact, parentType, componentId, componentType);
2026 if (res.isRight()) {
2027 throw new ByActionStatusComponentException(ActionStatus.MISSING_HEAT);
2029 heatArtifactMDWrapper.setInnerElement(res.left().value());
2034 void validateHeatTimeoutValue(boolean isCreate, ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2035 log.trace("Started HEAT pre-payload validation for artifact {}", artifactInfo.getArtifactLabel());
2036 // timeout > 0 for HEAT artifacts
2037 Integer timeout = artifactInfo.getTimeout();
2038 if (timeout == null) {
2039 Integer defaultTimeout = isCreate ? NodeTemplateOperation.getDefaultHeatTimeout() : currentArtifact.getTimeout();
2040 artifactInfo.setTimeout(defaultTimeout);
2041 // HEAT artifact but timeout is invalid
2043 else if (timeout < 1) {
2044 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_TIMEOUT);
2046 // US649856 - Allow several HEAT files on Resource
2047 log.trace("Ended HEAT validation for artifact {}", artifactInfo.getArtifactLabel());
2051 void validateResourceType(ResourceTypeEnum resourceType, ArtifactDefinition artifactInfo, List<String> typeList) {
2052 if (typeList == null || !typeList.contains(resourceType.name())) {
2053 String listToString = (typeList != null) ? typeList.toString() : "";
2054 throw new ByActionStatusComponentException(MISMATCH_BETWEEN_ARTIFACT_TYPE_AND_COMPONENT_TYPE, artifactInfo.getArtifactName(),
2055 listToString, resourceType.getValue());
2060 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParameters(ArtifactDefinition artifactInfo, String artifactType) {
2061 if (artifactInfo.getHeatParameters() != null) {
2062 for (HeatParameterDefinition heatParam : artifactInfo.getListHeatParameters()) {
2063 String parameterType = heatParam.getType();
2064 HeatParameterType heatParameterType = HeatParameterType.isValidType(parameterType);
2065 String artifactTypeStr = artifactType != null ? artifactType : ArtifactTypeEnum.HEAT.getType();
2066 if (heatParameterType == null) {
2067 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_HEAT_PARAMETER_TYPE, artifactTypeStr, heatParam
2069 return Either.right(responseFormat);
2072 StorageOperationStatus validateAndUpdateProperty = heatParametersOperation.validateAndUpdateProperty(heatParam);
2073 if (validateAndUpdateProperty != StorageOperationStatus.OK) {
2074 log.debug("Heat parameter {} is invalid. Status is {}", heatParam.getName(), validateAndUpdateProperty);
2075 ActionStatus status = ActionStatus.INVALID_HEAT_PARAMETER_VALUE;
2076 ResponseFormat responseFormat = componentsUtils.getResponseFormat(status, artifactTypeStr, heatParam
2077 .getType(), heatParam.getName());
2078 return Either.right(responseFormat);
2082 return Either.left(artifactInfo);
2085 public List<ArtifactDefinition> getDeploymentArtifacts(Component parentComponent, NodeTypeEnum parentType, String ciId) {
2086 List<ArtifactDefinition> deploymentArtifacts = new ArrayList<>();
2087 if (parentComponent.getDeploymentArtifacts() != null) {
2088 if (NodeTypeEnum.ResourceInstance == parentType && ciId != null) {
2089 Either<ComponentInstance, ResponseFormat> getRI = getRIFromComponent(parentComponent, ciId, null, null, null);
2090 if (getRI.isRight()) {
2091 return deploymentArtifacts;
2093 ComponentInstance ri = getRI.left().value();
2094 if (ri.getDeploymentArtifacts() != null) {
2095 deploymentArtifacts.addAll(ri.getDeploymentArtifacts().values());
2098 else if (parentComponent.getDeploymentArtifacts() != null) {
2099 deploymentArtifacts.addAll(parentComponent.getDeploymentArtifacts().values());
2102 return deploymentArtifacts;
2105 private void checkCreateFields(User user, ArtifactDefinition artifactInfo, ArtifactGroupTypeEnum type) {
2106 // on create if null add informational to current
2107 if (artifactInfo.getArtifactGroupType() == null) {
2108 artifactInfo.setArtifactGroupType(type);
2110 if (artifactInfo.getUniqueId() != null) {
2111 log.error("artifact uniqid cannot be set ignoring");
2113 artifactInfo.setUniqueId(null);
2115 if (artifactInfo.getArtifactRef() != null) {
2116 log.error("artifact ref cannot be set ignoring");
2118 artifactInfo.setArtifactRef(null);
2120 if (artifactInfo.getArtifactRepository() != null) {
2121 log.error("artifact repository cannot be set ignoring");
2123 artifactInfo.setArtifactRepository(null);
2125 if (artifactInfo.getUserIdCreator() != null) {
2126 log.error("creator uuid cannot be set ignoring");
2128 artifactInfo.setArtifactCreator(user.getUserId());
2130 if (artifactInfo.getUserIdLastUpdater() != null) {
2131 log.error("userId of last updater cannot be set ignoring");
2133 artifactInfo.setUserIdLastUpdater(user.getUserId());
2135 if (artifactInfo.getCreatorFullName() != null) {
2136 log.error("creator Full name cannot be set ignoring");
2138 String fullName = user.getFirstName() + " " + user.getLastName();
2139 artifactInfo.setUpdaterFullName(fullName);
2141 if (artifactInfo.getUpdaterFullName() != null) {
2142 log.error("updater Full name cannot be set ignoring");
2144 artifactInfo.setUpdaterFullName(fullName);
2146 if (artifactInfo.getCreationDate() != null) {
2147 log.error("Creation Date cannot be set ignoring");
2149 long time = System.currentTimeMillis();
2150 artifactInfo.setCreationDate(time);
2152 if (artifactInfo.getLastUpdateDate() != null) {
2153 log.error("Last Update Date cannot be set ignoring");
2155 artifactInfo.setLastUpdateDate(time);
2157 if (artifactInfo.getEsId() != null) {
2158 log.error("es id cannot be set ignoring");
2160 artifactInfo.setEsId(null);
2165 private String composeArtifactId(String resourceId, String artifactId, ArtifactDefinition artifactInfo, String interfaceName, String operationName) {
2166 String id = artifactId;
2167 if (artifactId == null || artifactId.isEmpty()) {
2168 String uniqueId = null;
2169 if (interfaceName != null && operationName != null) {
2170 uniqueId = UniqueIdBuilder.buildArtifactByInterfaceUniqueId(resourceId, interfaceName, operationName, artifactInfo
2171 .getArtifactLabel());
2174 uniqueId = UniqueIdBuilder.buildPropertyUniqueId(resourceId, artifactInfo.getArtifactLabel());
2176 artifactInfo.setUniqueId(uniqueId);
2177 artifactInfo.setEsId(uniqueId);
2181 artifactInfo.setUniqueId(artifactId);
2182 artifactInfo.setEsId(artifactId);
2187 private Either<ActionStatus, ResponseFormat> validateArtifactType(String userId, ArtifactDefinition artifactInfo, NodeTypeEnum parentType) {
2188 if (artifactInfo.getArtifactType() == null || artifactInfo.getArtifactType().isEmpty()) {
2189 BeEcompErrorManager.getInstance()
2190 .logBeMissingArtifactInformationError("Artifact Update / Upload", "artifactLabel");
2191 log.debug("Missing artifact type for artifact {}", artifactInfo.getArtifactName());
2192 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_TYPE));
2195 boolean artifactTypeExist = false;
2196 Either<List<ArtifactType>, ActionStatus> allArtifactTypes = null;
2197 ArtifactGroupTypeEnum artifactGroupType = artifactInfo.getArtifactGroupType();
2199 if ((artifactGroupType != null) && artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
2200 allArtifactTypes = getDeploymentArtifactTypes(parentType);
2204 allArtifactTypes = elementOperation.getAllArtifactTypes();
2206 if (allArtifactTypes.isRight()) {
2207 BeEcompErrorManager.getInstance()
2208 .logBeInvalidConfigurationError("Artifact Upload / Update", "artifactTypes", allArtifactTypes
2212 log.debug("Failed to retrieve list of suported artifact types. error: {}", allArtifactTypes.right()
2214 return Either.right(componentsUtils.getResponseFormatByUserId(allArtifactTypes.right().value(), userId));
2217 for (ArtifactType type : allArtifactTypes.left().value()) {
2218 if (type.getName().equalsIgnoreCase(artifactInfo.getArtifactType())) {
2219 artifactInfo.setArtifactType(artifactInfo.getArtifactType().toUpperCase());
2220 artifactTypeExist = true;
2225 if (!artifactTypeExist) {
2226 BeEcompErrorManager.getInstance()
2227 .logBeInvalidTypeError("Artifact Upload / Delete / Update - Not supported artifact type", artifactInfo
2228 .getArtifactType(), "Artifact " + artifactInfo.getArtifactName());
2229 log.debug("Not supported artifact type = {}", artifactInfo.getArtifactType());
2230 return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED, artifactInfo
2231 .getArtifactType()));
2234 return Either.left(ActionStatus.OK);
2237 private Either<List<ArtifactType>, ActionStatus> getDeploymentArtifactTypes(NodeTypeEnum parentType) {
2239 Map<String, ArtifactTypeConfig> deploymentArtifacts ;
2240 List<ArtifactType> artifactTypes = new ArrayList<>();
2242 if (parentType == NodeTypeEnum.Service) {
2243 deploymentArtifacts = ConfigurationManager.getConfigurationManager()
2245 .getServiceDeploymentArtifacts();
2247 else if (parentType == NodeTypeEnum.ResourceInstance) {
2248 deploymentArtifacts = ConfigurationManager.getConfigurationManager()
2250 .getResourceInstanceDeploymentArtifacts();
2253 deploymentArtifacts = ConfigurationManager.getConfigurationManager()
2255 .getResourceDeploymentArtifacts();
2257 if (deploymentArtifacts != null) {
2258 for (String artifactType : deploymentArtifacts.keySet()) {
2259 ArtifactType artifactT = new ArtifactType();
2260 artifactT.setName(artifactType);
2261 artifactTypes.add(artifactT);
2263 return Either.left(artifactTypes);
2266 return Either.right(ActionStatus.GENERAL_ERROR);
2271 private Either<Boolean, ResponseFormat> validateFirstUpdateHasPayload(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2272 if (currentArtifact.getEsId() == null && (artifactInfo.getPayloadData() == null || artifactInfo.getPayloadData().length == 0)) {
2273 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD));
2275 return Either.left(true);
2280 Either<Boolean, ResponseFormat> validateAndSetArtifactName(ArtifactDefinition artifactInfo) {
2281 if (artifactInfo.getArtifactName() == null || artifactInfo.getArtifactName().isEmpty()) {
2282 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_NAME));
2284 String normalizeFileName = ValidationUtils.normalizeFileName(artifactInfo.getArtifactName());
2285 if (normalizeFileName == null || normalizeFileName.isEmpty()) {
2286 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_ARTIFACT_NAME));
2288 artifactInfo.setArtifactName(normalizeFileName);
2290 if (!ValidationUtils.validateArtifactNameLength(artifactInfo.getArtifactName())) {
2291 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_NAME, String.valueOf(ValidationUtils.ARTIFACT_NAME_LENGTH)));
2294 return Either.left(true);
2297 private void validateArtifactTypeNotChanged(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2298 if (StringUtils.isEmpty(artifactInfo.getArtifactType())) {
2299 log.info("artifact type is missing operation ignored");
2300 throw new ByActionStatusComponentException(ActionStatus.MISSING_ARTIFACT_TYPE);
2303 if (!currentArtifact.getArtifactType().equalsIgnoreCase(artifactInfo.getArtifactType())) {
2304 log.info("artifact type cannot be changed operation ignored");
2305 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2309 private Either<ArtifactDefinition, ResponseFormat> validateOrSetArtifactGroupType(ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact) {
2311 if (Objects.nonNull(artifactInfo) && Objects.nonNull(currentArtifact)) {
2312 if (artifactInfo.getArtifactGroupType() == null) {
2313 artifactInfo.setArtifactGroupType(currentArtifact.getArtifactGroupType());
2314 } else if (!currentArtifact.getArtifactGroupType()
2316 .equalsIgnoreCase(artifactInfo.getArtifactGroupType().getType())) {
2317 log.info("artifact group type cannot be changed. operation failed");
2318 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2321 return Either.left(artifactInfo);
2324 private void checkAndSetUnUpdatableFields(User user, ArtifactDefinition artifactInfo, ArtifactDefinition currentArtifact, ArtifactGroupTypeEnum type) {
2326 // on update if null add informational to current
2327 if (currentArtifact.getArtifactGroupType() == null && type != null) {
2328 currentArtifact.setArtifactGroupType(type);
2331 if (artifactInfo.getUniqueId() != null && !currentArtifact.getUniqueId().equals(artifactInfo.getUniqueId())) {
2332 log.error("artifact uniqid cannot be set ignoring");
2334 artifactInfo.setUniqueId(currentArtifact.getUniqueId());
2336 if (artifactInfo.getArtifactRef() != null && !currentArtifact.getArtifactRef()
2337 .equals(artifactInfo.getArtifactRef())) {
2338 log.error("artifact ref cannot be set ignoring");
2340 artifactInfo.setArtifactRef(currentArtifact.getArtifactRef());
2342 if (artifactInfo.getArtifactRepository() != null && !currentArtifact.getArtifactRepository()
2343 .equals(artifactInfo.getArtifactRepository())) {
2344 log.error("artifact repository cannot be set ignoring");
2346 artifactInfo.setArtifactRepository(currentArtifact.getArtifactRepository());
2348 if (artifactInfo.getUserIdCreator() != null && !currentArtifact.getUserIdCreator()
2349 .equals(artifactInfo.getUserIdCreator())) {
2350 log.error("creator uuid cannot be set ignoring");
2352 artifactInfo.setUserIdCreator(currentArtifact.getUserIdCreator());
2354 if (artifactInfo.getArtifactCreator() != null && !currentArtifact.getArtifactCreator()
2355 .equals(artifactInfo.getArtifactCreator())) {
2356 log.error("artifact creator cannot be set ignoring");
2358 artifactInfo.setArtifactCreator(currentArtifact.getArtifactCreator());
2360 if (artifactInfo.getUserIdLastUpdater() != null && !currentArtifact.getUserIdLastUpdater()
2361 .equals(artifactInfo.getUserIdLastUpdater())) {
2362 log.error("userId of last updater cannot be set ignoring");
2364 artifactInfo.setUserIdLastUpdater(user.getUserId());
2366 if (artifactInfo.getCreatorFullName() != null && !currentArtifact.getCreatorFullName()
2367 .equals(artifactInfo.getCreatorFullName())) {
2368 log.error("creator Full name cannot be set ignoring");
2370 artifactInfo.setCreatorFullName(currentArtifact.getCreatorFullName());
2372 if (artifactInfo.getUpdaterFullName() != null && !currentArtifact.getUpdaterFullName()
2373 .equals(artifactInfo.getUpdaterFullName())) {
2374 log.error("updater Full name cannot be set ignoring");
2376 String fullName = user.getFirstName() + " " + user.getLastName();
2377 artifactInfo.setUpdaterFullName(fullName);
2379 if (artifactInfo.getCreationDate() != null && !currentArtifact.getCreationDate()
2380 .equals(artifactInfo.getCreationDate())) {
2381 log.error("Creation Date cannot be set ignoring");
2383 artifactInfo.setCreationDate(currentArtifact.getCreationDate());
2385 if (artifactInfo.getLastUpdateDate() != null && !currentArtifact.getLastUpdateDate()
2386 .equals(artifactInfo.getLastUpdateDate())) {
2387 log.error("Last Update Date cannot be set ignoring");
2389 long time = System.currentTimeMillis();
2390 artifactInfo.setLastUpdateDate(time);
2392 if (artifactInfo.getEsId() != null && !currentArtifact.getEsId().equals(artifactInfo.getEsId())) {
2393 log.error("es id cannot be set ignoring");
2395 artifactInfo.setEsId(currentArtifact.getUniqueId());
2397 if (artifactInfo.getArtifactDisplayName() != null && !currentArtifact.getArtifactDisplayName()
2398 .equals(artifactInfo.getArtifactDisplayName())) {
2399 log.error(" Artifact Display Name cannot be set ignoring");
2401 artifactInfo.setArtifactDisplayName(currentArtifact.getArtifactDisplayName());
2403 if (artifactInfo.getServiceApi() != null && !currentArtifact.getServiceApi()
2404 .equals(artifactInfo.getServiceApi())) {
2405 log.debug("serviceApi cannot be set. ignoring.");
2407 artifactInfo.setServiceApi(currentArtifact.getServiceApi());
2409 if (artifactInfo.getArtifactGroupType() != null && currentArtifact.getArtifactGroupType() != artifactInfo.getArtifactGroupType()) {
2410 log.debug("artifact group cannot be set. ignoring.");
2412 artifactInfo.setArtifactGroupType(currentArtifact.getArtifactGroupType());
2414 artifactInfo.setArtifactVersion(currentArtifact.getArtifactVersion());
2416 if (artifactInfo.getArtifactUUID() != null && !artifactInfo.getArtifactUUID()
2417 .isEmpty() && !currentArtifact.getArtifactUUID()
2418 .equals(artifactInfo.getArtifactUUID())) {
2419 log.debug("artifact UUID cannot be set. ignoring.");
2421 artifactInfo.setArtifactUUID(currentArtifact.getArtifactUUID());
2423 if ((artifactInfo.getHeatParameters() != null) && (currentArtifact.getHeatParameters() != null) && !artifactInfo
2424 .getHeatParameters()
2425 .isEmpty() && !currentArtifact.getHeatParameters().isEmpty()) {
2426 checkAndSetUnupdatableHeatParams(artifactInfo.getListHeatParameters(), currentArtifact.getListHeatParameters());
2430 private void checkAndSetUnupdatableHeatParams(List<HeatParameterDefinition> heatParameters, List<HeatParameterDefinition> currentParameters) {
2432 Map<String, HeatParameterDefinition> currentParametersMap = getMapOfParameters(currentParameters);
2433 for (HeatParameterDefinition parameter : heatParameters) {
2434 HeatParameterDefinition currentParam = currentParametersMap.get(parameter.getUniqueId());
2436 if (currentParam != null) {
2438 if (parameter.getName() != null && !parameter.getName().equalsIgnoreCase(currentParam.getName())) {
2439 log.debug("heat parameter name cannot be updated ({}). ignoring.", parameter.getName());
2440 parameter.setName(currentParam.getName());
2442 if (parameter.getDefaultValue() != null && !parameter.getDefaultValue()
2443 .equalsIgnoreCase(currentParam.getDefaultValue())) {
2444 log.debug("heat parameter defaultValue cannot be updated ({}). ignoring.", parameter.getDefaultValue());
2445 parameter.setDefaultValue(currentParam.getDefaultValue());
2447 if (parameter.getType() != null && !parameter.getType().equalsIgnoreCase(currentParam.getType())) {
2448 log.debug("heat parameter type cannot be updated ({}). ignoring.", parameter.getType());
2449 parameter.setType(currentParam.getType());
2451 if (parameter.getDescription() != null && !parameter.getDescription()
2452 .equalsIgnoreCase(currentParam.getDescription())) {
2453 log.debug("heat parameter description cannot be updated ({}). ignoring.", parameter.getDescription());
2454 parameter.setDescription(currentParam.getDescription());
2457 // check and set current value
2458 if ((parameter.getCurrentValue() == null) && (currentParam.getDefaultValue() != null)) {
2459 log.debug("heat parameter current value is null. set it to default value {}). ignoring.", parameter.getDefaultValue());
2460 parameter.setCurrentValue(currentParam.getDefaultValue());
2466 private Map<String, HeatParameterDefinition> getMapOfParameters(List<HeatParameterDefinition> currentParameters) {
2468 Map<String, HeatParameterDefinition> currentParamsMap = new HashMap<>();
2469 for (HeatParameterDefinition param : currentParameters) {
2470 currentParamsMap.put(param.getUniqueId(), param);
2472 return currentParamsMap;
2475 private Either<Boolean, ResponseFormat> validateAndServiceApiUrl(ArtifactDefinition artifactInfo) {
2476 if (!ValidationUtils.validateStringNotEmpty(artifactInfo.getApiUrl())) {
2477 log.debug("Artifact url cannot be empty.");
2478 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_URL));
2480 artifactInfo.setApiUrl(artifactInfo.getApiUrl().toLowerCase());
2482 if (!ValidationUtils.validateUrl(artifactInfo.getApiUrl())) {
2483 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_SERVICE_API_URL));
2485 if (!ValidationUtils.validateUrlLength(artifactInfo.getApiUrl())) {
2486 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_URL, String.valueOf(ValidationUtils.API_URL_LENGTH)));
2489 return Either.left(true);
2492 private Either<Boolean, ResponseFormat> validateAndCleanDescription(ArtifactDefinition artifactInfo) {
2493 if (artifactInfo.getDescription() == null || artifactInfo.getDescription().isEmpty()) {
2494 log.debug("Artifact description cannot be empty.");
2495 return Either.right(componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_DESCRIPTION));
2497 String description = artifactInfo.getDescription();
2498 description = ValidationUtils.removeNoneUtf8Chars(description);
2499 description = ValidationUtils.normaliseWhitespace(description);
2500 description = ValidationUtils.stripOctets(description);
2501 description = ValidationUtils.removeHtmlTagsOnly(description);
2502 if (!ValidationUtils.validateIsEnglish(description)) {
2503 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2505 if (!ValidationUtils.validateLength(description, ValidationUtils.ARTIFACT_DESCRIPTION_MAX_LENGTH)) {
2506 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, ARTIFACT_DESCRIPTION, String
2507 .valueOf(ValidationUtils.ARTIFACT_DESCRIPTION_MAX_LENGTH)));
2509 artifactInfo.setDescription(description);
2510 return Either.left(true);
2513 private Either<ArtifactDefinition, Operation> updateArtifactFlow(Component parent, String parentId, String artifactId, ArtifactDefinition artifactInfo, byte[] decodedPayload,
2514 ComponentTypeEnum componentType, AuditingActionEnum auditingAction, String interfaceType, String operationName) {
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 String prevArtifactId;
2522 String currArtifactId = artifactId;
2523 ArtifactDefinition artifactDefinition = artifactInfo;
2524 if (interfaceType != null && operationName != null) {
2525 BeEcompErrorManager.getInstance().logBeDaoSystemError(UPDATE_ARTIFACT);
2526 log.debug("Received both interface and operation for update artifact - something is wrong");
2527 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
2529 log.debug("Entry on graph is updated. Update artifact in ES");
2530 // Changing previous and current artifactId for auditing
2531 prevArtifactId = currArtifactId;
2532 currArtifactId = artifactDefinition.getUniqueId();
2534 NodeTypeEnum parentType = convertParentType(componentType);
2536 if (decodedPayload == null) {
2537 if (!artifactDefinition.getMandatory() || artifactDefinition.getEsId() != null) {
2538 Either<DAOArtifactData, CassandraOperationStatus> artifactFromCassandra = artifactCassandraDao.getArtifact(artifactDefinition.getEsId());
2539 if (artifactFromCassandra.isRight()) {
2540 throw new StorageException(artifactFromCassandra.right().value());
2542 // clone data to new artifact
2543 artifactData.setData(artifactFromCassandra.left().value().getData());
2544 artifactData.setId(artifactFromCassandra.left().value().getId());
2546 } else if (artifactDefinition.getEsId() == null) {
2547 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
2548 artifactData.setId(artifactDefinition.getUniqueId());
2551 Either<ArtifactDefinition, StorageOperationStatus> result = artifactToscaOperation.updateArtifactOnResource(artifactInfo,
2552 parent, artifactId, parentType, parentId, true);
2553 if (result.isRight()) {
2554 throw new StorageException(result.right().value());
2556 artifactDefinition = result.left().value();
2557 updateGeneratedIdInHeatEnv(parent, parentId, artifactId, artifactInfo, artifactDefinition, parentType);
2559 StorageOperationStatus storageOperationStatus = generateCustomizationUUIDOnInstance(parent.getUniqueId(), parentId, componentType);
2560 if (storageOperationStatus != StorageOperationStatus.OK) {
2561 throw new StorageException(storageOperationStatus);
2563 if (artifactData.getData() != null) {
2564 if (!artifactDefinition.getDuplicated() || artifactData.getId() == null) {
2565 artifactData.setId(artifactDefinition.getEsId());
2567 saveArtifactInCassandra(artifactData, parent, artifactInfo, currArtifactId, prevArtifactId, auditingAction, componentType);
2569 return Either.left(artifactDefinition);
2572 private Either<Either<ArtifactDefinition, Operation>, ResponseFormat> updateArtifactsFlowForInterfaceOperations(
2573 Component parent, String parentId, String artifactId, ArtifactDefinition artifactInfo, User user,
2574 byte[] decodedPayload, ComponentTypeEnum componentType, AuditingActionEnum auditingAction, String interfaceType,
2575 String operationUuid, DAOArtifactData artifactData, String prevArtifactId, String currArtifactId,
2576 ArtifactDefinition artifactDefinition) {
2577 StorageOperationStatus error;
2578 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> resultOp;
2579 if (decodedPayload == null) {
2580 if (!artifactDefinition.getMandatory() || artifactDefinition.getEsId() != null) {
2581 Either<DAOArtifactData, CassandraOperationStatus> artifactFromCassandra = artifactCassandraDao.getArtifact(artifactDefinition
2583 if (artifactFromCassandra.isRight()) {
2584 log.debug("Failed to get artifact data from ES for artifact id {}", artifactId);
2585 error = DaoStatusConverter.convertCassandraStatusToStorageStatus(artifactFromCassandra.right()
2587 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(error));
2588 handleAuditing(auditingAction, parent, parentId, user, artifactInfo, prevArtifactId, currArtifactId, responseFormat, componentType, null);
2589 resultOp = Either.right(responseFormat);
2592 // clone data to new artifact
2593 artifactData.setData(artifactFromCassandra.left().value().getData());
2594 artifactData.setId(artifactFromCassandra.left().value().getId());
2596 // todo if not exist(first time)
2600 if (artifactDefinition.getEsId() == null) {
2601 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
2602 artifactData.setId(artifactDefinition.getUniqueId());
2605 NodeTypeEnum convertParentType = convertParentType(componentType);
2606 // Set additional fields for artifact
2607 artifactInfo.setArtifactLabel(artifactInfo.getArtifactName());
2608 artifactInfo.setArtifactDisplayName(artifactInfo.getArtifactName());
2610 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactOnResourceEither =
2611 artifactToscaOperation.updateArtifactOnResource(artifactInfo, parent, artifactId, convertParentType, parentId, true);
2612 if(updateArtifactOnResourceEither.isRight()){
2613 log.debug("Failed to persist operation artifact {} in resource, error is {}",artifactInfo.getArtifactName(), updateArtifactOnResourceEither.right().value());
2614 ActionStatus convertedFromStorageResponse = componentsUtils.convertFromStorageResponse(updateArtifactOnResourceEither.right().value());
2615 return Either.right(componentsUtils.getResponseFormat(convertedFromStorageResponse));
2617 if (artifactData.getData() != null) {
2618 CassandraOperationStatus cassandraOperationStatus = artifactCassandraDao.saveArtifact(artifactData);
2619 if(cassandraOperationStatus != CassandraOperationStatus.OK){
2620 log.debug("Failed to persist operation artifact {}, error is {}",artifactInfo.getArtifactName(),cassandraOperationStatus);
2621 StorageOperationStatus storageStatus = DaoStatusConverter.convertCassandraStatusToStorageStatus(cassandraOperationStatus);
2622 ActionStatus convertedFromStorageResponse = componentsUtils.convertFromStorageResponse(storageStatus);
2623 return Either.right(componentsUtils.getResponseFormat(convertedFromStorageResponse));
2627 Either<ArtifactDefinition, ResponseFormat> updateOprEither = updateOperationArtifact(parentId, interfaceType, operationUuid, updateArtifactOnResourceEither.left().value());
2628 if(updateOprEither.isRight()){
2629 return Either.right(updateOprEither.right().value());
2632 return Either.left(Either.left(updateOprEither.left().value()));
2635 private String updateGeneratedIdInHeatEnv(Component parent, String parentId, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType) {
2636 if (NodeTypeEnum.Resource == parentType) {
2637 return updateGeneratedIdInHeatEnv(parent.getDeploymentArtifacts(), parent, parentId, artifactId, artifactInfo, artifactDefinition, parentType, false);
2639 return artifactDefinition.getUniqueId();
2642 private String updateGeneratedIdInHeatEnv(Map<String, ArtifactDefinition> deploymentArtifacts, Component parentComponent, String parentId, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType, boolean isInstanceArtifact) {
2643 String artifactUniqueId;
2644 artifactUniqueId = artifactDefinition.getUniqueId();
2645 String artifactType = artifactInfo.getArtifactType();
2646 if ((ArtifactTypeEnum.HEAT.getType().equalsIgnoreCase(artifactType) ||
2647 ArtifactTypeEnum.HEAT_VOL.getType().equalsIgnoreCase(artifactType) ||
2648 ArtifactTypeEnum.HEAT_NET.getType().equalsIgnoreCase(artifactType))
2649 && !artifactUniqueId.equals(artifactId)) {
2650 // need to update the generated id in heat env
2651 Optional<Entry<String, ArtifactDefinition>> findFirst = deploymentArtifacts.entrySet()
2653 .filter(a -> artifactId.equals(a.getValue().getGeneratedFromId()))
2655 if (findFirst.isPresent()) {
2656 ArtifactDefinition artifactEnvInfo = findFirst.get().getValue();
2657 artifactEnvInfo.setIsFromCsar(artifactDefinition.getIsFromCsar());
2658 artifactEnvInfo.setArtifactChecksum(null);
2659 if (isInstanceArtifact) {
2660 artifactToscaOperation.updateHeatEnvArtifactOnInstance(parentComponent, artifactEnvInfo, artifactId, artifactUniqueId, parentType, parentId);
2662 artifactToscaOperation.updateHeatEnvArtifact(parentComponent, artifactEnvInfo, artifactId, artifactUniqueId, parentType, parentId);
2666 return artifactUniqueId;
2669 private String updateGeneratedIdInHeatEnvOnInstance(ComponentInstance parent, Component parentComponent, String artifactId, ArtifactDefinition artifactInfo, ArtifactDefinition artifactDefinition, NodeTypeEnum parentType) {
2670 return updateGeneratedIdInHeatEnv(parent.getDeploymentArtifacts(), parentComponent, parent.getUniqueId(),artifactId, artifactInfo, artifactDefinition, parentType, true);
2674 private Either<byte[], ResponseFormat> handlePayload(ArtifactDefinition artifactInfo, boolean isArtifactMetadataUpdate) {
2675 log.trace("Starting payload handling");
2676 byte[] payload = artifactInfo.getPayloadData();
2677 byte[] decodedPayload = null;
2679 if (payload != null && payload.length != 0) {
2680 // the generated artifacts were already decoded by the handler
2681 decodedPayload = artifactInfo.getGenerated() ? payload : Base64.decodeBase64(payload);
2682 if (decodedPayload.length == 0) {
2683 log.debug("Failed to decode the payload.");
2684 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
2685 return Either.right(responseFormat);
2688 String checkSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload);
2689 artifactInfo.setArtifactChecksum(checkSum);
2690 log.trace("Calculated checksum, base64 payload: {}, checksum: {}", payload, checkSum);
2692 // Specific payload validations of different types
2693 Either<Boolean, ResponseFormat> result = Either.left(true);
2694 if (isDeploymentArtifact(artifactInfo)) {
2695 log.trace("Starting deployment artifacts payload validation");
2696 String artifactType = artifactInfo.getArtifactType();
2697 String fileExtension = GeneralUtility.getFilenameExtension(artifactInfo.getArtifactName());
2698 PayloadTypeEnum payloadType = ArtifactTypeToPayloadTypeSelector.getPayloadType(artifactType, fileExtension);
2699 Either<Boolean, ActionStatus> isPayloadValid = payloadType.isValid(decodedPayload);
2700 if (isPayloadValid.isRight()) {
2701 ResponseFormat responseFormat = componentsUtils.getResponseFormat(isPayloadValid.right().value(), artifactType);
2702 return Either.right(responseFormat);
2705 if (payloadType.isHeatRelated()) {
2706 log.trace("Payload is heat related so going to extract heat parameters for artifact type {}", artifactType);
2707 result = extractHeatParameters(artifactInfo);
2710 if (result.isRight()) {
2711 return Either.right(result.right().value());
2714 } // null/empty payload is normal if called from metadata update ONLY.
2715 // The validation of whether this is metadata/payload update case is
2716 // currently done separately
2718 if (!isArtifactMetadataUpdate) {
2719 log.debug("In artifact: {} Payload is missing.",artifactInfo.getArtifactName());
2720 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
2721 return Either.right(responseFormat);
2724 log.trace("Ended payload handling");
2725 return Either.left(decodedPayload);
2729 public Either<Operation, ResponseFormat> deleteArtifactByInterface(String resourceId, String userUserId, String artifactId,
2730 boolean inTransaction) {
2731 User user = new User();
2732 user.setUserId(userUserId);
2733 Either<Resource, StorageOperationStatus> parent = toscaOperationFacade.getToscaElement(resourceId, JsonParseFlagEnum.ParseMetadata);
2734 if (parent.isRight()) {
2735 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(parent
2738 return Either.right(responseFormat);
2740 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> handleDelete = handleDelete(resourceId, artifactId, user, AuditingActionEnum.ARTIFACT_DELETE, ComponentTypeEnum.RESOURCE, parent
2743 false, inTransaction);
2744 if (handleDelete.isRight()) {
2745 return Either.right(handleDelete.right().value());
2747 Either<ArtifactDefinition, Operation> result = handleDelete.left().value();
2748 return Either.left(result.right().value());
2752 private Operation convertToOperation(ArtifactDefinition artifactInfo, String operationName) {
2753 Operation op = new Operation();
2754 long time = System.currentTimeMillis();
2755 op.setCreationDate(time);
2757 String artifactName = artifactInfo.getArtifactName();
2758 artifactInfo.setArtifactName(createInterfaceArtifactNameFromOperation(operationName, artifactName));
2760 op.setImplementation(artifactInfo);
2761 op.setLastUpdateDate(time);
2765 private String createInterfaceArtifactNameFromOperation(String operationName, String artifactName) {
2766 String newArtifactName = operationName + "_" + artifactName;
2767 log.trace("converting artifact name {} to {}", artifactName, newArtifactName);
2768 return newArtifactName;
2772 public byte[] downloadRsrcArtifactByNames(String serviceName, String serviceVersion, String resourceName, String resourceVersion, String artifactName) {
2774 // General validation
2775 if (serviceName == null || serviceVersion == null || resourceName == null || resourceVersion == null || artifactName == null) {
2776 log.debug(NULL_PARAMETER);
2777 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2780 // Normalizing artifact name
2781 artifactName = ValidationUtils.normalizeFileName(artifactName);
2783 // Resource validation
2784 Resource resource = validateResourceNameAndVersion(resourceName, resourceVersion);
2785 String resourceId = resource.getUniqueId();
2787 // Service validation
2788 Service validateServiceNameAndVersion = validateServiceNameAndVersion(serviceName, serviceVersion);
2790 Map<String, ArtifactDefinition> artifacts = resource.getDeploymentArtifacts();
2791 if (artifacts == null || artifacts.isEmpty()) {
2792 log.debug("Deployment artifacts of resource {} are not found", resourceId);
2793 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactName);
2796 ArtifactDefinition deploymentArtifact = null;
2798 for (ArtifactDefinition artifactDefinition : artifacts.values()) {
2799 if (artifactDefinition.getArtifactName() != null && artifactDefinition.getArtifactName()
2800 .equals(artifactName)) {
2801 log.debug(FOUND_DEPLOYMENT_ARTIFACT, artifactName);
2802 deploymentArtifact = artifactDefinition;
2807 if (deploymentArtifact == null) {
2808 log.debug("No deployment artifact {} was found for resource {}", artifactName, resourceId);
2809 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactName);
2812 // Downloading the artifact
2813 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(deploymentArtifact);
2814 log.trace("Download of resource artifact succeeded, uniqueId {}", deploymentArtifact.getUniqueId());
2815 return downloadArtifactEither.getRight();
2819 public byte[] downloadRsrcInstArtifactByNames(String serviceName, String serviceVersion, String resourceInstanceName, String artifactName) {
2821 // General validation
2822 if (serviceName == null || serviceVersion == null || resourceInstanceName == null || artifactName == null) {
2823 log.debug(NULL_PARAMETER);
2824 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2827 // Normalizing artifact name
2828 artifactName = ValidationUtils.normalizeFileName(artifactName);
2830 // Service validation
2831 Service service = validateServiceNameAndVersion(serviceName, serviceVersion);
2833 // ResourceInstance validation
2834 ComponentInstance resourceInstance = validateResourceInstance(service, resourceInstanceName);
2836 Map<String, ArtifactDefinition> artifacts = resourceInstance.getDeploymentArtifacts();
2838 final String finalArtifactName = artifactName;
2839 Predicate<ArtifactDefinition> filterArtifactByName = p -> p.getArtifactName().equals(finalArtifactName);
2841 ArtifactDefinition deployableArtifact = artifacts==null ? null :
2842 artifacts.values().stream()
2843 .filter(filterArtifactByName)
2847 if (deployableArtifact == null) {
2848 log.debug("Deployment artifact with name {} not found", artifactName);
2849 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, artifactName));
2852 log.debug(FOUND_DEPLOYMENT_ARTIFACT, artifactName);
2853 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(deployableArtifact);
2855 log.trace("Download of resource artifact succeeded, uniqueId {}", deployableArtifact.getUniqueId());
2856 return downloadArtifactEither.getRight();
2859 private ComponentInstance validateResourceInstance(Service service, String resourceInstanceName) {
2861 List<ComponentInstance> riList = service.getComponentInstances();
2862 for (ComponentInstance ri : riList) {
2863 if (ri.getNormalizedName().equals(resourceInstanceName)) {
2867 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND, resourceInstanceName);
2870 private ComponentInstance validateResourceInstanceById(Component component, String resourceInstanceId) {
2872 List<ComponentInstance> riList = component.getComponentInstances();
2873 for (ComponentInstance ri : riList) {
2874 if (ri.getUniqueId().equals(resourceInstanceId)) {
2878 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceInstanceId);
2881 private Service validateServiceNameAndVersion(String serviceName, String serviceVersion) {
2883 Either<List<Service>, StorageOperationStatus> serviceListBySystemName = toscaOperationFacade.getBySystemName(ComponentTypeEnum.SERVICE, serviceName);
2884 if (serviceListBySystemName.isRight()) {
2885 log.debug("Couldn't fetch any service with name {}", serviceName);
2886 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceListBySystemName
2888 .value(), ComponentTypeEnum.SERVICE), serviceName);
2890 List<Service> serviceList = serviceListBySystemName.left().value();
2891 if (serviceList == null || serviceList.isEmpty()) {
2892 log.debug("Couldn't fetch any service with name {}", serviceName);
2893 throw new ByActionStatusComponentException(ActionStatus.SERVICE_NOT_FOUND, serviceName);
2896 Service foundService = null;
2897 for (Service service : serviceList) {
2898 if (service.getVersion().equals(serviceVersion)) {
2899 log.trace("Found service with version {}", serviceVersion);
2900 foundService = service;
2905 if (foundService == null) {
2906 log.debug("Couldn't find version {} for service {}", serviceVersion, serviceName);
2907 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_VERSION_NOT_FOUND, ComponentTypeEnum.SERVICE
2908 .getValue(), serviceVersion);
2910 return foundService;
2913 private Resource validateResourceNameAndVersion(String resourceName, String resourceVersion) {
2915 Either<Resource, StorageOperationStatus> resourceListBySystemName = toscaOperationFacade.getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion, JsonParseFlagEnum.ParseMetadata);
2916 if (resourceListBySystemName.isRight()) {
2917 log.debug("Couldn't fetch any resource with name {} and version {}. ", resourceName, resourceVersion);
2918 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(resourceListBySystemName
2920 .value()), resourceName);
2922 return resourceListBySystemName.left().value();
2925 /*private Either<Resource, ResponseFormat> validateResourceNameAndVersion(String resourceName, String resourceVersion) {
2927 Either<Resource, StorageOperationStatus> resourceListBySystemName = toscaOperationFacade.getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion, JsonParseFlagEnum.ParseMetadata);
2928 if (resourceListBySystemName.isRight()) {
2929 log.debug("Couldn't fetch any resource with name {} and version {}. ", resourceName, resourceVersion);
2930 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceListBySystemName
2932 .value()), resourceName));
2934 return Either.left(resourceListBySystemName.left().value());
2937 public byte[] downloadServiceArtifactByNames(String serviceName, String serviceVersion, String artifactName) {
2939 log.trace("Starting download of service interface artifact, serviceName {}, serviceVersion {}, artifact name {}", serviceName, serviceVersion, artifactName);
2940 if (serviceName == null || serviceVersion == null || artifactName == null) {
2941 log.debug(NULL_PARAMETER);
2942 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
2945 // Normalizing artifact name
2946 final String normalizedArtifactName = ValidationUtils.normalizeFileName(artifactName);
2948 // Service validation
2949 Service service = validateServiceNameAndVersion(serviceName, serviceVersion);
2950 // Looking for deployment or tosca artifacts
2951 String serviceId = service.getUniqueId();
2953 if (MapUtils.isEmpty(service.getDeploymentArtifacts()) && MapUtils.isEmpty(service.getToscaArtifacts())) {
2954 log.debug("Neither Deployment nor Tosca artifacts of service {} are found", serviceId);
2955 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, normalizedArtifactName);
2958 Optional<ArtifactDefinition> foundArtifactOptl = Optional.empty();
2960 if (!MapUtils.isEmpty(service.getDeploymentArtifacts())) {
2961 foundArtifactOptl = service.getDeploymentArtifacts().values().stream()
2962 // filters artifact by name
2963 .filter(a -> a.getArtifactName().equals(normalizedArtifactName)).findAny();
2965 if ((!foundArtifactOptl.isPresent()) && !MapUtils.isEmpty(service.getToscaArtifacts())) {
2966 foundArtifactOptl = service.getToscaArtifacts().values().stream()
2967 // filters TOSCA artifact by name
2968 .filter(a -> a.getArtifactName().equals(normalizedArtifactName)).findAny();
2970 if (!foundArtifactOptl.isPresent()) {
2971 log.debug("The artifact {} was not found for service {}", normalizedArtifactName, serviceId);
2972 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, normalizedArtifactName);
2974 log.debug(FOUND_DEPLOYMENT_ARTIFACT, normalizedArtifactName);
2975 // Downloading the artifact
2976 ImmutablePair<String, byte[]> downloadArtifactEither = downloadArtifact(foundArtifactOptl
2978 log.trace("Download of service artifact succeeded, uniqueId {}", foundArtifactOptl.get().getUniqueId());
2979 return downloadArtifactEither.getRight();
2982 public ImmutablePair<String, byte[]> downloadArtifact(String parentId, String artifactUniqueId) {
2983 log.trace("Starting download of artifact, uniqueId {}", artifactUniqueId);
2984 Either<ArtifactDefinition, StorageOperationStatus> artifactById = artifactToscaOperation.getArtifactById(parentId, artifactUniqueId);
2985 if (artifactById.isRight()) {
2986 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(artifactById.right().value());
2987 log.debug("Error when getting artifact info by id{}, error: {}", artifactUniqueId, actionStatus);
2988 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatByArtifactId(actionStatus, ""));
2990 ArtifactDefinition artifactDefinition = artifactById.left().value();
2991 if (artifactDefinition == null) {
2992 log.debug("Empty artifact definition returned from DB by artifact id {}", artifactUniqueId);
2993 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, ""));
2996 return downloadArtifact(artifactDefinition);
2999 private boolean checkArtifactInComponent(Component component, String artifactId) {
3000 boolean found = false;
3001 Map<String, ArtifactDefinition> artifactsS = component.getArtifacts();
3002 if (artifactsS != null) {
3003 for (Map.Entry<String, ArtifactDefinition> entry : artifactsS.entrySet()) {
3004 if (entry.getValue().getUniqueId().equals(artifactId)) {
3010 Map<String, ArtifactDefinition> deploymentArtifactsS = component.getDeploymentArtifacts();
3011 if (!found && deploymentArtifactsS != null) {
3012 for (Map.Entry<String, ArtifactDefinition> entry : deploymentArtifactsS.entrySet()) {
3013 if (entry.getValue().getUniqueId().equals(artifactId)) {
3019 Map<String, ArtifactDefinition> toscaArtifactsS = component.getToscaArtifacts();
3020 if (!found && toscaArtifactsS != null) {
3021 for (Map.Entry<String, ArtifactDefinition> entry : toscaArtifactsS.entrySet()) {
3022 if (entry.getValue().getUniqueId().equals(artifactId)) {
3029 Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
3030 if (!found && interfaces != null) {
3031 for (Map.Entry<String, InterfaceDefinition> entry : interfaces.entrySet()) {
3032 Map<String, Operation> operations = entry.getValue().getOperationsMap();
3033 for (Map.Entry<String, Operation> entryOp : operations.entrySet()) {
3034 if (entryOp.getValue().getImplementation() != null && entryOp.getValue()
3035 .getImplementation()
3037 .equals(artifactId)) {
3044 switch (component.getComponentType()) {
3048 Map<String, ArtifactDefinition> apiArtifacts = ((Service) component).getServiceApiArtifacts();
3049 if (!found && apiArtifacts != null) {
3050 for (Map.Entry<String, ArtifactDefinition> entry : apiArtifacts.entrySet()) {
3051 if (entry.getValue().getUniqueId().equals(artifactId)) {
3065 private boolean checkArtifactInResourceInstance(Component component, String resourceInstanceId, String artifactId) {
3067 boolean found = false;
3068 List<ComponentInstance> resourceInstances = component.getComponentInstances();
3069 ComponentInstance resourceInstance = null;
3070 for (ComponentInstance ri : resourceInstances) {
3071 if (ri.getUniqueId().equals(resourceInstanceId)) {
3072 resourceInstance = ri;
3076 if (resourceInstance != null) {
3077 Map<String, ArtifactDefinition> artifacts = resourceInstance.getDeploymentArtifacts();
3078 if (artifacts != null) {
3079 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
3080 if (entry.getValue().getUniqueId().equals(artifactId)) {
3087 artifacts = resourceInstance.getArtifacts();
3088 if (artifacts != null) {
3089 for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
3090 if (entry.getValue().getUniqueId().equals(artifactId)) {
3101 private Component validateComponentExists(String componentId, AuditingActionEnum auditingAction, User user, String artifactId, ComponentTypeEnum componentType,
3102 String containerComponentType) {
3104 ComponentTypeEnum componentForAudit = null == containerComponentType ? componentType : ComponentTypeEnum.findByParamName(containerComponentType);
3105 componentForAudit.getNodeType();
3107 Either<? extends Component, StorageOperationStatus> componentResult = toscaOperationFacade
3108 .getToscaFullElement(componentId);
3110 if (componentResult.isRight()) {
3111 ActionStatus status = componentForAudit == ComponentTypeEnum.RESOURCE ? ActionStatus.RESOURCE_NOT_FOUND : componentForAudit == ComponentTypeEnum.SERVICE ? ActionStatus.SERVICE_NOT_FOUND : ActionStatus.PRODUCT_NOT_FOUND;
3112 ResponseFormat responseFormat = componentsUtils.getResponseFormat(status, componentId);
3113 log.debug("Service not found, serviceId {}", componentId);
3114 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentForAudit, null);
3115 throw new ByActionStatusComponentException(status, componentId);
3117 return componentResult.left().value();
3120 private Boolean validateWorkOnComponent(Component component, String userId, AuditingActionEnum auditingAction, User user, String artifactId, ArtifactOperationInfo operation) {
3121 if (operation.getArtifactOperationEnum() != ArtifactOperationEnum.DOWNLOAD && !operation.ignoreLifecycleState()) {
3123 validateCanWorkOnComponent(component, userId);
3124 }catch (ComponentException e) {
3125 String uniqueId = component.getUniqueId();
3126 log.debug("Service status isn't CHECKOUT or user isn't owner, serviceId {}", uniqueId);
3127 handleAuditing(auditingAction, component, uniqueId, user, null, null, artifactId, e.getResponseFormat(),
3128 component.getComponentType(), null);
3135 private void validateUserRole(User user, AuditingActionEnum auditingAction, String componentId, String artifactId, ComponentTypeEnum componentType, ArtifactOperationInfo operation) {
3137 if (operation.getArtifactOperationEnum() != ArtifactOperationEnum.DOWNLOAD) {
3138 String role = user.getRole();
3139 if (!role.equals(Role.ADMIN.name()) && !role.equals(Role.DESIGNER.name())) {
3140 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
3141 log.debug("addArtifact - user isn't permitted to perform operation, userId {}, role {}", user.getUserId(), role);
3142 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentType, null);
3143 throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
3148 private User validateUserExists(String userId, AuditingActionEnum auditingAction, String componentId, String artifactId, ComponentTypeEnum componentType, boolean inTransaction) {
3151 user = validateUserExists(userId);
3152 } catch(ByResponseFormatComponentException e){
3153 ResponseFormat responseFormat = e.getResponseFormat();
3154 handleComponentException(auditingAction, componentId, artifactId, responseFormat, componentType, userId);
3156 } catch(ByActionStatusComponentException e){
3157 ResponseFormat responseFormat = componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
3158 handleComponentException(auditingAction, componentId, artifactId, responseFormat, componentType, userId);
3164 private void handleComponentException(AuditingActionEnum auditingAction, String componentId, String artifactId,
3165 ResponseFormat responseFormat, ComponentTypeEnum componentType, String userId){
3166 User user = new User();
3167 user.setUserId(userId);
3168 handleAuditing(auditingAction, null, componentId, user, null, null, artifactId, responseFormat, componentType, null);
3171 protected AuditingActionEnum detectAuditingType(ArtifactOperationInfo operation, String origMd5) {
3172 AuditingActionEnum auditingAction = null;
3173 switch (operation.getArtifactOperationEnum()) {
3175 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_UPLOAD_BY_API : AuditingActionEnum.ARTIFACT_UPLOAD;
3178 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_UPLOAD_BY_API : origMd5 == null ? AuditingActionEnum.ARTIFACT_METADATA_UPDATE : AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE;
3181 auditingAction = operation.isExternalApi() ? AuditingActionEnum.ARTIFACT_DELETE_BY_API : AuditingActionEnum.ARTIFACT_DELETE;
3184 auditingAction = operation.isExternalApi() ? AuditingActionEnum.DOWNLOAD_ARTIFACT : AuditingActionEnum.ARTIFACT_DOWNLOAD;
3189 return auditingAction;
3192 private ImmutablePair<String, byte[]> downloadArtifact(ArtifactDefinition artifactDefinition) {
3193 String esArtifactId = artifactDefinition.getEsId();
3194 Either<DAOArtifactData, CassandraOperationStatus> artifactfromES = artifactCassandraDao.getArtifact(esArtifactId);
3195 if (artifactfromES.isRight()) {
3196 CassandraOperationStatus resourceUploadStatus = artifactfromES.right().value();
3197 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
3198 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageResponse);
3199 log.debug("Error when getting artifact from ES, error: {}", actionStatus);
3200 throw new ByActionStatusComponentException(actionStatus, artifactDefinition.getArtifactDisplayName());
3203 DAOArtifactData DAOArtifactData = artifactfromES.left().value();
3204 byte[] data = DAOArtifactData.getDataAsArray();
3206 log.debug("Artifact data from cassandra is null");
3207 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactDefinition.getArtifactDisplayName());
3209 String artifactName = artifactDefinition.getArtifactName();
3210 log.trace("Download of artifact succeeded, uniqueId {}, artifact file name {}", artifactDefinition.getUniqueId(), artifactName);
3211 return new ImmutablePair<>(artifactName, data);
3214 public DAOArtifactData createEsArtifactData(ArtifactDataDefinition artifactInfo, byte[] artifactPayload) {
3215 return new DAOArtifactData(artifactInfo.getEsId(), artifactPayload);
3218 private void saveArtifactInCassandra(DAOArtifactData artifactData, Component parent, ArtifactDefinition artifactInfo,
3219 String currArtifactId, String prevArtifactId, AuditingActionEnum auditingAction, ComponentTypeEnum componentType) {
3220 CassandraOperationStatus resourceUploadStatus = artifactCassandraDao.saveArtifact(artifactData);
3222 if (resourceUploadStatus == CassandraOperationStatus.OK) {
3223 log.debug("Artifact {} was saved in component {}.", artifactData.getId(), parent.getUniqueId());
3224 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
3225 handleAuditing(auditingAction, parent, parent.getUniqueId(), null, artifactInfo, prevArtifactId,
3226 currArtifactId, responseFormat, componentType, null);
3229 BeEcompErrorManager.getInstance().logBeDaoSystemError(UPDATE_ARTIFACT);
3230 log.info(FAILED_SAVE_ARTIFACT);
3231 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3232 handleAuditing(auditingAction, parent, parent.getUniqueId(), null, artifactInfo, prevArtifactId, currArtifactId, responseFormat, componentType, null);
3233 throw new StorageException(resourceUploadStatus);
3237 private boolean isArtifactMetadataUpdate(AuditingActionEnum auditingActionEnum) {
3238 return auditingActionEnum == AuditingActionEnum.ARTIFACT_METADATA_UPDATE;
3241 private boolean isDeploymentArtifact(ArtifactDefinition artifactInfo) {
3242 return ArtifactGroupTypeEnum.DEPLOYMENT == artifactInfo.getArtifactGroupType();
3245 public ArtifactDefinition createArtifactPlaceHolderInfo(String resourceId, String logicalName, Map<String, Object> artifactInfoMap, String userUserId, ArtifactGroupTypeEnum groupType, boolean inTransaction) {
3246 User user = userBusinessLogic.getUser(userUserId, inTransaction);
3247 return createArtifactPlaceHolderInfo(resourceId, logicalName, artifactInfoMap, user, groupType);
3250 public ArtifactDefinition createArtifactPlaceHolderInfo(String resourceId, String logicalName, Map<String, Object> artifactInfoMap, User user, ArtifactGroupTypeEnum groupType) {
3251 ArtifactDefinition artifactInfo = new ArtifactDefinition();
3253 String artifactName = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_DISPLAY_NAME);
3254 String artifactType = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_TYPE);
3255 String artifactDescription = (String) artifactInfoMap.get(ARTIFACT_PLACEHOLDER_DESCRIPTION);
3257 artifactInfo.setArtifactDisplayName(artifactName);
3258 artifactInfo.setArtifactLabel(logicalName.toLowerCase());
3259 artifactInfo.setArtifactType(artifactType);
3260 artifactInfo.setDescription(artifactDescription);
3261 artifactInfo.setArtifactGroupType(groupType);
3262 nodeTemplateOperation.setDefaultArtifactTimeout(groupType, artifactInfo);
3264 setArtifactPlaceholderCommonFields(resourceId, user, artifactInfo);
3266 return artifactInfo;
3269 private void setArtifactPlaceholderCommonFields(String resourceId, User user, ArtifactDefinition artifactInfo) {
3270 String uniqueId = null;
3272 if (resourceId != null) {
3273 uniqueId = UniqueIdBuilder.buildPropertyUniqueId(resourceId.toLowerCase(), artifactInfo.getArtifactLabel()
3275 artifactInfo.setUniqueId(uniqueId);
3277 artifactInfo.setUserIdCreator(user.getUserId());
3278 String fullName = user.getFullName();
3279 artifactInfo.setUpdaterFullName(fullName);
3281 long time = System.currentTimeMillis();
3283 artifactInfo.setCreatorFullName(fullName);
3284 artifactInfo.setCreationDate(time);
3286 artifactInfo.setLastUpdateDate(time);
3287 artifactInfo.setUserIdLastUpdater(user.getUserId());
3289 artifactInfo.setMandatory(true);
3292 public Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifacts(String parentId, NodeTypeEnum parentType, ArtifactGroupTypeEnum groupType, String instanceId) {
3293 return artifactToscaOperation.getArtifacts(parentId, parentType, groupType, instanceId);
3296 public Either<ArtifactDefinition, StorageOperationStatus> addHeatEnvArtifact(ArtifactDefinition artifactHeatEnv, ArtifactDefinition artifact, Component component, NodeTypeEnum parentType, String instanceId) {
3297 return artifactToscaOperation.addHeatEnvArtifact(artifactHeatEnv, artifact, component, parentType, true, instanceId);
3300 private Either<DAOArtifactData, ResponseFormat> createEsHeatEnvArtifactDataFromString(ArtifactDefinition artifactDefinition, String payloadStr) {
3302 byte[] payload = payloadStr.getBytes();
3304 DAOArtifactData artifactData = createEsArtifactData(artifactDefinition, payload);
3305 return Either.left(artifactData);
3309 * @param artifactDefinition
3312 public Either<ArtifactDefinition, ResponseFormat> generateHeatEnvArtifact(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3313 String instanceId, boolean shouldLock, boolean inTransaction) {
3314 String payload = generateHeatEnvPayload(artifactDefinition);
3315 String prevUUID = artifactDefinition.getArtifactUUID();
3316 ArtifactDefinition clonedBeforeGenerate = new ArtifactDefinition(artifactDefinition);
3317 return generateAndSaveHeatEnvArtifact(artifactDefinition, payload, componentType, component, resourceInstanceName, modifier, instanceId, shouldLock, inTransaction)
3319 .bind(artifactDef -> updateArtifactOnGroupInstance(component, instanceId, prevUUID, clonedBeforeGenerate, artifactDef));
3322 public Either<ArtifactDefinition, ResponseFormat> forceGenerateHeatEnvArtifact(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3323 boolean shouldLock, boolean inTransaction, String instanceId) {
3324 String payload = generateHeatEnvPayload(artifactDefinition);
3325 String prevUUID = artifactDefinition.getArtifactUUID();
3326 ArtifactDefinition clonedBeforeGenerate = new ArtifactDefinition(artifactDefinition);
3327 return forceGenerateAndSaveHeatEnvArtifact(artifactDefinition, payload, componentType, component, resourceInstanceName, modifier, instanceId, shouldLock, inTransaction)
3329 .bind(artifactDef -> updateArtifactOnGroupInstance(component, instanceId, prevUUID, clonedBeforeGenerate, artifactDef));
3333 Either<ArtifactDefinition, ResponseFormat> updateArtifactOnGroupInstance(Component component, String instanceId, String prevUUID, ArtifactDefinition clonedBeforeGenerate, ArtifactDefinition updatedArtDef) {
3334 if (prevUUID == null || !prevUUID.equals(updatedArtDef.getArtifactUUID())) {
3335 List<ComponentInstance> componentInstances = component.getComponentInstances();
3336 if (componentInstances != null) {
3337 Optional<ComponentInstance> findFirst = componentInstances.stream()
3338 .filter(ci -> ci.getUniqueId()
3339 .equals(instanceId))
3341 if (findFirst.isPresent()) {
3342 ComponentInstance relevantInst = findFirst.get();
3343 List<GroupInstance> updatedGroupInstances = getUpdatedGroupInstances(updatedArtDef.getUniqueId(), clonedBeforeGenerate, relevantInst
3344 .getGroupInstances());
3346 if (CollectionUtils.isNotEmpty(updatedGroupInstances)) {
3347 updatedGroupInstances.forEach(gi -> {
3348 gi.getGroupInstanceArtifacts().add(updatedArtDef.getUniqueId());
3349 gi.getGroupInstanceArtifactsUuid().add(updatedArtDef.getArtifactUUID());
3351 Either<List<GroupInstance>, StorageOperationStatus> status = toscaOperationFacade.updateGroupInstancesOnComponent(component, instanceId, updatedGroupInstances);
3352 if (status.isRight()) {
3353 log.debug(FAILED_UPDATE_GROUPS, component.getUniqueId());
3354 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils
3355 .convertFromStorageResponse(status.right()
3356 .value()), clonedBeforeGenerate.getArtifactDisplayName());
3357 return Either.right(responseFormat);
3363 return Either.left(updatedArtDef);
3366 private String generateHeatEnvPayload(ArtifactDefinition artifactDefinition) {
3367 List<HeatParameterDefinition> heatParameters = artifactDefinition.getListHeatParameters();
3368 StringBuilder sb = new StringBuilder();
3369 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactHeader());
3370 sb.append("parameters:\n");
3371 if (heatParameters != null) {
3372 heatParameters.sort(Comparator.comparing(HeatParameterDataDefinition::getName));
3374 List<HeatParameterDefinition> empltyHeatValues = new ArrayList<>();
3376 for (HeatParameterDefinition heatParameterDefinition : heatParameters) {
3378 String heatValue = heatParameterDefinition.getCurrentValue();
3379 if (!ValidationUtils.validateStringNotEmpty(heatValue)) {
3380 heatValue = heatParameterDefinition.getDefaultValue();
3381 if (!ValidationUtils.validateStringNotEmpty(heatValue)) {
3382 empltyHeatValues.add(heatParameterDefinition);
3386 HeatParameterType type = HeatParameterType.isValidType(heatParameterDefinition.getType());
3391 .append(heatParameterDefinition.getName())
3394 .append(Boolean.parseBoolean(heatValue))
3399 .append(heatParameterDefinition.getName())
3402 .append(new BigDecimal(heatValue).toPlainString())
3405 case COMMA_DELIMITED_LIST:
3408 .append(heatParameterDefinition.getName())
3415 String value = heatValue;
3416 boolean starts = value.startsWith("\"");
3417 boolean ends = value.endsWith("\"");
3418 if (!(starts && ends)) {
3419 starts = value.startsWith("'");
3420 ends = value.endsWith("'");
3421 if (!(starts && ends)) {
3422 value = "\"" + value + "\"";
3426 .append(heatParameterDefinition.getName())
3436 if (!empltyHeatValues.isEmpty()) {
3437 empltyHeatValues.sort(Comparator.comparing(HeatParameterDataDefinition::getName));
3438 empltyHeatValues.forEach(hv -> {
3439 sb.append(" ").append(hv.getName()).append(":");
3440 HeatParameterType type = HeatParameterType.isValidType(hv.getType());
3441 if (type != null && type == HeatParameterType.STRING && (hv.getCurrentValue() != null && "".equals(hv
3442 .getCurrentValue()) || hv.getDefaultValue() != null && "".equals(hv.getDefaultValue()))) {
3443 sb.append(" \"\"").append("\n");
3446 sb.append(" ").append("\n");
3451 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter());
3454 return sb.toString().replaceAll("\\\\n", "\n");
3458 * @param artifactDefinition
3462 public Either<ArtifactDefinition, ResponseFormat> generateAndSaveHeatEnvArtifact(ArtifactDefinition artifactDefinition, String payload, ComponentTypeEnum componentType, Component component, String resourceInstanceName,
3463 User modifier, String instanceId, boolean shouldLock, boolean inTransaction) {
3464 return generateArtifactPayload(artifactDefinition, componentType, component, resourceInstanceName, modifier, shouldLock, inTransaction, artifactDefinition::getHeatParamsUpdateDate,
3465 () -> createEsHeatEnvArtifactDataFromString(artifactDefinition, payload), instanceId);
3469 public Either<ArtifactDefinition, ResponseFormat> forceGenerateAndSaveHeatEnvArtifact(ArtifactDefinition artifactDefinition, String payload, ComponentTypeEnum componentType, Component component, String resourceInstanceName,
3470 User modifier, String instanceId, boolean shouldLock, boolean inTransaction) {
3471 return generateArtifactPayload(artifactDefinition, componentType, component, resourceInstanceName, modifier, shouldLock, inTransaction, System::currentTimeMillis,
3472 () -> createEsHeatEnvArtifactDataFromString(artifactDefinition, payload), instanceId);
3476 protected Either<ArtifactDefinition, ResponseFormat> generateArtifactPayload(ArtifactDefinition artifactDefinition, ComponentTypeEnum componentType, Component component, String resourceInstanceName, User modifier,
3477 boolean shouldLock, boolean inTransaction, Supplier<Long> payloadUpdateDateGen, Supplier<Either<DAOArtifactData, ResponseFormat>> esDataCreator, String instanceId) {
3479 log.trace("Start generating payload for {} artifact {}", artifactDefinition.getArtifactType(), artifactDefinition
3481 if (artifactDefinition.getPayloadUpdateDate() == null || artifactDefinition.getPayloadUpdateDate() == 0 || artifactDefinition
3482 .getPayloadUpdateDate() <= payloadUpdateDateGen.get()) {
3484 log.trace("Generating payload for {} artifact {}", artifactDefinition.getArtifactType(), artifactDefinition.getEsId());
3485 Either<DAOArtifactData, ResponseFormat> artifactDataRes = esDataCreator.get();
3486 DAOArtifactData artifactData = null;
3488 if (artifactDataRes.isLeft()) {
3489 artifactData = artifactDataRes.left().value();
3492 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3493 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3494 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3495 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3497 return Either.right(artifactDataRes.right().value());
3499 String newCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(artifactData.getDataAsArray());
3501 String esArtifactId = artifactDefinition.getEsId();
3502 Either<DAOArtifactData, CassandraOperationStatus> artifactfromES;
3503 DAOArtifactData DAOArtifactData;
3504 if (esArtifactId != null && !esArtifactId.isEmpty() && artifactDefinition.getPayloadData() == null) {
3505 log.debug("Try to fetch artifact from cassandra with id : {}", esArtifactId);
3506 artifactfromES = artifactCassandraDao.getArtifact(esArtifactId);
3507 if (artifactfromES.isRight()) {
3508 CassandraOperationStatus resourceUploadStatus = artifactfromES.right().value();
3509 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
3510 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageResponse);
3511 log.debug("Error when getting artifact from ES, error: {} esid : {}", actionStatus, esArtifactId);
3512 return Either.right(componentsUtils.getResponseFormatByArtifactId(actionStatus, artifactDefinition.getArtifactDisplayName()));
3514 DAOArtifactData = artifactfromES.left().value();
3515 oldCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(DAOArtifactData.getDataAsArray());
3518 oldCheckSum = artifactDefinition.getArtifactChecksum();
3521 Either<ArtifactDefinition, StorageOperationStatus> updateArifactDefinitionStatus = null;
3525 lockComponent(component, "Update Artifact - lock resource: ");
3526 }catch (ComponentException e){
3527 handleAuditing(AuditingActionEnum.ARTIFACT_METADATA_UPDATE, component, component.getUniqueId(), modifier, null, null, artifactDefinition
3528 .getUniqueId(), e.getResponseFormat(), component.getComponentType(), null);
3533 if (oldCheckSum != null && oldCheckSum.equals(newCheckSum)) {
3535 artifactDefinition.setPayloadUpdateDate(payloadUpdateDateGen.get());
3536 updateArifactDefinitionStatus = artifactToscaOperation.updateArtifactOnResource(artifactDefinition, component
3537 ,artifactDefinition.getUniqueId(), componentType.getNodeType(), instanceId, true);
3538 log.trace("No real update done in payload for {} artifact, updating payloadUpdateDate {}", artifactDefinition
3539 .getArtifactType(), artifactDefinition.getEsId());
3540 if (updateArifactDefinitionStatus.isRight()) {
3541 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils.convertFromStorageResponse(updateArifactDefinitionStatus
3543 .value()), artifactDefinition.getArtifactDisplayName());
3544 log.trace("Failed to update payloadUpdateDate {}", artifactDefinition.getEsId());
3545 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3546 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3547 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3549 return Either.right(responseFormat);
3553 artifactDefinition.getArtifactChecksum();
3554 artifactDefinition.setArtifactChecksum(newCheckSum);
3555 artifactDefinition.setEsId(artifactDefinition.getUniqueId());
3556 log.trace("No real update done in payload for {} artifact, updating payloadUpdateDate {}", artifactDefinition
3557 .getArtifactType(), artifactDefinition.getEsId());
3558 updateArifactDefinitionStatus = artifactToscaOperation.updateArtifactOnResource(artifactDefinition, component,
3559 artifactDefinition.getUniqueId(), componentType.getNodeType(), instanceId, true);
3561 log.trace("Update Payload {}", artifactDefinition.getEsId());
3563 if (updateArifactDefinitionStatus.isLeft()) {
3565 artifactDefinition = updateArifactDefinitionStatus.left().value();
3566 artifactData.setId(artifactDefinition.getUniqueId());
3567 CassandraOperationStatus saveArtifactStatus = artifactCassandraDao.saveArtifact(artifactData);
3569 if (saveArtifactStatus == CassandraOperationStatus.OK) {
3570 if (!inTransaction) {
3571 janusGraphDao.commit();
3573 log.debug("Artifact Saved In cassandra {}", artifactData.getId());
3574 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
3575 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3576 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3577 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3581 if (!inTransaction) {
3582 janusGraphDao.rollback();
3584 log.info("Failed to save artifact {}.", artifactData.getId());
3585 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
3586 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3587 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3588 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3590 return Either.right(responseFormat);
3594 ResponseFormat responseFormat = componentsUtils.getResponseFormatByArtifactId(componentsUtils.convertFromStorageResponse(updateArifactDefinitionStatus
3596 .value()), artifactDefinition.getArtifactDisplayName());
3597 log.debug("Failed To update artifact {}", artifactData.getId());
3598 handleAuditing(AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, component, component.getUniqueId(), modifier, artifactDefinition, artifactDefinition
3599 .getUniqueId(), artifactDefinition.getUniqueId(), responseFormat,
3600 ComponentTypeEnum.RESOURCE_INSTANCE, resourceInstanceName);
3602 return Either.right(responseFormat);
3608 graphLockOperation.unlockComponent(component.getUniqueId(), component.getComponentType()
3614 return Either.left(artifactDefinition);
3618 public Map<String, Object> buildJsonForUpdateArtifact(ArtifactDefinition artifactDef, ArtifactGroupTypeEnum artifactGroupType, List<ArtifactTemplateInfo> updatedRequiredArtifacts) {
3619 return this.buildJsonForUpdateArtifact(artifactDef.getUniqueId(), artifactDef.getArtifactName(), artifactDef.getArtifactType(), artifactGroupType, artifactDef
3620 .getArtifactLabel(), artifactDef.getArtifactDisplayName(),
3621 artifactDef.getDescription(), artifactDef.getPayloadData(), updatedRequiredArtifacts, artifactDef.getListHeatParameters());
3625 public Map<String, Object> buildJsonForUpdateArtifact(String artifactId, String artifactName, String artifactType, ArtifactGroupTypeEnum artifactGroupType, String label, String displayName, String description, byte[] artifactContent,
3626 List<ArtifactTemplateInfo> updatedRequiredArtifacts, List<HeatParameterDefinition> heatParameters) {
3628 Map<String, Object> json = new HashMap<>();
3629 if (artifactId != null && !artifactId.isEmpty()) {
3630 json.put(Constants.ARTIFACT_ID, artifactId);
3633 json.put(Constants.ARTIFACT_NAME, artifactName);
3634 json.put(Constants.ARTIFACT_TYPE, artifactType);
3635 json.put(Constants.ARTIFACT_DESCRIPTION, description);
3637 if (artifactContent != null) {
3638 log.debug("payload is encoded. perform decode");
3639 String encodedPayload = Base64.encodeBase64String(artifactContent);
3640 json.put(Constants.ARTIFACT_PAYLOAD_DATA, encodedPayload);
3642 json.put(Constants.ARTIFACT_DISPLAY_NAME, displayName);
3643 json.put(Constants.ARTIFACT_LABEL, label);
3644 json.put(Constants.ARTIFACT_GROUP_TYPE, artifactGroupType.getType());
3645 json.put(Constants.REQUIRED_ARTIFACTS, (updatedRequiredArtifacts == null || updatedRequiredArtifacts.isEmpty()) ? new ArrayList<>()
3646 : updatedRequiredArtifacts.stream()
3647 .filter(e -> e.getType().equals(ArtifactTypeEnum.HEAT_ARTIFACT.getType()) || e
3649 .equals(ArtifactTypeEnum.HEAT_NESTED.getType()))
3650 .map(ArtifactTemplateInfo::getFileName)
3651 .collect(Collectors.toList()));
3652 json.put(Constants.ARTIFACT_HEAT_PARAMS, (heatParameters == null || heatParameters.isEmpty()) ? new ArrayList<>()
3657 public Either<ArtifactDefinition, Operation> updateResourceInstanceArtifactNoContent(String resourceId, Component containerComponent, User user, Map<String, Object> json, ArtifactOperationInfo operation, ArtifactDefinition artifactInfo) {
3659 String jsonStr = gson.toJson(json);
3660 ArtifactDefinition artifactDefinitionFromJson = artifactInfo == null ? RepresentationUtils.convertJsonToArtifactDefinition(jsonStr, ArtifactDefinition.class, false) : artifactInfo;
3661 String artifactUniqueId = artifactDefinitionFromJson == null ? null : artifactDefinitionFromJson.getUniqueId();
3662 Either<ArtifactDefinition, Operation> uploadArtifactToService = validateAndHandleArtifact(resourceId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactUniqueId,
3663 artifactDefinitionFromJson, null, jsonStr, null, null, user, containerComponent, false, false, true);
3665 return Either.left(uploadArtifactToService.left().value());
3668 private Either<ArtifactDefinition, Operation> handleUpdateHeatEnvAndHeatMeta(String componentId, ArtifactDefinition artifactInfo, AuditingActionEnum auditingAction, String artifactId, User user, ComponentTypeEnum componentType,
3669 Component parent, String originData, String origMd5, ArtifactOperationInfo operation) {
3670 if (origMd5 != null) {
3671 validateMd5(origMd5, originData, artifactInfo.getPayloadData(), operation);
3672 if (ArrayUtils.isNotEmpty(artifactInfo.getPayloadData())) {
3673 validateDeploymentArtifact(parent, componentId, false, artifactInfo, artifactInfo, NodeTypeEnum.ResourceInstance);
3674 handlePayload(artifactInfo, isArtifactMetadataUpdate(auditingAction));
3675 } else { // duplicate
3676 throw new ByActionStatusComponentException(ActionStatus.MISSING_DATA, ARTIFACT_PAYLOAD);
3679 return updateHeatEnvParamsAndMetadata(componentId, artifactId, artifactInfo, user, auditingAction, parent, componentType, origMd5);
3682 private Either<ArtifactDefinition, Operation> updateHeatEnvParamsAndMetadata(String componentId, String artifactId, ArtifactDefinition artifactInfo, User user, AuditingActionEnum auditingAction, Component parent,
3683 ComponentTypeEnum componentType, String origMd5) {
3684 Either<ComponentInstance, ResponseFormat> getRI = getRIFromComponent(parent, componentId, artifactId, auditingAction, user);
3685 if (getRI.isRight()) {
3686 throw new ByResponseFormatComponentException(getRI.right().value());
3688 ComponentInstance ri = getRI.left().value();
3689 Either<ArtifactDefinition, ResponseFormat> getArtifactRes = getArtifactFromRI(parent, ri, componentId, artifactId, auditingAction, user);
3690 if (getArtifactRes.isRight()) {
3691 throw new ByResponseFormatComponentException(getArtifactRes.right().value());
3693 ArtifactDefinition currArtifact = getArtifactRes.left().value();
3695 if (currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT.getType()) ||
3696 currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT_VOL.getType()) ||
3697 currArtifact.getArtifactType().equals(ArtifactTypeEnum.HEAT_NET.getType())) {
3698 throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
3700 List<HeatParameterDefinition> currentHeatEnvParams = currArtifact.getListHeatParameters();
3701 List<HeatParameterDefinition> updatedHeatEnvParams = artifactInfo.getListHeatParameters();
3704 if (origMd5 != null) {
3705 Either<List<HeatParameterDefinition>, ResponseFormat> uploadParamsValidationResult = validateUploadParamsFromEnvFile(auditingAction, parent, user, artifactInfo,
3706 artifactId, componentType, ri.getName(), currentHeatEnvParams, updatedHeatEnvParams, currArtifact.getArtifactName());
3707 if (uploadParamsValidationResult.isRight()) {
3708 throw new ByResponseFormatComponentException(uploadParamsValidationResult.right().value());
3710 artifactInfo.setListHeatParameters(updatedHeatEnvParams);
3713 Either<ArtifactDefinition, ResponseFormat> validateAndConvertHeatParamers = validateAndConvertHeatParameters(artifactInfo, ArtifactTypeEnum.HEAT_ENV.getType());
3714 if (validateAndConvertHeatParamers.isRight()) {
3715 throw new ByResponseFormatComponentException(validateAndConvertHeatParamers.right().value());
3718 if (updatedHeatEnvParams != null && !updatedHeatEnvParams.isEmpty()) {
3719 // fill reduced heat env parameters List for updating
3720 boolean updateRequired = replaceCurrHeatValueWithUpdatedValue(currentHeatEnvParams, updatedHeatEnvParams);
3721 if (updateRequired) {
3722 currArtifact.setHeatParamsUpdateDate(System.currentTimeMillis());
3723 currArtifact.setListHeatParameters(currentHeatEnvParams);
3724 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactRes = artifactToscaOperation.updateArtifactOnResource(
3725 currArtifact, parent, currArtifact.getUniqueId(), componentType.getNodeType(), componentId, true);
3726 if (updateArtifactRes.isRight()) {
3727 log.debug("Failed to update artifact on graph - {}", artifactId);
3728 throw new StorageException(updateArtifactRes.right().value());
3730 StorageOperationStatus error = generateCustomizationUUIDOnGroupInstance(ri, updateArtifactRes.left().value().getUniqueId(), parent.getUniqueId());
3731 if (error != StorageOperationStatus.OK) {
3732 throw new StorageException(error);
3736 updateHeatMetaDataIfNeeded(componentId,user,auditingAction,componentType, parent,ri,artifactInfo);
3737 StorageOperationStatus error = generateCustomizationUUIDOnInstance(parent.getUniqueId(), ri.getUniqueId(), componentType);
3738 if (error != StorageOperationStatus.OK) {
3739 throw new StorageException(error);
3742 return Either.left(currArtifact);
3746 updateHeatMetaDataIfNeeded(String componentId, User user, AuditingActionEnum auditingAction, ComponentTypeEnum componentType, Component parent, ComponentInstance resourceInstance, ArtifactDefinition updatedHeatEnvArtifact) {
3747 String heatArtifactId = updatedHeatEnvArtifact.getGeneratedFromId();
3748 Either<ArtifactDefinition, ResponseFormat> getArtifactRes = getArtifactFromRI(parent, resourceInstance, componentId, heatArtifactId, auditingAction, user);
3749 if (getArtifactRes.isRight()) {
3750 throw new ByResponseFormatComponentException(getArtifactRes.right().value());
3752 ArtifactDefinition heatArtifactToUpdate = getArtifactRes.left().value();
3753 if (isUpdateHeatMetaDataNeeded(updatedHeatEnvArtifact, heatArtifactToUpdate)) {
3754 validateHeatMetaData(updatedHeatEnvArtifact);
3755 updateHeatMetadataFromHeatEnv(updatedHeatEnvArtifact, heatArtifactToUpdate);
3756 Either<ArtifactDefinition, StorageOperationStatus> updateArtifactRes = artifactToscaOperation.updateArtifactOnResource(heatArtifactToUpdate, parent,
3757 heatArtifactToUpdate.getUniqueId(), componentType.getNodeType(), componentId, false);
3759 if (updateArtifactRes.isRight()) {
3760 log.debug("Failed to update artifact on graph - {}", heatArtifactId);
3761 throw new StorageException(updateArtifactRes.right().value());
3763 ArtifactDefinition artifactDefinition = updateArtifactRes.left().value();
3764 updateGeneratedIdInHeatEnvOnInstance(resourceInstance, parent, heatArtifactId, heatArtifactToUpdate, artifactDefinition, componentType.getNodeType());
3765 StorageOperationStatus error = generateCustomizationUUIDOnGroupInstance(resourceInstance, artifactDefinition.getUniqueId(), parent.getUniqueId());
3766 if (error != StorageOperationStatus.OK) {
3767 throw new StorageException(error);
3772 private void validateHeatMetaData(ArtifactDefinition updatedHeatEnv) {
3773 Integer maxMinutes = ConfigurationManager.getConfigurationManager().getConfiguration().getHeatArtifactDeploymentTimeout().getMaxMinutes();
3774 Integer minMinutes = ConfigurationManager.getConfigurationManager().getConfiguration().getHeatArtifactDeploymentTimeout().getMinMinutes();
3775 Integer updateTimeout = updatedHeatEnv.getTimeout();
3776 if (updateTimeout > maxMinutes || updateTimeout < minMinutes) {
3777 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_INVALID_TIMEOUT);
3781 private boolean isUpdateHeatMetaDataNeeded(ArtifactDefinition updatedHeatEnv, ArtifactDefinition origHeat) {
3782 // currently only timeout metadata can be updated
3783 return !origHeat.getTimeout().equals(updatedHeatEnv.getTimeout());
3786 private void updateHeatMetadataFromHeatEnv(ArtifactDefinition updatedHeatEnv, ArtifactDefinition origHeat) {
3787 // currently only timeout metadata can be updated
3788 origHeat.setTimeout(updatedHeatEnv.getTimeout());
3791 private boolean replaceCurrHeatValueWithUpdatedValue(List<HeatParameterDefinition> currentHeatEnvParams, List<HeatParameterDefinition> updatedHeatEnvParams) {
3792 boolean isUpdate = false;
3793 List<String> currentParamsNames = currentHeatEnvParams.stream().map(x -> x.getName()).collect(Collectors.toList());
3794 for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) {
3795 String paramName = heatEnvParam.getName();
3796 validateParamName(paramName, currentParamsNames);
3797 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3798 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3799 String updatedParamValue = heatEnvParam.getCurrentValue();
3800 if (!Objects.equals(updatedParamValue, currHeatParam.getCurrentValue())) {
3801 currHeatParam.setCurrentValue(updatedParamValue);
3810 private void validateParamName(String paramName, List<String> heatParamsNames) {
3811 if (!heatParamsNames.contains(paramName)) {
3812 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, paramName);
3816 private Either<ArtifactDefinition, Operation> updateHeatParams(String componentId, ArtifactDefinition artifactEnvInfo, AuditingActionEnum auditingAction, Component parent,
3817 ComponentTypeEnum componentType, ArtifactDefinition currHeatArtifact, boolean needToUpdateGroup) {
3818 Either<ArtifactDefinition, Operation> insideEither = null;
3819 String currentHeatId = currHeatArtifact.getUniqueId();
3821 String esArtifactId = currHeatArtifact.getEsId();
3822 Either<DAOArtifactData, CassandraOperationStatus> artifactFromES = artifactCassandraDao.getArtifact(esArtifactId);
3823 if (artifactFromES.isRight()) {
3824 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(artifactFromES.right().value());
3825 throw new StorageException(storageResponse, currHeatArtifact.getArtifactDisplayName());
3828 DAOArtifactData DAOArtifactData = artifactFromES.left().value();
3829 ArtifactDefinition updatedHeatArt = currHeatArtifact;
3830 List<HeatParameterDefinition> updatedHeatEnvParams = artifactEnvInfo.getListHeatParameters();
3831 List<HeatParameterDefinition> currentHeatEnvParams = currHeatArtifact.getListHeatParameters();
3832 List<HeatParameterDefinition> newHeatEnvParams = new ArrayList<>();
3834 if (CollectionUtils.isNotEmpty(updatedHeatEnvParams) && CollectionUtils.isNotEmpty(currentHeatEnvParams)) {
3835 //TODO: improve complexity - currently N^2
3837 for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) {
3838 paramName = heatEnvParam.getName();
3839 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3840 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3841 String updatedParamValue = heatEnvParam.getCurrentValue();
3842 if (updatedParamValue == null) {
3843 updatedParamValue = heatEnvParam.getDefaultValue();
3845 HeatParameterType paramType = HeatParameterType.isValidType(currHeatParam.getType());
3846 if (!paramType.getValidator().isValid(updatedParamValue, null)) {
3847 throw new ByActionStatusComponentException(ActionStatus.INVALID_HEAT_PARAMETER_VALUE,
3848 ArtifactTypeEnum.HEAT_ENV.getType(), paramType.getType(), paramName);
3850 currHeatParam.setCurrentValue(paramType.getConverter().convert(updatedParamValue, null, null));
3851 newHeatEnvParams.add(currHeatParam);
3856 if (!newHeatEnvParams.isEmpty()) {
3857 currHeatArtifact.setListHeatParameters(currentHeatEnvParams);
3858 Either<ArtifactDefinition, StorageOperationStatus> operationStatus = artifactToscaOperation.updateArtifactOnResource(
3859 currHeatArtifact, parent, currHeatArtifact.getUniqueId(), componentType.getNodeType(), componentId, true);
3861 if (operationStatus.isRight()) {
3862 log.debug("Failed to update artifact on graph - {}", currHeatArtifact.getUniqueId());
3863 throw new StorageException(operationStatus.right().value());
3865 updatedHeatArt = operationStatus.left().value();
3866 if (!updatedHeatArt.getDuplicated() || DAOArtifactData.getId() == null) {
3867 DAOArtifactData.setId(updatedHeatArt.getEsId());
3869 saveArtifactInCassandra(DAOArtifactData, parent, artifactEnvInfo, currentHeatId, updatedHeatArt
3870 .getUniqueId(), auditingAction, componentType);
3871 insideEither = Either.left(updatedHeatArt);
3874 Either<ArtifactDefinition, StorageOperationStatus> updateHeatEnvArtifact;
3875 if (!currentHeatId.equals(updatedHeatArt.getUniqueId())) {
3876 artifactEnvInfo.setArtifactChecksum(null);
3877 updateHeatEnvArtifact = artifactToscaOperation.updateHeatEnvArtifact(parent, artifactEnvInfo, currentHeatId, updatedHeatArt
3878 .getUniqueId(), componentType.getNodeType(), componentId);
3881 //TODO Andrey check if componentId = parent.getUniqeId
3882 updateHeatEnvArtifact = artifactToscaOperation.updateHeatEnvPlaceholder(artifactEnvInfo, parent, componentType
3886 if (needToUpdateGroup && updateHeatEnvArtifact.isLeft()) {
3887 ActionStatus result = updateGroupForHeat(currHeatArtifact, updatedHeatArt, artifactEnvInfo,
3888 updateHeatEnvArtifact.left().value(), parent);
3889 if (result != ActionStatus.OK) {
3890 throw new ByActionStatusComponentException(result);
3893 if (updatedHeatEnvParams.isEmpty()) {
3894 throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML, currHeatArtifact.getArtifactName());
3896 return insideEither;
3900 private StorageOperationStatus generateCustomizationUUIDOnGroupInstance(ComponentInstance ri, String artifactId, String componentId) {
3901 StorageOperationStatus error = StorageOperationStatus.OK;
3902 log.debug("Need to re-generate customization UUID for group instance on component instance {}", ri.getUniqueId());
3903 List<GroupInstance> groupsInstances = ri.getGroupInstances();
3904 List<String> groupInstancesId = null;
3905 if (groupsInstances != null && !groupsInstances.isEmpty()) {
3906 groupInstancesId = groupsInstances.stream()
3907 .filter(p -> p.getGroupInstanceArtifacts() != null && p.getGroupInstanceArtifacts()
3908 .contains(artifactId))
3909 .map(GroupInstanceDataDefinition::getUniqueId)
3910 .collect(Collectors.toList());
3912 if (groupInstancesId != null && !groupInstancesId.isEmpty()) {
3913 toscaOperationFacade.generateCustomizationUUIDOnInstanceGroup(componentId, ri.getUniqueId(), groupInstancesId);
3919 public Either<List<HeatParameterDefinition>, ResponseFormat> validateUploadParamsFromEnvFile(AuditingActionEnum auditingAction, Component parent, User user, ArtifactDefinition artifactInfo, String artifactId, ComponentTypeEnum componentType,
3920 String riName, List<HeatParameterDefinition> currentHeatEnvParams, List<HeatParameterDefinition> updatedHeatEnvParams, String currArtifactName) {
3922 if (updatedHeatEnvParams == null || updatedHeatEnvParams.isEmpty()) {
3923 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, artifactInfo
3924 .getArtifactName(), currArtifactName);
3925 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, artifactInfo, null, artifactId, responseFormat, componentType, riName);
3926 return Either.right(responseFormat);
3929 for (HeatParameterDefinition uploadedHeatParam : updatedHeatEnvParams) {
3930 String paramName = uploadedHeatParam.getName();
3931 boolean isExistsInHeat = false;
3932 for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
3933 if (paramName.equalsIgnoreCase(currHeatParam.getName())) {
3935 isExistsInHeat = true;
3936 uploadedHeatParam.setType(currHeatParam.getType());
3937 uploadedHeatParam.setCurrentValue(uploadedHeatParam.getDefaultValue());
3938 uploadedHeatParam.setDefaultValue(currHeatParam.getDefaultValue());
3939 uploadedHeatParam.setUniqueId(currHeatParam.getUniqueId());
3943 if (!isExistsInHeat) {
3944 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISMATCH_HEAT_VS_HEAT_ENV, currArtifactName);
3945 handleAuditing(auditingAction, parent, parent.getUniqueId(), user, artifactInfo, null, artifactId, responseFormat, componentType, riName);
3946 return Either.right(responseFormat);
3949 return Either.left(updatedHeatEnvParams);
3952 private Either<ComponentInstance, ResponseFormat> getRIFromComponent(Component component, String riID, String artifactId, AuditingActionEnum auditingAction, User user) {
3953 ResponseFormat responseFormat = null;
3954 List<ComponentInstance> ris = component.getComponentInstances();
3955 for (ComponentInstance ri : ris) {
3956 if (riID.equals(ri.getUniqueId())) {
3957 return Either.left(ri);
3960 responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, riID);
3961 log.debug("Resource Instance not found, resourceInstanceId {}", riID);
3962 handleAuditing(auditingAction, null, riID, user, null, null, artifactId, responseFormat, ComponentTypeEnum.RESOURCE_INSTANCE, null);
3963 return Either.right(responseFormat);
3966 private Either<ArtifactDefinition, ResponseFormat> getArtifactFromRI(Component component, ComponentInstance ri, String riID, String artifactId, AuditingActionEnum auditingAction, User user) {
3967 ResponseFormat responseFormat = null;
3968 Map<String, ArtifactDefinition> rtifactsMap = ri.getDeploymentArtifacts();
3969 for (ArtifactDefinition artifact : rtifactsMap.values()) {
3970 if (artifactId.equals(artifact.getUniqueId())) {
3971 return Either.left(artifact);
3974 responseFormat = componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, riID, component.getUniqueId());
3975 handleAuditing(auditingAction, component, riID, user, null, null, artifactId, responseFormat, ComponentTypeEnum.RESOURCE_INSTANCE, ri
3977 return Either.right(responseFormat);
3980 public ArtifactDefinition extractArtifactDefinition(Either<ArtifactDefinition, Operation> eitherArtifact) {
3981 ArtifactDefinition ret;
3982 if (eitherArtifact.isLeft()) {
3983 ret = eitherArtifact.left().value();
3986 ret = eitherArtifact.right().value().getImplementationArtifact();
3991 public byte[] downloadComponentArtifactByUUIDs(ComponentTypeEnum componentType, String componentUuid, String artifactUUID, ResourceCommonInfo resourceCommonInfo) {
3992 Component component = getComponentByUuid(componentType, componentUuid);
3993 resourceCommonInfo.setResourceName(component.getName());
3994 return downloadArtifact(component.getAllArtifacts(), artifactUUID, component.getName());
3998 * downloads an artifact of resource instance of component by UUIDs
4000 * @param componentType
4001 * @param componentUuid
4002 * @param resourceInstanceName
4003 * @param artifactUUID
4006 public byte[] downloadResourceInstanceArtifactByUUIDs(ComponentTypeEnum componentType, String componentUuid,
4007 String resourceInstanceName, String artifactUUID) {
4008 ComponentInstance resourceInstance = getRelatedComponentInstance(componentType, componentUuid, resourceInstanceName);
4009 return downloadArtifact(resourceInstance == null ? null : resourceInstance.getDeploymentArtifacts(),
4010 artifactUUID, resourceInstance.getName());
4014 * uploads an artifact to a component by UUID
4018 * @param componentType
4019 * @param componentUuid
4020 * @param resourceCommonInfo
4024 public ArtifactDefinition uploadArtifactToComponentByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType,
4025 String componentUuid, ResourceCommonInfo resourceCommonInfo, ArtifactOperationInfo operation) {
4026 Either<ArtifactDefinition, Operation> actionResult;
4027 Component component;
4029 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
4030 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4031 String userId = request.getHeader(Constants.USER_ID_HEADER);
4033 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes =
4034 toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4035 if (getComponentRes.isRight()) {
4036 StorageOperationStatus status = getComponentRes.right().value();
4037 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4038 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), componentUuid);
4041 ComponentMetadataDataDefinition componentMetadataDataDefinition = getComponentRes.left().value().getMetadataDataDefinition();
4042 componentId = componentMetadataDataDefinition.getUniqueId();
4043 String componentName = componentMetadataDataDefinition.getName();
4045 if (!componentMetadataDataDefinition
4047 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4048 component = checkoutParentComponent(componentType, componentId, userId);
4049 if (component != null) {
4050 componentId = component.getUniqueId();
4051 componentName = component.getName();
4054 resourceCommonInfo.setResourceName(componentName);
4056 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, null, artifactInfo,
4057 origMd5, data, null, null, null, null);
4058 return actionResult.left().value();
4062 * upload an artifact to a resource instance by UUID
4066 * @param componentType
4067 * @param componentUuid
4068 * @param resourceInstanceName
4072 public ArtifactDefinition uploadArtifactToRiByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName,
4073 ArtifactOperationInfo operation) {
4074 Either<ArtifactDefinition, Operation> actionResult;
4075 Component component = null;
4076 String componentInstanceId;
4078 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4079 String userId = request.getHeader(Constants.USER_ID_HEADER);
4081 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4082 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4083 if (getComponentRes.isRight()) {
4084 StorageOperationStatus status = getComponentRes.right().value();
4085 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4086 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), resourceInstanceName);
4088 if (!getComponentRes.left()
4090 .getMetadataDataDefinition()
4092 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4093 component = checkoutParentComponent(componentType, getComponentRes.left()
4095 .getMetadataDataDefinition()
4096 .getUniqueId(), userId);
4098 if (component == null) {
4099 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4102 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4104 componentInstanceId = componentRiPair.getRight().getUniqueId();
4105 componentId = componentRiPair.getLeft().getUniqueId();
4106 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
4108 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE,
4109 operation, null, artifactInfo, origMd5, data, null, null,
4110 componentId, ComponentTypeEnum.findParamByType(componentType));
4112 return actionResult.left().value();
4116 * updates an artifact on a component by UUID
4120 * @param componentType
4121 * @param componentUuid
4122 * @param artifactUUID
4123 * @param resourceCommonInfo
4124 * @param operation TODO
4127 public ArtifactDefinition updateArtifactOnComponentByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String artifactUUID,
4128 ResourceCommonInfo resourceCommonInfo, ArtifactOperationInfo operation) {
4129 Either<ArtifactDefinition, Operation> actionResult;
4130 Component component;
4133 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinitionForUpdate(data, ArtifactDefinition.class);
4134 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4135 String userId = request.getHeader(Constants.USER_ID_HEADER);
4137 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4138 if (getComponentRes.isRight()) {
4139 StorageOperationStatus status = getComponentRes.right().value();
4140 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4141 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4143 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4144 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4146 if (!getComponentRes.left()
4148 .getMetadataDataDefinition()
4150 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4151 component = checkoutParentComponent(componentType, componentId, userId);
4152 if (component != null) {
4153 componentId = component.getUniqueId();
4154 componentName = component.getName();
4157 resourceCommonInfo.setResourceName(componentName);
4158 artifactId = getLatestParentArtifactDataIdByArtifactUUID(artifactUUID, componentId, componentType);
4159 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, artifactId, artifactInfo,
4160 origMd5, data, null, null, null, null);
4161 if (actionResult.isRight()) {
4162 log.debug(FAILED_UPLOAD_ARTIFACT_TO_COMPONENT, componentType, componentUuid, actionResult
4167 return actionResult.left().value();
4171 * updates an artifact on a resource instance by UUID
4175 * @param componentType
4176 * @param componentUuid
4177 * @param resourceInstanceName
4178 * @param artifactUUID
4179 * @param operation TODO
4182 public ArtifactDefinition updateArtifactOnRiByUUID(String data, HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName, String artifactUUID,
4183 ArtifactOperationInfo operation) {
4185 Either<ArtifactDefinition, Operation> actionResult;
4186 Component component = null;
4187 String componentInstanceId;
4190 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4191 String userId = request.getHeader(Constants.USER_ID_HEADER);
4193 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4194 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4195 if (getComponentRes.isRight()) {
4196 StorageOperationStatus status = getComponentRes.right().value();
4197 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4198 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4200 if (!getComponentRes.left()
4202 .getMetadataDataDefinition()
4204 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4205 component = checkoutParentComponent(componentType, getComponentRes.left()
4207 .getMetadataDataDefinition()
4208 .getUniqueId(), userId);
4210 if (component == null) {
4211 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4214 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4216 componentInstanceId = componentRiPair.getRight().getUniqueId();
4217 componentId = componentRiPair.getLeft().getUniqueId();
4218 artifactId = findArtifactId(componentRiPair.getRight(), artifactUUID);
4219 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinition(data, ArtifactDefinition.class, false);
4221 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactId, artifactInfo, origMd5, data, null, null, componentId, ComponentTypeEnum
4222 .findParamByType(componentType));
4223 return actionResult.left().value();
4226 private Either<ArtifactDefinition, ResponseFormat> updateOperationArtifact(String componentId, String interfaceType, String operationUuid, ArtifactDefinition artifactInfo){
4227 Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId);
4228 if (componentStorageOperationStatusEither.isRight()) {
4229 StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value();
4230 log.debug("Failed to fetch resource information by resource id, error {}", errorStatus);
4231 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus)));
4233 Component storedComponent = componentStorageOperationStatusEither.left().value();
4235 Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils.getInterfaceDefinitionFromComponentByInterfaceType(storedComponent, interfaceType);
4236 if(!optionalInterface.isPresent()) {
4237 log.debug("Failed to get resource interface for resource Id {}", componentId);
4238 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceType));
4241 //fetch the operation from storage
4242 InterfaceDefinition gotInterface = optionalInterface.get();
4243 Map<String, Operation> operationsMap = gotInterface.getOperationsMap();
4244 Optional<Operation> optionalOperation = operationsMap.values()
4246 .filter(o -> o.getUniqueId().equals(operationUuid))
4248 if (!optionalOperation.isPresent()) {
4249 log.debug("Failed to get resource interface operation for resource Id {} and operationId {}", componentId, operationUuid);
4250 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NOT_FOUND, componentId);
4251 return Either.right(responseFormat);
4254 Operation operation = optionalOperation.get();
4255 ArtifactDefinition implementationArtifact = operation.getImplementationArtifact();
4256 implementationArtifact.setArtifactUUID(artifactInfo.getArtifactUUID());
4257 implementationArtifact.setUniqueId(artifactInfo.getUniqueId());
4258 implementationArtifact.setArtifactName(artifactInfo.getArtifactName());
4259 implementationArtifact.setDescription(artifactInfo.getDescription());
4260 implementationArtifact.setArtifactType(artifactInfo.getArtifactType());
4261 implementationArtifact.setArtifactLabel(artifactInfo.getArtifactLabel());
4262 implementationArtifact.setArtifactDisplayName(artifactInfo.getArtifactDisplayName());
4263 implementationArtifact.setEsId(artifactInfo.getEsId());
4264 operation.setImplementation(implementationArtifact);
4265 gotInterface.setOperationsMap(operationsMap);
4266 Either<List<InterfaceDefinition>, StorageOperationStatus> interfaceDefinitionStorageOperationStatusEither =
4267 interfaceOperation.updateInterfaces(storedComponent.getUniqueId(), Collections.singletonList(gotInterface));
4268 if (interfaceDefinitionStorageOperationStatusEither.isRight()){
4269 StorageOperationStatus storageOperationStatus = interfaceDefinitionStorageOperationStatusEither.right().value();
4270 ActionStatus actionStatus =
4271 componentsUtils.convertFromStorageResponseForDataType(storageOperationStatus);
4272 return Either.right(componentsUtils.getResponseFormat(actionStatus));
4275 return Either.left(artifactInfo);
4280 * updates an artifact on a component by UUID
4284 * @param componentType
4285 * @param componentUuid
4286 * @param artifactUUID
4290 public Either<ArtifactDefinition, ResponseFormat> updateArtifactOnInterfaceOperationByResourceUUID(
4291 String data, HttpServletRequest request, ComponentTypeEnum componentType,
4292 String componentUuid, String interfaceUUID, String operationUUID, String artifactUUID,
4293 ResourceCommonInfo resourceCommonInfo,ArtifactOperationInfo operation) {
4294 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
4295 Either<ArtifactDefinition, ResponseFormat> updateArtifactResult;
4296 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> actionResult = null;
4297 ArtifactDefinition updateArtifact = null;
4298 String componentId = null;
4299 ArtifactDefinition artifactInfo = RepresentationUtils.convertJsonToArtifactDefinitionForUpdate(data, ArtifactDefinition.class);
4300 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4301 String userId = request.getHeader(Constants.USER_ID_HEADER);
4302 ArtifactDefinition existingArtifactInfo = null;
4303 String interfaceName = null;
4305 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4306 if (getComponentRes.isRight()) {
4307 StorageOperationStatus status = getComponentRes.right().value();
4308 log.debug("Could not fetch component with type {} and uuid {}. Status is {}. ", componentType, componentUuid, status);
4309 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status)));
4312 if (errorWrapper.isEmpty()) {
4313 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4314 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4315 if (!getComponentRes.left()
4317 .getMetadataDataDefinition()
4319 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4320 Component component = checkoutParentComponent(componentType, componentId, userId);
4321 if (component != null) {
4322 componentId = component.getUniqueId();
4323 componentName = component.getName();
4327 resourceCommonInfo.setResourceName(componentName);
4330 if(errorWrapper.isEmpty()){
4331 Either<String, ResponseFormat> interfaceNameEither = fetchInterfaceName(componentId, interfaceUUID);
4332 if (interfaceNameEither.isRight()) {
4333 errorWrapper.setInnerElement(interfaceNameEither.right().value());
4336 interfaceName = interfaceNameEither.left().value();
4339 if(errorWrapper.isEmpty()){
4340 Either<Component, StorageOperationStatus> toscaComponentEither = toscaOperationFacade.getToscaElement(componentId);
4341 if (toscaComponentEither.isRight()) {
4342 StorageOperationStatus status = toscaComponentEither.right().value();
4343 log.debug("Could not fetch component with type {} and id {}. Status is {}. ", componentType, componentId, status);
4344 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status)));
4347 if (errorWrapper.isEmpty()) {
4348 NodeTypeEnum parentType = convertParentType(componentType);
4349 List<ArtifactDefinition> existingDeploymentArtifacts = getDeploymentArtifacts(toscaComponentEither.left().value(), parentType,null);
4350 for (ArtifactDefinition artifactDefinition: existingDeploymentArtifacts){
4351 if(artifactInfo.getArtifactName().equalsIgnoreCase(artifactDefinition.getArtifactName())){
4352 existingArtifactInfo = artifactDefinition;
4356 if(existingArtifactInfo != null){
4357 return updateOperationArtifact(componentId, interfaceName, operationUUID, existingArtifactInfo);
4363 if (errorWrapper.isEmpty()) {
4365 actionResult = Either.left(handleArtifactRequest(componentId, userId, componentType, operation,
4366 artifactUUID, artifactInfo, origMd5, data, interfaceName,
4367 operationUUID, null, null));
4368 }catch (ComponentException e){
4369 errorWrapper.setInnerElement(e.getResponseFormat());
4373 if (errorWrapper.isEmpty()) {
4374 updateArtifact = actionResult.left().value().left().value();
4375 updateArtifactResult = Either.left(updateArtifact);
4379 updateArtifactResult = Either.right(errorWrapper.getInnerElement());
4381 return updateArtifactResult;
4384 private Either<String, ResponseFormat> fetchInterfaceName(String componentId, String interfaceUUID) {
4385 Either<Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade.getToscaElement(componentId);
4386 if (componentStorageOperationStatusEither.isRight()) {
4387 StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value();
4388 log.debug("Failed to fetch component information by component id, error {}", errorStatus);
4389 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus)));
4391 Component storedComponent = componentStorageOperationStatusEither.left().value();
4393 Optional<InterfaceDefinition> optionalInterface = InterfaceOperationUtils
4394 .getInterfaceDefinitionFromComponentByInterfaceId(storedComponent, interfaceUUID);
4395 if(!optionalInterface.isPresent()) {
4396 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceUUID));
4398 return Either.left(optionalInterface.get().getType());
4402 * deletes an artifact on a component by UUID
4405 * @param componentType
4406 * @param componentUuid
4407 * @param artifactUUID
4408 * @param resourceCommonInfo
4409 * @param operation TODO
4412 public ArtifactDefinition deleteArtifactOnComponentByUUID(HttpServletRequest request, ComponentTypeEnum componentType, String componentUuid, String artifactUUID, ResourceCommonInfo resourceCommonInfo,
4413 ArtifactOperationInfo operation) {
4415 Either<ArtifactDefinition, Operation> actionResult;
4416 Component component;
4417 String componentId ;
4419 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4420 String userId = request.getHeader(Constants.USER_ID_HEADER);
4422 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4423 if (getComponentRes.isRight()) {
4424 StorageOperationStatus status = getComponentRes.right().value();
4425 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4426 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status, componentType), componentUuid);
4428 componentId = getComponentRes.left().value().getMetadataDataDefinition().getUniqueId();
4429 String componentName = getComponentRes.left().value().getMetadataDataDefinition().getName();
4430 if (!getComponentRes.left()
4432 .getMetadataDataDefinition()
4434 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4435 component = checkoutParentComponent(componentType, componentId, userId);
4436 if (component != null) {
4437 componentId = component.getUniqueId();
4438 componentName = component.getName();
4441 resourceCommonInfo.setResourceName(componentName);
4442 artifactId = getLatestParentArtifactDataIdByArtifactUUID(artifactUUID, componentId, componentType);
4443 actionResult = handleArtifactRequest(componentId, userId, componentType, operation, artifactId, null, origMd5, null, null, null, null, null);
4444 return actionResult.left().value();
4448 * deletes an artifact from a resource instance by UUID
4451 * @param componentType
4452 * @param componentUuid
4453 * @param resourceInstanceName
4454 * @param artifactUUID
4455 * @param operation TODO
4458 public ArtifactDefinition deleteArtifactOnRiByUUID(HttpServletRequest request, ComponentTypeEnum componentType,
4459 String componentUuid, String resourceInstanceName,
4460 String artifactUUID, ArtifactOperationInfo operation) {
4462 Either<ArtifactDefinition, Operation> actionResult;
4463 Component component = null;
4464 String componentInstanceId;
4467 String origMd5 = request.getHeader(Constants.MD5_HEADER);
4468 String userId = request.getHeader(Constants.USER_ID_HEADER);
4469 ImmutablePair<Component, ComponentInstance> componentRiPair = null;
4470 Either<ComponentMetadataData, StorageOperationStatus> getComponentRes =
4471 toscaOperationFacade.getLatestComponentMetadataByUuid(componentUuid, JsonParseFlagEnum.ParseMetadata, true);
4472 if (getComponentRes.isRight()) {
4473 StorageOperationStatus status = getComponentRes.right().value();
4474 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4475 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4477 if (!getComponentRes.left()
4479 .getMetadataDataDefinition()
4481 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
4482 component = checkoutParentComponent(componentType, getComponentRes.left()
4484 .getMetadataDataDefinition()
4485 .getUniqueId(), userId);
4487 if (component == null) {
4488 componentRiPair = getRelatedComponentComponentInstance(componentType, componentUuid, resourceInstanceName);
4491 componentRiPair = getRelatedComponentComponentInstance(component, resourceInstanceName);
4493 componentInstanceId = componentRiPair.getRight().getUniqueId();
4494 componentId = componentRiPair.getLeft().getUniqueId();
4495 artifactId = findArtifactId(componentRiPair.getRight(), artifactUUID);
4497 actionResult = handleArtifactRequest(componentInstanceId, userId, ComponentTypeEnum.RESOURCE_INSTANCE, operation, artifactId, null, origMd5, null, null, null, componentId, ComponentTypeEnum
4498 .findParamByType(componentType));
4499 return actionResult.left().value();
4502 private String findArtifactId(ComponentInstance instance, String artifactUUID) {
4503 String artifactId = null;
4504 ArtifactDefinition foundArtifact = null;
4505 if (instance.getDeploymentArtifacts() != null) {
4506 foundArtifact = instance.getDeploymentArtifacts()
4509 .filter(e -> e.getArtifactUUID() != null && e.getArtifactUUID()
4510 .equals(artifactUUID))
4514 if (foundArtifact == null && instance.getArtifacts() != null) {
4515 foundArtifact = instance.getArtifacts()
4518 .filter(e -> e.getArtifactUUID() != null && e.getArtifactUUID()
4519 .equals(artifactUUID))
4523 if (foundArtifact == null) {
4524 log.debug("The artifact {} was not found on instance {}. ", artifactUUID, instance.getUniqueId());
4525 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactUUID);
4528 artifactId = foundArtifact.getUniqueId();
4533 @SuppressWarnings("unchecked")
4534 public ArtifactDefinition createHeatEnvPlaceHolder(List<ArtifactDefinition> createdArtifacts, ArtifactDefinition heatArtifact,
4535 String envType, String parentId, NodeTypeEnum parentType,
4536 String parentName, User user, Component component,
4537 Map<String, String> existingEnvVersions) {
4538 Map<String, Object> deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager()
4540 .getDeploymentResourceInstanceArtifacts();
4541 if (deploymentResourceArtifacts == null) {
4542 log.debug("no deployment artifacts are configured for generated artifacts");
4543 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
4545 Map<String, Object> placeHolderData = (Map<String, Object>) deploymentResourceArtifacts.get(envType);
4546 if (placeHolderData == null) {
4547 log.debug("no env type {} are configured for generated artifacts", envType);
4548 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
4551 String envLabel = (heatArtifact.getArtifactLabel() + HEAT_ENV_SUFFIX).toLowerCase();
4552 ArtifactDefinition createArtifactPlaceHolder = createArtifactPlaceHolderInfo(parentId, envLabel, placeHolderData, user
4553 .getUserId(), ArtifactGroupTypeEnum.DEPLOYMENT, true);
4554 ArtifactDefinition artifactHeatEnv = createArtifactPlaceHolder;
4555 artifactHeatEnv.setGeneratedFromId(heatArtifact.getUniqueId());
4556 artifactHeatEnv.setHeatParamsUpdateDate(System.currentTimeMillis());
4557 artifactHeatEnv.setTimeout(0);
4558 artifactHeatEnv.setIsFromCsar(heatArtifact.getIsFromCsar());
4559 buildHeatEnvFileName(heatArtifact, artifactHeatEnv, placeHolderData);
4560 // rbetzer - keep env artifactVersion - changeComponentInstanceVersion flow
4561 handleEnvArtifactVersion(artifactHeatEnv, existingEnvVersions);
4562 ArtifactDefinition heatEnvPlaceholder;
4563 // Evg : for resource instance artifact will be added later as block with other env artifacts from BL
4564 if (parentType != NodeTypeEnum.ResourceInstance) {
4565 String checkSum = artifactToscaOperation.sortAndCalculateChecksumForHeatParameters(heatArtifact.getHeatParameters());
4566 artifactHeatEnv.setArtifactChecksum(checkSum);
4567 Either<ArtifactDefinition, StorageOperationStatus> addHeatEnvArtifact = addHeatEnvArtifact(artifactHeatEnv, heatArtifact, component, parentType, parentId);
4568 if (addHeatEnvArtifact.isRight()) {
4569 log.debug("failed to create heat env artifact on resource instance");
4570 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(addHeatEnvArtifact
4572 .value(), false), "", null));
4574 heatEnvPlaceholder = createArtifactPlaceHolder;
4577 heatEnvPlaceholder = artifactHeatEnv;
4578 artifactToscaOperation.generateUUID(heatEnvPlaceholder, heatEnvPlaceholder.getArtifactVersion());
4579 setHeatCurrentValuesOnHeatEnvDefaultValues(heatArtifact, heatEnvPlaceholder);
4581 ComponentTypeEnum componentType = component.getComponentType();
4582 if (parentType == NodeTypeEnum.ResourceInstance) {
4583 componentType = ComponentTypeEnum.RESOURCE_INSTANCE;
4585 createdArtifacts.add(heatEnvPlaceholder);
4586 componentsUtils.auditComponent(componentsUtils.getResponseFormat(ActionStatus.OK), user, component, AuditingActionEnum.ARTIFACT_UPLOAD,
4587 new ResourceCommonInfo(parentName, componentType.getValue()),
4588 ResourceVersionInfo.newBuilder().build(),
4589 ResourceVersionInfo.newBuilder().artifactUuid(heatEnvPlaceholder.getUniqueId()).build(),
4590 null, heatEnvPlaceholder, null);
4591 return heatEnvPlaceholder;
4594 private void setHeatCurrentValuesOnHeatEnvDefaultValues(ArtifactDefinition artifact, ArtifactDefinition artifactDefinition) {
4595 if (artifact.getListHeatParameters() == null) {
4598 List<HeatParameterDefinition> heatEnvParameters = new ArrayList<>();
4599 for (HeatParameterDefinition parameter : artifact.getListHeatParameters()) {
4600 HeatParameterDefinition heatEnvParameter = new HeatParameterDefinition(parameter);
4601 heatEnvParameter.setDefaultValue(parameter.getCurrentValue());
4602 heatEnvParameter.setCurrentValue(null);
4603 heatEnvParameters.add(heatEnvParameter);
4605 artifactDefinition.setListHeatParameters(heatEnvParameters);
4608 private void buildHeatEnvFileName(ArtifactDefinition heatArtifact, ArtifactDefinition heatEnvArtifact, Map<String, Object> placeHolderData) {
4609 String heatExtension = GeneralUtility.getFilenameExtension(heatArtifact.getArtifactName());
4610 String envExtension = (String) placeHolderData.get(ARTIFACT_PLACEHOLDER_FILE_EXTENSION);
4611 String name = heatArtifact.getArtifactName();
4614 name = heatArtifact.getArtifactLabel();
4615 fileName = name + "." + envExtension;
4618 fileName = name.replaceAll("." + heatExtension, "." + envExtension);
4620 heatEnvArtifact.setArtifactName(fileName);
4623 private void handleEnvArtifactVersion(ArtifactDefinition heatEnvArtifact, Map<String, String> existingEnvVersions) {
4624 if (null != existingEnvVersions) {
4625 String prevVersion = existingEnvVersions.get(heatEnvArtifact.getArtifactName());
4626 if (null != prevVersion) {
4627 heatEnvArtifact.setArtifactVersion(prevVersion);
4632 public List<ArtifactDefinition> handleArtifactsForInnerVfcComponent(List<ArtifactDefinition> artifactsToHandle, Resource component, User user, List<ArtifactDefinition> vfcsNewCreatedArtifacts,
4633 ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction) {
4634 ComponentTypeEnum componentType = component.getComponentType();
4635 List<ArtifactDefinition> uploadedArtifacts = new ArrayList<>();
4636 Either<ArtifactDefinition, Operation> result;
4638 for (ArtifactDefinition artifactDefinition : artifactsToHandle) {
4639 result = handleLoadedArtifact(component, user, operation, shouldLock, inTransaction, componentType, artifactDefinition);
4640 uploadedArtifacts.add(result.left().value());
4642 } catch (ComponentException e) {
4643 log.debug(FAILED_UPLOAD_ARTIFACT_TO_COMPONENT, componentType, component
4644 .getName(), e.getResponseFormat());
4645 if (ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum())) {
4646 vfcsNewCreatedArtifacts.addAll(uploadedArtifacts);
4650 return uploadedArtifacts;
4653 public Either<ArtifactDefinition, Operation> handleLoadedArtifact(Resource component, User user, ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction,
4654 ComponentTypeEnum componentType, ArtifactDefinition artifactDefinition) {
4655 AuditingActionEnum auditingAction = detectAuditingType(operation, "");
4656 String componentId = component.getUniqueId();
4657 String artifactId = artifactDefinition.getUniqueId();
4658 Either<ArtifactDefinition, Operation> result;
4659 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
4660 //artifact validation
4661 artifactDefinition = validateArtifact(componentId, componentType, operation,
4662 artifactId, artifactDefinition, auditingAction, user,
4663 component, shouldLock, inTransaction);
4664 switch (operation.getArtifactOperationEnum()) {
4666 byte[] validPayload = getValidPayload(componentId, artifactDefinition, operation, auditingAction, artifactId, user, componentType, component, null, null);
4667 result = createArtifact(component, componentId, artifactDefinition, validPayload,
4668 componentType, auditingAction, null, null);
4671 validPayload = getValidPayload(componentId, artifactDefinition, operation, auditingAction, artifactId, user, componentType, component, null, null);
4672 result = handleUpdate(componentId, componentType, operation, artifactId, artifactDefinition, validPayload, null, null, null, null,
4673 auditingAction, user, component, true);
4676 result = Either.left(handleDeleteInternal(componentId, artifactId, componentType, component));
4679 if (artifactGenerationRequired(component, artifactDefinition)) {
4680 result = Either.left(generateNotSavedArtifact(component, artifactDefinition));
4682 result = Either.left(handleDownload(componentId, artifactId, componentType, component));
4686 result = Either.left(handleLink(componentId, artifactDefinition, componentType, component));
4689 throw new UnsupportedOperationException("In ArtifactsBusinessLogic received illegal operation: " + operation.getArtifactOperationEnum());
4694 public List<ArtifactDefinition> handleArtifactsRequestForInnerVfcComponent(List<ArtifactDefinition> artifactsToHandle, Resource component, User user, List<ArtifactDefinition> vfcsNewCreatedArtifacts,
4695 ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction) {
4697 List<ArtifactDefinition> handleArtifactsResult;
4698 ComponentTypeEnum componentType = component.getComponentType();
4699 List<ArtifactDefinition> uploadedArtifacts = new ArrayList<>();
4700 Either<ArtifactDefinition, Operation> actionResult;
4704 for (ArtifactDefinition artifact : artifactsToHandle) {
4705 originData = buildJsonStringForCsarVfcArtifact(artifact);
4706 origMd5 = GeneralUtility.calculateMD5Base64EncodedByString(originData);
4707 actionResult = handleArtifactRequest(component.getUniqueId(), user.getUserId(), componentType, operation, artifact
4708 .getUniqueId(), artifact, origMd5, originData, null, null, null, null, shouldLock, inTransaction);
4709 uploadedArtifacts.add(actionResult.left().value());
4711 handleArtifactsResult = uploadedArtifacts;
4712 }catch (ComponentException e){
4713 if (ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum())) {
4714 vfcsNewCreatedArtifacts.addAll(uploadedArtifacts);
4718 return handleArtifactsResult;
4721 private ComponentInstance getRelatedComponentInstance(ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName) {
4722 ComponentInstance componentInstance;
4723 String normalizedName = ValidationUtils.normalizeComponentInstanceName(resourceInstanceName);
4724 Component component = getComponentByUuid(componentType, componentUuid);
4725 componentInstance = (component == null) ? null : component.getComponentInstances()
4727 .filter(ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName())
4728 .equals(normalizedName))
4731 if (componentInstance == null) {
4732 log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName());
4733 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, resourceInstanceName,
4734 RESOURCE_INSTANCE, component.getComponentType().getValue(), component.getName());
4736 return componentInstance;
4739 private ImmutablePair<Component, ComponentInstance> getRelatedComponentComponentInstance(Component component, String resourceInstanceName) {
4741 ImmutablePair<Component, ComponentInstance> relatedComponentComponentInstancePair = null;
4742 String normalizedName = ValidationUtils.normalizeComponentInstanceName(resourceInstanceName);
4743 ComponentInstance componentInstance = component.getComponentInstances()
4745 .filter(ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName())
4746 .equals(normalizedName))
4749 if (componentInstance == null) {
4750 log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName());
4751 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, resourceInstanceName,
4752 RESOURCE_INSTANCE, component.getComponentType().getValue(), component.getName());
4755 relatedComponentComponentInstancePair = new ImmutablePair<>(component, componentInstance);
4757 return relatedComponentComponentInstancePair;
4760 private ImmutablePair<Component, ComponentInstance> getRelatedComponentComponentInstance(ComponentTypeEnum componentType,
4761 String componentUuid, String resourceInstanceName) {
4762 ComponentInstance componentInstance;
4763 ImmutablePair<Component, ComponentInstance> relatedComponentComponentInstancePair;
4764 Component component = getLatestComponentByUuid(componentType, componentUuid);
4765 componentInstance = component.getComponentInstances()
4767 .filter(ci -> ci.getNormalizedName().equals(resourceInstanceName))
4770 if (componentInstance == null) {
4771 log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName());
4772 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
4773 resourceInstanceName, RESOURCE_INSTANCE, component
4774 .getComponentType().getValue(), component.getName());
4777 relatedComponentComponentInstancePair = new ImmutablePair<>(component, componentInstance);
4779 return relatedComponentComponentInstancePair;
4782 private byte[] downloadArtifact(Map<String, ArtifactDefinition> artifacts, String artifactUUID, String componentName) {
4784 ImmutablePair<String, byte[]> downloadArtifact;
4785 List<ArtifactDefinition> artifactsList = null;
4786 ArtifactDefinition deploymentArtifact;
4787 if (artifacts != null && !artifacts.isEmpty()) {
4788 artifactsList = artifacts.values()
4790 .filter(art -> art.getArtifactUUID() != null && art.getArtifactUUID()
4791 .equals(artifactUUID))
4792 .collect(Collectors.toList());
4794 if (artifactsList == null || artifactsList.isEmpty()) {
4795 log.debug("Deployment artifact with uuid {} was not found for component {}", artifactUUID, componentName);
4796 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND, artifactUUID);
4798 deploymentArtifact = artifactsList.get(0);
4799 downloadArtifact = downloadArtifact(deploymentArtifact);
4800 log.trace("Succeeded to download artifact with uniqueId {}", deploymentArtifact.getUniqueId());
4801 return downloadArtifact.getRight();
4804 private Component getLatestComponentByUuid(ComponentTypeEnum componentType, String componentUuid) {
4805 Component component;
4806 Either<Component, StorageOperationStatus> getComponentRes = toscaOperationFacade.getLatestComponentByUuid(componentUuid);
4807 if (getComponentRes.isRight()) {
4808 StorageOperationStatus status = getComponentRes.right().value();
4809 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4810 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4813 component = getComponentRes.left().value();
4818 private Component getComponentByUuid(ComponentTypeEnum componentType, String componentUuid) {
4819 Component component;
4820 Either<List<Component>, StorageOperationStatus> getComponentRes = toscaOperationFacade.getComponentListByUuid(componentUuid, null);
4821 if (getComponentRes.isRight()) {
4822 StorageOperationStatus status = getComponentRes.right().value();
4823 log.debug(FAILED_FETCH_COMPONENT, componentType, componentUuid, status);
4824 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
4827 List<Component> value = getComponentRes.left().value();
4828 if (value.isEmpty()) {
4829 log.debug("Could not fetch component with type {} and uuid {}.", componentType, componentUuid);
4830 ActionStatus status = componentType == ComponentTypeEnum.RESOURCE ? ActionStatus.RESOURCE_NOT_FOUND : ActionStatus.SERVICE_NOT_FOUND;
4831 throw new ByActionStatusComponentException(status);
4834 component = value.get(0);
4840 private String getLatestParentArtifactDataIdByArtifactUUID(String artifactUUID, String parentId, ComponentTypeEnum componentType) {
4841 ActionStatus actionStatus = ActionStatus.ARTIFACT_NOT_FOUND;
4842 StorageOperationStatus storageStatus;
4843 ArtifactDefinition latestArtifact;
4844 List<ArtifactDefinition> artifacts;
4845 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getArtifactsRes = artifactToscaOperation.getArtifacts(parentId);
4846 if (getArtifactsRes.isRight()) {
4847 storageStatus = getArtifactsRes.right().value();
4848 log.debug("Couldn't fetch artifacts data for parent component {} with uid {}, error: {}", componentType, parentId, storageStatus);
4849 if (storageStatus != StorageOperationStatus.NOT_FOUND) {
4850 actionStatus = componentsUtils.convertFromStorageResponse(storageStatus);
4852 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4854 artifacts = getArtifactsRes.left()
4858 .filter(a -> a.getArtifactUUID() != null && a.getArtifactUUID()
4859 .equals(artifactUUID))
4860 .collect(Collectors.toList());
4861 if (artifacts == null || artifacts.isEmpty()) {
4862 log.debug("Couldn't fetch artifact with UUID {} data for parent component {} with uid {}, error: {}", artifactUUID, componentType, parentId, actionStatus);
4863 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4865 latestArtifact = artifacts.stream().max((a1, a2) -> {
4866 int compareRes = Double.compare(Double.parseDouble(a1.getArtifactVersion()), Double.parseDouble(a2.getArtifactVersion()));
4867 if (compareRes == 0) {
4868 compareRes = Long.compare(a1.getLastUpdateDate() == null ? 0 : a1.getLastUpdateDate(), a2.getLastUpdateDate() == null ? 0 : a2
4869 .getLastUpdateDate());
4873 if (latestArtifact == null) {
4874 log.debug("Couldn't fetch latest artifact with UUID {} data for parent component {} with uid {}, error: {}", artifactUUID, componentType, parentId, actionStatus);
4875 throw new ByActionStatusComponentException(actionStatus, artifactUUID);
4877 return latestArtifact.getUniqueId();
4880 private Component checkoutParentComponent(ComponentTypeEnum componentType, String parentId, String userId) {
4882 Component component = null;
4883 User modifier = userBusinessLogic.getUser(userId, false);
4884 LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction("External API checkout", LifecycleChanceActionEnum.UPDATE_FROM_EXTERNAL_API);
4885 Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic.changeComponentState(componentType, parentId, modifier, LifeCycleTransitionEnum.CHECKOUT, changeInfo, false, true);
4886 if (checkoutRes.isRight()) {
4887 log.debug("Could not change state of component {} with uid {} to checked out. Status is {}. ", componentType
4888 .getNodeType(), parentId, checkoutRes.right().value().getStatus());
4889 throw new ByResponseFormatComponentException(checkoutRes.right().value());
4891 return checkoutRes.left().value();
4894 private String buildJsonStringForCsarVfcArtifact(ArtifactDefinition artifact) {
4895 Map<String, Object> json = new HashMap<>();
4896 String artifactName = artifact.getArtifactName();
4897 json.put(Constants.ARTIFACT_NAME, artifactName);
4898 json.put(Constants.ARTIFACT_LABEL, artifact.getArtifactLabel());
4899 json.put(Constants.ARTIFACT_TYPE, artifact.getArtifactType());
4900 json.put(Constants.ARTIFACT_GROUP_TYPE, ArtifactGroupTypeEnum.DEPLOYMENT.getType());
4901 json.put(Constants.ARTIFACT_DESCRIPTION, artifact.getDescription());
4902 json.put(Constants.ARTIFACT_PAYLOAD_DATA, artifact.getPayloadData());
4903 json.put(Constants.ARTIFACT_DISPLAY_NAME, artifact.getArtifactDisplayName());
4904 return gson.toJson(json);
4908 void setNodeTemplateOperation(NodeTemplateOperation nodeTemplateOperation) {
4909 this.nodeTemplateOperation = nodeTemplateOperation;