Add missing validation in Delete AC instance

Add missing validation in Delete AC instance
when deployState is in DELETING.
Fix send Heartbeat when "reportingTimeIntervalMs" is set to short time.

Issue-ID: POLICY-4773
Change-Id: Ia90902a68aadbde5c2bb63d093a2495f1a529571
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
(cherry picked from commit d944ef408f099cbd5e3644e4abb37886d16c985e)
diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/MessageSender.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/MessageSender.java
old mode 100644
new mode 100755
index 0810a8a..2388b3b
--- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/MessageSender.java
+++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/MessageSender.java
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Nordix Foundation.
+ *  Copyright (C) 2021,2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@
 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.context.event.EventListener;
 import org.springframework.stereotype.Component;
 
 /**
@@ -39,7 +41,8 @@
     private static final Logger LOGGER = LoggerFactory.getLogger(MessageSender.class);
 
     private final ParticipantHandler participantHandler;
-    private ScheduledExecutorService timerPool;
+    private final ScheduledExecutorService timerPool;
+    private final long interval;
 
     /**
      * Constructor, set the publisher.
@@ -52,7 +55,11 @@
 
         // Kick off the timer
         timerPool = makeTimerPool();
-        var interval = parameters.getIntermediaryParameters().getReportingTimeIntervalMs();
+        interval = parameters.getIntermediaryParameters().getReportingTimeIntervalMs();
+    }
+
+    @EventListener
+    public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
         timerPool.scheduleAtFixedRate(this, interval, interval, TimeUnit.MILLISECONDS);
     }
 
diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/ParticipantMessagePublisher.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/ParticipantMessagePublisher.java
old mode 100644
new mode 100755
index 4c885fa..0f0f5dd
--- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/ParticipantMessagePublisher.java
+++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/ParticipantMessagePublisher.java
@@ -24,6 +24,7 @@
 import io.micrometer.core.annotation.Timed;
 import java.util.List;
 import javax.ws.rs.core.Response.Status;
+import lombok.Getter;
 import org.onap.policy.clamp.acm.participant.intermediary.handler.Publisher;
 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
@@ -46,6 +47,7 @@
     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantMessagePublisher.class);
     private static final String NOT_ACTIVE_TEXT = "Not Active!";
 
+    @Getter
     private boolean active = false;
     private TopicSinkClient topicSinkClient;
 
diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandler.java
old mode 100644
new mode 100755
index 8f69fd5..ed1d918
--- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandler.java
+++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandler.java
@@ -222,7 +222,9 @@
      * Dispatch a heartbeat for this participant.
      */
     public void sendHeartbeat() {
-        publisher.sendHeartbeat(makeHeartbeat(false));
+        if (publisher.isActive()) {
+            publisher.sendHeartbeat(makeHeartbeat(false));
+        }
     }
 
     /**
diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/comm/ParticipantCommTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/comm/ParticipantCommTest.java
old mode 100644
new mode 100755
index db6a3a5..e959d13
--- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/comm/ParticipantCommTest.java
+++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/comm/ParticipantCommTest.java
@@ -137,9 +137,9 @@
         var participantHandler = mock(ParticipantHandler.class);
         var participantParameters = CommonTestData.getParticipantParameters();
         var messageSender = new MessageSender(participantHandler, participantParameters);
+        messageSender.handleContextRefreshEvent(null);
         messageSender.run();
         assertFalse(messageSender.makeTimerPool().isTerminated());
         messageSender.close();
     }
-
 }
diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandlerTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandlerTest.java
old mode 100644
new mode 100755
index 2f1eb22..c558e66
--- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandlerTest.java
+++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandlerTest.java
@@ -217,6 +217,7 @@
         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
         when(cacheProvider.getAutomationCompositions()).thenReturn(CommonTestData.getTestAutomationCompositionMap());
         var publisher = mock(ParticipantMessagePublisher.class);
+        when(publisher.isActive()).thenReturn(true);
         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
                 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
         participantHandler.sendHeartbeat();
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java
old mode 100644
new mode 100755
index 710a975..84944e7
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java
@@ -35,6 +35,7 @@
 import org.onap.policy.clamp.models.acm.concepts.DeployState;
 import org.onap.policy.clamp.models.acm.concepts.LockState;
 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
+import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.AcInstanceStateUpdate;
 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
@@ -254,6 +255,11 @@
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
                     "Automation composition state is still " + automationComposition.getDeployState());
         }
+        if (DeployState.DELETING.equals(automationComposition.getDeployState())
+                && StateChangeResult.NO_ERROR.equals(automationComposition.getStateChangeResult())) {
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
+                    "Automation composition state is still " + automationComposition.getDeployState());
+        }
         if (automationComposition.getRestarting() != null) {
             throw new PfModelRuntimeException(Status.BAD_REQUEST, "There is a restarting process, Delete not allowed");
         }
diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProviderTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProviderTest.java
old mode 100644
new mode 100755
index 3ac2efc..a59872f
--- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProviderTest.java
+++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProviderTest.java
@@ -44,6 +44,7 @@
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
 import org.onap.policy.clamp.models.acm.concepts.DeployState;
 import org.onap.policy.clamp.models.acm.concepts.LockState;
+import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.AcInstanceStateUpdate;
 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
@@ -257,7 +258,7 @@
     void testInstantiationDelete() {
         var automationComposition =
                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Delete");
-
+        automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
         var acProvider = mock(AutomationCompositionProvider.class);
         var acDefinitionProvider = mock(AcDefinitionProvider.class);
         var supervisionAcHandler = mock(SupervisionAcHandler.class);
@@ -278,6 +279,7 @@
         assertThatDeleteThrownBy(automationComposition, DeployState.DEPLOYED, LockState.LOCKED);
         assertThatDeleteThrownBy(automationComposition, DeployState.DEPLOYING, LockState.NONE);
         assertThatDeleteThrownBy(automationComposition, DeployState.UNDEPLOYING, LockState.LOCKED);
+        assertThatDeleteThrownBy(automationComposition, DeployState.DELETING, LockState.NONE);
 
         automationComposition.setDeployState(DeployState.UNDEPLOYED);
         automationComposition.setLockState(LockState.NONE);