Add reason for rApp instance deploy/undeploy failure

Reason field added to show why rApp instance deploy failed.

Issue-ID: NONRTRIC-913
Signed-off-by: aravind.est <aravindhan.a@est.tech>
Change-Id: Icb1779888bfbdea0f97a6059bba0a4c5a9ec01fb
diff --git a/rapp-manager-acm/src/main/java/com/oransc/rappmanager/acm/service/AcmDeployer.java b/rapp-manager-acm/src/main/java/com/oransc/rappmanager/acm/service/AcmDeployer.java
index 0bbf422..0cfeb9b 100755
--- a/rapp-manager-acm/src/main/java/com/oransc/rappmanager/acm/service/AcmDeployer.java
+++ b/rapp-manager-acm/src/main/java/com/oransc/rappmanager/acm/service/AcmDeployer.java
@@ -139,6 +139,8 @@
         } catch (Exception e) {
             logger.warn("Error in deploying Rapp", e);
         }
+        rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.ACMDEPLOYFAILED);
+        rappInstance.setReason("Unable to deploy ACM");
         return false;
     }
 
@@ -158,6 +160,8 @@
                 return true;
             }
         }
+        rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.ACMUNDEPLOYFAILED);
+        rappInstance.setReason("Unable to undeploy ACM");
         return false;
     }
 
@@ -191,7 +195,7 @@
                 return true;
             }
         } catch (Exception e) {
-            logger.error("Failed deprime automation composition", e);
+            logger.warn("Failed deprime automation composition", e);
         }
         rapp.setReason("Unable to delete automation composition");
         return false;
diff --git a/rapp-manager-acm/src/test/java/com/oransc/rappmanager/acm/service/AcmDeployerTest.java b/rapp-manager-acm/src/test/java/com/oransc/rappmanager/acm/service/AcmDeployerTest.java
index b4dd295..b866b5a 100755
--- a/rapp-manager-acm/src/test/java/com/oransc/rappmanager/acm/service/AcmDeployerTest.java
+++ b/rapp-manager-acm/src/test/java/com/oransc/rappmanager/acm/service/AcmDeployerTest.java
@@ -170,18 +170,36 @@
     }
 
     @Test
-    void testDeployRappInstanceFailure() throws Exception {
+    void testDeployRappInstanceFailureWithNoInstanceId() throws JsonProcessingException {
         UUID compositionId = UUID.randomUUID();
         UUID rappId = UUID.randomUUID();
-        UUID instanceId = UUID.randomUUID();
         Rapp rapp = Rapp.builder().name(rappId.toString()).packageName(validRappFile).compositionId(compositionId)
                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+        RappInstance rappInstance = rappResourceBuilder.getRappInstance();
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
         InstantiationResponse instantiationResponse = new InstantiationResponse();
-        instantiationResponse.setInstanceId(instanceId);
+        mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCES, compositionId)))
+                .andExpect(method(HttpMethod.POST)).andRespond(
+                        withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
+                                .body(objectMapper.writeValueAsString(instantiationResponse)));
+
+        boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappInstance);
+        mockServer.verify();
+        assertFalse(rappDeployStateActual);
+    }
+
+    @Test
+    void testDeployRappInstanceFailure() {
+        UUID compositionId = UUID.randomUUID();
+        UUID rappId = UUID.randomUUID();
+        Rapp rapp = Rapp.builder().name(rappId.toString()).packageName(validRappFile).compositionId(compositionId)
+                            .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+        RappInstance rappInstance = rappResourceBuilder.getRappInstance();
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCES, compositionId)))
                 .andExpect(method(HttpMethod.POST)).andRespond(withStatus(HttpStatus.BAD_GATEWAY));
 
-        boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappResourceBuilder.getRappInstance());
+        boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappInstance);
         mockServer.verify();
         assertFalse(rappDeployStateActual);
     }
@@ -311,6 +329,23 @@
     }
 
     @Test
