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.configuration.RappsEnvironmentConfiguration;
30 import com.oransc.rappmanager.models.rapp.Rapp;
31 import com.oransc.rappmanager.models.rapp.RappResources;
32 import com.oransc.rappmanager.models.rappinstance.RappInstance;
33 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
34 import java.io.ByteArrayOutputStream;
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;
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.stereotype.Service;
54 import org.yaml.snakeyaml.Yaml;
57 @RequiredArgsConstructor
58 public class RappCsarConfigurationHandler {
60 Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
62 private final ObjectMapper objectMapper;
63 private final RappsEnvironmentConfiguration rappsEnvironmentConfiguration;
65 public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
66 return Path.of(csarLocation, rappId, fileName);
69 public String getInstantiationPayload(Rapp rapp, RappInstance rappInstance, UUID compositionId) {
70 return getPayload(rapp, getResourceUri(RappCsarPathProvider.ACM_INSTANCES_LOCATION,
71 rappInstance.getAcm().getInstance())).replace("DO_NOT_CHANGE_THIS_COMPOSITION_ID",
72 String.valueOf(compositionId))
73 .replace("DO_NOT_CHANGE_THIS_RAPP_INSTANCE_ID", String.valueOf(rappInstance.getRappInstanceId()))
74 .replace("DO_NOT_CHANGE_THIS_SME_DISCOVERY_ENDPOINT",
75 rappsEnvironmentConfiguration.getSmeDiscoveryEndpoint());
78 public ByteArrayResource getArtifactPayload(Rapp rapp, String location) {
79 return new ByteArrayResource(getByteArrayStreamPayload(rapp, location).toByteArray());
82 String getPayload(Rapp rapp, String location) {
83 return getByteArrayStreamPayload(rapp, location).toString();
86 ByteArrayOutputStream getByteArrayStreamPayload(Rapp rapp, String location) {
87 logger.debug("Getting payload for {} from {}", rapp.getRappId(), location);
88 File csarFile = getCsarFile(rapp);
89 return getFileFromCsar(csarFile, location);
92 File getCsarFile(Rapp rapp) {
94 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
97 ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
98 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
99 try (FileInputStream fileInputStream = new FileInputStream(csarFile);
100 ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
101 byteArrayOutputStream = getFileFromCsar(zipArchiveInputStream, fileLocation);
102 } catch (IOException e) {
103 logger.info("Unable to get file {} from the CSAR file", fileLocation, e);
105 return byteArrayOutputStream;
108 public ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) {
109 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
112 while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
113 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
114 byte[] buffer = new byte[1024];
116 while ((bytesRead = zipArchiveInputStream.read(buffer)) != -1) {
117 byteArrayOutputStream.write(buffer, 0, bytesRead);
121 } catch (IOException e) {
122 logger.info("Unable to get file {} from the zip archive CSAR file", fileLocation, e);
124 return byteArrayOutputStream;
127 public JsonNode getAsdContentNode(String asdContent) throws JsonProcessingException {
128 return objectMapper.readTree(new Gson().toJsonTree(new Yaml().load(asdContent)).toString());
131 String getAsdDefinitionLocation(final File csarFile) {
132 return getAsdDefinitionLocation(
133 getFileFromCsar(csarFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION).toString());
136 public String getAsdDefinitionLocation(final String toscaMetadata) {
137 String asdLocation = "";
138 if (toscaMetadata != null && !toscaMetadata.isEmpty()) {
139 final String entry = filter(on("\n").split(toscaMetadata),
140 line -> line.contains(RappCsarPathProvider.ENTRY_DEFINITIONS_INDEX)).iterator().next();
141 asdLocation = entry.replace(RappCsarPathProvider.ENTRY_DEFINITIONS_INDEX + ":", "").trim();
147 public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
148 return getPayload(rapp, getResourceUri(RappCsarPathProvider.SME_PROVIDER_FUNCS_LOCATION,
149 rappSMEInstance.getProviderFunction()));
152 public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
153 return getPayload(rapp,
154 getResourceUri(RappCsarPathProvider.SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
157 public String getSmeInvokerPayload(Rapp rapp, RappInstance rappInstance) {
158 return getPayload(rapp, getResourceUri(RappCsarPathProvider.SME_INVOKERS_LOCATION,
159 rappInstance.getSme().getInvokers())).replace("DO_NOT_CHANGE_THIS_RAPP_INSTANCE_ID",
160 String.valueOf(rappInstance.getRappInstanceId()));
163 public String getAcmCompositionPayload(Rapp rapp) {
164 return getPayload(rapp, getResourceUri(RappCsarPathProvider.ACM_DEFINITION_LOCATION,
165 rapp.getRappResources().getAcm().getCompositionDefinitions()));
168 public String getDmeInfoProducerPayload(Rapp rapp, String producerIdentifier) {
169 return getPayload(rapp, getResourceUri(RappCsarPathProvider.DME_INFO_PRODUCERS_LOCATION, producerIdentifier));
172 public String getDmeProducerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
173 return getPayload(rapp,
174 getResourceUri(RappCsarPathProvider.DME_PRODUCER_INFO_TYPES_LOCATION, infoTypeIdentifier));
177 public String getDmeConsumerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
178 return getPayload(rapp,
179 getResourceUri(RappCsarPathProvider.DME_CONSUMER_INFO_TYPES_LOCATION, infoTypeIdentifier));
182 public String getDmeInfoConsumerPayload(Rapp rapp, String infoConsumerIdentifier) {
183 return getPayload(rapp,
184 getResourceUri(RappCsarPathProvider.DME_INFO_CONSUMERS_LOCATION, infoConsumerIdentifier));
187 String getResourceUri(String resourceLocation, String resource) {
188 return resourceLocation + "/" + resource + ".json";
191 public RappResources getRappResource(Rapp rapp) {
192 RappResources rappResources = new RappResources();
194 File csarFile = getCsarFile(rapp);
195 if (csarFile.exists()) {
196 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
197 getFileListFromCsar(csarFile, RappCsarPathProvider.ACM_DEFINITION_LOCATION).iterator().next())
198 .compositionInstances(getFileListFromCsar(csarFile,
199 RappCsarPathProvider.ACM_INSTANCES_LOCATION)).build());
200 rappResources.setSme(RappResources.SMEResources.builder().providerFunctions(
201 getFileListFromCsar(csarFile, RappCsarPathProvider.SME_PROVIDER_FUNCS_LOCATION)).serviceApis(
202 getFileListFromCsar(csarFile, RappCsarPathProvider.SME_SERVICE_APIS_LOCATION)).invokers(
203 getFileListFromCsar(csarFile, RappCsarPathProvider.SME_INVOKERS_LOCATION)).build());
204 rappResources.setDme(RappResources.DMEResources.builder().producerInfoTypes(
205 getFileListFromCsar(csarFile, RappCsarPathProvider.DME_PRODUCER_INFO_TYPES_LOCATION))
206 .consumerInfoTypes(getFileListFromCsar(csarFile,
207 RappCsarPathProvider.DME_CONSUMER_INFO_TYPES_LOCATION))
208 .infoProducers(getFileListFromCsar(csarFile,
209 RappCsarPathProvider.DME_INFO_PRODUCERS_LOCATION)).infoConsumers(
210 getFileListFromCsar(csarFile, RappCsarPathProvider.DME_INFO_CONSUMERS_LOCATION))
213 } catch (Exception e) {
214 logger.warn("Error in getting the rapp resources", e);
216 return rappResources;
219 public AsdMetadata getAsdMetadata(Rapp rApp) {
220 AsdMetadata asdMetadata = new AsdMetadata();
221 File csarFile = getCsarFile(rApp);
222 String asdDefinitionLocation = getAsdDefinitionLocation(csarFile);
223 if (asdDefinitionLocation != null && !asdDefinitionLocation.isEmpty()) {
225 String asdContent = getFileFromCsar(csarFile, asdDefinitionLocation).toString();
226 if (asdContent != null && !asdContent.isEmpty()) {
227 JsonNode jsonNode = getAsdContentNode(asdContent);
228 JsonNode asdJsonNode = jsonNode.at(RappCsarPathProvider.ASD_LOCATION_JSON_POINTER);
229 asdMetadata = objectMapper.convertValue(asdJsonNode.at("/properties"), AsdMetadata.class);
230 asdMetadata.setDescription(asdJsonNode.at("/description").asText());
232 JsonNode artifactsJsonNode = jsonNode.at(RappCsarPathProvider.ASD_ARTIFACTS_LOCATION_JSON_POINTER);
233 final List<DeploymentItem> deploymentItems = new ArrayList<>();
234 artifactsJsonNode.forEach(artifactJsonNode -> {
235 DeploymentItem deploymentItem =
236 objectMapper.convertValue(artifactJsonNode.at("/properties"), DeploymentItem.class);
237 deploymentItem.setFile(artifactJsonNode.at("/file").asText());
238 deploymentItems.add(deploymentItem);
240 asdMetadata.setDeploymentItems(deploymentItems);
242 } catch (Exception e) {
243 logger.warn("Unable to get the asd metadata items", e);
249 Set<String> getFileListFromCsar(File csarFile, String dirLocation) {
250 try (ZipFile zipFile = new ZipFile(csarFile)) {
251 return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
252 .filter(name -> name.startsWith(dirLocation))
253 .map(name -> name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf(".")))
254 .collect(Collectors.toSet());
255 } catch (IOException e) {
256 logger.warn("Error in listing the files from csar", e);