bebaa3319a73731f73f4fbc92424a34f07404b4f
[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());
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).send(participantRegisterMessage.getMessageId(), participantId);
162         verify(acDefinitionProvider).updateAcDefinition(any(AutomationCompositionDefinition.class),
163                 eq(CommonTestData.TOSCA_COMP_NAME));
164         verify(participantRestartPublisher).send(any(), any(AutomationCompositionDefinition.class), any());
165     }
166
167     @Test
168     void testHandleParticipantSyncRestart() {
169         var participantRegisterMessage = new ParticipantRegister();
170         participantRegisterMessage.setMessageId(UUID.randomUUID());
171         var participantId = CommonTestData.getParticipantId();
172         participantRegisterMessage.setParticipantId(participantId);
173         var replicaId = CommonTestData.getReplicaId();
174         participantRegisterMessage.setReplicaId(replicaId);
175         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
176         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
177
178         var participant = new Participant();
179         var replica = new ParticipantReplica();
180         replica.setReplicaId(replicaId);
181         participant.setParticipantId(participantId);
182         participant.getReplicas().put(replica.getReplicaId(), replica);
183         var participantProvider = mock(ParticipantProvider.class);
184         when(participantProvider.findParticipant(participantId)).thenReturn(Optional.of(participant));
185         when(participantProvider.findParticipantReplica(replicaId)).thenReturn(Optional.of(replica));
186         var compositionId = UUID.randomUUID();
187         var composition2Id = UUID.randomUUID();
188         when(participantProvider.getCompositionIds(participantId)).thenReturn(Set.of(compositionId, composition2Id));
189
190         var acDefinitionProvider = mock(AcDefinitionProvider.class);
191         var acDefinition = new AutomationCompositionDefinition();
192         acDefinition.setState(AcTypeState.COMMISSIONED);
193         acDefinition.setCompositionId(composition2Id);
194         when(acDefinitionProvider.getAcDefinition(composition2Id)).thenReturn(acDefinition);
195
196         acDefinition = new AutomationCompositionDefinition();
197         acDefinition.setCompositionId(compositionId);
198         acDefinition.setState(AcTypeState.PRIMED);
199         var nodeTemplateState = new NodeTemplateState();
200         nodeTemplateState.setParticipantId(participantId);
201         acDefinition.setElementStateMap(Map.of("code", nodeTemplateState));
202         when(acDefinitionProvider.getAcDefinition(compositionId)).thenReturn(acDefinition);
203
204         var automationComposition =
205                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
206         automationComposition.getElements().values().iterator().next().setParticipantId(participantId);
207         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
208         when(automationCompositionProvider.getAcInstancesByCompositionId(compositionId))
209                 .thenReturn(List.of(automationComposition));
210
211         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
212         var participantSyncPublisher = mock(ParticipantSyncPublisher.class);
213         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
214                 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider, acDefinitionProvider,
215                 mock(ParticipantRestartPublisher.class), participantSyncPublisher,
216                 CommonTestData.getTestParamaterGroup());
217         handler.handleParticipantMessage(participantRegisterMessage);
218
219         verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(), participantId);
220         verify(acDefinitionProvider, times(0)).updateAcDefinition(any(AutomationCompositionDefinition.class),
221                 eq(CommonTestData.TOSCA_COMP_NAME));
222         verify(participantSyncPublisher)
223                 .sendRestartMsg(any(), any(), any(AutomationCompositionDefinition.class), any());
224     }
225
226     @Test
227     void testHandleParticipantStatus() {
228         var participantStatusMessage = createParticipantStatus();
229         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
230
231         var participantProvider = mock(ParticipantProvider.class);
232         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
233         var handler =
234                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
235                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
236                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
237                         mock(ParticipantSyncPublisher.class), mock(AcRuntimeParameterGroup.class));
238         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
239         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
240                 .thenReturn(Optional.of(participant));
241         handler.handleParticipantMessage(participantStatusMessage);
242
243         verify(automationCompositionProvider).upgradeStates(any());
244     }
245
246     @Test
247     void testAcDefinitionOutProperties() {
248         var participantStatusMessage = createParticipantStatus();
249         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
250         var participantDefinition = new ParticipantDefinition();
251         participantStatusMessage.setParticipantDefinitionUpdates(List.of(participantDefinition));
252         participantDefinition.setParticipantId(participantStatusMessage.getParticipantId());
253         var acElementDefinition = new AutomationCompositionElementDefinition();
254         acElementDefinition.setAcElementDefinitionId(new ToscaConceptIdentifier("code", "1.0.0"));
255         participantDefinition.setAutomationCompositionElementDefinitionList(List.of(acElementDefinition));
256
257         var compositionId = UUID.randomUUID();
258         participantStatusMessage.setCompositionId(compositionId);
259         var acDefinition = new AutomationCompositionDefinition();
260         acDefinition.setState(AcTypeState.COMMISSIONED);
261         acDefinition.setCompositionId(compositionId);
262         var nodeTemplateState = new NodeTemplateState();
263         acDefinition.setElementStateMap(
264                 Map.of(acElementDefinition.getAcElementDefinitionId().getName(), nodeTemplateState));
265         var acDefinitionProvider = mock(AcDefinitionProvider.class);
266         when(acDefinitionProvider.findAcDefinition(compositionId)).thenReturn(Optional.of(acDefinition));
267
268         var participantProvider = mock(ParticipantProvider.class);
269         var handler =
270                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
271                         mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
272                         acDefinitionProvider, mock(ParticipantRestartPublisher.class),
273                         mock(ParticipantSyncPublisher.class), CommonTestData.getTestParamaterGroup());
274         handler.handleParticipantMessage(participantStatusMessage);
275
276         verify(acDefinitionProvider).updateAcDefinition(acDefinition, CommonTestData.TOSCA_COMP_NAME);
277     }
278
279     @Test
280     void testHandleParticipantStatusNotRegisterd() {
281         var participantStatusMessage = createParticipantStatus();
282         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
283
284         var participantProvider = mock(ParticipantProvider.class);
285         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
286         var handler =
287                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
288                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
289                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
290                         mock(ParticipantSyncPublisher.class), mock(AcRuntimeParameterGroup.class));
291         handler.handleParticipantMessage(participantStatusMessage);
292
293         verify(participantProvider).saveParticipant(any());
294         verify(automationCompositionProvider).upgradeStates(any());
295     }
296
297     @Test
298     void testHandleParticipantStatusCheckOnline() {
299         var participantStatusMessage = createParticipantStatus();
300         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
301
302         var participantProvider = mock(ParticipantProvider.class);
303         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
304         var handler =
305                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
306                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
307                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
308                         mock(ParticipantSyncPublisher.class), mock(AcRuntimeParameterGroup.class));
309         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
310         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
311                 .thenReturn(Optional.of(participant));
312         handler.handleParticipantMessage(participantStatusMessage);
313
314         verify(participantProvider).saveParticipant(any());
315         verify(automationCompositionProvider).upgradeStates(any());
316     }
317
318     private ParticipantStatus createParticipantStatus() {
319         var statusMessage = new ParticipantStatus();
320         statusMessage.setParticipantId(CommonTestData.getParticipantId());
321         statusMessage.setState(ParticipantState.ON_LINE);
322         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
323         statusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
324         return statusMessage;
325     }
326 }