2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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.models.acm.concepts.Participant;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
34 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
35 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
39 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
45 * This class handles supervision of participant status.
49 public class SupervisionParticipantHandler {
50 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionParticipantHandler.class);
52 private final ParticipantProvider participantProvider;
53 private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
54 private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
57 * Handle a ParticipantRegister message from a participant.
59 * @param participantRegisterMsg the ParticipantRegister message received from a participant
62 @Timed(value = "listener.participant_register", description = "PARTICIPANT_REGISTER messages received")
63 public void handleParticipantMessage(ParticipantRegister participantRegisterMsg) {
64 LOGGER.debug("Participant Register received {}", participantRegisterMsg);
65 saveParticipantStatus(participantRegisterMsg,
66 listToMap(participantRegisterMsg.getParticipantSupportedElementType()));
68 participantRegisterAckPublisher.send(participantRegisterMsg.getMessageId(),
69 participantRegisterMsg.getParticipantId());
73 * Handle a ParticipantDeregister message from a participant.
75 * @param participantDeregisterMsg the ParticipantDeregister message received from a participant
78 @Timed(value = "listener.participant_deregister", description = "PARTICIPANT_DEREGISTER messages received")
79 public void handleParticipantMessage(ParticipantDeregister participantDeregisterMsg) {
80 LOGGER.debug("Participant Deregister received {}", participantDeregisterMsg);
81 var participantOpt = participantProvider.findParticipant(participantDeregisterMsg.getParticipantId());
83 if (participantOpt.isPresent()) {
84 var participant = participantOpt.get();
85 participant.setParticipantState(ParticipantState.OFF_LINE);
86 participantProvider.updateParticipant(participant);
87 participantProvider.resetParticipantAcElementState(participant.getParticipantId());
90 participantDeregisterAckPublisher.send(participantDeregisterMsg.getMessageId());
94 * Handle a ParticipantStatus message from a participant.
96 * @param participantStatusMsg the ParticipantStatus message received from a participant
99 @Timed(value = "listener.participant_status", description = "PARTICIPANT_STATUS messages received")
100 public void handleParticipantMessage(ParticipantStatus participantStatusMsg) {
101 LOGGER.debug("Participant Status received {}", participantStatusMsg);
102 saveParticipantStatus(participantStatusMsg,
103 listToMap(participantStatusMsg.getParticipantSupportedElementType()));
106 private void saveParticipantStatus(ParticipantMessage participantMessage,
107 Map<UUID, ParticipantSupportedElementType> participantSupportedElementType) {
108 var participantOpt = participantProvider.findParticipant(participantMessage.getParticipantId());
110 if (participantOpt.isEmpty()) {
111 var participant = new Participant();
112 participant.setParticipantId(participantMessage.getParticipantId());
113 participant.setParticipantSupportedElementTypes(participantSupportedElementType);
114 participant.setParticipantState(ParticipantState.ON_LINE);
116 participantProvider.saveParticipant(participant);
118 var participant = participantOpt.get();
119 participant.setParticipantState(ParticipantState.ON_LINE);
121 participantProvider.updateParticipant(participant);
125 private Map<UUID, ParticipantSupportedElementType> listToMap(List<ParticipantSupportedElementType> elementList) {
126 Map<UUID, ParticipantSupportedElementType> map = new HashMap<>();
127 MapUtils.populateMap(map, elementList, ParticipantSupportedElementType::getId);