X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=rapp-manager-application%2Fsrc%2Fmain%2Fjava%2Fcom%2Foransc%2Frappmanager%2Fservice%2FRappService.java;h=23ab2d8d67522394622290d57923a2dd2fdc4f84;hb=a46c269e0972357c54378520a4bba531bf7bb12d;hp=82054f5a72fe508cded4226207b482f043f38cd6;hpb=a071d6befe8d38a5e589dffbbf1dc3904ff3aa79;p=nonrtric%2Fplt%2Frappmanager.git diff --git a/rapp-manager-application/src/main/java/com/oransc/rappmanager/service/RappService.java b/rapp-manager-application/src/main/java/com/oransc/rappmanager/service/RappService.java index 82054f5..23ab2d8 100755 --- a/rapp-manager-application/src/main/java/com/oransc/rappmanager/service/RappService.java +++ b/rapp-manager-application/src/main/java/com/oransc/rappmanager/service/RappService.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START====================================================================== * Copyright (C) 2023 Nordix Foundation. All rights reserved. + * Copyright (C) 2023-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. @@ -19,13 +20,17 @@ package com.oransc.rappmanager.service; import com.oransc.rappmanager.acm.service.AcmDeployer; +import com.oransc.rappmanager.models.RappDeployer; +import com.oransc.rappmanager.models.cache.RappCacheService; +import com.oransc.rappmanager.models.exception.RappHandlerException; import com.oransc.rappmanager.models.rapp.Rapp; import com.oransc.rappmanager.models.rapp.RappEvent; +import com.oransc.rappmanager.models.rapp.RappState; import com.oransc.rappmanager.models.rappinstance.RappInstance; import com.oransc.rappmanager.models.rappinstance.RappInstanceState; -import com.oransc.rappmanager.models.rapp.RappState; import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine; -import com.oransc.rappmanager.sme.service.SmeDeployer; +import java.util.List; +import java.util.UUID; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -36,69 +41,108 @@ import org.springframework.stereotype.Service; public class RappService { private final AcmDeployer acmDeployer; - private final SmeDeployer smeDeployer; + private final List rappDeployers; private final RappInstanceStateMachine rappInstanceStateMachine; + private final RappCacheService rappCacheService; + private final DeploymentArtifactsService deploymentArtifactsService; + private static final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted."; public ResponseEntity primeRapp(Rapp rapp) { if (rapp.getState().equals(RappState.COMMISSIONED)) { rapp.setState(RappState.PRIMING); - if (!acmDeployer.primeRapp(rapp)) { - rapp.setState(RappState.COMMISSIONED); + rapp.setReason(null); + //Configuring the deployment artifact needs to be done before starting the priming with other components + //If there are additional conditions needs to be checked before priming, This needs handled as part of pre-priming stage. + if (deploymentArtifactsService.configureDeploymentArtifacts(rapp) && rappDeployers.parallelStream() + .allMatch( + rappDeployer -> rappDeployer.primeRapp( + rapp))) { + rapp.setState(RappState.PRIMED); + return ResponseEntity.ok().build(); } - return ResponseEntity.ok().build(); - } else { - return ResponseEntity.badRequest() - .body("State transition from " + RappState.PRIMED.name() + " to " + rapp.getState().name() - + " is not permitted."); + rapp.setState(RappState.COMMISSIONED); + throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason()); } + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format(STATE_TRANSITION_NOT_PERMITTED, rapp.getState().name(), RappState.PRIMED.name())); + } public ResponseEntity deprimeRapp(Rapp rapp) { if (rapp.getState().equals(RappState.PRIMED) && rapp.getRappInstances().isEmpty()) { rapp.setState(RappState.DEPRIMING); - if (!acmDeployer.deprimeRapp(rapp)) { - rapp.setState(RappState.PRIMED); + rapp.setReason(null); + if (rappDeployers.parallelStream().allMatch(rappDeployer -> rappDeployer.deprimeRapp(rapp))) { + rapp.setState(RappState.COMMISSIONED); + return ResponseEntity.ok().build(); } + rapp.setState(RappState.PRIMED); + throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason()); + } + if (!rapp.getRappInstances().isEmpty()) { + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + "Unable to deprime as there are active rapp instances."); + } else { + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(), + rapp.getState().name())); + } + } + + public ResponseEntity deleteRapp(Rapp rApp) { + if (rApp.getRappInstances().isEmpty() && rApp.getState().equals(RappState.COMMISSIONED)) { + rappCacheService.deleteRapp(rApp); return ResponseEntity.ok().build(); + } + if (!rApp.getRappInstances().isEmpty()) { + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format("Unable to delete %s as there are active rApp instances.", rApp.getName())); } else { - if (!rapp.getRappInstances().isEmpty()) { - return ResponseEntity.badRequest().body("Unable to deprime as there are active rapp instances,"); - } else { - return ResponseEntity.badRequest() - .body("State transition from " + RappState.COMMISSIONED.name() + " to " + rapp.getState() - .name() - + " is not permitted."); - } + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rApp.getName())); } + } public ResponseEntity deployRappInstance(Rapp rapp, RappInstance rappInstance) { if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) { + rappInstance.setReason(null); rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING); - if (acmDeployer.deployRappInstance(rapp, rappInstance) && smeDeployer.deployRappInstance(rapp, - rappInstance)) { + if (rappDeployers.parallelStream() + .allMatch(rappDeployer -> rappDeployer.deployRappInstance(rapp, rappInstance))) { return ResponseEntity.accepted().build(); } - return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); - } else { - return ResponseEntity.badRequest().body("State transition from " + rappInstance.getState().name() + " to " - + RappInstanceState.DEPLOYED.name() + " is not permitted."); + throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rappInstance.getReason()); } + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state", + rappInstance.getRappInstanceId())); + } public ResponseEntity undeployRappInstance(Rapp rapp, RappInstance rappInstance) { if (rappInstance.getState().equals(RappInstanceState.DEPLOYED)) { + rappInstance.setReason(null); rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING); - if (acmDeployer.undeployRappInstance(rapp, rappInstance) && smeDeployer.undeployRappInstance(rapp, - rappInstance)) { + if (rappDeployers.parallelStream() + .allMatch(rappDeployer -> rappDeployer.undeployRappInstance(rapp, rappInstance))) { return ResponseEntity.accepted().build(); } - return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); - } else { - return ResponseEntity.badRequest().body("State transition from " + rappInstance.getState().name() + " to " - + RappInstanceState.UNDEPLOYED.name() - + " is not permitted."); + throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rappInstance.getReason()); + } + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format("Unable to undeploy rApp instance %s as it is not in DEPLOYED state", + rappInstance.getRappInstanceId())); + } + + public ResponseEntity deleteRappInstance(Rapp rApp, UUID rappInstanceId) { + if (rApp.getRappInstances().get(rappInstanceId).getState().equals(RappInstanceState.UNDEPLOYED)) { + rappInstanceStateMachine.deleteRappInstance(rApp.getRappInstances().get(rappInstanceId)); + rApp.getRappInstances().remove(rappInstanceId); + return ResponseEntity.noContent().build(); } + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", rappInstanceId)); } public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {