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=278bcbd57fc4a6e2557e82185a2d9eb3fd5756e2;hb=refs%2Fchanges%2F39%2F11839%2F1;hp=8546082d2122755a8b44bbcc9ca91c1500ed238f;hpb=7c96a8b483bb12318a04433ce36bccff314ddacb;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 8546082..278bcbd 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 @@ -21,6 +21,7 @@ package com.oransc.rappmanager.service; import com.oransc.rappmanager.acm.service.AcmDeployer; import com.oransc.rappmanager.dme.service.DmeDeployer; 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; @@ -28,6 +29,7 @@ import com.oransc.rappmanager.models.rappinstance.RappInstance; import com.oransc.rappmanager.models.rappinstance.RappInstanceState; import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine; import com.oransc.rappmanager.sme.service.SmeDeployer; +import java.util.UUID; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -51,15 +53,13 @@ public class RappService { if (acmDeployer.primeRapp(rapp) && dmeDeployer.primeRapp(rapp)) { rapp.setState(RappState.PRIMED); return ResponseEntity.ok().build(); - } else { - rapp.setState(RappState.COMMISSIONED); - return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); } - } else { - return ResponseEntity.badRequest() - .body(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.PRIMED.name(), - rapp.getState().name())); + rapp.setState(RappState.COMMISSIONED); + return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); } + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format(STATE_TRANSITION_NOT_PERMITTED, rapp.getState().name(), RappState.PRIMED.name())); + } public ResponseEntity deprimeRapp(Rapp rapp) { @@ -69,18 +69,17 @@ public class RappService { if (acmDeployer.deprimeRapp(rapp) && dmeDeployer.deprimeRapp(rapp)) { rapp.setState(RappState.COMMISSIONED); return ResponseEntity.ok().build(); - } else { - rapp.setState(RappState.PRIMED); - return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); } + rapp.setState(RappState.PRIMED); + return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); + } + if (!rapp.getRappInstances().isEmpty()) { + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + "Unable to deprime as there are active rapp instances."); } else { - if (!rapp.getRappInstances().isEmpty()) { - return ResponseEntity.badRequest().body("Unable to deprime as there are active rapp instances,"); - } else { - return ResponseEntity.badRequest() - .body(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(), - rapp.getState().name())); - } + throw new RappHandlerException(HttpStatus.BAD_REQUEST, + String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED.name(), + rapp.getState().name())); } } @@ -88,45 +87,56 @@ public class RappService { 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 delete '" + rApp.getName() + "' as there are active rApp instances."); - } else { - return ResponseEntity.badRequest().body("Unable to delete '" + rApp.getName() - + "' as the rApp is not in COMMISSIONED state."); - } + 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) && dmeDeployer.deployRappInstance(rapp, rappInstance)) { return ResponseEntity.accepted().build(); } return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); - } else { - return ResponseEntity.badRequest() - .body(String.format(STATE_TRANSITION_NOT_PERMITTED, rappInstance.getState().name(), - RappInstanceState.DEPLOYED.name())); } + 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) && dmeDeployer.undeployRappInstance(rapp, rappInstance)) { return ResponseEntity.accepted().build(); } return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); - } else { - return ResponseEntity.badRequest() - .body(String.format(STATE_TRANSITION_NOT_PERMITTED, rappInstance.getState().name(), - RappInstanceState.UNDEPLOYED.name())); } + 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) {