2f1eaf77ef68b4988cb68fd64ed4b08f72272d63
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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.comm;
22
23 import io.micrometer.core.annotation.Timed;
24 import java.time.Instant;
25 import java.util.UUID;
26 import java.util.stream.Collectors;
27 import lombok.AllArgsConstructor;
28 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
29 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
30 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
31 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionPrepare;
32 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
33 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 @AllArgsConstructor
40 public class AcPreparePublisher extends AbstractParticipantPublisher<AutomationCompositionPrepare> {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(AcPreparePublisher.class);
43
44     /**
45      * Send AutomationCompositionPrepare Prepare message to Participant.
46      *
47      * @param automationComposition the AutomationComposition
48      * @param stage the stage
49      * @param revisionIdComposition the last Update from Composition
50      */
51     @Timed(value = "publisher.prepare", description = "AC Prepare Pre Deploy published")
52     public void sendPrepare(AutomationComposition automationComposition, int stage, UUID revisionIdComposition) {
53         var acPrepare = createAutomationCompositionPrepare(automationComposition.getCompositionId(),
54             automationComposition.getInstanceId());
55         acPrepare.setStage(stage);
56         var participantUpdatesList = AcmUtils.createParticipantDeployList(automationComposition, DeployOrder.NONE);
57         acPrepare.setParticipantList(participantUpdatesList);
58         acPrepare.setParticipantIdList(participantUpdatesList.stream()
59                 .map(ParticipantDeploy::getParticipantId).collect(Collectors.toSet()));
60         acPrepare.setRevisionIdInstance(automationComposition.getRevisionId());
61         acPrepare.setRevisionIdComposition(revisionIdComposition);
62         LOGGER.debug("AC Prepare sent {}", acPrepare);
63         super.send(acPrepare);
64     }
65
66     /**
67      * Send AutomationCompositionPrepare Review message to Participant.
68      *
69      * @param automationComposition the AutomationComposition
70      * @param revisionIdComposition the last Update from Composition
71      */
72     @Timed(value = "publisher.review", description = "AC Review Post Deploy published")
73     public void sendReview(AutomationComposition automationComposition, UUID revisionIdComposition) {
74         var acPrepare = createAutomationCompositionPrepare(automationComposition.getCompositionId(),
75             automationComposition.getInstanceId());
76         acPrepare.setPreDeploy(false);
77         acPrepare.setParticipantIdList(automationComposition.getElements().values().stream()
78                 .map(AutomationCompositionElement::getParticipantId).collect(Collectors.toSet()));
79         acPrepare.setRevisionIdComposition(revisionIdComposition);
80         acPrepare.setRevisionIdInstance(automationComposition.getRevisionId());
81         LOGGER.debug("AC Review sent {}", acPrepare);
82         super.send(acPrepare);
83     }
84
85     private AutomationCompositionPrepare createAutomationCompositionPrepare(UUID compositionId, UUID instanceId) {
86         var acPrepare = new AutomationCompositionPrepare();
87         acPrepare.setCompositionId(compositionId);
88         acPrepare.setAutomationCompositionId(instanceId);
89         acPrepare.setMessageId(UUID.randomUUID());
90         acPrepare.setTimestamp(Instant.now());
91         return acPrepare;
92     }
93 }