Fix sample rapp generator to support helm artifact alone rApps
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / csar / validator / RappValidationUtils.java
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  *
18  */
19
20 package com.oransc.rappmanager.models.csar.validator;
21
22 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
23 import com.oransc.rappmanager.models.csar.RappCsarPathProvider;
24 import com.oransc.rappmanager.models.exception.RappValidationException;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import lombok.RequiredArgsConstructor;
28 import org.apache.commons.compress.archivers.ArchiveEntry;
29 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
30 import org.springframework.stereotype.Component;
31 import org.springframework.web.multipart.MultipartFile;
32
33 @Component
34 @RequiredArgsConstructor
35 public class RappValidationUtils {
36
37     private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
38
39     boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
40         try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
41             ArchiveEntry zipEntry;
42             while ((zipEntry = zipArchiveInputStream.getNextEntry()) != null) {
43                 if (zipEntry.getName().matches(fileLocation)) {
44                     return Boolean.TRUE;
45                 }
46             }
47             throw new RappValidationException("rApp package missing a file " + fileLocation);
48         } catch (IOException e) {
49             throw new RappValidationException("rApp package missing a file " + fileLocation);
50         }
51     }
52
53     public String getAsdDefinitionLocation(final MultipartFile multipartFile) {
54         return rappCsarConfigurationHandler.getAsdDefinitionLocation(
55                 getFileFromCsar(multipartFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION).toString());
56     }
57
58     ByteArrayOutputStream getFileFromCsar(MultipartFile multipartFile, String fileLocation) {
59         ByteArrayOutputStream byteArrayOutputStream;
60         try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
61             byteArrayOutputStream = rappCsarConfigurationHandler.getFileFromCsar(zipArchiveInputStream, fileLocation);
62         } catch (IOException e) {
63             throw new RappValidationException(
64                     String.format("Unable to get file %s from the multipart CSAR file", fileLocation));
65         }
66         return byteArrayOutputStream;
67     }
68 }