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 lombok.AllArgsConstructor;
25 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
26 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
27 import org.onap.policy.clamp.models.acm.concepts.Participant;
28 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
29 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
30 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
31 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
32 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
33 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Component;
39 * This class handles supervision of participant status.
43 public class SupervisionParticipantHandler {
44 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionParticipantHandler.class);
46 private final ParticipantProvider participantProvider;
47 private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
48 private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
51 * Handle a ParticipantRegister message from a participant.
53 * @param participantRegisterMsg the ParticipantRegister message received from a participant
56 @Timed(value = "listener.participant_register", description = "PARTICIPANT_REGISTER messages received")
57 public void handleParticipantMessage(ParticipantRegister participantRegisterMsg) {
58 LOGGER.debug("Participant Register received {}", participantRegisterMsg);
59 saveParticipantStatus(participantRegisterMsg);
61 participantRegisterAckPublisher.send(participantRegisterMsg.getMessageId(),
62 participantRegisterMsg.getParticipantId(), participantRegisterMsg.getParticipantType());
66 * Handle a ParticipantDeregister message from a participant.
68 * @param participantDeregisterMsg the ParticipantDeregister message received from a participant
71 @Timed(value = "listener.participant_deregister", description = "PARTICIPANT_DEREGISTER messages received")
72 public void handleParticipantMessage(ParticipantDeregister participantDeregisterMsg) {
73 LOGGER.debug("Participant Deregister received {}", participantDeregisterMsg);
74 var participantOpt = participantProvider.findParticipant(participantDeregisterMsg.getParticipantId());
76 if (participantOpt.isPresent()) {
77 var participant = participantOpt.get();
78 participant.setParticipantState(ParticipantState.OFF_LINE);
79 participantProvider.saveParticipant(participant);
82 participantDeregisterAckPublisher.send(participantDeregisterMsg.getMessageId());
86 * Handle a ParticipantStatus message from a participant.
88 * @param participantStatusMsg the ParticipantStatus message received from a participant
91 @Timed(value = "listener.participant_status", description = "PARTICIPANT_STATUS messages received")
92 public void handleParticipantMessage(ParticipantStatus participantStatusMsg) {
93 LOGGER.debug("Participant Status received {}", participantStatusMsg);
94 saveParticipantStatus(participantStatusMsg);
97 private void saveParticipantStatus(ParticipantMessage participantMessage) {
98 var participantOpt = participantProvider.findParticipant(participantMessage.getParticipantId());
100 if (participantOpt.isEmpty()) {
101 var participant = new Participant();
102 participant.setName(participantMessage.getParticipantId().getName());
103 participant.setVersion(participantMessage.getParticipantId().getVersion());
104 participant.setDefinition(participantMessage.getParticipantId());
105 participant.setParticipantType(participantMessage.getParticipantType());
106 participant.setParticipantState(ParticipantState.ON_LINE);
108 participantProvider.saveParticipant(participant);
110 var participant = participantOpt.get();
111 participant.setParticipantState(ParticipantState.ON_LINE);
113 participantProvider.saveParticipant(participant);