cab5adf9c3ee43c6252346a07b00631dc8e890a4
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.runtime.supervision.comm;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
30
31 import java.util.HashMap;
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.parameters.AcRuntimeParameterGroup;
38 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionAcHandler;
39 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
40 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionParticipantHandler;
41 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
42 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
45 import org.onap.policy.clamp.models.acm.concepts.DeployState;
46 import org.onap.policy.clamp.models.acm.concepts.LockState;
47 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeployAck;
48 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
49 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck;
50 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
51 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
52 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
53 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
54 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
55 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
56 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
57 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
58 import org.onap.policy.common.endpoints.event.comm.TopicSink;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
60
61 class SupervisionMessagesTest {
62
63     private static final String AC_INSTANTIATION_UPDATE_JSON =
64             "src/test/resources/rest/acm/AutomationCompositionUpdate.json";
65     private static final String NOT_ACTIVE = "Not Active!";
66     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
67     private static final String TOPIC = "my-topic";
68
69     private static final String TOSCA_ELEMENT_NAME = "org.onap.policy.clamp.acm.AutomationCompositionElement";
70
71     @Test
72     void testSendParticipantRegisterAck() {
73         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
74         var topicSink = mock(TopicSink.class);
75         acRegisterAckPublisher.active(topicSink);
76         acRegisterAckPublisher.send(new ParticipantRegisterAck());
77         verify(topicSink).send(anyString());
78         acRegisterAckPublisher.stop();
79     }
80
81     @Test
82     void testSendParticipantRegisterAckNoActive() {
83         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
84         assertThatThrownBy(() -> acRegisterAckPublisher.send(new ParticipantRegisterAck()))
85                 .hasMessageMatching(NOT_ACTIVE);
86     }
87
88     @Test
89     void testReceiveParticipantDeregister() {
90         final var participantDeregisterMsg = new ParticipantDeregister();
91         var supervisionHandler = mock(SupervisionParticipantHandler.class);
92         var participantDeregisterListener = new ParticipantDeregisterListener(supervisionHandler);
93         participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg);
94         verify(supervisionHandler).handleParticipantMessage(participantDeregisterMsg);
95     }
96
97     @Test
98     void testSendParticipantDeregisterAck() {
99         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
100         var topicSink = mock(TopicSink.class);
101         acDeregisterAckPublisher.active(topicSink);
102         acDeregisterAckPublisher.send(new ParticipantDeregisterAck());
103         verify(topicSink).send(anyString());
104         acDeregisterAckPublisher.stop();
105     }
106
107     @Test
108     void testSendParticipantDeregisterAckNoActive() {
109         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
110         assertThatThrownBy(() -> acDeregisterAckPublisher.send(new ParticipantDeregisterAck()))
111                 .hasMessageMatching(NOT_ACTIVE);
112     }
113
114     @Test
115     void testReceiveParticipantPrimeAckMessage() {
116         final var participantPrimeAckMsg = new ParticipantPrimeAck();
117         var supervisionHandler = mock(SupervisionHandler.class);
118         var participantPrimeAckListener = new ParticipantPrimeAckListener(supervisionHandler);
119         participantPrimeAckListener.onTopicEvent(INFRA, TOPIC, null, participantPrimeAckMsg);
120         verify(supervisionHandler).handleParticipantMessage(participantPrimeAckMsg);
121     }
122
123     @Test
124     void testSendAutomationCompositionStateChangePublisherNotActive() {
125         var publisher = new AutomationCompositionStateChangePublisher();
126         assertThatThrownBy(() -> publisher.send(getAutomationComposition(), 0, true)).hasMessage(NOT_ACTIVE);
127     }
128
129     private AutomationComposition getAutomationComposition() {
130         var automationComposition = new AutomationComposition();
131         automationComposition.setName("NAME");
132         automationComposition.setVersion("0.0.1");
133         automationComposition.setDeployState(DeployState.DEPLOYED);
134         automationComposition.setLockState(LockState.UNLOCKING);
135         return automationComposition;
136     }
137
138     @Test
139     void testSendAutomationCompositionStateChangePublisher() {
140         var publisher = new AutomationCompositionStateChangePublisher();
141         var topicSink = mock(TopicSink.class);
142         publisher.active(topicSink);
143         publisher.send(getAutomationComposition(), 0, true);
144         verify(topicSink).send(anyString());
145         publisher.stop();
146     }
147
148     @Test
149     void testParticipantPrimePublisherDecommissioning() {
150         var publisher = new ParticipantPrimePublisher(mock(ParticipantProvider.class),
151                 mock(AcRuntimeParameterGroup.class));
152         var topicSink = mock(TopicSink.class);
153         publisher.active(topicSink);
154         publisher.sendDepriming(UUID.randomUUID());
155         verify(topicSink).send(anyString());
156     }
157
158     @Test
159     void testParticipantPrimePublisherPriming() {
160         var participantId = UUID.randomUUID();
161         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
162         supportedElementMap.put(
163                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.PolicyAutomationCompositionElement", "1.0.0"),
164                 participantId);
165         supportedElementMap.put(new ToscaConceptIdentifier(
166                 "org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement", "1.0.0"), participantId);
167         supportedElementMap.put(
168                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.HttpAutomationCompositionElement", "1.0.0"),
169                 participantId);
170         var participantProvider = mock(ParticipantProvider.class);
171         when(participantProvider.getSupportedElementMap()).thenReturn(supportedElementMap);
172         var publisher = new ParticipantPrimePublisher(participantProvider, CommonTestData.getTestParamaterGroup());
173         var topicSink = mock(TopicSink.class);
174         publisher.active(topicSink);
175         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
176         serviceTemplate.setName("Name");
177         serviceTemplate.setVersion("1.0.0");
178         var acmDefinition = new AutomationCompositionDefinition();
179         acmDefinition.setCompositionId(UUID.randomUUID());
180         acmDefinition.setServiceTemplate(serviceTemplate);
181         var acElements = AcmUtils
182                 .extractAcElementsFromServiceTemplate(serviceTemplate, TOSCA_ELEMENT_NAME);
183         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
184         var preparation = publisher.prepareParticipantPriming(acmDefinition);
185         publisher.sendPriming(preparation, acmDefinition.getCompositionId(), null);
186         verify(topicSink).send(anyString());
187     }
188
189     @Test
190     void testParticipantStatusReqPublisher() {
191         var publisher = new ParticipantStatusReqPublisher();
192         var topicSink = mock(TopicSink.class);
193         publisher.active(topicSink);
194         publisher.send(CommonTestData.getParticipantId());
195         verify(topicSink).send(anyString());
196     }
197
198     @Test
199     void testParticipantRegisterAckPublisher() {
200         var publisher = new ParticipantRegisterAckPublisher();
201         var topicSink = mock(TopicSink.class);
202         publisher.active(topicSink);
203         publisher.send(UUID.randomUUID(), CommonTestData.getParticipantId());
204         verify(topicSink).send(anyString());
205     }
206
207     @Test
208     void testParticipantDeregisterAckPublisher() {
209         var publisher = new ParticipantDeregisterAckPublisher();
210         var topicSink = mock(TopicSink.class);
211         publisher.active(topicSink);
212         publisher.send(UUID.randomUUID());
213         verify(topicSink).send(anyString());
214     }
215
216     @Test
217     void testAcElementPropertiesPublisher() {
218         var publisher = new AcElementPropertiesPublisher();
219         var topicSink = mock(TopicSink.class);
220         publisher.active(topicSink);
221         var automationComposition =
222                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
223         publisher.send(automationComposition);
224         verify(topicSink).send(anyString());
225     }
226
227     @Test
228     void testAutomationCompositionMigrationPublisher() {
229         var publisher = new AutomationCompositionMigrationPublisher();
230         var topicSink = mock(TopicSink.class);
231         publisher.active(topicSink);
232         var automationComposition =
233                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
234         publisher.send(automationComposition, UUID.randomUUID());
235         verify(topicSink).send(anyString());
236     }
237
238     @Test
239     void testParticipantRestartPublisher() {
240         var publisher = new ParticipantRestartPublisher(CommonTestData.getTestParamaterGroup());
241         var topicSink = mock(TopicSink.class);
242         publisher.active(topicSink);
243
244         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
245         var acmDefinition = new AutomationCompositionDefinition();
246         acmDefinition.setCompositionId(UUID.randomUUID());
247         acmDefinition.setServiceTemplate(serviceTemplate);
248         var acElements = AcmUtils
249                 .extractAcElementsFromServiceTemplate(serviceTemplate, "");
250         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
251
252         var automationComposition =
253                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
254
255         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
256         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
257
258         publisher.send(participantId, acmDefinition, List.of(automationComposition));
259         verify(topicSink).send(anyString());
260     }
261
262     @Test
263     void testParticipantSyncPublisherAutomationComposition() {
264         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
265         var topicSink = mock(TopicSink.class);
266         publisher.active(topicSink);
267
268         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
269         var automationComposition =
270                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
271         publisher.sendSync(serviceTemplate, automationComposition);
272         verify(topicSink).send(anyString());
273     }
274
275     @Test
276     void testParticipantSyncPublisherAcDefinition() {
277         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
278         var topicSink = mock(TopicSink.class);
279         publisher.active(topicSink);
280
281         var acmDefinition = getAcmDefinition();
282         publisher.sendSync(acmDefinition, null);
283         verify(topicSink).send(anyString());
284     }
285
286     @Test
287     void testParticipantSyncPublisherAcDefinitionCommissioned() {
288         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
289         var topicSink = mock(TopicSink.class);
290         publisher.active(topicSink);
291
292         var acmDefinition = getAcmDefinition();
293         acmDefinition.setState(AcTypeState.COMMISSIONED);
294         publisher.sendSync(acmDefinition, UUID.randomUUID());
295         verify(topicSink).send(anyString());
296     }
297
298     @Test
299     void testParticipantSyncPublisherRestart() {
300         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
301         var topicSink = mock(TopicSink.class);
302         publisher.active(topicSink);
303
304         var automationComposition =
305                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
306         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
307         var acmDefinition = getAcmDefinition();
308         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
309         var replicaId = UUID.randomUUID();
310         publisher.sendRestartMsg(participantId, replicaId, acmDefinition, List.of(automationComposition));
311         verify(topicSink).send(anyString());
312     }
313
314     private AutomationCompositionDefinition getAcmDefinition() {
315         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
316         var acmDefinition = new AutomationCompositionDefinition();
317         acmDefinition.setCompositionId(UUID.randomUUID());
318         acmDefinition.setState(AcTypeState.PRIMED);
319         acmDefinition.setServiceTemplate(serviceTemplate);
320         var acElements = AcmUtils
321                 .extractAcElementsFromServiceTemplate(serviceTemplate, TOSCA_ELEMENT_NAME);
322         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
323         acmDefinition.getElementStateMap().values().forEach(element -> element.setParticipantId(UUID.randomUUID()));
324         return acmDefinition;
325     }
326
327     @Test
328     void testParticipantRegisterListener() {
329         final var participantRegister = new ParticipantRegister();
330         var supervisionHandler = mock(SupervisionParticipantHandler.class);
331         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
332         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
333         verify(supervisionHandler).handleParticipantMessage(participantRegister);
334     }
335
336     @Test
337     void testParticipantStatusListener() {
338         final var participantStatus = new ParticipantStatus();
339         var supervisionHandler = mock(SupervisionParticipantHandler.class);
340         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
341         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
342         verify(supervisionHandler).handleParticipantMessage(participantStatus);
343     }
344
345     @Test
346     void testAutomationCompositionUpdateAckListener() {
347         final var automationCompositionAck =
348                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
349         var supervisionHandler = mock(SupervisionAcHandler.class);
350         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
351         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
352         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
353     }
354
355     @Test
356     void testAutomationCompositionStateChangeAckListener() {
357         final var automationCompositionAck =
358                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
359         var supervisionHandler = mock(SupervisionAcHandler.class);
360         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
361         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
362         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);
363     }
364 }