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;
30 import java.util.List;
31 import java.util.UUID;
32 import java.util.function.Predicate;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipFile;
35 import java.util.zip.ZipInputStream;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
39 import org.springframework.web.multipart.MultipartFile;
42 public class RappCsarConfigurationHandler {
44 Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
45 private static final String ACM_COMPOSITION_JSON_LOCATION = "Files/Acm/definition/compositions.json";
46 private static final String ACM_DEFINITION_LOCATION = "Files/Acm/definition";
47 private static final String ACM_INSTANCES_LOCATION = "Files/Acm/instances";
49 private static final String SME_PROVIDER_FUNCS_LOCATION = "Files/Sme/providers";
50 private static final String SME_SERVICE_APIS_LOCATION = "Files/Sme/serviceapis";
52 private static final String SME_INVOKERS_LOCATION = "Files/Sme/invokers";
55 public boolean isValidRappPackage(MultipartFile multipartFile) {
56 return multipartFile != null && multipartFile.getOriginalFilename() != null
57 && multipartFile.getOriginalFilename().endsWith(".csar") && isFileExistsInCsar(multipartFile,
58 ACM_COMPOSITION_JSON_LOCATION);
61 boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
62 try (ZipInputStream zipInputStream = new ZipInputStream(multipartFile.getInputStream())) {
64 while ((zipEntry = zipInputStream.getNextEntry()) != null) {
65 if (zipEntry.getName().matches(fileLocation)) {
70 } catch (IOException e) {
71 logger.error("Unable to find the CSAR file", e);
76 public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
77 return Path.of(csarLocation, rappId, fileName);
80 public String getInstantiationPayload(Rapp rapp, RappACMInstance rappACMInstance, UUID compositionId) {
81 return getPayload(rapp, getResourceUri(ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll(
82 "COMPOSITIONID", String.valueOf(compositionId));
85 String getPayload(Rapp rapp, String location) {
86 logger.info("Getting payload for {} from {}", rapp.getRappId(), location);
87 File csarFile = getCsarFile(rapp);
88 return getFileFromCsar(csarFile, location).toString();
91 File getCsarFile(Rapp rapp) {
93 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
96 ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
97 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
98 try (FileInputStream fileInputStream = new FileInputStream(csarFile);
99 ZipInputStream zipInputStream = new ZipInputStream(fileInputStream)) {
101 while ((entry = zipInputStream.getNextEntry()) != null) {
102 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
103 byte[] buffer = new byte[1024];
105 while ((bytesRead = zipInputStream.read(buffer)) != -1) {
106 byteArrayOutputStream.write(buffer, 0, bytesRead);
110 } catch (IOException e) {
111 logger.error("Unable to find the CSAR file", e);
113 return byteArrayOutputStream;
117 public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
118 return getPayload(rapp, getResourceUri(SME_PROVIDER_FUNCS_LOCATION, rappSMEInstance.getProviderFunction()));
121 public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
122 return getPayload(rapp, getResourceUri(SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
125 public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
126 return getPayload(rapp, getResourceUri(SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
129 public String getAcmCompositionPayload(Rapp rapp) {
130 return getPayload(rapp,
131 getResourceUri(ACM_DEFINITION_LOCATION, rapp.getRappResources().getAcm().getCompositionDefinitions()));
134 String getResourceUri(String resourceLocation, String resource) {
135 return resourceLocation + "/" + resource + ".json";
138 public RappResources getRappResource(Rapp rapp) {
139 RappResources rappResources = new RappResources();
141 File csarFile = getCsarFile(rapp);
142 if (csarFile.exists()) {
143 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
144 getFileListFromCsar(csarFile, ACM_DEFINITION_LOCATION).get(0))
145 .compositionInstances(getFileListFromCsar(csarFile, ACM_INSTANCES_LOCATION))
147 rappResources.setSme(RappResources.SMEResources.builder()
148 .providerFunctions(getFileListFromCsar(csarFile,
149 SME_PROVIDER_FUNCS_LOCATION))
150 .serviceApis(getFileListFromCsar(csarFile, SME_SERVICE_APIS_LOCATION))
151 .invokers(getFileListFromCsar(csarFile, SME_INVOKERS_LOCATION)).build());
153 } catch (Exception e) {
154 logger.warn("Error in getting the rapp resources", e);
156 return rappResources;
159 List<String> getFileListFromCsar(File csarFile, String dirLocation) {
160 try (ZipFile zipFile = new ZipFile(csarFile)) {
161 return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
162 .filter(name -> name.startsWith(dirLocation))
163 .map(name -> name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf("."))).toList();
164 } catch (IOException e) {
165 logger.warn("Error in listing the files from csar", e);