2c9e84ba73a73caa320e0b7c81a017ab18c6d5a1
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.supervision;
22
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;
37
38 /**
39  * This class handles supervision of participant status.
40  */
41 @Component
42 @AllArgsConstructor
43 public class SupervisionParticipantHandler {
44     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionParticipantHandler.class);
45
46     private final ParticipantProvider participantProvider;
47     private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
48     private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
49
50     /**
51      * Handle a ParticipantRegister message from a participant.
52      *
53      * @param participantRegisterMsg the ParticipantRegister message received from a participant
54      */
55     @MessageIntercept
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);
60
61         participantRegisterAckPublisher.send(participantRegisterMsg.getMessageId(),
62             participantRegisterMsg.getParticipantId(), participantRegisterMsg.getParticipantType());
63     }
64
65     /**
66      * Handle a ParticipantDeregister message from a participant.
67      *
68      * @param participantDeregisterMsg the ParticipantDeregister message received from a participant
69      */
70     @MessageIntercept
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());
75
76         if (participantOpt.isPresent()) {
77             var participant = participantOpt.get();
78             participant.setParticipantState(ParticipantState.OFF_LINE);
79             participantProvider.updateParticipant(participant);
80         }
81
82         participantDeregisterAckPublisher.send(participantDeregisterMsg.getMessageId());
83     }
84
85     /**
86      * Handle a ParticipantStatus message from a participant.
87      *
88      * @param participantStatusMsg the ParticipantStatus message received from a participant
89      */
90     @MessageIntercept
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);
95     }
96
97     private void saveParticipantStatus(ParticipantMessage participantMessage) {
98         var participantOpt = participantProvider.findParticipant(participantMessage.getParticipantId());
99
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);
107
108             participantProvider.saveParticipant(participant);
109         } else {
110             var participant = participantOpt.get();
111             participant.setParticipantState(ParticipantState.ON_LINE);
112
113             participantProvider.updateParticipant(participant);
114         }
115     }
116 }