77b47f4e374a054382ad15d4eff416121bd5b747
[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(), CommonTestData.getReplicaId());
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);
235         verify(topicSink).send(anyString());
236     }
237
238     @Test
239     void testAcPreparePublisher() {
240         var publisher = new AcPreparePublisher();
241         var topicSink = mock(TopicSink.class);
242         publisher.active(topicSink);
243         var automationComposition =
244                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
245         publisher.sendPrepare(automationComposition);
246         verify(topicSink).send(anyString());
247     }
248
249     @Test
250     void testAcReviewPublisher() {
251         var publisher = new AcPreparePublisher();
252         var topicSink = mock(TopicSink.class);
253         publisher.active(topicSink);
254         var automationComposition =
255                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
256         publisher.sendRevew(automationComposition);
257         verify(topicSink).send(anyString());
258     }
259
260     @Test
261     void testParticipantRestartPublisher() {
262         var publisher = new ParticipantRestartPublisher(CommonTestData.getTestParamaterGroup());
263         var topicSink = mock(TopicSink.class);
264         publisher.active(topicSink);
265
266         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
267         var acmDefinition = new AutomationCompositionDefinition();
268         acmDefinition.setCompositionId(UUID.randomUUID());
269         acmDefinition.setServiceTemplate(serviceTemplate);
270         var acElements = AcmUtils
271                 .extractAcElementsFromServiceTemplate(serviceTemplate, "");
272         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
273
274         var automationComposition =
275                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
276
277         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
278         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
279
280         publisher.send(participantId, acmDefinition, List.of(automationComposition));
281         verify(topicSink).send(anyString());
282     }
283
284     @Test
285     void testParticipantSyncPublisherAutomationComposition() {
286         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
287         var topicSink = mock(TopicSink.class);
288         publisher.active(topicSink);
289
290         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
291         var automationComposition =
292                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
293         publisher.sendSync(serviceTemplate, automationComposition);
294         verify(topicSink).send(anyString());
295     }
296
297     @Test
298     void testParticipantSyncPublisherAcDefinition() {
299         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
300         var topicSink = mock(TopicSink.class);
301         publisher.active(topicSink);
302
303         var acmDefinition = getAcmDefinition();
304         publisher.sendSync(acmDefinition, null);
305         verify(topicSink).send(anyString());
306     }
307
308     @Test
309     void testParticipantSyncPublisherAcDefinitionCommissioned() {
310         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
311         var topicSink = mock(TopicSink.class);
312         publisher.active(topicSink);
313
314         var acmDefinition = getAcmDefinition();
315         acmDefinition.setState(AcTypeState.COMMISSIONED);
316         publisher.sendSync(acmDefinition, UUID.randomUUID());
317         verify(topicSink).send(anyString());
318     }
319
320     @Test
321     void testParticipantSyncPublisherRestart() {
322         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
323         var topicSink = mock(TopicSink.class);
324         publisher.active(topicSink);
325
326         var automationComposition =
327                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
328         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
329         var acmDefinition = getAcmDefinition();
330         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
331         var replicaId = UUID.randomUUID();
332         publisher.sendRestartMsg(participantId, replicaId, acmDefinition, List.of(automationComposition));
333         verify(topicSink).send(anyString());
334     }
335
336     private AutomationCompositionDefinition getAcmDefinition() {
337         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
338         var acmDefinition = new AutomationCompositionDefinition();
339         acmDefinition.setCompositionId(UUID.randomUUID());
340         acmDefinition.setState(AcTypeState.PRIMED);
341         acmDefinition.setServiceTemplate(serviceTemplate);
342         var acElements = AcmUtils
343                 .extractAcElementsFromServiceTemplate(serviceTemplate, TOSCA_ELEMENT_NAME);
344         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
345         acmDefinition.getElementStateMap().values().forEach(element -> element.setParticipantId(UUID.randomUUID()));
346         return acmDefinition;
347     }
348
349     @Test
350     void testParticipantRegisterListener() {
351         final var participantRegister = new ParticipantRegister();
352         var supervisionHandler = mock(SupervisionParticipantHandler.class);
353         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
354         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
355         verify(supervisionHandler).handleParticipantMessage(participantRegister);
356     }
357
358     @Test
359     void testParticipantStatusListener() {
360         final var participantStatus = new ParticipantStatus();
361         var supervisionHandler = mock(SupervisionParticipantHandler.class);
362         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
363         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
364         verify(supervisionHandler).handleParticipantMessage(participantStatus);
365     }
366
367     @Test
368     void testAutomationCompositionUpdateAckListener() {
369         final var automationCompositionAck =
370                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
371         var supervisionHandler = mock(SupervisionAcHandler.class);
372         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
373         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
374         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
375     }
376
377     @Test
378     void testAutomationCompositionStateChangeAckListener() {
379         final var automationCompositionAck =
380                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
381         var supervisionHandler = mock(SupervisionAcHandler.class);
382         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
383         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
384         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);
385     }
386 }