Pass rAppInstanceId to k8s participant and create the invoker with the instance Id
[nonrtric/plt/rappmanager.git] / rapp-manager-sme / src / main / java / com / oransc / rappmanager / sme / service / SmeDeployer.java
index adbf8fe..8989e82 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.
@@ -18,7 +19,6 @@
 
 package com.oransc.rappmanager.sme.service;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.oransc.rappmanager.models.RappDeployer;
@@ -89,41 +89,42 @@ public class SmeDeployer implements RappDeployer {
     @Override
     public boolean deployRappInstance(Rapp rapp, RappInstance rappInstance) {
         logger.debug("Deploying SME functions for RappInstance {}", rappInstance.getRappInstanceId());
-        try {
-            boolean deployState =
-                    createProviderDomain(rapp, rappInstance) && createPublishApi(rapp, rappInstance) && createInvoker(
-                            rapp, rappInstance);
-            if (deployState) {
+        if (rappInstance.isSMEEnabled()) {
+            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);
-            } else {
-                rappInstance.setReason("Unable to deploy SME");
-                rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYFAILED);
+                return true;
             }
-            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;
         }
-        rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYFAILED);
-        rappInstance.setReason("Unable to deploy SME");
-        return false;
+        return true;
     }
 
     @Override
     public boolean undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
         logger.debug("Undeploying SME functions for Rapp {}", rapp.getName());
-        try {
-            rappInstance.getSme().getInvokerIds().forEach(this::deleteInvoker);
-            rappInstance.getSme().getServiceApiIds()
-                    .forEach(s -> deletePublishApi(s, rappInstance.getSme().getApfId()));
-            rappInstance.getSme().getProviderFunctionIds().forEach(this::deleteProviderFunc);
-            rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEUNDEPLOYED);
-            return true;
-        } catch (Exception e) {
-            logger.warn("Failed to Undeploy SME functions for Rapp {}", rapp.getName(), e);
+        if (rappInstance.isSMEEnabled()) {
+            try {
+                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) {
+                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;
         }
-        rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEUNDEPLOYFAILED);
-        rappInstance.setReason("Unable to undeploy SME");
-        return false;
+        return true;
     }
 
     @Override
@@ -138,7 +139,7 @@ public class SmeDeployer implements RappDeployer {
         return true;
     }
 
-    boolean createProviderDomain(Rapp rapp, RappInstance rappInstance) throws JsonProcessingException {
+    boolean createProviderDomain(Rapp rapp, RappInstance rappInstance) {
         logger.debug("Creating provider domain for Rapp {}", rapp.getName());
         try {
             String providerDomainPayload =
@@ -185,7 +186,7 @@ public class SmeDeployer implements RappDeployer {
     }
 
 
-    boolean createPublishApi(Rapp rapp, RappInstance rappInstance) throws JsonProcessingException {
+    boolean createPublishApi(Rapp rapp, RappInstance rappInstance) {
         logger.debug("Creating publish api for Rapp {}", rapp.getName());
         try {
             String providerApiPayload =
@@ -214,10 +215,10 @@ public class SmeDeployer implements RappDeployer {
         publishServiceDefaultApiClient.deleteApfIdServiceApisServiceApiId(serviceApiId, apfId);
     }
 
-    boolean createInvoker(Rapp rapp, RappInstance rappInstance) throws JsonProcessingException {
+    boolean createInvoker(Rapp rapp, RappInstance rappInstance) {
         logger.debug("Creating provider domain for Rapp {}", rapp.getName());
         try {
-            String invokerPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappInstance.getSme());
+            String invokerPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappInstance);
             if (invokerPayload != null) {
                 List<APIInvokerEnrolmentDetails> apiInvokerEnrolmentDetails =
                         objectMapper.readValue(invokerPayload, new TypeReference<>() { });