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.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.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.ArrayList;
39 import java.util.List;
41 import java.util.UUID;
42 import java.util.function.Predicate;
43 import java.util.stream.Collectors;
44 import java.util.zip.ZipEntry;
45 import java.util.zip.ZipFile;
46 import lombok.RequiredArgsConstructor;
47 import org.apache.commons.compress.archivers.ArchiveEntry;
48 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.core.io.ByteArrayResource;
52 import org.springframework.stereotype.Service;
53 import org.yaml.snakeyaml.Yaml;
56 @RequiredArgsConstructor
57 public class RappCsarConfigurationHandler {
59 Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
61 private final ObjectMapper objectMapper;
63 public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
64 return Path.of(csarLocation, rappId, fileName);
67 public String getInstantiationPayload(Rapp rapp, RappACMInstance rappACMInstance, UUID compositionId) {
68 return getPayload(rapp,
69 getResourceUri(RappCsarPathProvider.ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll(
70 "DO_NOT_CHANGE_THIS_COMPOSITION_ID", String.valueOf(compositionId));
73 public ByteArrayResource getArtifactPayload(Rapp rapp, String location) {
74 return new ByteArrayResource(getByteArrayStreamPayload(rapp, location).toByteArray());
77 String getPayload(Rapp rapp, String location) {
78 return getByteArrayStreamPayload(rapp, location).toString();
81 ByteArrayOutputStream getByteArrayStreamPayload(Rapp rapp, String location) {
82 logger.debug("Getting payload for {} from {}", rapp.getRappId(), location);
83 File csarFile = getCsarFile(rapp);
84 return getFileFromCsar(csarFile, location);
87 File getCsarFile(Rapp rapp) {
89 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
92 ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
93 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
94 try (FileInputStream fileInputStream = new FileInputStream(csarFile);
95 ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
96 byteArrayOutputStream = getFileFromCsar(zipArchiveInputStream, fileLocation);
97 } catch (IOException e) {
98 logger.info("Unable to get file {} from the CSAR file", fileLocation, e);
100 return byteArrayOutputStream;
103 public ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) {
104 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
107 while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
108 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
109 byte[] buffer = new byte[1024];
111 while ((bytesRead = zipArchiveInputStream.read(buffer)) != -1) {
112 byteArrayOutputStream.write(buffer, 0, bytesRead);
116 } catch (IOException e) {
117 logger.info("Unable to get file {} from the zip archive CSAR file", fileLocation, e);
119 return byteArrayOutputStream;
122 public JsonNode getAsdContentNode(String asdContent) throws JsonProcessingException {
123 return objectMapper.readTree(new Gson().toJsonTree(new Yaml().load(asdContent)).toString());
126 String getAsdDefinitionLocation(final File csarFile) {
127 return getAsdDefinitionLocation(
128 getFileFromCsar(csarFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION).toString());
131 public String getAsdDefinitionLocation(final String toscaMetadata) {
132 String asdLocation = "";
133 if (toscaMetadata != null && !toscaMetadata.isEmpty()) {
134 final String entry = filter(on("\n").split(toscaMetadata),
135 line -> line.contains(RappCsarPathProvider.ENTRY_DEFINITIONS_INDEX)).iterator().next();
136 asdLocation = entry.replace(RappCsarPathProvider.ENTRY_DEFINITIONS_INDEX + ":", "").trim();
141 public List<DeploymentItem> getDeploymentItems(Rapp rApp) {
142 List<DeploymentItem> deploymentItems = new ArrayList<>();
143 File csarFile = getCsarFile(rApp);
144 String asdDefinitionLocation = getAsdDefinitionLocation(csarFile);
145 if (asdDefinitionLocation != null && !asdDefinitionLocation.isEmpty()) {
147 String asdContent = getFileFromCsar(csarFile, asdDefinitionLocation).toString();
148 JsonNode jsonNode = getAsdContentNode(asdContent);
149 JsonNode artifactsJsonNode = jsonNode.at(RappCsarPathProvider.ARTIFACTS_LOCATION_JSON_POINTER);
150 artifactsJsonNode.forEach(artifactJsonNode -> {
151 DeploymentItem deploymentItem =
152 objectMapper.convertValue(artifactJsonNode.at("/properties"), DeploymentItem.class);
153 deploymentItem.setFile(artifactJsonNode.at("/file").asText());
154 deploymentItems.add(deploymentItem);
156 } catch (Exception e) {
157 logger.warn("Unable to get the deployment items", e);
160 return deploymentItems;
164 public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
165 return getPayload(rapp, getResourceUri(RappCsarPathProvider.SME_PROVIDER_FUNCS_LOCATION,
166 rappSMEInstance.getProviderFunction()));
169 public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
170 return getPayload(rapp,
171 getResourceUri(RappCsarPathProvider.SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
174 public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
175 return getPayload(rapp,
176 getResourceUri(RappCsarPathProvider.SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
179 public String getAcmCompositionPayload(Rapp rapp) {
180 return getPayload(rapp, getResourceUri(RappCsarPathProvider.ACM_DEFINITION_LOCATION,
181 rapp.getRappResources().getAcm().getCompositionDefinitions()));
184 public String getDmeInfoProducerPayload(Rapp rapp, String producerIdentifier) {
185 return getPayload(rapp, getResourceUri(RappCsarPathProvider.DME_INFO_PRODUCERS_LOCATION, producerIdentifier));
188 public String getDmeProducerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
189 return getPayload(rapp,
190 getResourceUri(RappCsarPathProvider.DME_PRODUCER_INFO_TYPES_LOCATION, infoTypeIdentifier));
193 public String getDmeConsumerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
194 return getPayload(rapp,
195 getResourceUri(RappCsarPathProvider.DME_CONSUMER_INFO_TYPES_LOCATION, infoTypeIdentifier));
198 public String getDmeInfoConsumerPayload(Rapp rapp, String infoConsumerIdentifier) {
199 return getPayload(rapp,
200 getResourceUri(RappCsarPathProvider.DME_INFO_CONSUMERS_LOCATION, infoConsumerIdentifier));
203 String getResourceUri(String resourceLocation, String resource) {
204 return resourceLocation + "/" + resource + ".json";
207 public RappResources getRappResource(Rapp rapp) {
208 RappResources rappResources = new RappResources();
210 File csarFile = getCsarFile(rapp);
211 if (csarFile.exists()) {
212 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
213 getFileListFromCsar(csarFile, RappCsarPathProvider.ACM_DEFINITION_LOCATION).iterator().next())
214 .compositionInstances(getFileListFromCsar(csarFile,
215 RappCsarPathProvider.ACM_INSTANCES_LOCATION)).build());
216 rappResources.setSme(RappResources.SMEResources.builder().providerFunctions(
217 getFileListFromCsar(csarFile, RappCsarPathProvider.SME_PROVIDER_FUNCS_LOCATION)).serviceApis(
218 getFileListFromCsar(csarFile, RappCsarPathProvider.SME_SERVICE_APIS_LOCATION)).invokers(
219 getFileListFromCsar(csarFile, RappCsarPathProvider.SME_INVOKERS_LOCATION)).build());
220 rappResources.setDme(RappResources.DMEResources.builder().producerInfoTypes(
221 getFileListFromCsar(csarFile, RappCsarPathProvider.DME_PRODUCER_INFO_TYPES_LOCATION))
222 .consumerInfoTypes(getFileListFromCsar(csarFile,
223 RappCsarPathProvider.DME_CONSUMER_INFO_TYPES_LOCATION))
224 .infoProducers(getFileListFromCsar(csarFile,
225 RappCsarPathProvider.DME_INFO_PRODUCERS_LOCATION)).infoConsumers(
226 getFileListFromCsar(csarFile, RappCsarPathProvider.DME_INFO_CONSUMERS_LOCATION))
229 } catch (Exception e) {
230 logger.warn("Error in getting the rapp resources", e);
232 return rappResources;
235 Set<String> getFileListFromCsar(File csarFile, String dirLocation) {
236 try (ZipFile zipFile = new ZipFile(csarFile)) {
237 return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
238 .filter(name -> name.startsWith(dirLocation))
239 .map(name -> name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf(".")))
240 .collect(Collectors.toSet());
241 } catch (IOException e) {
242 logger.warn("Error in listing the files from csar", e);