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