Add validation for asd descriptor and invariant id
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / csar / RappCsarConfigurationHandler.java
index be6fe88..f71315f 100755 (executable)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START======================================================================
  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
  * ===============================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package com.oransc.rappmanager.models.csar;
 
+import static com.google.common.base.Splitter.on;
+import static com.google.common.collect.Iterables.filter;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
 import com.oransc.rappmanager.models.rapp.Rapp;
 import com.oransc.rappmanager.models.rapp.RappResources;
 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
@@ -27,72 +35,53 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Set;
 import java.util.UUID;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
+import lombok.RequiredArgsConstructor;
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.core.io.ByteArrayResource;
 import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
+import org.yaml.snakeyaml.Yaml;
 
 @Service
+@RequiredArgsConstructor
 public class RappCsarConfigurationHandler {
 
     Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
-    private static final String ACM_COMPOSITION_JSON_LOCATION = "Files/Acm/definition/compositions.json";
-    private static final String ACM_DEFINITION_LOCATION = "Files/Acm/definition";
-    private static final String ACM_INSTANCES_LOCATION = "Files/Acm/instances";
-    private static final String SME_PROVIDER_FUNCS_LOCATION = "Files/Sme/providers";
-    private static final String SME_SERVICE_APIS_LOCATION = "Files/Sme/serviceapis";
-    private static final String SME_INVOKERS_LOCATION = "Files/Sme/invokers";
-    private static final String DME_PRODUCER_INFO_TYPES_LOCATION = "Files/Dme/producerinfotypes";
-    private static final String DME_CONSUMER_INFO_TYPES_LOCATION = "Files/Dme/consumerinfotypes";
-    private static final String DME_INFO_PRODUCERS_LOCATION = "Files/Dme/infoproducers";
-    private static final String DME_INFO_CONSUMERS_LOCATION = "Files/Dme/infoconsumers";
-
-
-    public boolean isValidRappPackage(MultipartFile multipartFile) {
-        String originalFilename = multipartFile.getOriginalFilename();
-        if (originalFilename != null) {
-            return originalFilename.endsWith(".csar") && isFileExistsInCsar(multipartFile,
-                    ACM_COMPOSITION_JSON_LOCATION);
-        }
-        return false;
-    }
 
-    boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
-        try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
-            ArchiveEntry zipEntry;
-            while ((zipEntry = zipArchiveInputStream.getNextEntry()) != null) {
-                if (zipEntry.getName().matches(fileLocation)) {
-                    return Boolean.TRUE;
-                }
-            }
-            return Boolean.FALSE;
-        } catch (IOException e) {
-            logger.error("Unable to find the CSAR file", e);
-            return Boolean.FALSE;
-        }
-    }
+    private final ObjectMapper objectMapper;
 
     public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
         return Path.of(csarLocation, rappId, fileName);
     }
 
     public String getInstantiationPayload(Rapp rapp, RappACMInstance rappACMInstance, UUID compositionId) {
-        return getPayload(rapp, getResourceUri(ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll(
-                "COMPOSITIONID", String.valueOf(compositionId));
+        return getPayload(rapp,
+                getResourceUri(RappCsarPathProvider.ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll(
+                "DO_NOT_CHANGE_THIS_COMPOSITION_ID", String.valueOf(compositionId));
+    }
+
+    public ByteArrayResource getArtifactPayload(Rapp rapp, String location) {
+        return new ByteArrayResource(getByteArrayStreamPayload(rapp, location).toByteArray());
     }
 
     String getPayload(Rapp rapp, String location) {
+        return getByteArrayStreamPayload(rapp, location).toString();
+    }
+
+    ByteArrayOutputStream getByteArrayStreamPayload(Rapp rapp, String location) {
         logger.debug("Getting payload for {} from {}", rapp.getRappId(), location);
         File csarFile = getCsarFile(rapp);
-        return getFileFromCsar(csarFile, location).toString();
+        return getFileFromCsar(csarFile, location);
     }
 
     File getCsarFile(Rapp rapp) {
@@ -104,6 +93,16 @@ public class RappCsarConfigurationHandler {
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         try (FileInputStream fileInputStream = new FileInputStream(csarFile);
              ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
+            byteArrayOutputStream = getFileFromCsar(zipArchiveInputStream, fileLocation);
+        } catch (IOException e) {
+            logger.info("Unable to get file {} from the CSAR file", fileLocation, e);
+        }
+        return byteArrayOutputStream;
+    }
+
+    public ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) {
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        try {
             ArchiveEntry entry;
             while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
                 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
@@ -115,43 +114,70 @@ public class RappCsarConfigurationHandler {
                 }
             }
         } catch (IOException e) {
-            logger.error("Unable to find the CSAR file", e);
+            logger.info("Unable to get file {} from the zip archive CSAR file", fileLocation, e);
         }
         return byteArrayOutputStream;
     }
 
+    public JsonNode getAsdContentNode(String asdContent) throws JsonProcessingException {
+        return objectMapper.readTree(new Gson().toJsonTree(new Yaml().load(asdContent)).toString());
+    }
+
+    String getAsdDefinitionLocation(final File csarFile) {
+        return getAsdDefinitionLocation(
+                getFileFromCsar(csarFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION).toString());
+    }
+
+    public String getAsdDefinitionLocation(final String toscaMetadata) {
+        String asdLocation = "";
+        if (toscaMetadata != null && !toscaMetadata.isEmpty()) {
+            final String entry = filter(on("\n").split(toscaMetadata),
+                    line -> line.contains(RappCsarPathProvider.ENTRY_DEFINITIONS_INDEX)).iterator().next();
+            asdLocation = entry.replace(RappCsarPathProvider.ENTRY_DEFINITIONS_INDEX + ":", "").trim();
+        }
+        return asdLocation;
+    }
+
+
+
 
     public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
-        return getPayload(rapp, getResourceUri(SME_PROVIDER_FUNCS_LOCATION, rappSMEInstance.getProviderFunction()));
+        return getPayload(rapp, getResourceUri(RappCsarPathProvider.SME_PROVIDER_FUNCS_LOCATION,
+                rappSMEInstance.getProviderFunction()));
     }
 
     public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
-        return getPayload(rapp, getResourceUri(SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
+        return getPayload(rapp,
+                getResourceUri(RappCsarPathProvider.SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
     }
 
     public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
-        return getPayload(rapp, getResourceUri(SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
+        return getPayload(rapp,
+                getResourceUri(RappCsarPathProvider.SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
     }
 
     public String getAcmCompositionPayload(Rapp rapp) {
-        return getPayload(rapp,
-                getResourceUri(ACM_DEFINITION_LOCATION, rapp.getRappResources().getAcm().getCompositionDefinitions()));
+        return getPayload(rapp, getResourceUri(RappCsarPathProvider.ACM_DEFINITION_LOCATION,
+                rapp.getRappResources().getAcm().getCompositionDefinitions()));
     }
 
     public String getDmeInfoProducerPayload(Rapp rapp, String producerIdentifier) {
-        return getPayload(rapp, getResourceUri(DME_INFO_PRODUCERS_LOCATION, producerIdentifier));
+        return getPayload(rapp, getResourceUri(RappCsarPathProvider.DME_INFO_PRODUCERS_LOCATION, producerIdentifier));
     }
 
     public String getDmeProducerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
-        return getPayload(rapp, getResourceUri(DME_PRODUCER_INFO_TYPES_LOCATION, infoTypeIdentifier));
+        return getPayload(rapp,
+                getResourceUri(RappCsarPathProvider.DME_PRODUCER_INFO_TYPES_LOCATION, infoTypeIdentifier));
     }
 
     public String getDmeConsumerInfoTypePayload(Rapp rapp, String infoTypeIdentifier) {
-        return getPayload(rapp, getResourceUri(DME_CONSUMER_INFO_TYPES_LOCATION, infoTypeIdentifier));
+        return getPayload(rapp,
+                getResourceUri(RappCsarPathProvider.DME_CONSUMER_INFO_TYPES_LOCATION, infoTypeIdentifier));
     }
 
     public String getDmeInfoConsumerPayload(Rapp rapp, String infoConsumerIdentifier) {
-        return getPayload(rapp, getResourceUri(DME_INFO_CONSUMERS_LOCATION, infoConsumerIdentifier));
+        return getPayload(rapp,
+                getResourceUri(RappCsarPathProvider.DME_INFO_CONSUMERS_LOCATION, infoConsumerIdentifier));
     }
 
     String getResourceUri(String resourceLocation, String resource) {
@@ -164,17 +190,20 @@ public class RappCsarConfigurationHandler {
             File csarFile = getCsarFile(rapp);
             if (csarFile.exists()) {
                 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
-                        getFileListFromCsar(csarFile, ACM_DEFINITION_LOCATION).iterator().next()).compositionInstances(
-                        getFileListFromCsar(csarFile, ACM_INSTANCES_LOCATION)).build());
+                                getFileListFromCsar(csarFile, RappCsarPathProvider.ACM_DEFINITION_LOCATION).iterator().next())
+                                             .compositionInstances(getFileListFromCsar(csarFile,
+                                                     RappCsarPathProvider.ACM_INSTANCES_LOCATION)).build());
                 rappResources.setSme(RappResources.SMEResources.builder().providerFunctions(
-                                getFileListFromCsar(csarFile, SME_PROVIDER_FUNCS_LOCATION))
-                                             .serviceApis(getFileListFromCsar(csarFile, SME_SERVICE_APIS_LOCATION))
-                                             .invokers(getFileListFromCsar(csarFile, SME_INVOKERS_LOCATION)).build());
+                        getFileListFromCsar(csarFile, RappCsarPathProvider.SME_PROVIDER_FUNCS_LOCATION)).serviceApis(
+                        getFileListFromCsar(csarFile, RappCsarPathProvider.SME_SERVICE_APIS_LOCATION)).invokers(
+                        getFileListFromCsar(csarFile, RappCsarPathProvider.SME_INVOKERS_LOCATION)).build());
                 rappResources.setDme(RappResources.DMEResources.builder().producerInfoTypes(
-                                getFileListFromCsar(csarFile, DME_PRODUCER_INFO_TYPES_LOCATION)).consumerInfoTypes(
-                                getFileListFromCsar(csarFile, DME_CONSUMER_INFO_TYPES_LOCATION))
-                                             .infoProducers(getFileListFromCsar(csarFile, DME_INFO_PRODUCERS_LOCATION))
-                                             .infoConsumers(getFileListFromCsar(csarFile, DME_INFO_CONSUMERS_LOCATION))
+                                getFileListFromCsar(csarFile, RappCsarPathProvider.DME_PRODUCER_INFO_TYPES_LOCATION))
+                                             .consumerInfoTypes(getFileListFromCsar(csarFile,
+                                                     RappCsarPathProvider.DME_CONSUMER_INFO_TYPES_LOCATION))
+                                             .infoProducers(getFileListFromCsar(csarFile,
+                                                     RappCsarPathProvider.DME_INFO_PRODUCERS_LOCATION)).infoConsumers(
+                                getFileListFromCsar(csarFile, RappCsarPathProvider.DME_INFO_CONSUMERS_LOCATION))
                                              .build());
             }
         } catch (Exception e) {
@@ -183,6 +212,36 @@ public class RappCsarConfigurationHandler {
         return rappResources;
     }
 
+    public AsdMetadata getAsdMetadata(Rapp rApp) {
+        AsdMetadata asdMetadata = new AsdMetadata();
+        File csarFile = getCsarFile(rApp);
+        String asdDefinitionLocation = getAsdDefinitionLocation(csarFile);
+        if (asdDefinitionLocation != null && !asdDefinitionLocation.isEmpty()) {
+            try {
+                String asdContent = getFileFromCsar(csarFile, asdDefinitionLocation).toString();
+                if (asdContent != null && !asdContent.isEmpty()) {
+                    JsonNode jsonNode = getAsdContentNode(asdContent);
+                    JsonNode asdJsonNode = jsonNode.at(RappCsarPathProvider.ASD_LOCATION_JSON_POINTER);
+                    asdMetadata = objectMapper.convertValue(asdJsonNode.at("/properties"), AsdMetadata.class);
+                    asdMetadata.setDescription(asdJsonNode.at("/description").asText());
+
+                    JsonNode artifactsJsonNode = jsonNode.at(RappCsarPathProvider.ASD_ARTIFACTS_LOCATION_JSON_POINTER);
+                    final List<DeploymentItem> deploymentItems = new ArrayList<>();
+                    artifactsJsonNode.forEach(artifactJsonNode -> {
+                        DeploymentItem deploymentItem =
+                                objectMapper.convertValue(artifactJsonNode.at("/properties"), DeploymentItem.class);
+                        deploymentItem.setFile(artifactJsonNode.at("/file").asText());
+                        deploymentItems.add(deploymentItem);
+                    });
+                    asdMetadata.setDeploymentItems(deploymentItems);
+                }
+            } catch (Exception e) {
+                logger.warn("Unable to get the asd metadata items", e);
+            }
+        }
+        return asdMetadata;
+    }
+
     Set<String> getFileListFromCsar(File csarFile, String dirLocation) {
         try (ZipFile zipFile = new ZipFile(csarFile)) {
             return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)