2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.acm.runtime.supervision.comm;
25 import io.micrometer.core.annotation.Timed;
26 import java.time.Instant;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
32 import java.util.UUID;
33 import java.util.stream.Collectors;
34 import lombok.AllArgsConstructor;
35 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
36 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
41 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
42 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
43 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
50 * This class is used to send ParticipantPrime messages to participants on Kafka.
54 public class ParticipantPrimePublisher extends AbstractParticipantPublisher<ParticipantPrime> {
56 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantPrimePublisher.class);
58 private final ParticipantProvider participantProvider;
59 private final AcRuntimeParameterGroup acRuntimeParameterGroup;
62 * Send ParticipantPrime to Participant
63 * if participantId is null then message is broadcast.
65 * @param participantDefinitions the list of ParticipantDefinition to send
66 * @param compositionId the compositionId
67 * @param revisionId last update
69 @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
70 public void sendPriming(List<ParticipantDefinition> participantDefinitions, UUID compositionId,
72 var message = new ParticipantPrime();
73 message.setCompositionId(compositionId);
74 message.setParticipantIdList(participantDefinitions.stream()
75 .map(ParticipantDefinition::getParticipantId).collect(Collectors.toSet()));
76 message.setTimestamp(Instant.now());
77 message.setRevisionIdComposition(revisionId);
78 message.setParticipantDefinitionUpdates(participantDefinitions);
79 LOGGER.debug("Participant Update sent {}", message.getMessageId());
84 * Prepare the Priming message creating the list of ParticipantDefinition to send
85 * and fill the ElementState map of the AC Definition.
87 * @param acmDefinition the AutomationComposition Definition
88 * @return list of ParticipantDefinition
90 public List<ParticipantDefinition> prepareParticipantPriming(AutomationCompositionDefinition acmDefinition) {
91 acmDefinition.setStateChangeResult(StateChangeResult.NO_ERROR);
92 acmDefinition.setState(AcTypeState.PRIMING);
93 acmDefinition.setRevisionId(UUID.randomUUID());
94 acmDefinition.setLastMsg(TimestampHelper.now());
95 var acElements = AcmUtils.extractAcElementsFromServiceTemplate(acmDefinition.getServiceTemplate(),
96 acRuntimeParameterGroup.getAcmParameters().getToscaElementName());
97 Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
98 var participantIds = new HashSet<UUID>();
99 if (AcTypeState.PRIMED.equals(acmDefinition.getState())) {
100 // scenario Prime again, participants already assigned
101 for (var elementEntry : acElements) {
102 var elementState = acmDefinition.getElementStateMap().get(elementEntry.getKey());
103 elementState.setState(AcTypeState.PRIMING);
104 participantIds.add(elementState.getParticipantId());
105 supportedElementMap.put(AcmUtils.getType(elementEntry.getValue()), elementState.getParticipantId());
108 // scenario Prime participants not assigned yet
109 supportedElementMap = participantProvider.getSupportedElementMap();
110 for (var elementEntry : acElements) {
111 var elementState = acmDefinition.getElementStateMap().get(elementEntry.getKey());
112 elementState.setState(AcTypeState.PRIMING);
113 var participantId = supportedElementMap.get(AcmUtils.getType(elementEntry.getValue()));
114 if (participantId != null) {
115 elementState.setParticipantId(participantId);
116 participantIds.add(participantId);
120 participantProvider.verifyParticipantState(participantIds);
121 return AcmUtils.prepareParticipantPriming(acElements, supportedElementMap);
125 * Send ParticipantPrime to Participant after that commissioning has been removed.
127 @Timed(value = "publisher.participant_update", description = "PARTICIPANT_UPDATE messages published")
128 public void sendDepriming(UUID compositionId, Set<UUID> participantIds, UUID revisionId) {
129 var message = new ParticipantPrime();
130 message.setCompositionId(compositionId);
131 message.setParticipantIdList(participantIds);
132 message.setTimestamp(Instant.now());
133 message.setRevisionIdComposition(revisionId);
134 // DeCommission the automation composition but deleting participantdefinitions on participants
135 message.setParticipantDefinitionUpdates(null);
137 LOGGER.debug("Participant Update sent {}", message.getMessageId());