Add support for optional parameters in SME 96/12496/1
authoraravind.est <aravindhan.a@est.tech>
Wed, 31 Jan 2024 19:12:52 +0000 (19:12 +0000)
committeraravind.est <aravindhan.a@est.tech>
Wed, 31 Jan 2024 19:15:56 +0000 (19:15 +0000)
SME resources can be provided as optional.
rApp and rApp instances failures are provided in API response.

Issue-ID: NONRTRIC-975
Change-Id: I139fbb23289cb71b0c35187a1135cc1dd3899379
Signed-off-by: aravind.est <aravindhan.a@est.tech>
rapp-manager-application/src/main/java/com/oransc/rappmanager/service/RappService.java
rapp-manager-application/src/test/java/com/oransc/rappmanager/service/RappServiceTest.java
rapp-manager-sme/src/main/java/com/oransc/rappmanager/sme/service/SmeDeployer.java
rapp-manager-sme/src/test/java/com/oransc/rappmanager/sme/service/SmeDeployerTest.java

index 1e606d1..8e71d50 100755 (executable)
@@ -55,7 +55,7 @@ public class RappService {
                 return ResponseEntity.ok().build();
             }
             rapp.setState(RappState.COMMISSIONED);
-            return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
+            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()));
@@ -71,7 +71,7 @@ public class RappService {
                 return ResponseEntity.ok().build();
             }
             rapp.setState(RappState.PRIMED);
-            return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
+            throw new RappHandlerException(HttpStatus.BAD_GATEWAY, rapp.getReason());
         }
         if (!rapp.getRappInstances().isEmpty()) {
             throw new RappHandlerException(HttpStatus.BAD_REQUEST,
@@ -106,7 +106,7 @@ public class RappService {
                         .allMatch(rappDeployer -> rappDeployer.deployRappInstance(rapp, rappInstance))) {
                 return ResponseEntity.accepted().build();
             }
-            return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
+            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",
@@ -122,7 +122,7 @@ public class RappService {
                         .allMatch(rappDeployer -> rappDeployer.undeployRappInstance(rapp, rappInstance))) {
                 return ResponseEntity.accepted().build();
             }
-            return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
+            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",
index 29e0245..4154b6c 100755 (executable)
@@ -102,7 +102,9 @@ class RappServiceTest {
                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
         when(acmDeployer.primeRapp(any())).thenReturn(false);
         when(dmeDeployer.primeRapp(any())).thenReturn(true);
-        assertEquals(HttpStatus.BAD_GATEWAY, rappService.primeRapp(rapp).getStatusCode());
+        RappHandlerException rappHandlerException =
+                assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
+        assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
         assertEquals(RappState.COMMISSIONED, rapp.getState());
     }
 
@@ -112,7 +114,9 @@ class RappServiceTest {
                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
         when(acmDeployer.primeRapp(any())).thenReturn(true);
         when(dmeDeployer.primeRapp(any())).thenReturn(false);
-        assertEquals(HttpStatus.BAD_GATEWAY, rappService.primeRapp(rapp).getStatusCode());
+        RappHandlerException rappHandlerException =
+                assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp));
+        assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
         assertEquals(RappState.COMMISSIONED, rapp.getState());
     }
 
@@ -134,7 +138,9 @@ class RappServiceTest {
                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
         when(acmDeployer.deprimeRapp(any())).thenReturn(false);
         when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
-        assertEquals(HttpStatus.BAD_GATEWAY, rappService.deprimeRapp(rapp).getStatusCode());
+        RappHandlerException rappHandlerException =
+                assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
+        assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
         assertEquals(RappState.PRIMED, rapp.getState());
     }
 
@@ -144,7 +150,9 @@ class RappServiceTest {
                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
         when(acmDeployer.deprimeRapp(any())).thenReturn(true);
         when(dmeDeployer.deprimeRapp(any())).thenReturn(false);
-        assertEquals(HttpStatus.BAD_GATEWAY, rappService.deprimeRapp(rapp).getStatusCode());
+        RappHandlerException rappHandlerException =
+                assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp));
+        assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
         assertEquals(RappState.PRIMED, rapp.getState());
     }
 
@@ -192,7 +200,9 @@ class RappServiceTest {
         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
-        assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
+        RappHandlerException rappHandlerException =
+                assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance));
+        assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
     }
 
     @Test
