ea3ef0ff7a8d60dc202f789a046dfe2bd1bd9964
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 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.main.parameters.AcRuntimeParameterGroup;
31 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
32 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
33 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
34 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.Participant;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantReplica;
40 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
43 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
44 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
45 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
46 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
47 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
48 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.stereotype.Component;
52
53 /**
54  * This class handles supervision of participant status.
55  */
56 @Component
57 @AllArgsConstructor
58 public class SupervisionParticipantHandler {
59     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionParticipantHandler.class);
60
61     private final ParticipantProvider participantProvider;
62     private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
63     private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
64     private final AutomationCompositionProvider automationCompositionProvider;
65     private final AcDefinitionProvider acDefinitionProvider;
66     private final ParticipantSyncPublisher participantSyncPublisher;
67     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
68
69     /**
70      * Handle a ParticipantRegister message from a participant.
71      *
72      * @param participantRegisterMsg the ParticipantRegister message received from a participant
73      */
74     @Timed(value = "listener.participant_register", description = "PARTICIPANT_REGISTER messages received")
75     public void handleParticipantMessage(ParticipantRegister participantRegisterMsg) {
76         saveIfNotPresent(participantRegisterMsg.getReplicaId(),
77                 participantRegisterMsg.getParticipantId(),
78                 participantRegisterMsg.getParticipantSupportedElementType(), true);
79
80         participantRegisterAckPublisher.send(participantRegisterMsg.getMessageId(),
81                 participantRegisterMsg.getParticipantId(), participantRegisterMsg.getReplicaId());
82     }
83
84     /**
85      * Handle a ParticipantDeregister message from a participant.
86      *
87      * @param participantDeregisterMsg the ParticipantDeregister message received from a participant
88      */
89     @Timed(value = "listener.participant_deregister", description = "PARTICIPANT_DEREGISTER messages received")
90     public void handleParticipantMessage(ParticipantDeregister participantDeregisterMsg) {
91         var replicaId = participantDeregisterMsg.getReplicaId() != null
92                 ? participantDeregisterMsg.getReplicaId() : participantDeregisterMsg.getParticipantId();
93         var replicaOpt = participantProvider.findParticipantReplica(replicaId);
94         if (replicaOpt.isPresent()) {
95             participantProvider.deleteParticipantReplica(replicaId);
96         }
97
98         participantDeregisterAckPublisher.send(participantDeregisterMsg.getMessageId());
99     }
100
101     /**
102      * Handle a ParticipantStatus message from a participant.
103      *
104      * @param participantStatusMsg the ParticipantStatus message received from a participant
105      */
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             automationCompositionProvider.upgradeStates(participantStatusMsg.getAutomationCompositionInfoList());
113         }
114         if (!participantStatusMsg.getParticipantDefinitionUpdates().isEmpty()
115                 && participantStatusMsg.getCompositionId() != null) {
116             updateAcDefinitionOutProperties(participantStatusMsg.getCompositionId(),
117                 participantStatusMsg.getReplicaId(), participantStatusMsg.getParticipantDefinitionUpdates());
118         }
119     }
120
121     private void saveIfNotPresent(UUID msgReplicaId, UUID participantId,
122             List<ParticipantSupportedElementType> participantSupportedElementType, boolean registration) {
123         var replicaId = msgReplicaId != null ? msgReplicaId : participantId;
124         var replicaOpt = participantProvider.findParticipantReplica(replicaId);
125         if (replicaOpt.isPresent()) {
126             var replica = replicaOpt.get();
127             checkOnline(replica);
128         } else {
129             var participant = getParticipant(participantId, listToMap(participantSupportedElementType));
130             participant.getReplicas().put(replicaId, createReplica(replicaId));
131             participantProvider.saveParticipant(participant);
132         }
133         if (registration) {
134             handleRestart(participantId, replicaId);
135         }
136     }
137
138     private Participant getParticipant(UUID participantId,
139             Map<UUID, ParticipantSupportedElementType> participantSupportedElementType) {
140         var participantOpt = participantProvider.findParticipant(participantId);
141         return participantOpt.orElseGet(() -> createParticipant(participantId, participantSupportedElementType));
142     }
143
144     private ParticipantReplica createReplica(UUID replicaId) {
145         var replica = new ParticipantReplica();
146         replica.setReplicaId(replicaId);
147         replica.setParticipantState(ParticipantState.ON_LINE);
148         replica.setLastMsg(TimestampHelper.now());
149         return replica;
150
151     }
152
153     private void updateAcDefinitionOutProperties(UUID compositionId, UUID replicaId, List<ParticipantDefinition> list) {
154         var acDefinitionOpt = acDefinitionProvider.findAcDefinition(compositionId);
155         if (acDefinitionOpt.isEmpty()) {
156             LOGGER.error("Ac Definition with id {} not found", compositionId);
157             return;
158         }
159         var acDefinition = acDefinitionOpt.get();
160         for (var acElements : list) {
161             for (var element : acElements.getAutomationCompositionElementDefinitionList()) {
162                 var state = acDefinition.getElementStateMap().get(element.getAcElementDefinitionId().getName());
163                 if (state != null) {
164                     state.setOutProperties(element.getOutProperties());
165                 }
166             }
167         }
168         acDefinitionProvider.updateAcDefinition(acDefinition,
169                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
170         participantSyncPublisher.sendSync(acDefinition, replicaId);
171     }
172
173     private void checkOnline(ParticipantReplica replica) {
174         if (ParticipantState.OFF_LINE.equals(replica.getParticipantState())) {
175             replica.setParticipantState(ParticipantState.ON_LINE);
176         }
177         replica.setLastMsg(TimestampHelper.now());
178         participantProvider.saveParticipantReplica(replica);
179     }
180
181     private void handleRestart(UUID participantId, UUID replicaId) {
182         var compositionIds = participantProvider.getCompositionIds(participantId);
183         for (var compositionId : compositionIds) {
184             var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
185             LOGGER.debug("Scan Composition {} for restart", acDefinition.getCompositionId());
186             handleSyncRestart(participantId, replicaId, acDefinition);
187         }
188     }
189
190     private void handleSyncRestart(final UUID participantId, UUID replicaId,
191             AutomationCompositionDefinition acDefinition) {
192         if (AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
193             LOGGER.debug("Composition {} COMMISSIONED", acDefinition.getCompositionId());
194             return;
195         }
196         LOGGER.debug("Composition to be send in Restart message {}", acDefinition.getCompositionId());
197         var automationCompositionList =
198                 automationCompositionProvider.getAcInstancesByCompositionId(acDefinition.getCompositionId());
199         var automationCompositions = automationCompositionList.stream()
200                 .filter(ac -> isAcToBeSyncRestarted(participantId, ac)).toList();
201         participantSyncPublisher.sendRestartMsg(participantId, replicaId, acDefinition, automationCompositions);
202     }
203
204     private boolean isAcToBeSyncRestarted(UUID participantId, AutomationComposition automationComposition) {
205         for (var element : automationComposition.getElements().values()) {
206             if (participantId.equals(element.getParticipantId())) {
207                 return true;
208             }
209         }
210         return false;
211     }
212
213     private Participant createParticipant(UUID participantId,
214             Map<UUID, ParticipantSupportedElementType> participantSupportedElementType) {
215         var participant = new Participant();
216         participant.setParticipantId(participantId);
217         participant.setParticipantSupportedElementTypes(participantSupportedElementType);
218         return participant;
219     }
220
221     private Map<UUID, ParticipantSupportedElementType> listToMap(List<ParticipantSupportedElementType> elementList) {
222         Map<UUID, ParticipantSupportedElementType> map = new HashMap<>();
223         MapUtils.populateMap(map, elementList, ParticipantSupportedElementType::getId);
224         return map;
225     }
226 }