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 * ================================================================================
22 package org.openecomp.sdc.be.components.csar;
24 import fj.data.Either;
26 import java.util.Optional;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.CsarValidationUtils;
30 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
31 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
32 import org.openecomp.sdc.be.config.BeEcompErrorManager;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.model.Component;
35 import org.openecomp.sdc.be.model.NodeTypeInfo;
36 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
37 import org.openecomp.sdc.be.model.Resource;
38 import org.openecomp.sdc.be.model.Service;
39 import org.openecomp.sdc.be.model.User;
40 import org.openecomp.sdc.be.model.VendorSoftwareProduct;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
43 import org.openecomp.sdc.be.model.operations.StorageException;
44 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
45 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
46 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
47 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
48 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
49 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
50 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
51 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
52 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
53 import org.openecomp.sdc.common.log.wrappers.Logger;
54 import org.openecomp.sdc.exception.ResponseFormat;
55 import org.springframework.beans.factory.annotation.Autowired;
57 @org.springframework.stereotype.Component("csarBusinessLogic")
58 public class CsarBusinessLogic extends BaseBusinessLogic {
60 private static final Logger log = Logger.getLogger(CsarBusinessLogic.class);
61 private static final String CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID = "Creating resource from CSAR: fetching CSAR with id ";
62 private static final String FAILED = " failed";
63 private final YamlTemplateParsingHandler yamlHandler;
64 private CsarOperation csarOperation;
67 public CsarBusinessLogic(IElementOperation elementDao, IGroupOperation groupOperation, IGroupInstanceOperation groupInstanceOperation,
68 IGroupTypeOperation groupTypeOperation, InterfaceOperation interfaceOperation,
69 InterfaceLifecycleOperation interfaceLifecycleTypeOperation, YamlTemplateParsingHandler yamlHandler,
70 ArtifactsOperations artifactToscaOperation) {
71 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation,
72 artifactToscaOperation);
73 this.yamlHandler = yamlHandler;
77 public void setCsarOperation(CsarOperation csarOperation) {
78 this.csarOperation = csarOperation;
81 public void validateCsarBeforeCreate(Resource resource, AuditingActionEnum auditingAction, User user, String csarUUID) {
82 // check if VF with the same Csar UUID or with he same name already
85 StorageOperationStatus status = toscaOperationFacade.validateCsarUuidUniqueness(csarUUID);
86 if (status == StorageOperationStatus.ENTITY_ALREADY_EXISTS) {
87 log.debug("Failed to create resource {}, csarUUID {} already exist for a different VF ", resource.getSystemName(), csarUUID);
88 auditAndThrowException(resource, user, auditingAction, ActionStatus.VSP_ALREADY_EXISTS, csarUUID);
89 } else if (status != StorageOperationStatus.OK) {
90 log.debug("Failed to validate uniqueness of CsarUUID {} for resource", csarUUID, resource.getSystemName());
91 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
95 public void validateCsarBeforeCreate(Service resource, String csarUUID) {
96 // check if VF with the same Csar UUID or with he same name already
99 StorageOperationStatus status = toscaOperationFacade.validateCsarUuidUniqueness(csarUUID);
100 log.debug("enter validateCsarBeforeCreate,get status:{}", status);
101 if (status == StorageOperationStatus.ENTITY_ALREADY_EXISTS) {
102 log.debug("Failed to create resource {}, csarUUID {} already exist for a different VF ", resource.getSystemName(), csarUUID);
103 } else if (status != StorageOperationStatus.OK) {
104 log.debug("Failed to validate uniqueness of CsarUUID '{}' for resource '{}'", csarUUID, resource.getSystemName());
105 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
109 public OnboardedCsarInfo getCsarInfo(Resource resource, Resource oldResource, User user, Map<String, byte[]> payload, String csarUUID) {
110 Map<String, byte[]> csar = payload;
112 final var vendorSoftwareProduct = getCsar(resource, user);
113 validateModel(resource, vendorSoftwareProduct);
115 csar = vendorSoftwareProduct.getFileMap();
117 ImmutablePair<String, String> toscaYamlCsarStatus = validateAndParseCsar(resource, user, csar, csarUUID).left()
118 .on(this::throwComponentException);
119 String checksum = CsarValidationUtils.getToscaYamlChecksum(csar, csarUUID, componentsUtils).left()
120 .on(r -> logAndThrowComponentException(r, "Failed to calculate checksum for casrUUID {} error {} ", csarUUID));
121 if (oldResource != null && !checksum
122 .equals(oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum())) {
124 "The checksum of main template yaml of csar with csarUUID {} is not equal to the previous one, existing checksum is {}, new one is {}.",
125 csarUUID, oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum(), checksum);
126 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().setImportedToscaChecksum(checksum);
128 return new OnboardedCsarInfo(user, csarUUID, resource.getCsarVersionId(), csar, resource.getName(), toscaYamlCsarStatus.getKey(),
129 toscaYamlCsarStatus.getValue(), true);
132 private void validateModel(final Resource resource, final VendorSoftwareProduct vendorSoftwareProduct) {
133 if (resource.getModel() == null) {
134 if (!vendorSoftwareProduct.getModelList().isEmpty()) {
135 var modelStringList = String.join(", ", vendorSoftwareProduct.getModelList());
136 throw new ByActionStatusComponentException(ActionStatus.VSP_MODEL_NOT_ALLOWED, "SDC AID", modelStringList);
140 if (!vendorSoftwareProduct.getModelList().contains(resource.getModel())) {
141 var modelStringList =
142 vendorSoftwareProduct.getModelList().isEmpty() ? "SDC AID" : String.join(", ", vendorSoftwareProduct.getModelList());
143 throw new ByActionStatusComponentException(ActionStatus.VSP_MODEL_NOT_ALLOWED, resource.getModel(), modelStringList);
147 public ServiceCsarInfo getCsarInfo(Service service, Service oldResource, User user, Map<String, byte[]> payload, String csarUUID) {
148 Map<String, byte[]> csar = getCsar(service, user, payload, csarUUID);
149 ImmutablePair<String, String> toscaYamlCsarStatus = validateAndParseCsar(service, user, csar, csarUUID).left()
150 .on(this::throwComponentException);
151 String checksum = CsarValidationUtils.getToscaYamlChecksum(csar, csarUUID, componentsUtils).left()
152 .on(r -> logAndThrowComponentException(r, "Failed to calculate checksum for casrUUID {} error {} ", csarUUID));
153 if (oldResource != null && !checksum
154 .equals(oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum())) {
156 "The checksum of main template yaml of csar with csarUUID {} is not equal to the previous one, existing checksum is {}, new one is {}.",
157 csarUUID, oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum(), checksum);
158 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().setImportedToscaChecksum(checksum);
160 return new ServiceCsarInfo(user, csarUUID, csar, service.getName(), service.getModel(), toscaYamlCsarStatus.getKey(),
161 toscaYamlCsarStatus.getValue(), true);
164 public ParsedToscaYamlInfo getParsedToscaYamlInfo(String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo,
165 CsarInfo csarInfo, String nodeName, Component component) {
167 .parseResourceInfoFromYAML(yamlName, topologyTemplateYaml, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, nodeName,
168 component, getInterfaceTemplateYaml(csarInfo).orElse(""));
171 private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
173 String interfaceTemplateYaml = "";
174 if (csarInfo.getMainTemplateName().contains(".yml")) {
175 yamlFile = csarInfo.getMainTemplateName().split(".yml");
176 interfaceTemplateYaml = yamlFile[0] + "-interface.yml";
177 } else if (csarInfo.getMainTemplateName().contains(".yaml")) {
178 yamlFile = csarInfo.getMainTemplateName().split(".yaml");
179 interfaceTemplateYaml = yamlFile[0] + "-interface.yaml";
181 if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
182 return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
184 return Optional.empty();
187 private String logAndThrowComponentException(ResponseFormat responseFormat, String logMessage, String... params) {
188 log.debug(logMessage, params, responseFormat);
189 throw new ByResponseFormatComponentException(responseFormat);
192 private ImmutablePair<String, String> throwComponentException(ResponseFormat responseFormat) {
193 throw new ByResponseFormatComponentException(responseFormat);
196 private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Component component, User user, Map<String, byte[]> csar,
198 Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar, csarUUID, componentsUtils);
199 if (validateCsarStatus.isRight()) {
200 ResponseFormat responseFormat = validateCsarStatus.right().value();
201 log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat);
202 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
203 if (component instanceof Resource) {
204 componentsUtils.auditResource(responseFormat, user, (Resource) component, AuditingActionEnum.CREATE_RESOURCE);
206 return Either.right(responseFormat);
208 Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils.getToscaYaml(csar, csarUUID, componentsUtils);
209 if (toscaYamlCsarStatus.isRight()) {
210 ResponseFormat responseFormat = toscaYamlCsarStatus.right().value();
211 log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
212 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
213 if (component instanceof Resource) {
214 componentsUtils.auditResource(responseFormat, user, (Resource) component, AuditingActionEnum.CREATE_RESOURCE);
216 return Either.right(responseFormat);
218 return toscaYamlCsarStatus;
221 private Map<String, byte[]> getCsar(Component component, User user, Map<String, byte[]> payload, String csarUUID) {
222 if (payload != null) {
225 Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.findVspLatestPackage(csarUUID, user);
226 if (csar.isRight()) {
227 StorageOperationStatus value = csar.right().value();
228 log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, value);
229 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
230 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(value), csarUUID);
231 if (component instanceof Resource) {
232 Resource newResource = (Resource) component;
233 componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.CREATE_RESOURCE);
235 throw new StorageException(csar.right().value());
237 return csar.left().value();
240 private VendorSoftwareProduct getCsar(final Resource resource, final User user) {
241 final Optional<VendorSoftwareProduct> vendorSoftwareProductOpt;
243 if (resource.getCsarVersionId() == null) {
244 vendorSoftwareProductOpt = csarOperation.findLatestVsp(resource.getCsarUUID(), user);
246 vendorSoftwareProductOpt = csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user);
248 } catch (final Exception exception) {
249 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, CsarBusinessLogic.class.getName(), exception.getMessage());
250 auditGetCsarError(resource, user, resource.getCsarUUID(), StorageOperationStatus.GENERAL_ERROR);
251 throw new ByActionStatusComponentException(ActionStatus.VSP_FIND_ERROR, resource.getCsarUUID(), resource.getCsarVersionId());
253 if (vendorSoftwareProductOpt.isEmpty()) {
254 auditGetCsarError(resource, user, resource.getCsarUUID(), StorageOperationStatus.CSAR_NOT_FOUND);
255 throw new ByActionStatusComponentException(ActionStatus.VSP_NOT_FOUND, resource.getCsarUUID(), resource.getCsarVersionId());
257 return vendorSoftwareProductOpt.get();
260 private void auditGetCsarError(Component component, User user, String csarUUID, StorageOperationStatus storageOperationStatus) {
261 log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, storageOperationStatus);
262 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
263 var responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageOperationStatus), csarUUID);
264 if (component instanceof Resource) {
265 componentsUtils.auditResource(responseFormat, user, (Resource) component, AuditingActionEnum.CREATE_RESOURCE);
269 private void auditAndThrowException(Resource resource, User user, AuditingActionEnum auditingAction, ActionStatus status, String... params) {
270 ResponseFormat errorResponse = componentsUtils.getResponseFormat(status, params);
271 componentsUtils.auditResource(errorResponse, user, resource, auditingAction);
272 throw new ByResponseFormatComponentException(errorResponse, params);
275 private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Service service, User user, Map<String, byte[]> payload,
277 Map<String, byte[]> csar = getCsar(service, user, payload, csarUUID);
278 Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar, csarUUID, componentsUtils);
279 if (validateCsarStatus.isRight()) {
280 ResponseFormat responseFormat = validateCsarStatus.right().value();
281 log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat);
282 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
283 return Either.right(responseFormat);
285 Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils.getToscaYaml(csar, csarUUID, componentsUtils);
286 if (toscaYamlCsarStatus.isRight()) {
287 ResponseFormat responseFormat = toscaYamlCsarStatus.right().value();
288 log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
289 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
290 return Either.right(responseFormat);
292 return toscaYamlCsarStatus;