c78c7bb433ae664b00162a88118010d6c7893cee
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.acm.participant.intermediary.handler;
24
25 import io.micrometer.core.annotation.Timed;
26 import java.util.ArrayList;
27 import java.util.List;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
30 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
34 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
35 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRestart;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.stereotype.Component;
51
52 /**
53  * This class is responsible for managing the state of a participant.
54  */
55 @Component
56 @RequiredArgsConstructor
57 public class ParticipantHandler {
58     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
59
60     private final AutomationCompositionHandler automationCompositionHandler;
61     private final AutomationCompositionOutHandler automationCompositionOutHandler;
62     private final ParticipantMessagePublisher publisher;
63     private final CacheProvider cacheProvider;
64
65     /**
66      * Method which handles a participant health check event from clamp.
67      *
68      * @param participantStatusReqMsg participant participantStatusReq message
69      */
70     @Timed(value = "listener.participant_status_req", description = "PARTICIPANT_STATUS_REQ messages received")
71     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
72         var participantStatus = makeHeartbeat(true);
73         publisher.sendParticipantStatus(participantStatus);
74     }
75
76     /**
77      * Handle a automation composition update message.
78      *
79      * @param updateMsg the update message
80      */
81     @Timed(
82             value = "listener.automation_composition_update",
83             description = "AUTOMATION_COMPOSITION_UPDATE messages received")
84     public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg) {
85         automationCompositionHandler.handleAutomationCompositionDeploy(updateMsg);
86     }
87
88     /**
89      * Handle a automation composition state change message.
90      *
91      * @param stateChangeMsg the state change message
92      */
93     @Timed(
94             value = "listener.automation_composition_state_change",
95             description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received")
96     public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
97         automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg);
98     }
99
100     /**
101      * Handle a automation composition property update message.
102      *
103      * @param propertyUpdateMsg the property update message
104      */
105     @Timed(value = "listener.properties_update", description = "PROPERTIES_UPDATE message received")
106     public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
107         automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg);
108     }
109
110     /**
111      * Check if a participant message applies to this participant handler.
112      *
113      * @param participantMsg the message to check
114      * @return true if it applies, false otherwise
115      */
116     public boolean appliesTo(ParticipantMessage participantMsg) {
117         return participantMsg.appliesTo(cacheProvider.getParticipantId());
118     }
119
120     /**
121      * Check if a participant message applies to this participant handler.
122      *
123      * @param participantMsg the message to check
124      * @return true if it applies, false otherwise
125      */
126     public boolean appliesTo(ParticipantAckMessage participantMsg) {
127         return participantMsg.appliesTo(cacheProvider.getParticipantId());
128     }
129
130     /**
131      * Method to send ParticipantRegister message to automation composition runtime.
132      */
133     public void sendParticipantRegister() {
134         var participantRegister = new ParticipantRegister();
135         participantRegister.setParticipantId(cacheProvider.getParticipantId());
136         participantRegister.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
137
138         publisher.sendParticipantRegister(participantRegister);
139     }
140
141     /**
142      * Handle a participantRegister Ack message.
143      *
144      * @param participantRegisterAckMsg the participantRegisterAck message
145      */
146     @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
147     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
148         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
149                 participantRegisterAckMsg.getResponseTo());
150         publisher.sendParticipantStatus(makeHeartbeat(false));
151     }
152
153     /**
154      * Method to send ParticipantDeregister message to automation composition runtime.
155      */
156     public void sendParticipantDeregister() {
157         var participantDeregister = new ParticipantDeregister();
158         participantDeregister.setParticipantId(cacheProvider.getParticipantId());
159         publisher.sendParticipantDeregister(participantDeregister);
160     }
161
162     /**
163      * Handle a participantDeregister Ack message.
164      *
165      * @param participantDeregisterAckMsg the participantDeregisterAck message
166      */
167     @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
168     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
169         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
170                 participantDeregisterAckMsg.getResponseTo());
171     }
172
173     /**
174      * Handle a ParticipantPrime message.
175      *
176      * @param participantPrimeMsg the ParticipantPrime message
177      */
178     @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
179     public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
180         LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId());
181
182         if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) {
183             // prime
184             List<AutomationCompositionElementDefinition> list = new ArrayList<>();
185             for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) {
186                 if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) {
187                     list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
188                 }
189             }
190             cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list);
191             automationCompositionHandler.prime(participantPrimeMsg.getMessageId(),
192                     participantPrimeMsg.getCompositionId(), list);
193         } else {
194             // deprime
195             cacheProvider.removeElementDefinition(participantPrimeMsg.getCompositionId());
196             automationCompositionHandler.deprime(participantPrimeMsg.getMessageId(),
197                     participantPrimeMsg.getCompositionId());
198         }
199     }
200
201     /**
202      * Handle a ParticipantRestart message.
203      *
204      * @param participantRestartMsg the participantRestart message
205      */
206     @Timed(value = "listener.participant_restart", description = "PARTICIPANT_RESTART messages received")
207     public void handleParticipantRestart(ParticipantRestart participantRestartMsg) {
208         LOGGER.debug("ParticipantRestart message received for participantId {}",
209                 participantRestartMsg.getParticipantId());
210         List<AutomationCompositionElementDefinition> list = new ArrayList<>();
211         for (var participantDefinition : participantRestartMsg.getParticipantDefinitionUpdates()) {
212             list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
213         }
214         if (!AcTypeState.COMMISSIONED.equals(participantRestartMsg.getState())) {
215             cacheProvider.addElementDefinition(participantRestartMsg.getCompositionId(), list);
216         }
217         automationCompositionHandler.restarted(participantRestartMsg.getMessageId(),
218                 participantRestartMsg.getCompositionId(), list, participantRestartMsg.getState(),
219                 participantRestartMsg.getAutomationcompositionList());
220     }
221
222     /**
223      * Dispatch a heartbeat for this participant.
224      */
225     public void sendHeartbeat() {
226         publisher.sendHeartbeat(makeHeartbeat(false));
227     }
228
229     /**
230      * Method to send heartbeat to automation composition runtime.
231      */
232     public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
233         var heartbeat = new ParticipantStatus();
234         heartbeat.setParticipantId(cacheProvider.getParticipantId());
235         heartbeat.setState(ParticipantState.ON_LINE);
236         heartbeat.setAutomationCompositionInfoList(getAutomationCompositionInfoList());
237         heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
238
239         if (responseToParticipantStatusReq) {
240             var acElementDefsMap = cacheProvider.getAcElementsDefinitions();
241             List<ParticipantDefinition> participantDefinitionList = new ArrayList<>(acElementDefsMap.size());
242             for (var acElementDefs : acElementDefsMap.values()) {
243                 var participantDefinition = new ParticipantDefinition();
244                 participantDefinition.setParticipantId(cacheProvider.getParticipantId());
245                 participantDefinition
246                         .setAutomationCompositionElementDefinitionList(new ArrayList<>(acElementDefs.values()));
247                 participantDefinitionList.add(participantDefinition);
248             }
249             heartbeat.setParticipantDefinitionUpdates(participantDefinitionList);
250         }
251
252         return heartbeat;
253     }
254
255     /**
256      * get AutomationComposition Info List.
257      *
258      * @return list of AutomationCompositionInfo
259      */
260     private List<AutomationCompositionInfo> getAutomationCompositionInfoList() {
261         List<AutomationCompositionInfo> automationCompositionInfoList = new ArrayList<>();
262         for (var entry : cacheProvider.getAutomationCompositions().entrySet()) {
263             var acInfo = new AutomationCompositionInfo();
264             acInfo.setAutomationCompositionId(entry.getKey());
265             acInfo.setDeployState(entry.getValue().getDeployState());
266             acInfo.setLockState(entry.getValue().getLockState());
267             for (var element : entry.getValue().getElements().values()) {
268                 acInfo.getElements().add(automationCompositionOutHandler.getAutomationCompositionElementInfo(element));
269             }
270             automationCompositionInfoList.add(acInfo);
271         }
272         return automationCompositionInfoList;
273     }
274 }