re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / csar / CsarBusinessLogic.java
1 package org.openecomp.sdc.be.components.csar;
2
3 import fj.data.Either;
4 import org.apache.commons.lang3.tuple.ImmutablePair;
5 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
6 import org.openecomp.sdc.be.components.impl.CsarValidationUtils;
7 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
8 import org.openecomp.sdc.be.config.BeEcompErrorManager;
9 import org.openecomp.sdc.be.dao.api.ActionStatus;
10 import org.openecomp.sdc.be.model.NodeTypeInfo;
11 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
12 import org.openecomp.sdc.be.model.Resource;
13 import org.openecomp.sdc.be.model.User;
14 import org.openecomp.sdc.be.model.operations.StorageException;
15 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
16 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
17 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
18 import org.openecomp.sdc.common.log.wrappers.Logger;
19 import org.openecomp.sdc.exception.ResponseFormat;
20 import org.springframework.beans.factory.annotation.Autowired;
21
22 import java.util.Map;
23
24 @org.springframework.stereotype.Component("csarBusinessLogic")
25 public class CsarBusinessLogic extends BaseBusinessLogic {
26
27     private static final Logger log = Logger.getLogger(CsarBusinessLogic.class);
28
29     private static final String CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID = "Creating resource from CSAR: fetching CSAR with id ";
30     private static final String FAILED = " failed";
31
32     @Autowired
33     private CsarOperation csarOperation;
34
35     @Autowired
36     private YamlTemplateParsingHandler yamlHandler;
37
38     public void setCsarOperation(CsarOperation csarOperation) {
39         this.csarOperation = csarOperation;
40     }
41
42     public void validateCsarBeforeCreate(Resource resource, AuditingActionEnum auditingAction, User user, String csarUUID) {
43         // check if VF with the same Csar UUID or with he same name already
44         // exists
45         StorageOperationStatus status = toscaOperationFacade.validateCsarUuidUniqueness(csarUUID);
46         if(status == StorageOperationStatus.ENTITY_ALREADY_EXISTS){
47             log.debug("Failed to create resource {}, csarUUID {} already exist for a different VF ",
48                     resource.getSystemName(), csarUUID);
49             auditAndThrowException(resource, user, auditingAction, ActionStatus.VSP_ALREADY_EXISTS,
50                     csarUUID);
51         } else if (status != StorageOperationStatus.OK) {
52             log.debug("Failed to validate uniqueness of CsarUUID {} for resource", csarUUID,
53                     resource.getSystemName());
54             throw new ComponentException(componentsUtils.convertFromStorageResponse(status));
55         }
56     }
57
58     public CsarInfo getCsarInfo(Resource resource, Resource oldResource,User user, Map<String, byte[]> payload, String csarUUID){
59         Map<String, byte[]> csar = getCsar(resource, user, payload, csarUUID);
60         ImmutablePair<String, String> toscaYamlCsarStatus = validateAndParseCsar(resource,
61                 user, csar, csarUUID)
62                 .left().on(this::throwComponentException);
63
64         String checksum = CsarValidationUtils.getToscaYamlChecksum(csar,
65                 csarUUID, componentsUtils).left().on(r->logAndThrowComponentException(r, "Failed to calculate checksum for casrUUID {} error {} ", csarUUID));
66         if (oldResource!=null && !checksum.equals(
67                 oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum())) {
68             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,
69                     oldResource.getComponentMetadataDefinition().getMetadataDataDefinition()
70                             .getImportedToscaChecksum(),
71                     checksum);
72             oldResource.getComponentMetadataDefinition().getMetadataDataDefinition()
73                     .setImportedToscaChecksum(checksum);
74         }
75
76         return new CsarInfo(user, csarUUID, csar, resource.getName(),
77                 toscaYamlCsarStatus.getKey(), toscaYamlCsarStatus.getValue(), true);
78     }
79
80
81     public ParsedToscaYamlInfo getParsedToscaYamlInfo(String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, String nodeName) {
82         return yamlHandler.parseResourceInfoFromYAML(
83                 yamlName, topologyTemplateYaml, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo,
84                 nodeName);
85     }
86
87     private String logAndThrowComponentException(ResponseFormat responseFormat, String logMessage, String ...params) {
88         log.debug(logMessage, params, responseFormat);
89         throw new ComponentException(responseFormat);
90     }
91
92     private ImmutablePair<String,String> throwComponentException(ResponseFormat responseFormat) {
93         throw new ComponentException(responseFormat);
94     }
95
96     private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Resource resource, User user,
97                                                                                       Map<String, byte[]> payload, String csarUUID) {
98         Map<String, byte[]> csar = getCsar(resource, user, payload, csarUUID);
99         Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar,
100                 csarUUID, componentsUtils);
101         if (validateCsarStatus.isRight()) {
102             ResponseFormat responseFormat = validateCsarStatus.right().value();
103             log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat);
104             BeEcompErrorManager.getInstance()
105                     .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
106             componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE);
107             return Either.right(responseFormat);
108         }
109
110         Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils
111                 .getToscaYaml(csar, csarUUID, componentsUtils);
112
113         if (toscaYamlCsarStatus.isRight()) {
114             ResponseFormat responseFormat = toscaYamlCsarStatus.right().value();
115             log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
116             BeEcompErrorManager.getInstance()
117                     .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
118             componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE);
119             return Either.right(responseFormat);
120         }
121         return toscaYamlCsarStatus;
122     }
123
124     private Map<String, byte[]> getCsar(Resource resource, User user, Map<String, byte[]> payload, String csarUUID) {
125         if (payload != null) {
126             return payload;
127         }
128         Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUUID, user);
129         if (csar.isRight()) {
130             StorageOperationStatus value = csar.right().value();
131             log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, value);
132             BeEcompErrorManager.getInstance()
133                     .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
134             ResponseFormat responseFormat = componentsUtils
135                     .getResponseFormat(componentsUtils.convertFromStorageResponse(value), csarUUID);
136             componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE);
137             throw new StorageException(csar.right().value());
138         }
139         return csar.left().value();
140     }
141
142     private void auditAndThrowException(Resource resource, User user, AuditingActionEnum auditingAction, ActionStatus status, String... params){
143         ResponseFormat errorResponse = componentsUtils.getResponseFormat(status, params);
144         componentsUtils.auditResource(errorResponse, user, resource, auditingAction);
145         throw new ComponentException(errorResponse);
146     }
147 }