2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2025 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;
23 import io.micrometer.core.annotation.Timed;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.UUID;
28 import lombok.AllArgsConstructor;
29 import org.apache.commons.collections4.MapUtils;
30 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
31 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
32 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
33 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
36 import org.onap.policy.clamp.models.acm.concepts.Participant;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantReplica;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
43 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
44 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
45 import org.onap.policy.clamp.models.acm.persistence.provider.MessageProvider;
46 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
47 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.stereotype.Component;
53 * This class handles supervision of participant status.
57 public class SupervisionParticipantHandler {
58 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionParticipantHandler.class);
60 private final ParticipantProvider participantProvider;
61 private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
62 private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
63 private final AutomationCompositionProvider automationCompositionProvider;
64 private final AcDefinitionProvider acDefinitionProvider;
65 private final ParticipantSyncPublisher participantSyncPublisher;
66 private final MessageProvider messageProvider;
69 * Handle a ParticipantRegister message from a participant.
71 * @param participantRegisterMsg the ParticipantRegister message received from a participant
73 @Timed(value = "listener.participant_register", description = "PARTICIPANT_REGISTER messages received")
74 public void handleParticipantMessage(ParticipantRegister participantRegisterMsg) {
75 saveIfNotPresent(participantRegisterMsg.getReplicaId(),
76 participantRegisterMsg.getParticipantId(),
77 participantRegisterMsg.getParticipantSupportedElementType(), true);
79 participantRegisterAckPublisher.send(participantRegisterMsg.getMessageId(),
80 participantRegisterMsg.getParticipantId(), participantRegisterMsg.getReplicaId());
84 * Handle a ParticipantDeregister message from a participant.
86 * @param participantDeregisterMsg the ParticipantDeregister message received from a participant
88 @Timed(value = "listener.participant_deregister", description = "PARTICIPANT_DEREGISTER messages received")
89 public void handleParticipantMessage(ParticipantDeregister participantDeregisterMsg) {
90 var replicaId = participantDeregisterMsg.getReplicaId() != null
91 ? participantDeregisterMsg.getReplicaId() : participantDeregisterMsg.getParticipantId();
92 var replicaOpt = participantProvider.findParticipantReplica(replicaId);
93 if (replicaOpt.isPresent()) {
94 participantProvider.deleteParticipantReplica(replicaId);
97 participantDeregisterAckPublisher.send(participantDeregisterMsg.getMessageId());
101 * Handle a ParticipantStatus message from a participant.
103 * @param participantStatusMsg the ParticipantStatus message received from a participant
106 @Timed(value = "listener.participant_status", description = "PARTICIPANT_STATUS messages received")
107 public void handleParticipantMessage(ParticipantStatus participantStatusMsg) {
108 saveIfNotPresent(participantStatusMsg.getReplicaId(), participantStatusMsg.getParticipantId(),
109 participantStatusMsg.getParticipantSupportedElementType(), false);
111 if (!participantStatusMsg.getAutomationCompositionInfoList().isEmpty()) {
112 messageProvider.save(participantStatusMsg);
114 if (!participantStatusMsg.getParticipantDefinitionUpdates().isEmpty()
115 && participantStatusMsg.getCompositionId() != null) {
116 messageProvider.save(participantStatusMsg);
120 private void saveIfNotPresent(UUID msgReplicaId, UUID participantId,
121 List<ParticipantSupportedElementType> participantSupportedElementType, boolean registration) {
122 var replicaId = msgReplicaId != null ? msgReplicaId : participantId;
123 var replicaOpt = participantProvider.findParticipantReplica(replicaId);
124 if (replicaOpt.isPresent()) {
125 var replica = replicaOpt.get();
126 checkOnline(replica);
128 var participant = getParticipant(participantId, listToMap(participantSupportedElementType));
129 participant.getReplicas().put(replicaId, createReplica(replicaId));
130 participantProvider.saveParticipant(participant);
133 handleRestart(participantId, replicaId);
137 private Participant getParticipant(UUID participantId,
138 Map<UUID, ParticipantSupportedElementType> participantSupportedElementType) {
139 var participantOpt = participantProvider.findParticipant(participantId);
140 return participantOpt.orElseGet(() -> createParticipant(participantId, participantSupportedElementType));
143 private ParticipantReplica createReplica(UUID replicaId) {
144 var replica = new ParticipantReplica();
145 replica.setReplicaId(replicaId);
146 replica.setParticipantState(ParticipantState.ON_LINE);
147 replica.setLastMsg(TimestampHelper.now());
152 private void checkOnline(ParticipantReplica replica) {
153 if (ParticipantState.OFF_LINE.equals(replica.getParticipantState())) {
154 replica.setParticipantState(ParticipantState.ON_LINE);
156 replica.setLastMsg(TimestampHelper.now());
157 participantProvider.saveParticipantReplica(replica);
160 private void handleRestart(UUID participantId, UUID replicaId) {
161 var compositionIds = participantProvider.getCompositionIds(participantId);
162 for (var compositionId : compositionIds) {
163 var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
164 LOGGER.debug("Scan Composition {} for restart", acDefinition.getCompositionId());
165 handleSyncRestart(participantId, replicaId, acDefinition);
169 private void handleSyncRestart(final UUID participantId, UUID replicaId,
170 AutomationCompositionDefinition acDefinition) {
171 if (AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
172 LOGGER.debug("Composition {} COMMISSIONED", acDefinition.getCompositionId());
175 LOGGER.debug("Composition to be send in Restart message {}", acDefinition.getCompositionId());
176 var automationCompositionList =
177 automationCompositionProvider.getAcInstancesByCompositionId(acDefinition.getCompositionId());
178 var automationCompositions = automationCompositionList.stream()
179 .filter(ac -> isAcToBeSyncRestarted(participantId, ac)).toList();
180 participantSyncPublisher.sendRestartMsg(participantId, replicaId, acDefinition, automationCompositions);
183 private boolean isAcToBeSyncRestarted(UUID participantId, AutomationComposition automationComposition) {
184 for (var element : automationComposition.getElements().values()) {
185 if (participantId.equals(element.getParticipantId())) {
192 private Participant createParticipant(UUID participantId,
193 Map<UUID, ParticipantSupportedElementType> participantSupportedElementType) {
194 var participant = new Participant();
195 participant.setParticipantId(participantId);
196 participant.setParticipantSupportedElementTypes(participantSupportedElementType);
200 private Map<UUID, ParticipantSupportedElementType> listToMap(List<ParticipantSupportedElementType> elementList) {
201 Map<UUID, ParticipantSupportedElementType> map = new HashMap<>();
202 MapUtils.populateMap(map, elementList, ParticipantSupportedElementType::getId);