My first commit
[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.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.ArrayList;
39 import java.util.List;
40 import java.util.Set;
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;
54
55 @Service
56 @RequiredArgsConstructor
57 public class RappCsarConfigurationHandler {
58
59     Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
60
61     private final ObjectMapper objectMapper;
62
63     public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
64         return Path.of(csarLocation, rappId, fileName);
65     }
66
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));
71     }
72
73     public ByteArrayResource getArtifactPayload(Rapp rapp, String location) {
74         return new ByteArrayResource(getByteArrayStreamPayload(rapp, location).toByteArray());
75     }
76
77     String getPayload(Rapp rapp, String location) {
78         return getByteArrayStreamPayload(rapp, location).toString();
79     }
80
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);
85     }
86
87     File getCsarFile(Rapp rapp) {
88         return new File(
89                 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
90     }
91
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);
99         }
100         return byteArrayOutputStream;
101     }
102
103     public ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) {
104         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
105         try {
106             ArchiveEntry entry;
107             while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
108                 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
109                     byte[] buffer = new byte[1024];
110                     int bytesRead;
111                     while ((bytesRead = zipArchiveInputStream.read(buffer)) != -1) {
112                         byteArrayOutputStream.write(buffer, 0, bytesRead);
113                     }
114                 }
115             }
116         } catch (IOException e) {
117             logger.info("Unable to get file {} from the zip archive CSAR file", fileLocation, e);
118         }
119         return byteArrayOutputStream;
120     }
121
122     public JsonNode getAsdContentNode(String asdContent) throws JsonProcessingException {
123         return objectMapper.readTree(new Gson().toJsonTree(new Yaml().load(asdContent)).toString());
124     }
125
126     String getAsdDefinitionLocation(final File csarFile) {
127         return getAsdDefinitionLocation(
128                 getFileFromCsar(csarFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION).toString());
129     }
130
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();
137         }
138         return asdLocation;
139     }
140
141
142
143
144     public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
145         return getPayload(rapp, getResourceUri(RappCsarPathProvider.SME_PROVIDER_FUNCS_LOCATION,
146                 rappSMEInstance.getProviderFunction()));
147     }
148
149     public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
150         return getPayload(rapp,
151                 getResourceUri(RappCsarPathProvider.SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
152     }
153
154     public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
155         return getPayload(rapp,
156                 getResourceUri(RappCsarPathProvider.SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
157     }
158
159     public String getAcmCompositionPayload(Rapp rapp) {
160         return getPayload(rapp, getResourceUri(RappCsarPathProvider.ACM_DEFINITION_LOCATION,
161                 rapp.getRappResources().getAcm().getCompositionDefinitions()));
162     }
163
164     public String getDmeInfoProducerPayload(Rapp rapp, String producerIdentifier) {
165         return getPayload(rapp, getResourceUri(RappCsarPathProvider.DME_INFO_PRODUCERS_LOCATION, producerIdentifier));
166     }
167
168     public String getDmeProducerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
169         return getPayload(rapp,
170                 getResourceUri(RappCsarPathProvider.DME_PRODUCER_INFO_TYPES_LOCATION, infoTypeIdentifier));
171     }
172
173     public String getDmeConsumerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
174         return getPayload(rapp,
175                 getResourceUri(RappCsarPathProvider.DME_CONSUMER_INFO_TYPES_LOCATION, infoTypeIdentifier));
176     }
177
178     public String getDmeInfoConsumerPayload(Rapp rapp, String infoConsumerIdentifier) {
179         return getPayload(rapp,
180                 getResourceUri(RappCsarPathProvider.DME_INFO_CONSUMERS_LOCATION, infoConsumerIdentifier));
181     }
182
183     String getResourceUri(String resourceLocation, String resource) {
184         return resourceLocation + "/" + resource + ".json";
185     }
186
187     public RappResources getRappResource(Rapp rapp) {
188         RappResources rappResources = new RappResources();
189         try {
190             File csarFile = getCsarFile(rapp);
191             if (csarFile.exists()) {
192                 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
193                                 getFileListFromCsar(csarFile, RappCsarPathProvider.ACM_DEFINITION_LOCATION).iterator().next())
194                                              .compositionInstances(getFileListFromCsar(csarFile,
195                                                      RappCsarPathProvider.ACM_INSTANCES_LOCATION)).build());
196                 rappResources.setSme(RappResources.SMEResources.builder().providerFunctions(
197                         getFileListFromCsar(csarFile, RappCsarPathProvider.SME_PROVIDER_FUNCS_LOCATION)).serviceApis(
198                         getFileListFromCsar(csarFile, RappCsarPathProvider.SME_SERVICE_APIS_LOCATION)).invokers(
199                         getFileListFromCsar(csarFile, RappCsarPathProvider.SME_INVOKERS_LOCATION)).build());
200                 rappResources.setDme(RappResources.DMEResources.builder().producerInfoTypes(
201                                 getFileListFromCsar(csarFile, RappCsarPathProvider.DME_PRODUCER_INFO_TYPES_LOCATION))
202                                              .consumerInfoTypes(getFileListFromCsar(csarFile,
203                                                      RappCsarPathProvider.DME_CONSUMER_INFO_TYPES_LOCATION))
204                                              .infoProducers(getFileListFromCsar(csarFile,
205                                                      RappCsarPathProvider.DME_INFO_PRODUCERS_LOCATION)).infoConsumers(
206                                 getFileListFromCsar(csarFile, RappCsarPathProvider.DME_INFO_CONSUMERS_LOCATION))
207                                              .build());
208             }
209         } catch (Exception e) {
210             logger.warn("Error in getting the rapp resources", e);
211         }
212         return rappResources;
213     }
214
215     public AsdMetadata getAsdMetadata(Rapp rApp) {
216         AsdMetadata asdMetadata = new AsdMetadata();
217         File csarFile = getCsarFile(rApp);
218         String asdDefinitionLocation = getAsdDefinitionLocation(csarFile);
219         if (asdDefinitionLocation != null && !asdDefinitionLocation.isEmpty()) {
220             try {
221                 String asdContent = getFileFromCsar(csarFile, asdDefinitionLocation).toString();
222                 if (asdContent != null && !asdContent.isEmpty()) {
223                     JsonNode jsonNode = getAsdContentNode(asdContent);
224                     JsonNode asdJsonNode = jsonNode.at(RappCsarPathProvider.ASD_LOCATION_JSON_POINTER);
225                     asdMetadata = objectMapper.convertValue(asdJsonNode.at("/properties"), AsdMetadata.class);
226                     asdMetadata.setDescription(asdJsonNode.at("/description").asText());
227
228                     JsonNode artifactsJsonNode = jsonNode.at(RappCsarPathProvider.ASD_ARTIFACTS_LOCATION_JSON_POINTER);
229                     final List<DeploymentItem> deploymentItems = new ArrayList<>();
230                     artifactsJsonNode.forEach(artifactJsonNode -> {
231                         DeploymentItem deploymentItem =
232                                 objectMapper.convertValue(artifactJsonNode.at("/properties"), DeploymentItem.class);
233                         deploymentItem.setFile(artifactJsonNode.at("/file").asText());
234                         deploymentItems.add(deploymentItem);
235                     });
236                     asdMetadata.setDeploymentItems(deploymentItems);
237                 }
238             } catch (Exception e) {
239                 logger.warn("Unable to get the asd metadata items", e);
240             }
241         }
242         return asdMetadata;
243     }
244
245     Set<String> getFileListFromCsar(File csarFile, String dirLocation) {
246         try (ZipFile zipFile = new ZipFile(csarFile)) {
247             return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
248                            .filter(name -> name.startsWith(dirLocation))
249                            .map(name -> name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf(".")))
250                            .collect(Collectors.toSet());
251         } catch (IOException e) {
252             logger.warn("Error in listing the files from csar", e);
253         }
254         return Set.of();
255     }
256 }