2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END========================================================================
19 package com.oransc.rappmanager.models.csar;
21 import com.oransc.rappmanager.models.rapp.Rapp;
22 import com.oransc.rappmanager.models.rapp.RappResources;
23 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
24 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
25 import java.io.ByteArrayOutputStream;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.nio.file.Path;
31 import java.util.UUID;
32 import java.util.function.Predicate;
33 import java.util.stream.Collectors;
34 import java.util.zip.ZipEntry;
35 import java.util.zip.ZipFile;
36 import org.apache.commons.compress.archivers.ArchiveEntry;
37 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Service;
41 import org.springframework.web.multipart.MultipartFile;
44 public class RappCsarConfigurationHandler {
46 Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
47 private static final String ACM_COMPOSITION_JSON_LOCATION = "Files/Acm/definition/compositions.json";
48 private static final String ACM_DEFINITION_LOCATION = "Files/Acm/definition";
49 private static final String ACM_INSTANCES_LOCATION = "Files/Acm/instances";
51 private static final String SME_PROVIDER_FUNCS_LOCATION = "Files/Sme/providers";
52 private static final String SME_SERVICE_APIS_LOCATION = "Files/Sme/serviceapis";
54 private static final String SME_INVOKERS_LOCATION = "Files/Sme/invokers";
57 public boolean isValidRappPackage(MultipartFile multipartFile) {
58 String originalFilename = multipartFile.getOriginalFilename();
59 if (originalFilename != null) {
60 return originalFilename.endsWith(".csar") && isFileExistsInCsar(multipartFile,
61 ACM_COMPOSITION_JSON_LOCATION);
66 boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
67 try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
68 ArchiveEntry zipEntry;
69 while ((zipEntry = zipArchiveInputStream.getNextEntry()) != null) {
70 if (zipEntry.getName().matches(fileLocation)) {
75 } catch (IOException e) {
76 logger.error("Unable to find the CSAR file", e);
81 public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
82 return Path.of(csarLocation, rappId, fileName);
85 public String getInstantiationPayload(Rapp rapp, RappACMInstance rappACMInstance, UUID compositionId) {
86 return getPayload(rapp, getResourceUri(ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll(
87 "COMPOSITIONID", String.valueOf(compositionId));
90 String getPayload(Rapp rapp, String location) {
91 logger.info("Getting payload for {} from {}", rapp.getRappId(), location);
92 File csarFile = getCsarFile(rapp);
93 return getFileFromCsar(csarFile, location).toString();
96 File getCsarFile(Rapp rapp) {
98 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
101 ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
102 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
103 try (FileInputStream fileInputStream = new FileInputStream(csarFile);
104 ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
106 while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
107 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
108 byte[] buffer = new byte[1024];
110 while ((bytesRead = zipArchiveInputStream.read(buffer)) != -1) {
111 byteArrayOutputStream.write(buffer, 0, bytesRead);
115 } catch (IOException e) {
116 logger.error("Unable to find the CSAR file", e);
118 return byteArrayOutputStream;
122 public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
123 return getPayload(rapp, getResourceUri(SME_PROVIDER_FUNCS_LOCATION, rappSMEInstance.getProviderFunction()));
126 public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
127 return getPayload(rapp, getResourceUri(SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
130 public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
131 return getPayload(rapp, getResourceUri(SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
134 public String getAcmCompositionPayload(Rapp rapp) {
135 return getPayload(rapp,
136 getResourceUri(ACM_DEFINITION_LOCATION, rapp.getRappResources().getAcm().getCompositionDefinitions()));
139 String getResourceUri(String resourceLocation, String resource) {
140 return resourceLocation + "/" + resource + ".json";
143 public RappResources getRappResource(Rapp rapp) {
144 RappResources rappResources = new RappResources();
146 File csarFile = getCsarFile(rapp);
147 if (csarFile.exists()) {
148 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
149 getFileListFromCsar(csarFile, ACM_DEFINITION_LOCATION).iterator().next()).compositionInstances(
150 getFileListFromCsar(csarFile, ACM_INSTANCES_LOCATION)).build());
151 rappResources.setSme(RappResources.SMEResources.builder().providerFunctions(
152 getFileListFromCsar(csarFile, SME_PROVIDER_FUNCS_LOCATION))
153 .serviceApis(getFileListFromCsar(csarFile, SME_SERVICE_APIS_LOCATION))
154 .invokers(getFileListFromCsar(csarFile, SME_INVOKERS_LOCATION)).build());
156 } catch (Exception e) {
157 logger.warn("Error in getting the rapp resources", e);
159 return rappResources;
162 Set<String> getFileListFromCsar(File csarFile, String dirLocation) {
163 try (ZipFile zipFile = new ZipFile(csarFile)) {
164 return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
165 .filter(name -> name.startsWith(dirLocation))
166 .map(name -> name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf(".")))
167 .collect(Collectors.toSet());
168 } catch (IOException e) {
169 logger.warn("Error in listing the files from csar", e);