f51d43d5277f72004ac580aacdcec09b08d46a75
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.supervision.scanner;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.clearInvocations;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
31
32 import java.util.List;
33 import java.util.Map;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
37 import org.onap.policy.clamp.acm.runtime.main.utils.EncryptionUtils;
38 import org.onap.policy.clamp.acm.runtime.supervision.comm.AcPreparePublisher;
39 import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionMigrationPublisher;
40 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
41 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
43 import org.onap.policy.clamp.models.acm.concepts.DeployState;
44 import org.onap.policy.clamp.models.acm.concepts.LockState;
45 import org.onap.policy.clamp.models.acm.concepts.SubState;
46 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
47 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
48
49 class StageScannerTest {
50     private static final String AC_JSON = "src/test/resources/rest/acm/AutomationCompositionSmoke.json";
51     private static final UUID COMPOSITION_ID = UUID.randomUUID();
52
53     @Test
54     void testSendAutomationCompositionMigrate() {
55         var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
56         automationComposition.setInstanceId(UUID.randomUUID());
57         automationComposition.setDeployState(DeployState.MIGRATING);
58         automationComposition.setCompositionId(COMPOSITION_ID);
59         var compositionTargetId = UUID.randomUUID();
60         automationComposition.setCompositionTargetId(compositionTargetId);
61         automationComposition.setLockState(LockState.LOCKED);
62         automationComposition.setLastMsg(TimestampHelper.now());
63         automationComposition.setPhase(0);
64         for (var element : automationComposition.getElements().values()) {
65             element.setDeployState(DeployState.DEPLOYED);
66             element.setLockState(LockState.LOCKED);
67         }
68         // first element is not migrated yet
69         var element = automationComposition.getElements().entrySet().iterator().next().getValue();
70         element.setDeployState(DeployState.MIGRATING);
71
72         var acProvider = mock(AutomationCompositionProvider.class);
73         when(acProvider.updateAutomationComposition(any())).thenReturn(automationComposition);
74         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
75         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
76         var supervisionScanner = new StageScanner(acProvider, mock(ParticipantSyncPublisher.class),
77                 mock(AutomationCompositionMigrationPublisher.class), mock(AcPreparePublisher.class),
78                 acRuntimeParameterGroup, encryptionUtils);
79         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
80         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
81         verify(acProvider, times(0)).updateAutomationComposition(any(AutomationComposition.class));
82         assertEquals(DeployState.MIGRATING, automationComposition.getDeployState());
83
84         // send message for next stage
85         clearInvocations(acProvider);
86         var toscaNodeTemplate = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
87                 .get(element.getDefinition().getName());
88         toscaNodeTemplate.setProperties(Map.of("stage", List.of(1)));
89
90         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
91         verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
92         assertEquals(DeployState.MIGRATING, automationComposition.getDeployState());
93
94         // first element is migrated
95         clearInvocations(acProvider);
96         element.setDeployState(DeployState.DEPLOYED);
97         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
98         verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
99
100         assertEquals(DeployState.DEPLOYED, automationComposition.getDeployState());
101         assertEquals(compositionTargetId, automationComposition.getCompositionId());
102     }
103
104     @Test
105     void testSendAutomationCompositionMigrationReverting() {
106         var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
107         automationComposition.setInstanceId(UUID.randomUUID());
108         automationComposition.setDeployState(DeployState.MIGRATION_REVERTING);
109         automationComposition.setCompositionId(COMPOSITION_ID);
110         var compositionTargetId = UUID.randomUUID();
111         automationComposition.setCompositionTargetId(compositionTargetId);
112         automationComposition.setLockState(LockState.LOCKED);
113         automationComposition.setLastMsg(TimestampHelper.now());
114         automationComposition.setPhase(0);
115         for (var element : automationComposition.getElements().values()) {
116             element.setDeployState(DeployState.DEPLOYED);
117             element.setLockState(LockState.LOCKED);
118         }
119         // first element is not migrated yet
120         var element = automationComposition.getElements().entrySet().iterator().next().getValue();
121         element.setDeployState(DeployState.MIGRATION_REVERTING);
122
123         var acProvider = mock(AutomationCompositionProvider.class);
124         when(acProvider.updateAutomationComposition(any())).thenReturn(automationComposition);
125         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
126         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
127         var supervisionScanner = new StageScanner(acProvider, mock(ParticipantSyncPublisher.class),
128                 mock(AutomationCompositionMigrationPublisher.class), mock(AcPreparePublisher.class),
129                 acRuntimeParameterGroup, encryptionUtils);
130         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
131         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
132         verify(acProvider, times(0)).updateAutomationComposition(any(AutomationComposition.class));
133         assertEquals(DeployState.MIGRATION_REVERTING, automationComposition.getDeployState());
134
135         // send message for next stage
136         clearInvocations(acProvider);
137         var toscaNodeTemplate = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
138                 .get(element.getDefinition().getName());
139         toscaNodeTemplate.setProperties(Map.of("stage", List.of(1)));
140
141         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
142         verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
143         assertEquals(DeployState.MIGRATION_REVERTING, automationComposition.getDeployState());
144
145         // first element is migrated
146         clearInvocations(acProvider);
147         element.setDeployState(DeployState.DEPLOYED);
148         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
149         verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
150
151         assertEquals(DeployState.DEPLOYED, automationComposition.getDeployState());
152         assertEquals(compositionTargetId, automationComposition.getCompositionId());
153     }
154
155     @Test
156     void testSendAutomationCompositionPrepare() {
157         var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
158         automationComposition.setDeployState(DeployState.UNDEPLOYED);
159         automationComposition.setSubState(SubState.PREPARING);
160         automationComposition.setLockState(LockState.NONE);
161         automationComposition.setCompositionId(COMPOSITION_ID);
162         automationComposition.setLastMsg(TimestampHelper.now());
163         automationComposition.setPhase(0);
164         for (var element : automationComposition.getElements().values()) {
165             element.setDeployState(DeployState.UNDEPLOYED);
166             element.setLockState(LockState.NONE);
167             element.setSubState(SubState.NONE);
168         }
169         // first element is not prepared yet
170         var element = automationComposition.getElements().entrySet().iterator().next().getValue();
171         element.setSubState(SubState.PREPARING);
172
173         var acProvider = mock(AutomationCompositionProvider.class);
174         when(acProvider.updateAutomationComposition(any())).thenReturn(automationComposition);
175
176         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
177         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
178         var supervisionScanner = new StageScanner(acProvider, mock(ParticipantSyncPublisher.class),
179                 mock(AutomationCompositionMigrationPublisher.class), mock(AcPreparePublisher.class),
180                 acRuntimeParameterGroup, encryptionUtils);
181
182         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
183         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
184         verify(acProvider, times(0)).updateAutomationComposition(any(AutomationComposition.class));
185         assertEquals(SubState.PREPARING, automationComposition.getSubState());
186
187         // send message for next stage
188         clearInvocations(acProvider);
189         var toscaNodeTemplate = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
190                 .get(element.getDefinition().getName());
191         var prepare = Map.of("prepare", List.of(1));
192         toscaNodeTemplate.setProperties(Map.of("stage", prepare));
193
194         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
195         verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
196         assertEquals(SubState.PREPARING, automationComposition.getSubState());
197
198         // first element is prepared
199         clearInvocations(acProvider);
200         element.setSubState(SubState.NONE);
201         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
202         verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
203
204         assertEquals(SubState.NONE, automationComposition.getSubState());
205     }
206 }