b5fe222124de8b7a5edab754d2ec1ee9dee07f1c
[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 lombok.AllArgsConstructor;
28 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
29 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
32 import org.onap.policy.clamp.models.acm.concepts.DeployState;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
34 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantSync;
35 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 @AllArgsConstructor
42 public class ParticipantSyncPublisher extends AbstractParticipantPublisher<ParticipantSync> {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantSyncPublisher.class);
45     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
46
47     /**
48      * Send Restart sync msg to Participant by participantId.
49      *
50      * @param participantId the participantId
51      * @param replicaId the replicaId
52      * @param acmDefinition the AutomationComposition Definition
53      * @param automationCompositions the list of automationCompositions
54      */
55     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
56     public void sendRestartMsg(UUID participantId, UUID replicaId, AutomationCompositionDefinition acmDefinition,
57                      List<AutomationComposition> automationCompositions) {
58
59         var message = new ParticipantSync();
60         message.setParticipantId(participantId);
61         message.setReplicaId(replicaId);
62         message.setRestarting(true);
63         message.setCompositionId(acmDefinition.getCompositionId());
64         message.setMessageId(UUID.randomUUID());
65         message.setTimestamp(Instant.now());
66         message.setState(acmDefinition.getState());
67         message.setRevisionIdComposition(acmDefinition.getRevisionId());
68         message.setParticipantDefinitionUpdates(AcmUtils.prepareParticipantRestarting(participantId, acmDefinition,
69                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName()));
70
71         for (var automationComposition : automationCompositions) {
72             var syncAc = AcmUtils.createAcRestart(automationComposition, participantId);
73             message.getAutomationcompositionList().add(syncAc);
74         }
75
76         LOGGER.debug("Participant Restarting Sync sent {}", message);
77         super.send(message);
78     }
79
80     /**
81      * Is default topic.
82      * @return true if default
83      */
84     @Override
85     public boolean isDefaultTopic() {
86         return false;
87     }
88
89     /**
90      * Send AutomationCompositionDefinition sync msg to all Participants.
91      *
92      * @param acDefinition the AutomationComposition Definition
93      * @param excludeReplicaId the replica to be excluded
94      */
95     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
96     public void sendSync(AutomationCompositionDefinition acDefinition, UUID excludeReplicaId) {
97         var message = new ParticipantSync();
98         message.setCompositionId(acDefinition.getCompositionId());
99         if (excludeReplicaId != null) {
100             message.getExcludeReplicas().add(excludeReplicaId);
101         }
102         message.setState(acDefinition.getState());
103         message.setStateChangeResult(acDefinition.getStateChangeResult());
104         message.setMessageId(UUID.randomUUID());
105         message.setTimestamp(Instant.now());
106         message.setRevisionIdComposition(acDefinition.getRevisionId());
107         if (AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
108             message.setDelete(true);
109         } else {
110             message.setParticipantDefinitionUpdates(AcmUtils.prepareParticipantRestarting(null, acDefinition,
111                     acRuntimeParameterGroup.getAcmParameters().getToscaElementName()));
112         }
113         LOGGER.debug("Participant AutomationCompositionDefinition Sync sent {}", message);
114         super.send(message);
115     }
116
117     /**
118      * Send AutomationComposition sync msg to all Participants.
119      *
120      * @param automationComposition the automationComposition
121      */
122     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
123     public void sendSync(AutomationComposition automationComposition) {
124         var message = new ParticipantSync();
125         message.setCompositionId(automationComposition.getCompositionId());
126         message.setAutomationCompositionId(automationComposition.getInstanceId());
127         message.setState(AcTypeState.PRIMED);
128         message.setMessageId(UUID.randomUUID());
129         message.setTimestamp(Instant.now());
130         var syncAc = new ParticipantRestartAc();
131         syncAc.setAutomationCompositionId(automationComposition.getInstanceId());
132         syncAc.setRevisionId(automationComposition.getRevisionId());
133         syncAc.setDeployState(automationComposition.getDeployState());
134         syncAc.setLockState(automationComposition.getLockState());
135         syncAc.setStateChangeResult(automationComposition.getStateChangeResult());
136         if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
137             message.setDelete(true);
138         } else {
139             for (var element : automationComposition.getElements().values()) {
140                 var acElementSync = AcmUtils.createAcElementRestart(element);
141                 syncAc.getAcElementList().add(acElementSync);
142
143             }
144         }
145         message.getAutomationcompositionList().add(syncAc);
146
147         LOGGER.debug("Participant AutomationComposition Sync sent {}", message.getMessageId());
148         super.send(message);
149     }
150 }