a1ef9b4e293b838ed687cdd529d231320c4d68fa
[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, 0);
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 testParticipantSyncPublisherAutomationComposition() {
262         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
263         var topicSink = mock(TopicSink.class);
264         publisher.active(topicSink);
265
266         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
267         var automationComposition =
268                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
269         publisher.sendSync(serviceTemplate, automationComposition);
270         verify(topicSink).send(anyString());
271     }
272
273     @Test
274     void testParticipantSyncPublisherAcDefinition() {
275         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
276         var topicSink = mock(TopicSink.class);
277         publisher.active(topicSink);
278
279         var acmDefinition = getAcmDefinition();
280         publisher.sendSync(acmDefinition, null);
281         verify(topicSink).send(anyString());
282     }
283
284     @Test
285     void testParticipantSyncPublisherAcDefinitionCommissioned() {
286         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
287         var topicSink = mock(TopicSink.class);
288         publisher.active(topicSink);
289
290         var acmDefinition = getAcmDefinition();
291         acmDefinition.setState(AcTypeState.COMMISSIONED);
292         publisher.sendSync(acmDefinition, UUID.randomUUID());
293         verify(topicSink).send(anyString());
294     }
295
296     @Test
297     void testParticipantSyncPublisherRestart() {
298         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
299         var topicSink = mock(TopicSink.class);
300         publisher.active(topicSink);
301
302         var automationComposition =
303                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
304         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
305         var acmDefinition = getAcmDefinition();
306         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
307         var replicaId = UUID.randomUUID();
308         publisher.sendRestartMsg(participantId, replicaId, acmDefinition, List.of(automationComposition));
309         verify(topicSink).send(anyString());
310     }
311
312     private AutomationCompositionDefinition getAcmDefinition() {
313         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
314         var acmDefinition = new AutomationCompositionDefinition();
315         acmDefinition.setCompositionId(UUID.randomUUID());
316         acmDefinition.setState(AcTypeState.PRIMED);
317         acmDefinition.setServiceTemplate(serviceTemplate);
318         var acElements = AcmUtils
319                 .extractAcElementsFromServiceTemplate(serviceTemplate, TOSCA_ELEMENT_NAME);
320         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
321         acmDefinition.getElementStateMap().values().forEach(element -> element.setParticipantId(UUID.randomUUID()));
322         return acmDefinition;
323     }
324
325     @Test
326     void testParticipantRegisterListener() {
327         final var participantRegister = new ParticipantRegister();
328         var supervisionHandler = mock(SupervisionParticipantHandler.class);
329         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
330         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
331         verify(supervisionHandler).handleParticipantMessage(participantRegister);
332     }
333
334     @Test
335     void testParticipantStatusListener() {
336         final var participantStatus = new ParticipantStatus();
337         var supervisionHandler = mock(SupervisionParticipantHandler.class);
338         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
339         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
340         verify(supervisionHandler).handleParticipantMessage(participantStatus);
341     }
342
343     @Test
344     void testAutomationCompositionUpdateAckListener() {
345         final var automationCompositionAck =
346                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
347         var supervisionHandler = mock(SupervisionAcHandler.class);
348         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
349         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
350         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
351     }
352
353     @Test
354     void testAutomationCompositionStateChangeAckListener() {
355         final var automationCompositionAck =
356                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
357         var supervisionHandler = mock(SupervisionAcHandler.class);
358         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
359         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
360         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);
361     }
362 }