3e42f940c1bb8c89628f3b4b61184c75af1d52c7
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Nordix Foundation.
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.setParticipantDefinitionUpdates(AcmUtils.prepareParticipantRestarting(participantId, acmDefinition,
68                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName()));
69
70         for (var automationComposition : automationCompositions) {
71             var syncAc = AcmUtils.createAcRestart(automationComposition, participantId);
72             message.getAutomationcompositionList().add(syncAc);
73         }
74
75         LOGGER.debug("Participant Restarting Sync sent {}", message);
76         super.send(message);
77     }
78
79     /**
80      * Is default topic.
81      * @return true if default
82      */
83     @Override
84     public boolean isDefaultTopic() {
85         return false;
86     }
87
88     /**
89      * Send AutomationCompositionDefinition sync msg to all Participants.
90      *
91      * @param acDefinition the AutomationComposition Definition
92      * @param excludeReplicaId the replica to be excluded
93      */
94     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
95     public void sendSync(AutomationCompositionDefinition acDefinition, UUID excludeReplicaId) {
96         var message = new ParticipantSync();
97         message.setCompositionId(acDefinition.getCompositionId());
98         if (excludeReplicaId != null) {
99             message.getExcludeReplicas().add(excludeReplicaId);
100         }
101         message.setState(acDefinition.getState());
102         message.setStateChangeResult(acDefinition.getStateChangeResult());
103         message.setMessageId(UUID.randomUUID());
104         message.setTimestamp(Instant.now());
105         if (AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
106             message.setDelete(true);
107         } else {
108             message.setParticipantDefinitionUpdates(AcmUtils.prepareParticipantRestarting(null, acDefinition,
109                     acRuntimeParameterGroup.getAcmParameters().getToscaElementName()));
110         }
111         LOGGER.debug("Participant AutomationCompositionDefinition Sync sent {}", message);
112         super.send(message);
113     }
114
115     /**
116      * Send AutomationComposition sync msg to all Participants.
117      *
118      * @param automationComposition the automationComposition
119      */
120     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
121     public void sendSync(AutomationComposition automationComposition) {
122         var message = new ParticipantSync();
123         message.setCompositionId(automationComposition.getCompositionId());
124         message.setAutomationCompositionId(automationComposition.getInstanceId());
125         message.setState(AcTypeState.PRIMED);
126         message.setMessageId(UUID.randomUUID());
127         message.setTimestamp(Instant.now());
128         var syncAc = new ParticipantRestartAc();
129         syncAc.setAutomationCompositionId(automationComposition.getInstanceId());
130         syncAc.setDeployState(automationComposition.getDeployState());
131         syncAc.setLockState(automationComposition.getLockState());
132         syncAc.setStateChangeResult(automationComposition.getStateChangeResult());
133         if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
134             message.setDelete(true);
135         } else {
136             for (var element : automationComposition.getElements().values()) {
137                 var acElementSync = AcmUtils.createAcElementRestart(element);
138                 syncAc.getAcElementList().add(acElementSync);
139
140             }
141         }
142         message.getAutomationcompositionList().add(syncAc);
143
144         LOGGER.debug("Participant AutomationComposition Sync sent {}", message.getMessageId());
145         super.send(message);
146     }
147 }