e0d11ed9f602cbcf54384ce24a516d326c62eb5f
[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.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.UUID;
29 import lombok.AllArgsConstructor;
30 import org.apache.commons.collections4.MapUtils;
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.ParticipantRestartPublisher;
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.ParticipantState;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
40 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
44 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
45 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
46 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Component;
50
51 /**
52  * This class handles supervision of participant status.
53  */
54 @Component
55 @AllArgsConstructor
56 public class SupervisionParticipantHandler {
57     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionParticipantHandler.class);
58
59     private final ParticipantProvider participantProvider;
60     private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
61     private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
62     private final AutomationCompositionProvider automationCompositionProvider;
63     private final AcDefinitionProvider acDefinitionProvider;
64     private final ParticipantRestartPublisher participantRestartPublisher;
65
66     /**
67      * Handle a ParticipantRegister message from a participant.
68      *
69      * @param participantRegisterMsg the ParticipantRegister message received from a participant
70      */
71     @MessageIntercept
72     @Timed(value = "listener.participant_register", description = "PARTICIPANT_REGISTER messages received")
73     public void handleParticipantMessage(ParticipantRegister participantRegisterMsg) {
74         LOGGER.debug("Participant Register received {}", participantRegisterMsg);
75         var participantOpt = participantProvider.findParticipant(participantRegisterMsg.getParticipantId());
76
77         if (participantOpt.isPresent()) {
78             var participant = participantOpt.get();
79             if (ParticipantState.OFF_LINE.equals(participant.getParticipantState())) {
80                 participant.setParticipantState(ParticipantState.ON_LINE);
81                 participantProvider.saveParticipant(participant);
82             }
83             handleRestart(participant.getParticipantId());
84         } else {
85             var participant = createParticipant(participantRegisterMsg.getParticipantId(),
86                     listToMap(participantRegisterMsg.getParticipantSupportedElementType()));
87             participantProvider.saveParticipant(participant);
88
89         }
90
91         participantRegisterAckPublisher.send(participantRegisterMsg.getMessageId(),
92                 participantRegisterMsg.getParticipantId());
93     }
94
95     /**
96      * Handle a ParticipantDeregister message from a participant.
97      *
98      * @param participantDeregisterMsg the ParticipantDeregister message received from a participant
99      */
100     @MessageIntercept
101     @Timed(value = "listener.participant_deregister", description = "PARTICIPANT_DEREGISTER messages received")
102     public void handleParticipantMessage(ParticipantDeregister participantDeregisterMsg) {
103         LOGGER.debug("Participant Deregister received {}", participantDeregisterMsg);
104         var participantOpt = participantProvider.findParticipant(participantDeregisterMsg.getParticipantId());
105
106         if (participantOpt.isPresent()) {
107             var participant = participantOpt.get();
108             participant.setParticipantState(ParticipantState.OFF_LINE);
109             participantProvider.updateParticipant(participant);
110         }
111
112         participantDeregisterAckPublisher.send(participantDeregisterMsg.getMessageId());
113     }
114
115     /**
116      * Handle a ParticipantStatus message from a participant.
117      *
118      * @param participantStatusMsg the ParticipantStatus message received from a participant
119      */
120     @MessageIntercept
121     @Timed(value = "listener.participant_status", description = "PARTICIPANT_STATUS messages received")
122     public void handleParticipantMessage(ParticipantStatus participantStatusMsg) {
123         LOGGER.debug("Participant Status received {}", participantStatusMsg);
124
125         var participantOpt = participantProvider.findParticipant(participantStatusMsg.getParticipantId());
126         if (participantOpt.isEmpty()) {
127             var participant = createParticipant(participantStatusMsg.getParticipantId(),
128                     listToMap(participantStatusMsg.getParticipantSupportedElementType()));
129             participantProvider.saveParticipant(participant);
130         }
131         if (!participantStatusMsg.getAutomationCompositionInfoList().isEmpty()) {
132             automationCompositionProvider.upgradeStates(participantStatusMsg.getAutomationCompositionInfoList());
133         }
134     }
135
136     private void handleRestart(UUID participantId) {
137         var compositionIds = participantProvider.getCompositionIds(participantId);
138         for (var compositionId : compositionIds) {
139             var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
140             LOGGER.debug("Scan Composition {} for restart", acDefinition.getCompositionId());
141             handleRestart(participantId, acDefinition);
142         }
143     }
144
145     private void handleRestart(UUID participantId, AutomationCompositionDefinition acDefinition) {
146         if (AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
147             LOGGER.debug("Composition {} COMMISSIONED", acDefinition.getCompositionId());
148             return;
149         }
150         LOGGER.debug("Composition to be send in Restart message {}", acDefinition.getCompositionId());
151         for (var elementState : acDefinition.getElementStateMap().values()) {
152             if (participantId.equals(elementState.getParticipantId())) {
153                 elementState.setRestarting(true);
154             }
155         }
156         var automationCompositionList =
157                 automationCompositionProvider.getAcInstancesByCompositionId(acDefinition.getCompositionId());
158         List<AutomationComposition> automationCompositions = new ArrayList<>();
159         for (var automationComposition : automationCompositionList) {
160             if (isAcToBeRestarted(participantId, automationComposition)) {
161                 automationCompositions.add(automationComposition);
162             }
163         }
164         // expected final state
165         if (StateChangeResult.TIMEOUT.equals(acDefinition.getStateChangeResult())) {
166             acDefinition.setStateChangeResult(StateChangeResult.NO_ERROR);
167         }
168         acDefinition.setRestarting(true);
169         acDefinitionProvider.updateAcDefinition(acDefinition);
170         participantRestartPublisher.send(participantId, acDefinition, automationCompositions);
171     }
172
173     private boolean isAcToBeRestarted(UUID participantId, AutomationComposition automationComposition) {
174         boolean toAdd = false;
175         for (var element : automationComposition.getElements().values()) {
176             if (participantId.equals(element.getParticipantId())) {
177                 element.setRestarting(true);
178                 toAdd = true;
179             }
180         }
181         if (toAdd) {
182             automationComposition.setRestarting(true);
183             // expected final state
184             if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
185                 automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
186             }
187             automationCompositionProvider.updateAutomationComposition(automationComposition);
188         }
189         return toAdd;
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         participant.setParticipantState(ParticipantState.ON_LINE);
198         return participant;
199     }
200
201     private Map<UUID, ParticipantSupportedElementType> listToMap(List<ParticipantSupportedElementType> elementList) {
202         Map<UUID, ParticipantSupportedElementType> map = new HashMap<>();
203         MapUtils.populateMap(map, elementList, ParticipantSupportedElementType::getId);
204         return map;
205     }
206 }