Add migration support in ACM participant simulator

Issue-ID: POLICY-4826
Change-Id: Idf492cc8941ce6b2800d1ea88789cca103a33b2c
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
diff --git a/participant/participant-impl/participant-impl-simulator/pom.xml b/participant/participant-impl/participant-impl-simulator/pom.xml
index 13b8ec3..912c1a7 100755
--- a/participant/participant-impl/participant-impl-simulator/pom.xml
+++ b/participant/participant-impl/participant-impl-simulator/pom.xml
@@ -51,6 +51,7 @@
                             <generateSupportingFiles>false</generateSupportingFiles>
                             <importMappings>
                                 AutomationCompositions=org.onap.policy.clamp.models.acm.concepts.AutomationCompositions,
+                                AutomationComposition=org.onap.policy.clamp.models.acm.concepts.AutomationComposition,
                                 InternalDatas=org.onap.policy.clamp.acm.participant.sim.model.InternalDatas,
                                 InternalData=org.onap.policy.clamp.acm.participant.sim.model.InternalData,
                                 SimConfig=org.onap.policy.clamp.acm.participant.sim.model.SimConfig
diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandler.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandler.java
index a1564b2..7409c11 100755
--- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandler.java
+++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandler.java
@@ -35,6 +35,7 @@
 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
+import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
 import org.onap.policy.clamp.models.acm.concepts.DeployState;
@@ -217,6 +218,10 @@
         return result;
     }
 
