564fa8d0e395323f33a91c9083cfebb7cb3f3bb5
[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 java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
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.AutomationCompositionProvider;
40 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Component;
44
45 /**
46  * This class handles supervision of participant status.
47  */
48 @Component
49 @AllArgsConstructor
50 public class SupervisionParticipantHandler {
51     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionParticipantHandler.class);
52
53     private final ParticipantProvider participantProvider;
54     private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
55     private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
56     private final AutomationCompositionProvider automationCompositionProvider;
57
58     /**
59      * Handle a ParticipantRegister message from a participant.
60      *
61      * @param participantRegisterMsg the ParticipantRegister message received from a participant
62      */
63     @MessageIntercept
64     @Timed(value = "listener.participant_register", description = "PARTICIPANT_REGISTER messages received")
65     public void handleParticipantMessage(ParticipantRegister participantRegisterMsg) {
66         LOGGER.debug("Participant Register received {}", participantRegisterMsg);
67         saveParticipantStatus(participantRegisterMsg,
68             listToMap(participantRegisterMsg.getParticipantSupportedElementType()));
69
70         participantRegisterAckPublisher.send(participantRegisterMsg.getMessageId(),
71             participantRegisterMsg.getParticipantId());
72     }
73
74     /**
75      * Handle a ParticipantDeregister message from a participant.
76      *
77      * @param participantDeregisterMsg the ParticipantDeregister message received from a participant
78      */
79     @MessageIntercept
80     @Timed(value = "listener.participant_deregister", description = "PARTICIPANT_DEREGISTER messages received")
81     public void handleParticipantMessage(ParticipantDeregister participantDeregisterMsg) {
82         LOGGER.debug("Participant Deregister received {}", participantDeregisterMsg);
83         var participantOpt = participantProvider.findParticipant(participantDeregisterMsg.getParticipantId());
84
85         if (participantOpt.isPresent()) {
86             var participant = participantOpt.get();
87             participant.setParticipantState(ParticipantState.OFF_LINE);
88             participantProvider.updateParticipant(participant);
89             participantProvider.resetParticipantAcElementState(participant.getParticipantId());
90         }
91
92         participantDeregisterAckPublisher.send(participantDeregisterMsg.getMessageId());
93     }
94
95     /**
96      * Handle a ParticipantStatus message from a participant.
97      *
98      * @param participantStatusMsg the ParticipantStatus message received from a participant
99      */
100     @MessageIntercept
101     @Timed(value = "listener.participant_status", description = "PARTICIPANT_STATUS messages received")
102     public void handleParticipantMessage(ParticipantStatus participantStatusMsg) {
103         LOGGER.debug("Participant Status received {}", participantStatusMsg);
104         saveParticipantStatus(participantStatusMsg,
105             listToMap(participantStatusMsg.getParticipantSupportedElementType()));
106         if (!participantStatusMsg.getAutomationCompositionInfoList().isEmpty()) {
107             automationCompositionProvider.upgradeStates(participantStatusMsg.getAutomationCompositionInfoList());
108         }
109     }
110
111     private void saveParticipantStatus(ParticipantMessage participantMessage,
112             Map<UUID, ParticipantSupportedElementType> participantSupportedElementType) {
113         var participantOpt = participantProvider.findParticipant(participantMessage.getParticipantId());
114
115         if (participantOpt.isEmpty()) {
116             var participant = new Participant();
117             participant.setParticipantId(participantMessage.getParticipantId());
118             participant.setParticipantSupportedElementTypes(participantSupportedElementType);
119             participant.setParticipantState(ParticipantState.ON_LINE);
120
121             participantProvider.saveParticipant(participant);
122         } else {
123             var participant = participantOpt.get();
124             participant.setParticipantState(ParticipantState.ON_LINE);
125
126             participantProvider.updateParticipant(participant);
127         }
128     }
129
130     private Map<UUID, ParticipantSupportedElementType> listToMap(List<ParticipantSupportedElementType> elementList) {
131         Map<UUID, ParticipantSupportedElementType> map = new HashMap<>();
132         MapUtils.populateMap(map, elementList, ParticipantSupportedElementType::getId);
133         return map;
134     }
135 }