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;
21 import java.io.ByteArrayOutputStream;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.nio.file.Path;
26 import java.util.UUID;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipInputStream;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
32 import org.springframework.web.multipart.MultipartFile;
35 public class RappCsarConfigurationHandler {
37 Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
38 private final String acmInstantiationJsonLocation = "Files/Acm/instantiation.json";
40 private final String smeProviderDomainLocation = "Files/Sme/provider-domain.json";
42 private final String smeProviderApiLocation = "Files/Sme/provider-api.json";
44 private final String smeInvokerLocation = "Files/Sme/invoker.json";
47 public boolean isValidRappPackage(MultipartFile multipartFile) {
48 return multipartFile.getOriginalFilename() != null && multipartFile.getOriginalFilename().endsWith(".csar")
49 && isFileExistsInCsar(multipartFile, acmInstantiationJsonLocation);
52 boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
53 try (ZipInputStream zipInputStream = new ZipInputStream(multipartFile.getInputStream())) {
55 while ((zipEntry = zipInputStream.getNextEntry()) != null) {
56 if (zipEntry.getName().matches(fileLocation)) {
61 } catch (IOException e) {
62 logger.error("Unable to find the CSAR file", e);
67 public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
68 return Path.of(csarLocation, rappId, fileName);
71 public String getInstantiationPayload(Rapp rapp, UUID compositionId) {
72 return getPayload(rapp, acmInstantiationJsonLocation).replaceAll("COMPOSITIONID",
73 String.valueOf(compositionId));
76 String getPayload(Rapp rapp, String location) {
77 File csarFile = new File(
78 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
79 return getFileFromCsar(csarFile, location).toString();
82 ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
83 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
84 try (FileInputStream fileInputStream = new FileInputStream(csarFile);
85 ZipInputStream zipInputStream = new ZipInputStream(fileInputStream)) {
87 while ((entry = zipInputStream.getNextEntry()) != null) {
88 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
89 byte[] buffer = new byte[1024];
91 while ((bytesRead = zipInputStream.read(buffer)) != -1) {
92 byteArrayOutputStream.write(buffer, 0, bytesRead);
96 } catch (IOException e) {
97 logger.error("Unable to find the CSAR file", e);
99 return byteArrayOutputStream;
103 public String getSmeProviderDomainPayload(Rapp rapp) {
104 return getPayload(rapp, smeProviderDomainLocation);
107 public String getSmeProviderApiPayload(Rapp rapp) {
108 return getPayload(rapp, smeProviderApiLocation);
111 public String getSmeInvokerPayload(Rapp rapp) {
112 return getPayload(rapp, smeInvokerLocation);