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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.runtime.supervision.comm;
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;
43 public class ParticipantSyncPublisher extends AbstractParticipantPublisher<ParticipantSync> {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantSyncPublisher.class);
46 private final AcRuntimeParameterGroup acRuntimeParameterGroup;
49 * Send Restart sync msg to Participant by participantId.
51 * @param participantId the participantId
52 * @param replicaId the replicaId
53 * @param acmDefinition the AutomationComposition Definition
54 * @param automationCompositions the list of automationCompositions
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) {
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());
72 for (var automationComposition : automationCompositions) {
73 var syncAc = AcmUtils.createAcRestart(automationComposition, participantId, toscaServiceTemplateFragment);
74 message.getAutomationcompositionList().add(syncAc);
77 LOGGER.debug("Participant Restarting Sync sent {}", message);
83 * @return true if default
86 public boolean isDefaultTopic() {
91 * Send AutomationCompositionDefinition sync msg to all Participants.
93 * @param acDefinition the AutomationComposition Definition
94 * @param excludeReplicaId the replica to be excluded
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);
103 message.setState(acDefinition.getState());
104 message.setMessageId(UUID.randomUUID());
105 message.setTimestamp(Instant.now());
106 if (AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
107 message.setDelete(true);
109 message.setParticipantDefinitionUpdates(AcmUtils.prepareParticipantRestarting(null, acDefinition,
110 acRuntimeParameterGroup.getAcmParameters().getToscaElementName()));
112 LOGGER.debug("Participant AutomationCompositionDefinition Sync sent {}", message);
117 * Send AutomationComposition sync msg to all Participants.
119 * @param serviceTemplate the ServiceTemplate
120 * @param automationComposition the automationComposition
122 @Timed(value = "publisher.participant_sync_msg", description = "Participant Sync published")
123 public void sendSync(ToscaServiceTemplate serviceTemplate, 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.setDeployState(automationComposition.getDeployState());
133 syncAc.setLockState(automationComposition.getLockState());
134 if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
135 message.setDelete(true);
137 var toscaServiceTemplateFragment = AcmUtils.getToscaServiceTemplateFragment(serviceTemplate);
138 for (var element : automationComposition.getElements().values()) {
139 var acElementSync = AcmUtils.createAcElementRestart(element);
140 acElementSync.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
141 syncAc.getAcElementList().add(acElementSync);
145 message.getAutomationcompositionList().add(syncAc);
147 LOGGER.debug("Participant AutomationComposition Sync sent {}", message.getMessageId());