1def6e527898c74a9fb68874e1325cdc767b66d4
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / csar / RappCsarConfigurationHandler.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END========================================================================
18  */
19
20 package com.oransc.rappmanager.models.csar;
21
22 import static com.google.common.base.Splitter.on;
23 import static com.google.common.collect.Iterables.filter;
24
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.google.gson.Gson;
28 import com.oransc.rappmanager.models.exception.RappHandlerException;
29 import com.oransc.rappmanager.models.rapp.Rapp;
30 import com.oransc.rappmanager.models.rapp.RappResources;
31 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
32 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
33 import java.io.ByteArrayOutputStream;
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.nio.file.Path;
38 import java.util.List;
39 import java.util.Set;
40 import java.util.UUID;
41 import java.util.function.Predicate;
42 import java.util.stream.Collectors;
43 import java.util.zip.ZipEntry;
44 import java.util.zip.ZipFile;
45 import lombok.RequiredArgsConstructor;
46 import org.apache.commons.compress.archivers.ArchiveEntry;
47 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.stereotype.Service;
52 import org.springframework.web.multipart.MultipartFile;
53 import org.yaml.snakeyaml.Yaml;
54
55 @Service
56 @RequiredArgsConstructor
57 public class RappCsarConfigurationHandler {
58
59     Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
60
61     private final ObjectMapper objectMapper;
62     private static final String TOSCA_METADATA_LOCATION = "TOSCA-Metadata/TOSCA.meta";
63     private static final String ENTRY_DEFINITIONS_INDEX = "Entry-Definitions";
64     private static final String ACM_COMPOSITION_JSON_LOCATION = "Files/Acm/definition/compositions.json";
65     private static final String ACM_DEFINITION_LOCATION = "Files/Acm/definition";
66     private static final String ACM_INSTANCES_LOCATION = "Files/Acm/instances";
67     private static final String SME_PROVIDER_FUNCS_LOCATION = "Files/Sme/providers";
68     private static final String SME_SERVICE_APIS_LOCATION = "Files/Sme/serviceapis";
69     private static final String SME_INVOKERS_LOCATION = "Files/Sme/invokers";
70     private static final String DME_PRODUCER_INFO_TYPES_LOCATION = "Files/Dme/producerinfotypes";
71     private static final String DME_CONSUMER_INFO_TYPES_LOCATION = "Files/Dme/consumerinfotypes";
72     private static final String DME_INFO_PRODUCERS_LOCATION = "Files/Dme/infoproducers";
73     private static final String DME_INFO_CONSUMERS_LOCATION = "Files/Dme/infoconsumers";
74
75
76     public boolean isValidRappPackage(MultipartFile multipartFile) {
77         String originalFilename = multipartFile.getOriginalFilename();
78         if (originalFilename != null) {
79             return originalFilename.endsWith(".csar") && isFileExistsInCsar(multipartFile,
80                     ACM_COMPOSITION_JSON_LOCATION) && isFileExistsInCsar(multipartFile, TOSCA_METADATA_LOCATION)
81                            && containsValidArtifactDefinition(multipartFile);
82         }
83         return false;
84     }
85
86     boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
87         try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
88             ArchiveEntry zipEntry;
89             while ((zipEntry = zipArchiveInputStream.getNextEntry()) != null) {
90                 if (zipEntry.getName().matches(fileLocation)) {
91                     return Boolean.TRUE;
92                 }
93             }
94             throw new RappHandlerException(HttpStatus.BAD_REQUEST, "rApp package missing a file " + fileLocation);
95         } catch (IOException e) {
96             logger.error("Unable to find the CSAR file", e);
97             throw new RappHandlerException(HttpStatus.BAD_REQUEST, "rApp package missing a file " + fileLocation);
98         }
99     }
100
101     public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
102         return Path.of(csarLocation, rappId, fileName);
103     }
104
105     public String getInstantiationPayload(Rapp rapp, RappACMInstance rappACMInstance, UUID compositionId) {
106         return getPayload(rapp, getResourceUri(ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll(
107                 "COMPOSITIONID", String.valueOf(compositionId));
108     }
109
110     String getPayload(Rapp rapp, String location) {
111         logger.debug("Getting payload for {} from {}", rapp.getRappId(), location);
112         File csarFile = getCsarFile(rapp);
113         return getFileFromCsar(csarFile, location).toString();
114     }
115
116     File getCsarFile(Rapp rapp) {
117         return new File(
118                 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
119     }
120
121     ByteArrayOutputStream getFileFromCsar(MultipartFile multipartFile, String fileLocation) {
122         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
123         try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
124             byteArrayOutputStream = getFileFromCsar(zipArchiveInputStream, fileLocation);
125         } catch (IOException e) {
126             logger.info("Unable to get file {} from the multipart CSAR file", fileLocation, e);
127         }
128         return byteArrayOutputStream;
129     }
130
131     ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
132         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
133         try (FileInputStream fileInputStream = new FileInputStream(csarFile);
134              ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
135             byteArrayOutputStream = getFileFromCsar(zipArchiveInputStream, fileLocation);
136         } catch (IOException e) {
137             logger.info("Unable to get file {} from the CSAR file", fileLocation, e);
138         }
139         return byteArrayOutputStream;
140     }
141
142     ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) {
143         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
144         try {
145             ArchiveEntry entry;
146             while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
147                 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
148                     byte[] buffer = new byte[1024];
149                     int bytesRead;
150                     while ((bytesRead = zipArchiveInputStream.read(buffer)) != -1) {
151                         byteArrayOutputStream.write(buffer, 0, bytesRead);
152                     }
153                 }
154             }
155         } catch (IOException e) {
156             logger.info("Unable to get file {} from the zip archive CSAR file", fileLocation, e);
157         }
158         return byteArrayOutputStream;
159     }
160
161     boolean containsValidArtifactDefinition(MultipartFile multipartFile) {
162         String asdLocation = getAsdDefinitionLocation(multipartFile);
163         if (asdLocation != null && !asdLocation.isEmpty() && isFileExistsInCsar(multipartFile, asdLocation)) {
164             try {
165                 String asdContent = getFileFromCsar(multipartFile, asdLocation).toString();
166                 String asdJsonContent = new Gson().toJsonTree(new Yaml().load(asdContent)).toString();
167                 JsonNode jsonNode = objectMapper.readTree(asdJsonContent);
168                 List<String> artifactFileList =
169                         jsonNode.at("/topology_template/node_templates/applicationServiceDescriptor/artifacts")
170                                 .findValuesAsText("file");
171                 return artifactFileList.stream()
172                                .allMatch(artifactFile -> isFileExistsInCsar(multipartFile, artifactFile));
173             } catch (RappHandlerException e) {
174                 throw e;
175             } catch (Exception e) {
176                 logger.warn("Unable to validate artifact definition", e);
177                 throw new RappHandlerException(HttpStatus.BAD_REQUEST, "ASD definition in rApp package is invalid.");
178             }
179         }
180         throw new RappHandlerException(HttpStatus.BAD_REQUEST, "ASD definition in rApp package is invalid.");
181     }
182
183     String getAsdDefinitionLocation(final MultipartFile multipartFile) {
184         String asdLocation = "";
185         final ByteArrayOutputStream fileContent = getFileFromCsar(multipartFile, TOSCA_METADATA_LOCATION);
186         if (fileContent != null) {
187             final String toscaMetadata = fileContent.toString();
188             if (!toscaMetadata.isEmpty()) {
189                 final String entry =
190                         filter(on("\n").split(toscaMetadata), line -> line.contains(ENTRY_DEFINITIONS_INDEX)).iterator()
191                                 .next();
192                 asdLocation = entry.replace(ENTRY_DEFINITIONS_INDEX + ":", "").trim();
193             }
194         }
195         return asdLocation;
196     }
197
198
199     public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
200         return getPayload(rapp, getResourceUri(SME_PROVIDER_FUNCS_LOCATION, rappSMEInstance.getProviderFunction()));
201     }
202
203     public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
204         return getPayload(rapp, getResourceUri(SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
205     }
206
207     public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
208         return getPayload(rapp, getResourceUri(SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
209     }
210
211     public String getAcmCompositionPayload(Rapp rapp) {
212         return getPayload(rapp,
213                 getResourceUri(ACM_DEFINITION_LOCATION, rapp.getRappResources().getAcm().getCompositionDefinitions()));
214     }
215
216     public String getDmeInfoProducerPayload(Rapp rapp, String producerIdentifier) {
217         return getPayload(rapp, getResourceUri(DME_INFO_PRODUCERS_LOCATION, producerIdentifier));
218     }
219
220     public String getDmeProducerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
221         return getPayload(rapp, getResourceUri(DME_PRODUCER_INFO_TYPES_LOCATION, infoTypeIdentifier));
222     }
223
224     public String getDmeConsumerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
225         return getPayload(rapp, getResourceUri(DME_CONSUMER_INFO_TYPES_LOCATION, infoTypeIdentifier));
226     }
227
228     public String getDmeInfoConsumerPayload(Rapp rapp, String infoConsumerIdentifier) {
229         return getPayload(rapp, getResourceUri(DME_INFO_CONSUMERS_LOCATION, infoConsumerIdentifier));
230     }
231
232     String getResourceUri(String resourceLocation, String resource) {
233         return resourceLocation + "/" + resource + ".json";
234     }
235
236     public RappResources getRappResource(Rapp rapp) {
237         RappResources rappResources = new RappResources();
238         try {
239             File csarFile = getCsarFile(rapp);
240             if (csarFile.exists()) {
241                 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
242                         getFileListFromCsar(csarFile, ACM_DEFINITION_LOCATION).iterator().next()).compositionInstances(
243                         getFileListFromCsar(csarFile, ACM_INSTANCES_LOCATION)).build());
244                 rappResources.setSme(RappResources.SMEResources.builder().providerFunctions(
245                                 getFileListFromCsar(csarFile, SME_PROVIDER_FUNCS_LOCATION))
246                                              .serviceApis(getFileListFromCsar(csarFile, SME_SERVICE_APIS_LOCATION))
247                                              .invokers(getFileListFromCsar(csarFile, SME_INVOKERS_LOCATION)).build());
248                 rappResources.setDme(RappResources.DMEResources.builder().producerInfoTypes(
249                                 getFileListFromCsar(csarFile, DME_PRODUCER_INFO_TYPES_LOCATION)).consumerInfoTypes(
250                                 getFileListFromCsar(csarFile, DME_CONSUMER_INFO_TYPES_LOCATION))
251                                              .infoProducers(getFileListFromCsar(csarFile, DME_INFO_PRODUCERS_LOCATION))
252                                              .infoConsumers(getFileListFromCsar(csarFile, DME_INFO_CONSUMERS_LOCATION))
253                                              .build());
254             }
255         } catch (Exception e) {
256             logger.warn("Error in getting the rapp resources", e);
257         }
258         return rappResources;
259     }
260
261     Set<String> getFileListFromCsar(File csarFile, String dirLocation) {
262         try (ZipFile zipFile = new ZipFile(csarFile)) {
263             return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
264                            .filter(name -> name.startsWith(dirLocation))
265                            .map(name -> name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf(".")))
266                            .collect(Collectors.toSet());
267         } catch (IOException e) {
268             logger.warn("Error in listing the files from csar", e);
269         }
270         return Set.of();
271     }
272 }