X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=rapp-manager-models%2Fsrc%2Fmain%2Fjava%2Fcom%2Foransc%2Frappmanager%2Fmodels%2Fcsar%2FRappCsarConfigurationHandler.java;h=41c0eaf97612de21a37ea7282d84b2a897a0b8c2;hb=refs%2Fchanges%2F52%2F12552%2F2;hp=9e2f953360fd6e27885cf936888c364c2b4f8dc3;hpb=acb1b85ddb80af9c7243abb949ac49c241ef8f33;p=nonrtric%2Fplt%2Frappmanager.git diff --git a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/csar/RappCsarConfigurationHandler.java b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/csar/RappCsarConfigurationHandler.java index 9e2f953..41c0eaf 100755 --- a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/csar/RappCsarConfigurationHandler.java +++ b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/csar/RappCsarConfigurationHandler.java @@ -26,7 +26,6 @@ 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.exception.RappHandlerException; import com.oransc.rappmanager.models.rapp.Rapp; import com.oransc.rappmanager.models.rapp.RappResources; import com.oransc.rappmanager.models.rappinstance.RappACMInstance; @@ -50,9 +49,7 @@ 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.http.HttpStatus; import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; import org.yaml.snakeyaml.Yaml; @Service @@ -62,53 +59,14 @@ public class RappCsarConfigurationHandler { Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class); private final ObjectMapper objectMapper; - private static final String TOSCA_METADATA_LOCATION = "TOSCA-Metadata/TOSCA.meta"; - private static final String ENTRY_DEFINITIONS_INDEX = "Entry-Definitions"; - 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"; - private static final String ARTIFACTS_LOCATION_JSON_POINTER = - "/topology_template/node_templates/applicationServiceDescriptor/artifacts"; - - - public boolean isValidRappPackage(MultipartFile multipartFile) { - String originalFilename = multipartFile.getOriginalFilename(); - if (originalFilename != null) { - return originalFilename.endsWith(".csar") && isFileExistsInCsar(multipartFile, - ACM_COMPOSITION_JSON_LOCATION) && isFileExistsInCsar(multipartFile, TOSCA_METADATA_LOCATION) - && containsValidArtifactDefinition(multipartFile); - } - 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; - } - } - throw new RappHandlerException(HttpStatus.BAD_REQUEST, "rApp package missing a file " + fileLocation); - } catch (IOException e) { - logger.error("Unable to find the CSAR file", e); - throw new RappHandlerException(HttpStatus.BAD_REQUEST, "rApp package missing a file " + fileLocation); - } - } 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( + return getPayload(rapp, + getResourceUri(RappCsarPathProvider.ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll( "COMPOSITIONID", String.valueOf(compositionId)); } @@ -131,16 +89,6 @@ public class RappCsarConfigurationHandler { getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri()); } - ByteArrayOutputStream getFileFromCsar(MultipartFile multipartFile, String fileLocation) { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) { - byteArrayOutputStream = getFileFromCsar(zipArchiveInputStream, fileLocation); - } catch (IOException e) { - logger.info("Unable to get file {} from the multipart CSAR file", fileLocation, e); - } - return byteArrayOutputStream; - } - ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try (FileInputStream fileInputStream = new FileInputStream(csarFile); @@ -152,7 +100,7 @@ public class RappCsarConfigurationHandler { return byteArrayOutputStream; } - ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) { + public ByteArrayOutputStream getFileFromCsar(ZipArchiveInputStream zipArchiveInputStream, String fileLocation) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { ArchiveEntry entry; @@ -171,44 +119,21 @@ public class RappCsarConfigurationHandler { return byteArrayOutputStream; } - boolean containsValidArtifactDefinition(MultipartFile multipartFile) { - String asdLocation = getAsdDefinitionLocation(multipartFile); - if (asdLocation != null && !asdLocation.isEmpty() && isFileExistsInCsar(multipartFile, asdLocation)) { - try { - String asdContent = getFileFromCsar(multipartFile, asdLocation).toString(); - JsonNode jsonNode = getAsdContentNode(asdContent); - List artifactFileList = jsonNode.at(ARTIFACTS_LOCATION_JSON_POINTER).findValuesAsText("file"); - return artifactFileList.stream() - .allMatch(artifactFile -> isFileExistsInCsar(multipartFile, artifactFile)); - } catch (RappHandlerException e) { - throw e; - } catch (Exception e) { - logger.warn("Unable to validate artifact definition", e); - throw new RappHandlerException(HttpStatus.BAD_REQUEST, "ASD definition in rApp package is invalid."); - } - } - throw new RappHandlerException(HttpStatus.BAD_REQUEST, "ASD definition in rApp package is invalid."); - } - - JsonNode getAsdContentNode(String asdContent) throws JsonProcessingException { + 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, TOSCA_METADATA_LOCATION).toString()); + return getAsdDefinitionLocation( + getFileFromCsar(csarFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION).toString()); } - String getAsdDefinitionLocation(final MultipartFile multipartFile) { - return getAsdDefinitionLocation(getFileFromCsar(multipartFile, TOSCA_METADATA_LOCATION).toString()); - } - - String getAsdDefinitionLocation(final String toscaMetadata) { + public String getAsdDefinitionLocation(final String toscaMetadata) { String asdLocation = ""; if (toscaMetadata != null && !toscaMetadata.isEmpty()) { - final String entry = - filter(on("\n").split(toscaMetadata), line -> line.contains(ENTRY_DEFINITIONS_INDEX)).iterator() - .next(); - asdLocation = entry.replace(ENTRY_DEFINITIONS_INDEX + ":", "").trim(); + 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; } @@ -221,7 +146,7 @@ public class RappCsarConfigurationHandler { try { String asdContent = getFileFromCsar(csarFile, asdDefinitionLocation).toString(); JsonNode jsonNode = getAsdContentNode(asdContent); - JsonNode artifactsJsonNode = jsonNode.at(ARTIFACTS_LOCATION_JSON_POINTER); + JsonNode artifactsJsonNode = jsonNode.at(RappCsarPathProvider.ARTIFACTS_LOCATION_JSON_POINTER); artifactsJsonNode.forEach(artifactJsonNode -> { DeploymentItem deploymentItem = objectMapper.convertValue(artifactJsonNode.at("/properties"), DeploymentItem.class); @@ -237,36 +162,42 @@ public class RappCsarConfigurationHandler { 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) { @@ -279,17 +210,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) {