d0b7b62b6c8a1bd9eacc8484c33f28758fda9b97
[policy/clamp.git] /
1 /*-
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
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.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;
51
52 /**
53  * This class handles supervision of participant status.
54  */
55 @Component
56 @AllArgsConstructor
57 public class SupervisionParticipantHandler {
58     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionParticipantHandler.class);
59
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;
67
68     /**
69      * Handle a ParticipantRegister message from a participant.
70      *
71      * @param participantRegisterMsg the ParticipantRegister message received from a participant
72      */
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);
78
79         participantRegisterAckPublisher.send(participantRegisterMsg.getMessageId(),
80                 participantRegisterMsg.getParticipantId(), participantRegisterMsg.getReplicaId());
81     }
82
83     /**
84      * Handle a ParticipantDeregister message from a participant.
85      *
86      * @param participantDeregisterMsg the ParticipantDeregister message received from a participant
87      */
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);
95         }
96
97         participantDeregisterAckPublisher.send(participantDeregisterMsg.getMessageId());
98     }
99
100     /**
101      * Handle a ParticipantStatus message from a participant.
102      *
103      * @param participantStatusMsg the ParticipantStatus message received from a participant
104      */
105     @MessageIntercept
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);
110
111         if (!participantStatusMsg.getAutomationCompositionInfoList().isEmpty()) {
112             messageProvider.save(participantStatusMsg);
113         }
114         if (!participantStatusMsg.getParticipantDefinitionUpdates().isEmpty()
115                 && participantStatusMsg.getCompositionId() != null) {
116             messageProvider.save(participantStatusMsg);
117         }
118     }
119
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);
127         } else {
128             var participant = getParticipant(participantId, listToMap(participantSupportedElementType));
129             participant.getReplicas().put(replicaId, createReplica(replicaId));
130             participantProvider.saveParticipant(participant);
131         }
132         if (registration) {
133             handleRestart(participantId, replicaId);
134         }
135     }
136
137     private Participant getParticipant(UUID participantId,
138             Map<UUID, ParticipantSupportedElementType> participantSupportedElementType) {
139         var participantOpt = participantProvider.findParticipant(participantId);
140         return participantOpt.orElseGet(() -> createParticipant(participantId, participantSupportedElementType));
141     }
142
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());
148         return replica;
149
150     }
151
152     private void checkOnline(ParticipantReplica replica) {
153         if (ParticipantState.OFF_LINE.equals(replica.getParticipantState())) {
154             replica.setParticipantState(ParticipantState.ON_LINE);
155         }
156         replica.setLastMsg(TimestampHelper.now());
157         participantProvider.saveParticipantReplica(replica);
158     }
159
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);
166         }
167     }
168
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());
173             return;
174         }
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);
181     }
182
183     private boolean isAcToBeSyncRestarted(UUID participantId, AutomationComposition automationComposition) {
184         for (var element : automationComposition.getElements().values()) {
185             if (participantId.equals(element.getParticipantId())) {
186                 return true;
187             }
188         }
189         return false;
190     }
191
192     private Participant createParticipant(UUID participantId,
193             Map<UUID, ParticipantSupportedElementType> participantSupportedElementType) {
194         var participant = new Participant();
195         participant.setParticipantId(participantId);
196         participant.setParticipantSupportedElementTypes(participantSupportedElementType);
197         return participant;
198     }
199
200     private Map<UUID, ParticipantSupportedElementType> listToMap(List<ParticipantSupportedElementType> elementList) {
201         Map<UUID, ParticipantSupportedElementType> map = new HashMap<>();
202         MapUtils.populateMap(map, elementList, ParticipantSupportedElementType::getId);
203         return map;
204     }
205 }