@@ -234,7 +244,9 @@ class RappServiceTest {
         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
-        assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
+        RappHandlerException rappHandlerException =
+                assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance));
+        assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode());
     }
 
     @Test
index 2050b51..cab3ef8 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.
@@ -89,8 +90,9 @@ public class SmeDeployer implements RappDeployer {
     public boolean deployRappInstance(Rapp rapp, RappInstance rappInstance) {
         logger.debug("Deploying SME functions for RappInstance {}", rappInstance.getRappInstanceId());
         if (rappInstance.isSMEEnabled()) {
-            if (createProviderDomain(rapp, rappInstance) && createPublishApi(rapp, rappInstance) && createInvoker(rapp,
-                    rappInstance)) {
+            if ((rappInstance.getSme().getProviderFunction() == null || createProviderDomain(rapp, rappInstance)) && (
+                    rappInstance.getSme().getServiceApis() == null || createPublishApi(rapp, rappInstance)) && (
+                    rappInstance.getSme().getInvokers() == null || createInvoker(rapp, rappInstance))) {
                 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYED);
                 return true;
             }
@@ -106,10 +108,13 @@ public class SmeDeployer implements RappDeployer {
         logger.debug("Undeploying SME functions for Rapp {}", rapp.getName());
         if (rappInstance.isSMEEnabled()) {
             try {
-                rappInstance.getSme().getInvokerIds().forEach(this::deleteInvoker);
-                rappInstance.getSme().getServiceApiIds()
-                        .forEach(s -> deletePublishApi(s, rappInstance.getSme().getApfId()));
-                rappInstance.getSme().getProviderFunctionIds().forEach(this::deleteProviderFunc);
+                Optional.ofNullable(rappInstance.getSme().getInvokerIds())
+                        .ifPresent(invokerList -> invokerList.forEach(this::deleteInvoker));
+                Optional.ofNullable(rappInstance.getSme().getServiceApiIds()).ifPresent(
+                        serviceApiList -> serviceApiList.forEach(
+                                s -> deletePublishApi(s, rappInstance.getSme().getApfId())));
+                Optional.ofNullable(rappInstance.getSme().getProviderFunctionIds())
+                        .ifPresent(providerList -> providerList.forEach(this::deleteProviderFunc));
                 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEUNDEPLOYED);
                 return true;
             } catch (Exception e) {
index 4574ada..53d5b5d 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.
@@ -158,7 +159,7 @@ class SmeDeployerTest {
     }
 
     @Test
-    void testCreateProviderDomainFailure() throws Exception {
+    void testCreateProviderDomainFailure() {
         UUID rappId = UUID.randomUUID();
         Rapp rapp =
                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
@@ -201,7 +202,7 @@ class SmeDeployerTest {
 
 
     @Test
-    void testCreatePublishApiFailure() throws Exception {
+    void testCreatePublishApiFailure() {
         UUID rappId = UUID.randomUUID();
         UUID apfId = UUID.randomUUID();
         Rapp rapp =
@@ -298,7 +299,55 @@ class SmeDeployerTest {
     }
 
     @Test
-    void testDeployRappInstanceWithoutSme() throws Exception {
+    void testDeployRappInstanceNoInvoker() throws Exception {
+        UUID rappId = UUID.randomUUID();
+        APIProviderEnrolmentDetails apiProviderEnrolmentDetails = getProviderDomainApiEnrollmentDetails();
+        APIProviderFunctionDetails apfProviderFunctionDetails = apiProviderEnrolmentDetails.getApiProvFuncs().stream()
+                                                                        .filter(apiProviderFunctionDetails -> apiProviderFunctionDetails.getApiProvFuncRole()
+                                                                                                                      .equals(ApiProviderFuncRole.APF))
+                                                                        .findFirst().get();
+        Rapp rapp =
+                Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
+                        .state(RappState.COMMISSIONED).build();
+        mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
+                .andExpect(method(HttpMethod.POST)).andRespond(
+                        withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
+                                .body(objectMapper.writeValueAsString(apiProviderEnrolmentDetails)));
+        ServiceAPIDescription serviceAPIDescription = getServiceApiDescription();
+        mockServer.expect(ExpectedCount.once(),
+                        requestTo(String.format(URI_PUBLISH_APIS, apfProviderFunctionDetails.getApiProvFuncId())))
+                .andExpect(method(HttpMethod.POST)).andRespond(
+                        withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
+                                .body(objectMapper.writeValueAsString(serviceAPIDescription)));
+        RappInstance rappInstance = getRappInstance();
+        rappInstance.getSme().setInvokers(null);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
+        boolean deployRapp = smeDeployer.deployRappInstance(rapp, rappInstance);
+        mockServer.verify();
+        assertTrue(deployRapp);
+    }
+
+    @Test
+    void testDeployRappInstanceNoProvider() throws Exception {
+        UUID rappId = UUID.randomUUID();
+        Rapp rapp =
+                Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
+                        .state(RappState.COMMISSIONED).build();
+        APIInvokerEnrolmentDetails apiInvokerEnrolmentDetails = getApiInvokerEnrollmentDetails();
+        mockServer.expect(ExpectedCount.once(), requestTo(URI_INVOKERS)).andExpect(method(HttpMethod.POST)).andRespond(
+                withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
+                        .body(objectMapper.writeValueAsString(apiInvokerEnrolmentDetails)));
+        RappInstance rappInstance = getRappInstance();
+        rappInstance.getSme().setProviderFunction(null);
+        rappInstance.getSme().setServiceApis(null);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
+        boolean deployRapp = smeDeployer.deployRappInstance(rapp, rappInstance);
+        mockServer.verify();
+        assertTrue(deployRapp);
+    }
+
+    @Test
+    void testDeployRappInstanceWithoutSme() {
         UUID rappId = UUID.randomUUID();
         Rapp rapp =
                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
@@ -374,6 +423,58 @@ class SmeDeployerTest {
         assertTrue(undeployRapp);
     }
 
+    @Test
+    void testUndeployRappInstanceNoInvokers() {
+        UUID rappId = UUID.randomUUID();
+        UUID apfId = UUID.randomUUID();
+        List<String> serviceApis = List.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
+        Map<String, String> providerFuncs = Map.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()),
+                String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
+        Rapp rapp = Rapp.builder().rappId(rappId).name(rappId.toString()).packageName(validRappFile)
+                            .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+        mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApis.get(0))))
+                .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
+        mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApis.get(1))))
+                .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
+        mockServer.expect(ExpectedCount.once(),
+                        requestTo(String.format(URI_PROVIDER_REGISTRATION, providerFuncs.values().toArray()[0])))
+                .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
+        mockServer.expect(ExpectedCount.once(),
+                        requestTo(String.format(URI_PROVIDER_REGISTRATION, providerFuncs.values().toArray()[1])))
+                .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
+        RappInstance rappInstance = getRappInstance();
+        rappInstance.getSme().setApfId(String.valueOf(apfId));
+        rappInstance.getSme().setProviderFunctionIds(providerFuncs.values().stream().toList());
+        rappInstance.getSme().setServiceApiIds(serviceApis);
+        rappInstance.getSme().setInvokerIds(null);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
+        boolean undeployRapp = smeDeployer.undeployRappInstance(rapp, rappInstance);
+        mockServer.verify();
+        assertTrue(undeployRapp);
+    }
+
+    @Test
+    void testUndeployRappInstanceNoProviders() {
+        UUID rappId = UUID.randomUUID();
+        UUID apfId = UUID.randomUUID();
+        List<String> invokers = List.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
+        Rapp rapp = Rapp.builder().rappId(rappId).name(rappId.toString()).packageName(validRappFile)
+                            .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+        mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokers.get(0))))
+                .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
+        mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokers.get(1))))
+                .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
+        RappInstance rappInstance = getRappInstance();
+        rappInstance.getSme().setApfId(String.valueOf(apfId));
+        rappInstance.getSme().setProviderFunctionIds(null);
+        rappInstance.getSme().setServiceApiIds(null);
+        rappInstance.getSme().setInvokerIds(invokers);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
+        boolean undeployRapp = smeDeployer.undeployRappInstance(rapp, rappInstance);
+        mockServer.verify();
+        assertTrue(undeployRapp);
+    }
+
     @Test
     void testUndeployRappInstanceWithoutSme() {
         UUID rappId = UUID.randomUUID();