d90b6f6673ae5ec47684ff350abcb6e7d44b3813
[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.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 @AllArgsConstructor
43 public class ParticipantSyncPublisher extends AbstractParticipantPublisher<ParticipantSync> {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantSyncPublisher.class);
46     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
47
48     /**
49      * Send Restart sync msg to Participant by participantId.
50      *
51      * @param participantId the participantId
52      * @param replicaId the replicaId
53      * @param acmDefinition the AutomationComposition Definition
54      * @param automationCompositions the list of automationCompositions
55      */
56     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
57     public void sendRestartMsg(UUID participantId, UUID replicaId, AutomationCompositionDefinition acmDefinition,
58                      List<AutomationComposition> automationCompositions) {
59
60         var message = new ParticipantSync();
61         message.setParticipantId(participantId);
62         message.setReplicaId(replicaId);
63         message.setRestarting(true);
64         message.setCompositionId(acmDefinition.getCompositionId());
65         message.setMessageId(UUID.randomUUID());
66         message.setTimestamp(Instant.now());
67         message.setState(acmDefinition.getState());
68         message.setParticipantDefinitionUpdates(AcmUtils.prepareParticipantRestarting(participantId, acmDefinition,
69                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName()));
70         var toscaServiceTemplateFragment = AcmUtils.getToscaServiceTemplateFragment(acmDefinition.getServiceTemplate());
71
72         for (var automationComposition : automationCompositions) {
73             var syncAc = AcmUtils.createAcRestart(automationComposition, participantId, toscaServiceTemplateFragment);
74             message.getAutomationcompositionList().add(syncAc);
75         }
76
77         LOGGER.debug("Participant Restarting Sync sent {}", message);
78         super.send(message);
79     }
80
81     /**
82      * Is default topic.
83      * @return true if default
84      */
85     @Override
86     public boolean isDefaultTopic() {
87         return false;
88     }
89
90     /**
91      * Send AutomationCompositionDefinition sync msg to all Participants.
92      *
93      * @param acDefinition the AutomationComposition Definition
94      * @param excludeReplicaId the replica to be excluded
95      */
96     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
97     public void sendSync(AutomationCompositionDefinition acDefinition, UUID excludeReplicaId) {
98         var message = new ParticipantSync();
99         message.setCompositionId(acDefinition.getCompositionId());
100         if (excludeReplicaId != null) {
101             message.getExcludeReplicas().add(excludeReplicaId);
102         }
103         message.setState(acDefinition.getState());
104         message.setStateChangeResult(acDefinition.getStateChangeResult());
105         message.setMessageId(UUID.randomUUID());
106         message.setTimestamp(Instant.now());
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 serviceTemplate the ServiceTemplate
121      * @param automationComposition the automationComposition
122      */
123     @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
124     public void sendSync(ToscaServiceTemplate serviceTemplate, AutomationComposition automationComposition) {
125         var message = new ParticipantSync();
126         message.setCompositionId(automationComposition.getCompositionId());
127         message.setAutomationCompositionId(automationComposition.getInstanceId());
128         message.setState(AcTypeState.PRIMED);
129         message.setMessageId(UUID.randomUUID());
130         message.setTimestamp(Instant.now());
131         var syncAc = new ParticipantRestartAc();
132         syncAc.setAutomationCompositionId(automationComposition.getInstanceId());
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             var toscaServiceTemplateFragment = AcmUtils.getToscaServiceTemplateFragment(serviceTemplate);
140             for (var element : automationComposition.getElements().values()) {
141                 var acElementSync = AcmUtils.createAcElementRestart(element);
142                 acElementSync.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
143                 syncAc.getAcElementList().add(acElementSync);
144
145             }
146         }
147         message.getAutomationcompositionList().add(syncAc);
148
149         LOGGER.debug("Participant AutomationComposition Sync sent {}", message.getMessageId());
150         super.send(message);
151     }
152 }