Add support to save outProperties in AC Definition in ACM runtime

Add support to save outProperties in AC Definition
from Participant Status message.
Add outProperties support in restart message.

Issue-ID: POLICY-4781
Change-Id: I9f9bed5c9c3e6692a1e4666162b752db95994f95
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandler.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandler.java
index f8bc233..ebccc2f 100755
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandler.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandler.java
@@ -35,6 +35,7 @@
 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
 import org.onap.policy.clamp.models.acm.concepts.Participant;
+import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
@@ -130,6 +131,29 @@
         if (!participantStatusMsg.getAutomationCompositionInfoList().isEmpty()) {
             automationCompositionProvider.upgradeStates(participantStatusMsg.getAutomationCompositionInfoList());
         }
+        if (!participantStatusMsg.getParticipantDefinitionUpdates().isEmpty()
+                && participantStatusMsg.getCompositionId() != null) {
+            updateAcDefinitionOutProperties(participantStatusMsg.getCompositionId(),
+                    participantStatusMsg.getParticipantDefinitionUpdates());
+        }
+    }
+
+    private void updateAcDefinitionOutProperties(UUID composotionId, List<ParticipantDefinition> list) {
+        var acDefinitionOpt = acDefinitionProvider.findAcDefinition(composotionId);
+        if (acDefinitionOpt.isEmpty()) {
+            LOGGER.error("Ac Definition with id {} not found", composotionId);
+            return;
+        }
+        var acDefinition = acDefinitionOpt.get();
+        for (var acElements : list) {
+            for (var element : acElements.getAutomationCompositionElementDefinitionList()) {
+                var state = acDefinition.getElementStateMap().get(element.getAcElementDefinitionId().getName());
+                if (state != null) {
+                    state.setOutProperties(element.getOutProperties());
+                }
+            }
+        }
+        acDefinitionProvider.updateAcDefinition(acDefinition);
     }
 
     private void checkOnline(Participant participant) {
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/ParticipantRestartPublisher.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/ParticipantRestartPublisher.java
old mode 100644
new mode 100755
index b086b19..c00116e
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/ParticipantRestartPublisher.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/ParticipantRestartPublisher.java
@@ -100,6 +100,15 @@
                 elementList.add(elementEntry);
             }
         }
-        return AcmUtils.prepareParticipantPriming(elementList, supportedElementMap);
+        var list = AcmUtils.prepareParticipantPriming(elementList, supportedElementMap);
+        for (var participantDefinition : list) {
+            for (var elementDe : participantDefinition.getAutomationCompositionElementDefinitionList()) {
+                var state = acmDefinition.getElementStateMap().get(elementDe.getAcElementDefinitionId().getName());
+                if (state != null) {
+                    elementDe.setOutProperties(state.getOutProperties());
+                }
+            }
+        }
+        return list;
     }
 }
diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java
index 49aa030..670806f 100755
--- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java
+++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java
@@ -38,17 +38,19 @@
 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
+import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
 import org.onap.policy.clamp.models.acm.concepts.Participant;
+import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
-import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
 class SupervisionParticipantHandlerTest {
 
@@ -149,11 +151,7 @@
 
     @Test
     void testHandleParticipantStatus() {
-        var participantStatusMessage = new ParticipantStatus();
-        participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
-        participantStatusMessage.setState(ParticipantState.ON_LINE);
-        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
-        participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
+        var participantStatusMessage = createParticipantStatus();
         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
 
         var participantProvider = mock(ParticipantProvider.class);
@@ -171,12 +169,40 @@
     }
 
     @Test
+    void testAcDefinitionOutProperties() {
+        var participantStatusMessage = createParticipantStatus();
+        participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
+        var participantDefinition = new ParticipantDefinition();
+        participantStatusMessage.setParticipantDefinitionUpdates(List.of(participantDefinition));
+        participantDefinition.setParticipantId(participantStatusMessage.getParticipantId());
+        var acElementDefinition = new AutomationCompositionElementDefinition();
+        acElementDefinition.setAcElementDefinitionId(new ToscaConceptIdentifier("code", "1.0.0"));
+        participantDefinition.setAutomationCompositionElementDefinitionList(List.of(acElementDefinition));
+
+        var compositionId = UUID.randomUUID();
+        participantStatusMessage.setCompositionId(compositionId);
+        var acDefinition = new AutomationCompositionDefinition();
+        acDefinition.setState(AcTypeState.COMMISSIONED);
+        acDefinition.setCompositionId(compositionId);
+        var nodeTemplateState = new NodeTemplateState();
+        acDefinition.setElementStateMap(
+                Map.of(acElementDefinition.getAcElementDefinitionId().getName(), nodeTemplateState));
+        var acDefinitionProvider = mock(AcDefinitionProvider.class);
+        when(acDefinitionProvider.findAcDefinition(compositionId)).thenReturn(Optional.of(acDefinition));
+
+        var participantProvider = mock(ParticipantProvider.class);
+        var handler =
+                new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
+                        mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
+                        acDefinitionProvider, mock(ParticipantRestartPublisher.class));
+        handler.handleParticipantMessage(participantStatusMessage);
+
+        verify(acDefinitionProvider).updateAcDefinition(acDefinition);
+    }
+
+    @Test
     void testHandleParticipantStatusNotRegisterd() {
-        var participantStatusMessage = new ParticipantStatus();
-        participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
-        participantStatusMessage.setState(ParticipantState.ON_LINE);
-        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
-        participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
+        var participantStatusMessage = createParticipantStatus();
         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
 
         var participantProvider = mock(ParticipantProvider.class);
@@ -193,11 +219,7 @@
 
     @Test
     void testHandleParticipantStatusCheckOnline() {
-        var participantStatusMessage = new ParticipantStatus();
-        participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
-        participantStatusMessage.setState(ParticipantState.ON_LINE);
-        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
-        participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
+        var participantStatusMessage = createParticipantStatus();
         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
 
         var participantProvider = mock(ParticipantProvider.class);
@@ -215,4 +237,13 @@
         verify(participantProvider).saveParticipant(any());
         verify(automationCompositionProvider).upgradeStates(any());
     }
+
+    private ParticipantStatus createParticipantStatus() {
+        var statusMessage = new ParticipantStatus();
+        statusMessage.setParticipantId(CommonTestData.getParticipantId());
+        statusMessage.setState(ParticipantState.ON_LINE);
+        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
+        statusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
+        return statusMessage;
+    }
 }