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;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.CsarValidationUtils;
28 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
30 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
31 import org.openecomp.sdc.be.config.BeEcompErrorManager;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.model.NodeTypeInfo;
34 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
39 import org.openecomp.sdc.be.model.operations.StorageException;
40 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
41 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
42 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
43 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
46 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
47 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
48 import org.openecomp.sdc.common.log.wrappers.Logger;
49 import org.openecomp.sdc.exception.ResponseFormat;
50 import org.springframework.beans.factory.annotation.Autowired;
54 @org.springframework.stereotype.Component("csarBusinessLogic")
55 public class CsarBusinessLogic extends BaseBusinessLogic {
57 private static final Logger log = Logger.getLogger(CsarBusinessLogic.class);
59 private static final String CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID = "Creating resource from CSAR: fetching CSAR with id ";
60 private static final String FAILED = " failed";
62 private final YamlTemplateParsingHandler yamlHandler;
63 private CsarOperation csarOperation;
66 public CsarBusinessLogic(IElementOperation elementDao,
67 IGroupOperation groupOperation,
68 IGroupInstanceOperation groupInstanceOperation,
69 IGroupTypeOperation groupTypeOperation,
70 GroupBusinessLogic groupBusinessLogic,
71 InterfaceOperation interfaceOperation,
72 InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
73 YamlTemplateParsingHandler yamlHandler,
74 ArtifactsOperations artifactToscaOperation) {
75 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
76 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
77 this.yamlHandler = yamlHandler;
81 public void setCsarOperation(CsarOperation csarOperation) {
82 this.csarOperation = csarOperation;
85 public void validateCsarBeforeCreate(Resource resource, AuditingActionEnum auditingAction, User user, String csarUUID) {
86 // check if VF with the same Csar UUID or with he same name already
88 StorageOperationStatus status = toscaOperationFacade.validateCsarUuidUniqueness(csarUUID);
89 if(status == StorageOperationStatus.ENTITY_ALREADY_EXISTS){
90 log.debug("Failed to create resource {}, csarUUID {} already exist for a different VF ",
91 resource.getSystemName(), csarUUID);
92 auditAndThrowException(resource, user, auditingAction, ActionStatus.VSP_ALREADY_EXISTS,
94 } else if (status != StorageOperationStatus.OK) {
95 log.debug("Failed to validate uniqueness of CsarUUID {} for resource", csarUUID,
96 resource.getSystemName());
97 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
101 public CsarInfo getCsarInfo(Resource resource, Resource oldResource,User user, Map<String, byte[]> payload, String csarUUID){
102 Map<String, byte[]> csar = getCsar(resource, user, payload, csarUUID);
103 ImmutablePair<String, String> toscaYamlCsarStatus = validateAndParseCsar(resource,
104 user, csar, csarUUID)
105 .left().on(this::throwComponentException);
107 String checksum = CsarValidationUtils.getToscaYamlChecksum(csar,
108 csarUUID, componentsUtils).left().on(r->logAndThrowComponentException(r, "Failed to calculate checksum for casrUUID {} error {} ", csarUUID));
109 if (oldResource!=null && !checksum.equals(
110 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum())) {
111 log.debug("The checksum of main template yaml of csar with csarUUID {} is not equal to the previous one, existing checksum is {}, new one is {}.", csarUUID,
112 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition()
113 .getImportedToscaChecksum(),
115 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition()
116 .setImportedToscaChecksum(checksum);
119 return new CsarInfo(user, csarUUID, csar, resource.getName(),
120 toscaYamlCsarStatus.getKey(), toscaYamlCsarStatus.getValue(), true);
124 public ParsedToscaYamlInfo getParsedToscaYamlInfo(String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, String nodeName) {
125 return yamlHandler.parseResourceInfoFromYAML(
126 yamlName, topologyTemplateYaml, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo,
130 private String logAndThrowComponentException(ResponseFormat responseFormat, String logMessage, String ...params) {
131 log.debug(logMessage, params, responseFormat);
132 throw new ByResponseFormatComponentException(responseFormat);
135 private ImmutablePair<String,String> throwComponentException(ResponseFormat responseFormat) {
136 throw new ByResponseFormatComponentException(responseFormat);
139 private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Resource resource, User user,
140 Map<String, byte[]> payload, String csarUUID) {
141 Map<String, byte[]> csar = getCsar(resource, user, payload, csarUUID);
142 Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar,
143 csarUUID, componentsUtils);
144 if (validateCsarStatus.isRight()) {
145 ResponseFormat responseFormat = validateCsarStatus.right().value();
146 log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat);
147 BeEcompErrorManager.getInstance()
148 .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
149 componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE);
150 return Either.right(responseFormat);
153 Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils
154 .getToscaYaml(csar, csarUUID, componentsUtils);
156 if (toscaYamlCsarStatus.isRight()) {
157 ResponseFormat responseFormat = toscaYamlCsarStatus.right().value();
158 log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
159 BeEcompErrorManager.getInstance()
160 .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
161 componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE);
162 return Either.right(responseFormat);
164 return toscaYamlCsarStatus;
167 private Map<String, byte[]> getCsar(Resource resource, User user, Map<String, byte[]> payload, String csarUUID) {
168 if (payload != null) {
171 Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUUID, user);
172 if (csar.isRight()) {
173 StorageOperationStatus value = csar.right().value();
174 log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, value);
175 BeEcompErrorManager.getInstance()
176 .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
177 ResponseFormat responseFormat = componentsUtils
178 .getResponseFormat(componentsUtils.convertFromStorageResponse(value), csarUUID);
179 componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE);
180 throw new StorageException(csar.right().value());
182 return csar.left().value();
185 private void auditAndThrowException(Resource resource, User user, AuditingActionEnum auditingAction, ActionStatus status, String... params){
186 ResponseFormat errorResponse = componentsUtils.getResponseFormat(status, params);
187 componentsUtils.auditResource(errorResponse, user, resource, auditingAction);
188 throw new ByResponseFormatComponentException(errorResponse, params);