932529d79c83d38f023afb2d69326b233c66db78
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation.
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;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Optional;
33 import java.util.Set;
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.parameters.AcRuntimeParameterGroup;
38 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
39 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
40 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRestartPublisher;
41 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
42 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
43 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
45 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
46 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
47 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
48 import org.onap.policy.clamp.models.acm.concepts.Participant;
49 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
50 import org.onap.policy.clamp.models.acm.concepts.ParticipantReplica;
51 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
52 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
53 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
54 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
55 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
56 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
57 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
59
60 class SupervisionParticipantHandlerTest {
61
62     private static final String AC_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/acm/AutomationComposition.json";
63
64     @Test
65     void testHandleParticipantDeregister() {
66         var replica = CommonTestData.createParticipantReplica(CommonTestData.getReplicaId());
67
68         var participantProvider = mock(ParticipantProvider.class);
69         when(participantProvider.findParticipantReplica(replica.getReplicaId()))
70                 .thenReturn(Optional.of(replica));
71
72         var participantDeregisterMessage = new ParticipantDeregister();
73         participantDeregisterMessage.setMessageId(UUID.randomUUID());
74         participantDeregisterMessage.setParticipantId(CommonTestData.getParticipantId());
75         participantDeregisterMessage.setReplicaId(replica.getReplicaId());
76         var participantDeregisterAckPublisher = mock(ParticipantDeregisterAckPublisher.class);
77         var handler =
78                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
79                         participantDeregisterAckPublisher, mock(AutomationCompositionProvider.class),
80                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
81                         mock(ParticipantSyncPublisher.class), mock(AcRuntimeParameterGroup.class));
82
83         handler.handleParticipantMessage(participantDeregisterMessage);
84
85         verify(participantProvider).deleteParticipantReplica(CommonTestData.getReplicaId());
86         verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId());
87     }
88
89     @Test
90     void testHandleParticipantRegister() {
91         var participantRegisterMessage = new ParticipantRegister();
92         participantRegisterMessage.setMessageId(UUID.randomUUID());
93         participantRegisterMessage.setParticipantId(CommonTestData.getParticipantId());
94         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
95         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
96
97         var participantProvider = mock(ParticipantProvider.class);
98         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
99         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
100                 mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
101                 mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
102                 mock(ParticipantSyncPublisher.class), mock(AcRuntimeParameterGroup.class));
103         handler.handleParticipantMessage(participantRegisterMessage);
104
105         verify(participantProvider).saveParticipant(any());
106         verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(),
107                 CommonTestData.getParticipantId(), null);
108     }
109
110     @Test
111     void testHandleParticipantRestart() {
112         var participantRegisterMessage = new ParticipantRegister();
113         participantRegisterMessage.setMessageId(UUID.randomUUID());
114         var participantId = CommonTestData.getParticipantId();
115         participantRegisterMessage.setParticipantId(participantId);
116         participantRegisterMessage.setReplicaId(participantId);
117         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
118         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
119
120         var participant = new Participant();
121         var replica = new ParticipantReplica();
122         replica.setReplicaId(participantId);
123         participant.setParticipantId(participantId);
124         participant.getReplicas().put(replica.getReplicaId(), replica);
125         var participantProvider = mock(ParticipantProvider.class);
126         when(participantProvider.findParticipant(participantId)).thenReturn(Optional.of(participant));
127         when(participantProvider.findParticipantReplica(participantId)).thenReturn(Optional.of(replica));
128         var compositionId = UUID.randomUUID();
129         var composition2Id = UUID.randomUUID();
130         when(participantProvider.getCompositionIds(participantId)).thenReturn(Set.of(compositionId, composition2Id));
131
132         var acDefinitionProvider = mock(AcDefinitionProvider.class);
133         var acDefinition = new AutomationCompositionDefinition();
134         acDefinition.setState(AcTypeState.COMMISSIONED);
135         acDefinition.setCompositionId(composition2Id);
136         when(acDefinitionProvider.getAcDefinition(composition2Id)).thenReturn(acDefinition);
137
138         acDefinition = new AutomationCompositionDefinition();
139         acDefinition.setCompositionId(compositionId);
140         acDefinition.setState(AcTypeState.PRIMED);
141         var nodeTemplateState = new NodeTemplateState();
142         nodeTemplateState.setParticipantId(participantId);
143         acDefinition.setElementStateMap(Map.of("code", nodeTemplateState));
144         when(acDefinitionProvider.getAcDefinition(compositionId)).thenReturn(acDefinition);
145
146         var automationComposition =
147                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
148         automationComposition.getElements().values().iterator().next().setParticipantId(participantId);
149         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
150         when(automationCompositionProvider.getAcInstancesByCompositionId(compositionId))
151                 .thenReturn(List.of(automationComposition));
152
153         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
154         var participantRestartPublisher = mock(ParticipantRestartPublisher.class);
155         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
156                 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider, acDefinitionProvider,
157                 participantRestartPublisher, mock(ParticipantSyncPublisher.class),
158                 CommonTestData.getTestParamaterGroup());
159         handler.handleParticipantMessage(participantRegisterMessage);
160
161         verify(participantRegisterAckPublisher)
162                 .send(participantRegisterMessage.getMessageId(), participantId, participantId);
163         verify(acDefinitionProvider).updateAcDefinition(any(AutomationCompositionDefinition.class),
164                 eq(CommonTestData.TOSCA_COMP_NAME));
165         verify(participantRestartPublisher).send(any(), any(AutomationCompositionDefinition.class), any());
166     }
167
168     @Test
169     void testHandleParticipantSyncRestart() {
170         var participantRegisterMessage = new ParticipantRegister();
171         participantRegisterMessage.setMessageId(UUID.randomUUID());
172         var participantId = CommonTestData.getParticipantId();
173         participantRegisterMessage.setParticipantId(participantId);
174         var replicaId = CommonTestData.getReplicaId();
175         participantRegisterMessage.setReplicaId(replicaId);
176         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
177         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
178
179         var participant = new Participant();
180         var replica = new ParticipantReplica();
181         replica.setReplicaId(replicaId);
182         participant.setParticipantId(participantId);
183         participant.getReplicas().put(replica.getReplicaId(), replica);
184         var participantProvider = mock(ParticipantProvider.class);
185         when(participantProvider.findParticipant(participantId)).thenReturn(Optional.of(participant));
186         when(participantProvider.findParticipantReplica(replicaId)).thenReturn(Optional.of(replica));
187         var compositionId = UUID.randomUUID();
188         var composition2Id = UUID.randomUUID();
189         when(participantProvider.getCompositionIds(participantId)).thenReturn(Set.of(compositionId, composition2Id));
190
191         var acDefinitionProvider = mock(AcDefinitionProvider.class);
192         var acDefinition = new AutomationCompositionDefinition();
193         acDefinition.setState(AcTypeState.COMMISSIONED);
194         acDefinition.setCompositionId(composition2Id);
195         when(acDefinitionProvider.getAcDefinition(composition2Id)).thenReturn(acDefinition);
196
197         acDefinition = new AutomationCompositionDefinition();
198         acDefinition.setCompositionId(compositionId);
199         acDefinition.setState(AcTypeState.PRIMED);
200         var nodeTemplateState = new NodeTemplateState();
201         nodeTemplateState.setParticipantId(participantId);
202         acDefinition.setElementStateMap(Map.of("code", nodeTemplateState));
203         when(acDefinitionProvider.getAcDefinition(compositionId)).thenReturn(acDefinition);
204
205         var automationComposition =
206                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
207         automationComposition.getElements().values().iterator().next().setParticipantId(participantId);
208         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
209         when(automationCompositionProvider.getAcInstancesByCompositionId(compositionId))
210                 .thenReturn(List.of(automationComposition));
211
212         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
213         var participantSyncPublisher = mock(ParticipantSyncPublisher.class);
214         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
215                 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider, acDefinitionProvider,
216                 mock(ParticipantRestartPublisher.class), participantSyncPublisher,
217                 CommonTestData.getTestParamaterGroup());
218         handler.handleParticipantMessage(participantRegisterMessage);
219
220         verify(participantRegisterAckPublisher)
221                 .send(participantRegisterMessage.getMessageId(), participantId, replicaId);
222         verify(acDefinitionProvider, times(0)).updateAcDefinition(any(AutomationCompositionDefinition.class),
223                 eq(CommonTestData.TOSCA_COMP_NAME));
224         verify(participantSyncPublisher)
225                 .sendRestartMsg(any(), any(), any(AutomationCompositionDefinition.class), any());
226     }
227
228     @Test
229     void testHandleParticipantStatus() {
230         var participantStatusMessage = createParticipantStatus();
231         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
232
233         var participantProvider = mock(ParticipantProvider.class);
234         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
235         var handler =
236                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
237                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
238                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
239                         mock(ParticipantSyncPublisher.class), mock(AcRuntimeParameterGroup.class));
240         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
241         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
242                 .thenReturn(Optional.of(participant));
243         handler.handleParticipantMessage(participantStatusMessage);
244
245         verify(automationCompositionProvider).upgradeStates(any());
246     }
247
248     @Test
249     void testAcDefinitionOutProperties() {
250         var participantStatusMessage = createParticipantStatus();
251         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
252         var participantDefinition = new ParticipantDefinition();
253         participantStatusMessage.setParticipantDefinitionUpdates(List.of(participantDefinition));
254         participantDefinition.setParticipantId(participantStatusMessage.getParticipantId());
255         var acElementDefinition = new AutomationCompositionElementDefinition();
256         acElementDefinition.setAcElementDefinitionId(new ToscaConceptIdentifier("code", "1.0.0"));
257         participantDefinition.setAutomationCompositionElementDefinitionList(List.of(acElementDefinition));
258
259         var compositionId = UUID.randomUUID();
260         participantStatusMessage.setCompositionId(compositionId);
261         var acDefinition = new AutomationCompositionDefinition();
262         acDefinition.setState(AcTypeState.COMMISSIONED);
263         acDefinition.setCompositionId(compositionId);
264         var nodeTemplateState = new NodeTemplateState();
265         acDefinition.setElementStateMap(
266                 Map.of(acElementDefinition.getAcElementDefinitionId().getName(), nodeTemplateState));
267         var acDefinitionProvider = mock(AcDefinitionProvider.class);
268         when(acDefinitionProvider.findAcDefinition(compositionId)).thenReturn(Optional.of(acDefinition));
269
270         var participantProvider = mock(ParticipantProvider.class);
271         var handler =
272                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
273                         mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
274                         acDefinitionProvider, mock(ParticipantRestartPublisher.class),
275                         mock(ParticipantSyncPublisher.class), CommonTestData.getTestParamaterGroup());
276         handler.handleParticipantMessage(participantStatusMessage);
277
278         verify(acDefinitionProvider).updateAcDefinition(acDefinition, CommonTestData.TOSCA_COMP_NAME);
279     }
280
281     @Test
282     void testHandleParticipantStatusNotRegisterd() {
283         var participantStatusMessage = createParticipantStatus();
284         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
285
286         var participantProvider = mock(ParticipantProvider.class);
287         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
288         var handler =
289                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
290                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
291                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
292                         mock(ParticipantSyncPublisher.class), mock(AcRuntimeParameterGroup.class));
293         handler.handleParticipantMessage(participantStatusMessage);
294
295         verify(participantProvider).saveParticipant(any());
296         verify(automationCompositionProvider).upgradeStates(any());
297     }
298
299     @Test
300     void testHandleParticipantStatusCheckOnline() {
301         var participantStatusMessage = createParticipantStatus();
302         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
303
304         var participantProvider = mock(ParticipantProvider.class);
305         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
306         var handler =
307                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
308                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
309                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
310                         mock(ParticipantSyncPublisher.class), mock(AcRuntimeParameterGroup.class));
311         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
312         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
313                 .thenReturn(Optional.of(participant));
314         handler.handleParticipantMessage(participantStatusMessage);
315
316         verify(participantProvider).saveParticipant(any());
317         verify(automationCompositionProvider).upgradeStates(any());
318     }
319
320     private ParticipantStatus createParticipantStatus() {
321         var statusMessage = new ParticipantStatus();
322         statusMessage.setParticipantId(CommonTestData.getParticipantId());
323         statusMessage.setState(ParticipantState.ON_LINE);
324         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
325         statusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
326         return statusMessage;
327     }
328 }