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.components.impl.exceptions.ComponentException;
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.jsonjanusgraph.operations.ArtifactsOperations;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
42 import org.openecomp.sdc.be.model.operations.StorageException;
43 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
44 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
45 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
46 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
47 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
48 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
49 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
50 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
51 import org.openecomp.sdc.common.log.wrappers.Logger;
52 import org.openecomp.sdc.exception.ResponseFormat;
53 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);
62 private static final String CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID = "Creating resource from CSAR: fetching CSAR with id ";
63 private static final String FAILED = " failed";
65 private final YamlTemplateParsingHandler yamlHandler;
66 private CsarOperation csarOperation;
69 public CsarBusinessLogic(IElementOperation elementDao,
70 IGroupOperation groupOperation,
71 IGroupInstanceOperation groupInstanceOperation,
72 IGroupTypeOperation groupTypeOperation,
73 GroupBusinessLogic groupBusinessLogic,
74 InterfaceOperation interfaceOperation,
75 InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
76 YamlTemplateParsingHandler yamlHandler,
77 ArtifactsOperations artifactToscaOperation) {
78 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
79 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
80 this.yamlHandler = yamlHandler;
84 public void setCsarOperation(CsarOperation csarOperation) {
85 this.csarOperation = csarOperation;
88 public void validateCsarBeforeCreate(Resource resource, AuditingActionEnum auditingAction, User user, String csarUUID) {
89 // check if VF with the same Csar UUID or with he same name already
91 StorageOperationStatus status = toscaOperationFacade.validateCsarUuidUniqueness(csarUUID);
92 if(status == StorageOperationStatus.ENTITY_ALREADY_EXISTS){
93 log.debug("Failed to create resource {}, csarUUID {} already exist for a different VF ",
94 resource.getSystemName(), csarUUID);
95 auditAndThrowException(resource, user, auditingAction, ActionStatus.VSP_ALREADY_EXISTS,
97 } else if (status != StorageOperationStatus.OK) {
98 log.debug("Failed to validate uniqueness of CsarUUID {} for resource", csarUUID,
99 resource.getSystemName());
100 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
104 public void validateCsarBeforeCreate(Service resource, String csarUUID) {
105 // check if VF with the same Csar UUID or with he same name already
107 StorageOperationStatus status = toscaOperationFacade.validateCsarUuidUniqueness(csarUUID);
108 log.debug("enter validateCsarBeforeCreate,get status:{}", status);
109 if (status == StorageOperationStatus.ENTITY_ALREADY_EXISTS) {
110 log.debug("Failed to create resource {}, csarUUID {} already exist for a different VF ",
111 resource.getSystemName(), csarUUID);
112 } else if (status != StorageOperationStatus.OK) {
113 log.debug("Failed to validate uniqueness of CsarUUID {} for resource", csarUUID,
114 resource.getSystemName());
115 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status));
119 public CsarInfo getCsarInfo(Resource resource, Resource oldResource,User user, Map<String, byte[]> payload, String csarUUID){
120 Map<String, byte[]> csar = getCsar(resource, user, payload, csarUUID);
121 ImmutablePair<String, String> toscaYamlCsarStatus = validateAndParseCsar(resource,
122 user, csar, csarUUID)
123 .left().on(this::throwComponentException);
125 String checksum = CsarValidationUtils.getToscaYamlChecksum(csar,
126 csarUUID, componentsUtils).left().on(r->logAndThrowComponentException(r, "Failed to calculate checksum for casrUUID {} error {} ", csarUUID));
127 if (oldResource!=null && !checksum.equals(
128 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum())) {
129 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,
130 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition()
131 .getImportedToscaChecksum(),
133 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition()
134 .setImportedToscaChecksum(checksum);
137 return new CsarInfo(user, csarUUID, csar, resource.getName(),
138 toscaYamlCsarStatus.getKey(), toscaYamlCsarStatus.getValue(), true);
141 public CsarInfo getCsarInfo(Service service, Service oldResource,User user, Map<String, byte[]> payload, String csarUUID){
142 Map<String, byte[]> csar = getCsar(service, user, payload, csarUUID);
143 ImmutablePair<String, String> toscaYamlCsarStatus = validateAndParseCsar(service,
144 user, csar, csarUUID)
145 .left().on(this::throwComponentException);
147 String checksum = CsarValidationUtils.getToscaYamlChecksum(csar,
148 csarUUID, componentsUtils).left().on(r->logAndThrowComponentException(r, "Failed to calculate checksum for casrUUID {} error {} ", csarUUID));
149 if (oldResource!=null && !checksum.equals(
150 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum())) {
151 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,
152 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition()
153 .getImportedToscaChecksum(),
155 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition()
156 .setImportedToscaChecksum(checksum);
159 return new CsarInfo(user, csarUUID, csar, service.getName(),
160 toscaYamlCsarStatus.getKey(), toscaYamlCsarStatus.getValue(), true);
163 public ParsedToscaYamlInfo getParsedToscaYamlInfo(String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, String nodeName, Component component) {
164 return yamlHandler.parseResourceInfoFromYAML(
165 yamlName, topologyTemplateYaml, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo,
166 nodeName, component);
169 private String logAndThrowComponentException(ResponseFormat responseFormat, String logMessage, String ...params) {
170 log.debug(logMessage, params, responseFormat);
171 throw new ByResponseFormatComponentException(responseFormat);
174 private ImmutablePair<String,String> throwComponentException(ResponseFormat responseFormat) {
175 throw new ByResponseFormatComponentException(responseFormat);
178 private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Component component, User user,
179 Map<String, byte[]> payload, String csarUUID) {
180 Map<String, byte[]> csar = getCsar(component, user, payload, csarUUID);
181 Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar,
182 csarUUID, componentsUtils);
183 if (validateCsarStatus.isRight()) {
184 ResponseFormat responseFormat = validateCsarStatus.right().value();
185 log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat);
186 BeEcompErrorManager.getInstance()
187 .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
188 if(component instanceof Resource){
189 componentsUtils.auditResource(responseFormat, user, (Resource)component, AuditingActionEnum.CREATE_RESOURCE);
192 return Either.right(responseFormat);
195 Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils
196 .getToscaYaml(csar, csarUUID, componentsUtils);
198 if (toscaYamlCsarStatus.isRight()) {
199 ResponseFormat responseFormat = toscaYamlCsarStatus.right().value();
200 log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
201 BeEcompErrorManager.getInstance()
202 .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 return toscaYamlCsarStatus;
211 private Map<String, byte[]> getCsar(Component component, User user, Map<String, byte[]> payload, String csarUUID) {
212 if (payload != null) {
215 Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUUID, user);
216 if (csar.isRight()) {
217 StorageOperationStatus value = csar.right().value();
218 log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, value);
219 BeEcompErrorManager.getInstance()
220 .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
221 ResponseFormat responseFormat = componentsUtils
222 .getResponseFormat(componentsUtils.convertFromStorageResponse(value), csarUUID);
223 if(component instanceof Resource){
224 Resource newResource = (Resource) component;
225 componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.CREATE_RESOURCE);
228 throw new StorageException(csar.right().value());
230 return csar.left().value();
233 private void auditAndThrowException(Resource resource, User user, AuditingActionEnum auditingAction, ActionStatus status, String... params){
234 ResponseFormat errorResponse = componentsUtils.getResponseFormat(status, params);
235 componentsUtils.auditResource(errorResponse, user, resource, auditingAction);
236 throw new ByResponseFormatComponentException(errorResponse, params);
239 private Map<String, byte[]> getCsar(User user, Map<String, byte[]> payload, String csarUUID) {
240 if (payload != null) {
243 Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUUID, user);
244 if (csar.isRight()) {
245 StorageOperationStatus value = csar.right().value();
246 log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, value);
247 throw new StorageException(value);
249 return csar.left().value();
252 private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Service service, User user,
253 Map<String, byte[]> payload, String csarUUID) {
254 Map<String, byte[]> csar = getCsar(service, user, payload, csarUUID);
255 Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar,
256 csarUUID, componentsUtils);
257 if (validateCsarStatus.isRight()) {
258 ResponseFormat responseFormat = validateCsarStatus.right().value();
259 log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat);
260 BeEcompErrorManager.getInstance()
261 .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
263 return Either.right(responseFormat);
266 Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils
267 .getToscaYaml(csar, csarUUID, componentsUtils);
269 if (toscaYamlCsarStatus.isRight()) {
270 ResponseFormat responseFormat = toscaYamlCsarStatus.right().value();
271 log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
272 BeEcompErrorManager.getInstance()
273 .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
274 return Either.right(responseFormat);
276 return toscaYamlCsarStatus;