+    void testPrimeRappFailureWithoutCompositionId() throws JsonProcessingException {
+        UUID compositionId = UUID.randomUUID();
+        RappResources rappResources = rappResourceBuilder.getResources();
+        Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
+                            .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
+                            .compositionId(compositionId).rappResources(rappResources).build();
+
+        CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
+        mockServer.expect(ExpectedCount.once(), requestTo(URI_ACM_COMPOSITIONS)).andExpect(method(HttpMethod.POST))
+                .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
+                                    .body(objectMapper.writeValueAsString(commissioningResponseExpected)));
+        boolean primeRapp = acmDeployer.primeRapp(rapp);
+        mockServer.verify();
+        assertFalse(primeRapp);
+    }
+
+    @Test
     void testDeprimeRapp() throws JsonProcessingException {
         UUID compositionId = UUID.randomUUID();
         RappResources rappResources = rappResourceBuilder.getResources();
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..6df12d4 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
@@ -101,6 +101,7 @@
 
     public ResponseEntity<String> 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)) {
@@ -116,6 +117,7 @@
 
     public ResponseEntity<String> 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)) {
diff --git a/rapp-manager-dme/src/main/java/com/oransc/rappmanager/dme/service/DmeDeployer.java b/rapp-manager-dme/src/main/java/com/oransc/rappmanager/dme/service/DmeDeployer.java
index d82ef47..329841f 100755
--- a/rapp-manager-dme/src/main/java/com/oransc/rappmanager/dme/service/DmeDeployer.java
+++ b/rapp-manager-dme/src/main/java/com/oransc/rappmanager/dme/service/DmeDeployer.java
@@ -80,6 +80,7 @@
         if (deployState) {
             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DMEDEPLOYED);
         } else {
+            rappInstance.setReason("Unable to deploy DME");
             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DMEDEPLOYFAILED);
         }
         return deployState;
@@ -98,6 +99,7 @@
         if (undeployState) {
             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DMEUNDEPLOYED);
         } else {
+            rappInstance.setReason("Unable to undeploy DME");
             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DMEUNDEPLOYFAILED);
         }
         return undeployState;
diff --git a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/rappinstance/RappInstance.java b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/rappinstance/RappInstance.java
index 39d2709..463baa2 100755
--- a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/rappinstance/RappInstance.java
+++ b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/rappinstance/RappInstance.java
@@ -26,6 +26,7 @@
 
     UUID rappInstanceId = UUID.randomUUID();
     RappInstanceState state = RappInstanceState.UNDEPLOYED;
+    String reason;
     RappACMInstance acm;
     RappSMEInstance sme;
     RappDMEInstance dme;
diff --git a/rapp-manager-sme/src/main/java/com/oransc/rappmanager/sme/service/SmeDeployer.java b/rapp-manager-sme/src/main/java/com/oransc/rappmanager/sme/service/SmeDeployer.java
index fc42f4d..adbf8fe 100755
--- a/rapp-manager-sme/src/main/java/com/oransc/rappmanager/sme/service/SmeDeployer.java
+++ b/rapp-manager-sme/src/main/java/com/oransc/rappmanager/sme/service/SmeDeployer.java
@@ -96,12 +96,15 @@
             if (deployState) {
                 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYED);
             } else {
+                rappInstance.setReason("Unable to deploy SME");
                 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYFAILED);
             }
             return deployState;
         } catch (JsonProcessingException e) {
             logger.warn("Failed to deploy SME functions for Rapp {}", rapp.getName(), e);
         }
+        rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYFAILED);
+        rappInstance.setReason("Unable to deploy SME");
         return false;
     }
 
@@ -118,6 +121,8 @@
         } catch (Exception e) {
             logger.warn("Failed to Undeploy SME functions for Rapp {}", rapp.getName(), e);
         }
+        rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEUNDEPLOYFAILED);
+        rappInstance.setReason("Unable to undeploy SME");
         return false;
     }
 
diff --git a/rapp-manager-sme/src/test/java/com/oransc/rappmanager/sme/service/SmeDeployerTest.java b/rapp-manager-sme/src/test/java/com/oransc/rappmanager/sme/service/SmeDeployerTest.java
index 28814da..ffa5457 100755
--- a/rapp-manager-sme/src/test/java/com/oransc/rappmanager/sme/service/SmeDeployerTest.java
+++ b/rapp-manager-sme/src/test/java/com/oransc/rappmanager/sme/service/SmeDeployerTest.java
@@ -391,6 +391,7 @@
         rappInstance.getSme().setProviderFunctionIds(providerFuncs.values().stream().toList());
         rappInstance.getSme().setServiceApiIds(serviceApis);
         rappInstance.getSme().setInvokerIds(invokers);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
         boolean undeployRapp = smeDeployer.undeployRappInstance(rapp, rappInstance);
         mockServer.verify();
         assertFalse(undeployRapp);