+    public AutomationComposition getAutomationComposition(UUID instanceId) {
+        return intermediaryApi.getAutomationComposition(instanceId);
+    }
+
     /**
      * Set OutProperties.
      *
@@ -280,6 +285,7 @@
         for (var instance : map.values()) {
             for (var element : instance.getElements().values()) {
                 var data = new InternalData();
+                data.setCompositionId(instance.getCompositionId());
                 data.setAutomationCompositionId(instance.getInstanceId());
                 data.setAutomationCompositionElementId(element.getId());
                 data.setIntProperties(element.getProperties());
@@ -375,8 +381,20 @@
 
     @Override
     public void migrate(UUID automationCompositionId, AcElementDeploy element, UUID compositionTargetId,
-                        Map<String, Object> properties) throws PfModelException {
-        intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
-            DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
+            Map<String, Object> properties) throws PfModelException {
+        LOGGER.debug("migrate call");
+
+        if (!execution(config.getMigrateTimerMs(), "Current Thread migrate is Interrupted during execution {}",
+                element.getId())) {
+            return;
+        }
+
+        if (config.isMigrateSuccess()) {
+            intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
+                    DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
+        } else {
+            intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
+                    DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
+        }
     }
 }
diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/model/SimConfig.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/model/SimConfig.java
old mode 100644
new mode 100755
index cf216c1..f4d4c6e
--- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/model/SimConfig.java
+++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/model/SimConfig.java
@@ -36,6 +36,8 @@
 
     private boolean updateSuccess = true;
 
+    private boolean migrateSuccess = true;
+
     private boolean primeSuccess = true;
 
     private boolean deprimeSuccess = true;
@@ -50,6 +52,8 @@
 
     private int updateTimerMs = 100;
 
+    private int migrateTimerMs = 100;
+
     private int deleteTimerMs = 100;
 
     private int primeTimerMs = 100;
diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/rest/SimulatorController.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/rest/SimulatorController.java
index 445a2a1..cf4ac8e 100755
--- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/rest/SimulatorController.java
+++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/rest/SimulatorController.java
@@ -29,6 +29,7 @@
 import org.onap.policy.clamp.acm.participant.sim.model.InternalData;
 import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas;
 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
+import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -60,6 +61,12 @@
     }
 
     @Override
+    public ResponseEntity<AutomationComposition> getAutomationComposition(UUID instanceId, UUID xonapRequestId) {
+        return new ResponseEntity<>(automationCompositionElementHandler.getAutomationComposition(instanceId),
+                HttpStatus.OK);
+    }
+
+    @Override
     public ResponseEntity<InternalDatas> getDatas(UUID xonapRequestId) {
         return new ResponseEntity<>(automationCompositionElementHandler.getDataList(), HttpStatus.OK);
     }
diff --git a/participant/participant-impl/participant-impl-simulator/src/main/resources/openapi/openapi.yaml b/participant/participant-impl/participant-impl-simulator/src/main/resources/openapi/openapi.yaml
index c3b02e4..197e3cb 100755
--- a/participant/participant-impl/participant-impl-simulator/src/main/resources/openapi/openapi.yaml
+++ b/participant/participant-impl/participant-impl-simulator/src/main/resources/openapi/openapi.yaml
@@ -170,6 +170,54 @@
           description: Internal Server Error
       security:
         - basicAuth: []
+  /instances/{instanceId}:
+    get:
+      tags:
+        - Simulator-participant-controller
+      summary: Get automation composition instance details.
+      description: Get details of the requested automation composition instance.
+      operationId: getAutomationComposition
+      parameters:
+      - name : instanceId
+        in: path
+        description: The UUID of the automation composition instance to get
+        required: true
+        schema:
+          type: string
+          format: uuid
+      - name: X-onap-RequestId
+        in: header
+        description: RequestID for http transaction
+        schema:
+          type: string
+          format: uuid
+      responses:
+        200:
+          description: Serialised instance of
+            [AutomationComposition](https://github.com/onap/policy-clamp/blob/master/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java)
+            containing the automation composition instance
+          headers:
+            X-LatestVersion:
+              $ref: '#/components/headers/X-LatestVersion'
+            X-PatchVersion:
+              $ref: '#/components/headers/X-PatchVersion'
+            X-MinorVersion:
+              $ref: '#/components/headers/X-MinorVersion'
+            X-onap-RequestId:
+              $ref: '#/components/headers/X-onap-RequestId'
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/AutomationComposition'
+            application/yaml:
+              schema:
+                $ref: '#/components/schemas/AutomationComposition'
+        401:
+          description: Authorization Error
+        500:
+          description: Internal Server Error
+      security:
+        - basicAuth: []
   /datas:
     get:
       tags:
@@ -353,6 +401,9 @@
     AutomationCompositions:
       title: AutomationCompositions
       type: object
+    AutomationComposition:
+      title: AutomationComposition
+      type: object
     InternalDatas:
       title: InternalDatas
       type: object
diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java
index ed3f074..d7c9e17 100755
--- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java
+++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java
@@ -162,7 +162,7 @@
     }
 
     @Test
-    void testgetAutomationComposition() throws PfModelException {
+    void testgetAutomationCompositions() throws PfModelException {
         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
         var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi);
 
@@ -173,6 +173,17 @@
     }
 
     @Test
+    void testgetAutomationComposition() throws PfModelException {
+        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
+        var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi);
+
+        var instance = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
+        when(intermediaryApi.getAutomationComposition(instance.getInstanceId())).thenReturn(instance);
+        var result = acElementHandler.getAutomationComposition(instance.getInstanceId());
+        assertEquals(instance, result);
+    }
+
+    @Test
     void testsetOutProperties() {
         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
         var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi);
@@ -334,4 +345,24 @@
         acElementHandler.setCompositionOutProperties(compositionId, null, Map.of());
         verify(intermediaryApi).sendAcDefinitionInfo(compositionId, null, Map.of());
     }
+
+    @Test
+    void testMigrate() throws PfModelException {
+        var config = new SimConfig();
+        config.setUpdateTimerMs(1);
+        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
+        var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi);
+        acElementHandler.setConfig(config);
+        var instanceId = UUID.randomUUID();
+        var element = new AcElementDeploy();
+        element.setId(UUID.randomUUID());
+        acElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of());
+        verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
+                DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
+
+        config.setMigrateSuccess(false);
+        acElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of());
+        verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
+                DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
+    }
 }