aa5706c77f6dc422144073ee169f8b8f3a558db4
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / csar / CsarBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components.csar;
23
24 import fj.data.Either;
25 import java.util.Map;
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;
56
57 @org.springframework.stereotype.Component("csarBusinessLogic")
58 public class CsarBusinessLogic extends BaseBusinessLogic {
59
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;
65
66     @Autowired
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;
74     }
75
76     @Autowired
77     public void setCsarOperation(CsarOperation csarOperation) {
78         this.csarOperation = csarOperation;
79     }
80
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
83
84         // exists
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));
92         }
93     }
94
95     public void validateCsarBeforeCreate(Service resource, String csarUUID) {
96         // check if VF with the same Csar UUID or with he same name already
97
98         // exists
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));
106         }
107     }
108
109     public OnboardedCsarInfo getCsarInfo(Resource resource, Resource oldResource, User user, Map<String, byte[]> payload, String csarUUID) {
110         Map<String, byte[]> csar = payload;
111         if (csar == null) {
112             final var vendorSoftwareProduct = getCsar(resource, user);
113             validateModel(resource, vendorSoftwareProduct);
114
115             csar = vendorSoftwareProduct.getFileMap();
116         }
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())) {
123             log.debug(
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);
127         }
128         return new OnboardedCsarInfo(user, csarUUID, resource.getCsarVersionId(), csar, resource.getName(), toscaYamlCsarStatus.getKey(),
129             toscaYamlCsarStatus.getValue(), true);
130     }
131
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);
137             }
138             return;
139         }
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);
144         }
145     }
146
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())) {
155             log.debug(
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);
159         }
160         return new ServiceCsarInfo(user, csarUUID, csar, service.getName(), toscaYamlCsarStatus.getKey(), toscaYamlCsarStatus.getValue(), true);
161     }
162
163     public ParsedToscaYamlInfo getParsedToscaYamlInfo(String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo,
164                                                       CsarInfo csarInfo, String nodeName, Component component) {
165         return yamlHandler
166             .parseResourceInfoFromYAML(yamlName, topologyTemplateYaml, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, nodeName,
167                 component, getInterfaceTemplateYaml(csarInfo).orElse(""));
168     }
169
170     private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
171         String[] yamlFile;
172         String interfaceTemplateYaml = "";
173         if (csarInfo.getMainTemplateName().contains(".yml")) {
174             yamlFile = csarInfo.getMainTemplateName().split(".yml");
175             interfaceTemplateYaml = yamlFile[0] + "-interface.yml";
176         } else if (csarInfo.getMainTemplateName().contains(".yaml")) {
177             yamlFile = csarInfo.getMainTemplateName().split(".yaml");
178             interfaceTemplateYaml = yamlFile[0] + "-interface.yaml";
179         }
180         if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
181             return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
182         }
183         return Optional.empty();
184     }
185
186     private String logAndThrowComponentException(ResponseFormat responseFormat, String logMessage, String... params) {
187         log.debug(logMessage, params, responseFormat);
188         throw new ByResponseFormatComponentException(responseFormat);
189     }
190
191     private ImmutablePair<String, String> throwComponentException(ResponseFormat responseFormat) {
192         throw new ByResponseFormatComponentException(responseFormat);
193     }
194
195     private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Component component, User user, Map<String, byte[]> csar,
196                                                                                        String csarUUID) {
197         Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar, csarUUID, componentsUtils);
198         if (validateCsarStatus.isRight()) {
199             ResponseFormat responseFormat = validateCsarStatus.right().value();
200             log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat);
201             BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
202             if (component instanceof Resource) {
203                 componentsUtils.auditResource(responseFormat, user, (Resource) component, AuditingActionEnum.CREATE_RESOURCE);
204             }
205             return Either.right(responseFormat);
206         }
207         Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils.getToscaYaml(csar, csarUUID, componentsUtils);
208         if (toscaYamlCsarStatus.isRight()) {
209             ResponseFormat responseFormat = toscaYamlCsarStatus.right().value();
210             log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
211             BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
212             if (component instanceof Resource) {
213                 componentsUtils.auditResource(responseFormat, user, (Resource) component, AuditingActionEnum.CREATE_RESOURCE);
214             }
215             return Either.right(responseFormat);
216         }
217         return toscaYamlCsarStatus;
218     }
219
220     private Map<String, byte[]> getCsar(Component component, User user, Map<String, byte[]> payload, String csarUUID) {
221         if (payload != null) {
222             return payload;
223         }
224         Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.findVspLatestPackage(csarUUID, user);
225         if (csar.isRight()) {
226             StorageOperationStatus value = csar.right().value();
227             log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, value);
228             BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
229             ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(value), csarUUID);
230             if (component instanceof Resource) {
231                 Resource newResource = (Resource) component;
232                 componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.CREATE_RESOURCE);
233             }
234             throw new StorageException(csar.right().value());
235         }
236         return csar.left().value();
237     }
238
239     private VendorSoftwareProduct getCsar(final Resource resource, final User user) {
240         final Optional<VendorSoftwareProduct> vendorSoftwareProductOpt;
241         try {
242             if (resource.getCsarVersionId() == null) {
243                 vendorSoftwareProductOpt = csarOperation.findLatestVsp(resource.getCsarUUID(), user);
244             } else {
245                 vendorSoftwareProductOpt = csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user);
246             }
247         } catch (final Exception exception) {
248             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, CsarBusinessLogic.class.getName(), exception.getMessage());
249             auditGetCsarError(resource, user, resource.getCsarUUID(), StorageOperationStatus.GENERAL_ERROR);
250             throw new ByActionStatusComponentException(ActionStatus.VSP_FIND_ERROR, resource.getCsarUUID(), resource.getCsarVersionId());
251         }
252         if (vendorSoftwareProductOpt.isEmpty()) {
253             auditGetCsarError(resource, user, resource.getCsarUUID(), StorageOperationStatus.CSAR_NOT_FOUND);
254             throw new ByActionStatusComponentException(ActionStatus.VSP_NOT_FOUND, resource.getCsarUUID(), resource.getCsarVersionId());
255         }
256         return vendorSoftwareProductOpt.get();
257     }
258
259     private void auditGetCsarError(Component component, User user, String csarUUID, StorageOperationStatus storageOperationStatus) {
260         log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, storageOperationStatus);
261         BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
262         var responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(storageOperationStatus), csarUUID);
263         if (component instanceof Resource) {
264             componentsUtils.auditResource(responseFormat, user, (Resource) component, AuditingActionEnum.CREATE_RESOURCE);
265         }
266     }
267
268     private void auditAndThrowException(Resource resource, User user, AuditingActionEnum auditingAction, ActionStatus status, String... params) {
269         ResponseFormat errorResponse = componentsUtils.getResponseFormat(status, params);
270         componentsUtils.auditResource(errorResponse, user, resource, auditingAction);
271         throw new ByResponseFormatComponentException(errorResponse, params);
272     }
273
274     private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Service service, User user, Map<String, byte[]> payload,
275                                                                                        String csarUUID) {
276         Map<String, byte[]> csar = getCsar(service, user, payload, csarUUID);
277         Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar, csarUUID, componentsUtils);
278         if (validateCsarStatus.isRight()) {
279             ResponseFormat responseFormat = validateCsarStatus.right().value();
280             log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat);
281             BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
282             return Either.right(responseFormat);
283         }
284         Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils.getToscaYaml(csar, csarUUID, componentsUtils);
285         if (toscaYamlCsarStatus.isRight()) {
286             ResponseFormat responseFormat = toscaYamlCsarStatus.right().value();
287             log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
288             BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED);
289             return Either.right(responseFormat);
290         }
291         return toscaYamlCsarStatus;
292     }
293 }