Merge "Add support to upload deployment helm artifacts to chartmuseum"
[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.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.google.gson.Gson;
29 import com.oransc.rappmanager.models.exception.RappHandlerException;
30 import com.oransc.rappmanager.models.rapp.Rapp;
31 import com.oransc.rappmanager.models.rapp.RappResources;
32 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
33 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
34 import java.io.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.nio.file.Path;
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Set;
42 import java.util.UUID;
43 import java.util.function.Predicate;
44 import java.util.stream.Collectors;
45 import java.util.zip.ZipEntry;
46 import java.util.zip.ZipFile;
47 import lombok.RequiredArgsConstructor;
48 import org.apache.commons.compress.archivers.ArchiveEntry;
49 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.core.io.ByteArrayResource;
53 import org.springframework.http.HttpStatus;
54 import org.springframework.stereotype.Service;
55 import org.springframework.web.multipart.MultipartFile;
56 import org.yaml.snakeyaml.Yaml;
57
58 @Service
59 @RequiredArgsConstructor
60 public class RappCsarConfigurationHandler {
61
62     Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
63
64     private final ObjectMapper objectMapper;
65     private static final String TOSCA_METADATA_LOCATION = "TOSCA-Metadata/TOSCA.meta";
66     private static final String ENTRY_DEFINITIONS_INDEX = "Entry-Definitions";
67     private static final String ACM_COMPOSITION_JSON_LOCATION = "Files/Acm/definition/compositions.json";
68     private static final String ACM_DEFINITION_LOCATION = "Files/Acm/definition";
69     private static final String ACM_INSTANCES_LOCATION = "Files/Acm/instances";
70     private static final String SME_PROVIDER_FUNCS_LOCATION = "Files/Sme/providers";
71     private static final String SME_SERVICE_APIS_LOCATION = "Files/Sme/serviceapis";
72     private static final String SME_INVOKERS_LOCATION = "Files/Sme/invokers";
73     private static final String DME_PRODUCER_INFO_TYPES_LOCATION = "Files/Dme/producerinfotypes";
74     private static final String DME_CONSUMER_INFO_TYPES_LOCATION = "Files/Dme/consumerinfotypes";
75     private static final String DME_INFO_PRODUCERS_LOCATION = "Files/Dme/infoproducers";
76     private static final String DME_INFO_CONSUMERS_LOCATION = "Files/Dme/infoconsumers";
77     private static final String ARTIFACTS_LOCATION_JSON_POINTER =
78             "/topology_template/node_templates/applicationServiceDescriptor/artifacts";
79
80
81     public boolean isValidRappPackage(MultipartFile multipartFile) {
82         String originalFilename = multipartFile.getOriginalFilename();
83         if (originalFilename != null) {
84             return originalFilename.endsWith(".csar") && isFileExistsInCsar(multipartFile,
85                     ACM_COMPOSITION_JSON_LOCATION) && isFileExistsInCsar(multipartFile, TOSCA_METADATA_LOCATION)
86                            && containsValidArtifactDefinition(multipartFile);
87         }
88         return false;
89     }
90
91     boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
92         try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
93             ArchiveEntry zipEntry;
94             while ((zipEntry = zipArchiveInputStream.getNextEntry()) != null) {
95                 if (zipEntry.getName().matches(fileLocation)) {
96                     return Boolean.TRUE;
97                 }
98             }
99             throw new RappHandlerException(HttpStatus.BAD_REQUEST, "rApp package missing a file " + fileLocation);
100         } catch (IOException e) {
101             logger.error("Unable to find the CSAR file", e);
102             throw new RappHandlerException(HttpStatus.BAD_REQUEST, "rApp package missing a file " + fileLocation);
103         }
104     }
105
106     public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
107         return Path.of(csarLocation, rappId, fileName);
108     }
109
110     public String getInstantiationPayload(Rapp rapp, RappACMInstance rappACMInstance, UUID compositionId) {
111         return getPayload(rapp, getResourceUri(ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll(
112                 "COMPOSITIONID", String.valueOf(compositionId));
113     }
114
115     public ByteArrayResource getArtifactPayload(Rapp rapp, String location) {
116         return new ByteArrayResource(getByteArrayStreamPayload(rapp, location).toByteArray());
117     }
118
119     String getPayload(Rapp rapp, String location) {
120         return getByteArrayStreamPayload(rapp, location).toString();
121     }
122
123     ByteArrayOutputStream getByteArrayStreamPayload(Rapp rapp, String location) {
124         logger.debug("Getting payload for {} from {}", rapp.getRappId(), location);
125         File csarFile = getCsarFile(rapp);
126         return getFileFromCsar(csarFile, location);
127     }
128
129     File getCsarFile(Rapp rapp) {
130         return new File(
131                 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
132     }
133
134     ByteArrayOutputStream getFileFromCsar(MultipartFile multipartFile, String fileLocation) {
135         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
136         try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
137             byteArrayOutputStream = getFileFromCsar(zipArchiveInputStream, fileLocation);
138         } catch (IOException e) {
139             logger.info("Unable to get file {} from the multipart CSAR file", fileLocation, e);
140         }
141         return byteArrayOutputStream;
142     }
143
144     ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
145         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
146         try (FileInputStream fileInputStream = new FileInputStream(csarFile);
147              ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
148             byteArrayOutputStream = getFileFromCsar(zipArchiveInputStream, fileLocation);
149         } catch (IOException e) {
150             logger.info("Unable to get file {} from the CSAR file", fileLocation, e);
151         }
152         return byteArrayOutputStream;
153     }
154
155     ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) {
156         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
157         try {
158             ArchiveEntry entry;
159             while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
160                 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
161                     byte[] buffer = new byte[1024];
162                     int bytesRead;
163                     while ((bytesRead = zipArchiveInputStream.read(buffer)) != -1) {
164                         byteArrayOutputStream.write(buffer, 0, bytesRead);
165                     }
166                 }
167             }
168         } catch (IOException e) {
169             logger.info("Unable to get file {} from the zip archive CSAR file", fileLocation, e);
170         }
171         return byteArrayOutputStream;
172     }
173
174     boolean containsValidArtifactDefinition(MultipartFile multipartFile) {
175         String asdLocation = getAsdDefinitionLocation(multipartFile);
176         if (asdLocation != null && !asdLocation.isEmpty() && isFileExistsInCsar(multipartFile, asdLocation)) {
177             try {
178                 String asdContent = getFileFromCsar(multipartFile, asdLocation).toString();
179                 JsonNode jsonNode = getAsdContentNode(asdContent);
180                 List<String> artifactFileList = jsonNode.at(ARTIFACTS_LOCATION_JSON_POINTER).findValuesAsText("file");
181                 return artifactFileList.stream()
182                                .allMatch(artifactFile -> isFileExistsInCsar(multipartFile, artifactFile));
183             } catch (RappHandlerException e) {
184                 throw e;
185             } catch (Exception e) {
186                 logger.warn("Unable to validate artifact definition", e);
187                 throw new RappHandlerException(HttpStatus.BAD_REQUEST, "ASD definition in rApp package is invalid.");
188             }
189         }
190         throw new RappHandlerException(HttpStatus.BAD_REQUEST, "ASD definition in rApp package is invalid.");
191     }
192
193     JsonNode getAsdContentNode(String asdContent) throws JsonProcessingException {
194         return objectMapper.readTree(new Gson().toJsonTree(new Yaml().load(asdContent)).toString());
195     }
196
197     String getAsdDefinitionLocation(final File csarFile) {
198         return getAsdDefinitionLocation(getFileFromCsar(csarFile, TOSCA_METADATA_LOCATION).toString());
199     }
200
201     String getAsdDefinitionLocation(final MultipartFile multipartFile) {
202         return getAsdDefinitionLocation(getFileFromCsar(multipartFile, TOSCA_METADATA_LOCATION).toString());
203     }
204
205     String getAsdDefinitionLocation(final String toscaMetadata) {
206         String asdLocation = "";
207         if (toscaMetadata != null && !toscaMetadata.isEmpty()) {
208             final String entry =
209                     filter(on("\n").split(toscaMetadata), line -> line.contains(ENTRY_DEFINITIONS_INDEX)).iterator()
210                             .next();
211             asdLocation = entry.replace(ENTRY_DEFINITIONS_INDEX + ":", "").trim();
212         }
213         return asdLocation;
214     }
215
216     public List<DeploymentItem> getDeploymentItems(Rapp rApp) {
217         List<DeploymentItem> deploymentItems = new ArrayList<>();
218         File csarFile = getCsarFile(rApp);
219         String asdDefinitionLocation = getAsdDefinitionLocation(csarFile);
220         if (asdDefinitionLocation != null && !asdDefinitionLocation.isEmpty()) {
221             try {
222                 String asdContent = getFileFromCsar(csarFile, asdDefinitionLocation).toString();
223                 JsonNode jsonNode = getAsdContentNode(asdContent);
224                 JsonNode artifactsJsonNode = jsonNode.at(ARTIFACTS_LOCATION_JSON_POINTER);
225                 artifactsJsonNode.forEach(artifactJsonNode -> {
226                     DeploymentItem deploymentItem =
227                             objectMapper.convertValue(artifactJsonNode.at("/properties"), DeploymentItem.class);
228                     deploymentItem.setFile(artifactJsonNode.at("/file").asText());
229                     deploymentItems.add(deploymentItem);
230                 });
231             } catch (Exception e) {
232                 logger.warn("Unable to get the deployment items", e);
233             }
234         }
235         return deploymentItems;
236     }
237
238
239     public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
240         return getPayload(rapp, getResourceUri(SME_PROVIDER_FUNCS_LOCATION, rappSMEInstance.getProviderFunction()));
241     }
242
243     public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
244         return getPayload(rapp, getResourceUri(SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
245     }
246
247     public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
248         return getPayload(rapp, getResourceUri(SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
249     }
250
251     public String getAcmCompositionPayload(Rapp rapp) {
252         return getPayload(rapp,
253                 getResourceUri(ACM_DEFINITION_LOCATION, rapp.getRappResources().getAcm().getCompositionDefinitions()));
254     }
255
256     public String getDmeInfoProducerPayload(Rapp rapp, String producerIdentifier) {
257         return getPayload(rapp, getResourceUri(DME_INFO_PRODUCERS_LOCATION, producerIdentifier));
258     }
259
260     public String getDmeProducerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
261         return getPayload(rapp, getResourceUri(DME_PRODUCER_INFO_TYPES_LOCATION, infoTypeIdentifier));
262     }
263
264     public String getDmeConsumerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
265         return getPayload(rapp, getResourceUri(DME_CONSUMER_INFO_TYPES_LOCATION, infoTypeIdentifier));
266     }
267
268     public String getDmeInfoConsumerPayload(Rapp rapp, String infoConsumerIdentifier) {
269         return getPayload(rapp, getResourceUri(DME_INFO_CONSUMERS_LOCATION, infoConsumerIdentifier));
270     }
271
272     String getResourceUri(String resourceLocation, String resource) {
273         return resourceLocation + "/" + resource + ".json";
274     }
275
276     public RappResources getRappResource(Rapp rapp) {
277         RappResources rappResources = new RappResources();
278         try {
279             File csarFile = getCsarFile(rapp);
280             if (csarFile.exists()) {
281                 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
282                         getFileListFromCsar(csarFile, ACM_DEFINITION_LOCATION).iterator().next()).compositionInstances(
283                         getFileListFromCsar(csarFile, ACM_INSTANCES_LOCATION)).build());
284                 rappResources.setSme(RappResources.SMEResources.builder().providerFunctions(
285                                 getFileListFromCsar(csarFile, SME_PROVIDER_FUNCS_LOCATION))
286                                              .serviceApis(getFileListFromCsar(csarFile, SME_SERVICE_APIS_LOCATION))
287                                              .invokers(getFileListFromCsar(csarFile, SME_INVOKERS_LOCATION)).build());
288                 rappResources.setDme(RappResources.DMEResources.builder().producerInfoTypes(
289                                 getFileListFromCsar(csarFile, DME_PRODUCER_INFO_TYPES_LOCATION)).consumerInfoTypes(
290                                 getFileListFromCsar(csarFile, DME_CONSUMER_INFO_TYPES_LOCATION))
291                                              .infoProducers(getFileListFromCsar(csarFile, DME_INFO_PRODUCERS_LOCATION))
292                                              .infoConsumers(getFileListFromCsar(csarFile, DME_INFO_CONSUMERS_LOCATION))
293                                              .build());
294             }
295         } catch (Exception e) {
296             logger.warn("Error in getting the rapp resources", e);
297         }
298         return rappResources;
299     }
300
301     Set<String> getFileListFromCsar(File csarFile, String dirLocation) {
302         try (ZipFile zipFile = new ZipFile(csarFile)) {
303             return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
304                            .filter(name -> name.startsWith(dirLocation))
305                            .map(name -> name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf(".")))
306                            .collect(Collectors.toSet());
307         } catch (IOException e) {
308             logger.warn("Error in listing the files from csar", e);
309         }
310         return Set.of();
311     }
312 }