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
10 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
20 package com.oransc.rappmanager.models.csar;
22 import static com.google.common.base.Splitter.on;
23 import static com.google.common.collect.Iterables.filter;
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;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.nio.file.Path;
38 import java.util.List;
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;
56 @RequiredArgsConstructor
57 public class RappCsarConfigurationHandler {
59 Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
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";
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);
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)) {
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);
101 public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
102 return Path.of(csarLocation, rappId, fileName);
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));
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();
116 File getCsarFile(Rapp rapp) {
118 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
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);
128 return byteArrayOutputStream;
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);
139 return byteArrayOutputStream;
142 ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) {
143 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
146 while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
147 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
148 byte[] buffer = new byte[1024];
150 while ((bytesRead = zipArchiveInputStream.read(buffer)) != -1) {
151 byteArrayOutputStream.write(buffer, 0, bytesRead);
155 } catch (IOException e) {
156 logger.info("Unable to get file {} from the zip archive CSAR file", fileLocation, e);
158 return byteArrayOutputStream;
161 boolean containsValidArtifactDefinition(MultipartFile multipartFile) {
162 String asdLocation = getAsdDefinitionLocation(multipartFile);
163 if (asdLocation != null && !asdLocation.isEmpty() && isFileExistsInCsar(multipartFile, asdLocation)) {
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) {
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.");
180 throw new RappHandlerException(HttpStatus.BAD_REQUEST, "ASD definition in rApp package is invalid.");
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()) {
190 filter(on("\n").split(toscaMetadata), line -> line.contains(ENTRY_DEFINITIONS_INDEX)).iterator()
192 asdLocation = entry.replace(ENTRY_DEFINITIONS_INDEX + ":", "").trim();
199 public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
200 return getPayload(rapp, getResourceUri(SME_PROVIDER_FUNCS_LOCATION, rappSMEInstance.getProviderFunction()));
203 public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
204 return getPayload(rapp, getResourceUri(SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
207 public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
208 return getPayload(rapp, getResourceUri(SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
211 public String getAcmCompositionPayload(Rapp rapp) {
212 return getPayload(rapp,
213 getResourceUri(ACM_DEFINITION_LOCATION, rapp.getRappResources().getAcm().getCompositionDefinitions()));
216 public String getDmeInfoProducerPayload(Rapp rapp, String producerIdentifier) {
217 return getPayload(rapp, getResourceUri(DME_INFO_PRODUCERS_LOCATION, producerIdentifier));
220 public String getDmeProducerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
221 return getPayload(rapp, getResourceUri(DME_PRODUCER_INFO_TYPES_LOCATION, infoTypeIdentifier));
224 public String getDmeConsumerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
225 return getPayload(rapp, getResourceUri(DME_CONSUMER_INFO_TYPES_LOCATION, infoTypeIdentifier));
228 public String getDmeInfoConsumerPayload(Rapp rapp, String infoConsumerIdentifier) {
229 return getPayload(rapp, getResourceUri(DME_INFO_CONSUMERS_LOCATION, infoConsumerIdentifier));
232 String getResourceUri(String resourceLocation, String resource) {
233 return resourceLocation + "/" + resource + ".json";
236 public RappResources getRappResource(Rapp rapp) {
237 RappResources rappResources = new RappResources();
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))
255 } catch (Exception e) {
256 logger.warn("Error in getting the rapp resources", e);
258 return rappResources;
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);