5574f389f6465747e69378e6b97c5ea0e57c09a1
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / RappCsarConfigurationHandler.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. 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 package com.oransc.rappmanager.models;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.nio.file.Path;
26 import java.util.UUID;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipInputStream;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
32 import org.springframework.web.multipart.MultipartFile;
33
34 @Service
35 public class RappCsarConfigurationHandler {
36
37     Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
38     private final String acmInstantiationJsonLocation = "Files/Acm/instantiation.json";
39
40     private final String smeProviderDomainLocation = "Files/Sme/provider-domain.json";
41
42     private final String smeProviderApiLocation = "Files/Sme/provider-api.json";
43
44     private final String smeInvokerLocation = "Files/Sme/invoker.json";
45
46
47     public boolean isValidRappPackage(MultipartFile multipartFile) {
48         return multipartFile.getOriginalFilename() != null && multipartFile.getOriginalFilename().endsWith(".csar")
49                        && isFileExistsInCsar(multipartFile, acmInstantiationJsonLocation);
50     }
51
52     boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
53         try (ZipInputStream zipInputStream = new ZipInputStream(multipartFile.getInputStream())) {
54             ZipEntry zipEntry;
55             while ((zipEntry = zipInputStream.getNextEntry()) != null) {
56                 if (zipEntry.getName().matches(fileLocation)) {
57                     return Boolean.TRUE;
58                 }
59             }
60             return Boolean.FALSE;
61         } catch (IOException e) {
62             logger.error("Unable to find the CSAR file", e);
63             return Boolean.FALSE;
64         }
65     }
66
67     public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
68         return Path.of(csarLocation, rappId, fileName);
69     }
70
71     public String getInstantiationPayload(Rapp rapp, UUID compositionId) {
72         return getPayload(rapp, acmInstantiationJsonLocation).replaceAll("COMPOSITIONID",
73                 String.valueOf(compositionId));
74     }
75
76     String getPayload(Rapp rapp, String location) {
77         File csarFile = new File(
78                 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
79         return getFileFromCsar(csarFile, location).toString();
80     }
81
82     ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
83         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
84         try (FileInputStream fileInputStream = new FileInputStream(csarFile);
85              ZipInputStream zipInputStream = new ZipInputStream(fileInputStream)) {
86             ZipEntry entry;
87             while ((entry = zipInputStream.getNextEntry()) != null) {
88                 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
89                     byte[] buffer = new byte[1024];
90                     int bytesRead;
91                     while ((bytesRead = zipInputStream.read(buffer)) != -1) {
92                         byteArrayOutputStream.write(buffer, 0, bytesRead);
93                     }
94                 }
95             }
96         } catch (IOException e) {
97             logger.error("Unable to find the CSAR file", e);
98         }
99         return byteArrayOutputStream;
100     }
101
102
103     public String getSmeProviderDomainPayload(Rapp rapp) {
104         return getPayload(rapp, smeProviderDomainLocation);
105     }
106
107     public String getSmeProviderApiPayload(Rapp rapp) {
108         return getPayload(rapp, smeProviderApiLocation);
109     }
110
111     public String getSmeInvokerPayload(Rapp rapp) {
112         return getPayload(rapp, smeInvokerLocation);
113     }
114
115 }