9e1216cec397855cfc3f1526bd98a95aa2043aa4
[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.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31 import lombok.Getter;
32 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
33 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrimeAck;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
48 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
49 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
50 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.stereotype.Component;
54
55 /**
56  * This class is responsible for managing the state of a participant.
57  */
58 @Component
59 public class ParticipantHandler {
60     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
61
62     @Getter
63     private final UUID participantId;
64
65     private final AutomationCompositionHandler automationCompositionHandler;
66     private final ParticipantMessagePublisher publisher;
67
68     private final Map<UUID, List<AutomationCompositionElementDefinition>> acElementDefsMap = new HashMap<>();
69
70     private final List<ParticipantSupportedElementType> supportedAcElementTypes;
71
72     /**
73      * Constructor, set the participant ID and sender.
74      *
75      * @param parameters the parameters of the participant
76      * @param publisher the publisher for sending responses to messages
77      * @param automationCompositionHandler the publisher for sending responses to messages
78      */
79     public ParticipantHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher,
80             AutomationCompositionHandler automationCompositionHandler) {
81         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
82         this.publisher = publisher;
83         this.automationCompositionHandler = automationCompositionHandler;
84         this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
85     }
86
87     /**
88      * Method which handles a participant health check event from clamp.
89      *
90      * @param participantStatusReqMsg participant participantStatusReq message
91      */
92     @Timed(value = "listener.participant_status_req", description = "PARTICIPANT_STATUS_REQ messages received")
93     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
94         var participantStatus = makeHeartbeat(true);
95         publisher.sendParticipantStatus(participantStatus);
96     }
97
98     /**
99      * Handle a automation composition update message.
100      *
101      * @param updateMsg the update message
102      */
103     @Timed(
104             value = "listener.automation_composition_update",
105             description = "AUTOMATION_COMPOSITION_UPDATE messages received")
106     public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg) {
107         automationCompositionHandler.handleAutomationCompositionDeploy(updateMsg,
108                 acElementDefsMap.get(updateMsg.getCompositionId()));
109     }
110
111     /**
112      * Handle a automation composition state change message.
113      *
114      * @param stateChangeMsg the state change message
115      */
116     @Timed(
117             value = "listener.automation_composition_state_change",
118             description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received")
119     public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
120         automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg,
121                 acElementDefsMap.get(stateChangeMsg.getCompositionId()));
122     }
123
124     /**
125      * Handle a automation composition property update message.
126      *
127      * @param propertyUpdateMsg the property update message
128      */
129     @Timed(
130             value = "listener.properties_update",
131             description = "PROPERTIES_UPDATE message received")
132     public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
133         automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg,
134                 acElementDefsMap.get(propertyUpdateMsg.getCompositionId()));
135     }
136
137     /**
138      * Check if a participant message applies to this participant handler.
139      *
140      * @param participantMsg the message to check
141      * @return true if it applies, false otherwise
142      */
143     public boolean appliesTo(ParticipantMessage participantMsg) {
144         return participantMsg.appliesTo(participantId);
145     }
146
147     /**
148      * Check if a participant message applies to this participant handler.
149      *
150      * @param participantMsg the message to check
151      * @return true if it applies, false otherwise
152      */
153     public boolean appliesTo(ParticipantAckMessage participantMsg) {
154         return participantMsg.appliesTo(participantId);
155     }
156
157     /**
158      * Method to send ParticipantRegister message to automation composition runtime.
159      */
160     public void sendParticipantRegister() {
161         var participantRegister = new ParticipantRegister();
162         participantRegister.setParticipantId(participantId);
163         participantRegister.setParticipantSupportedElementType(supportedAcElementTypes);
164
165         publisher.sendParticipantRegister(participantRegister);
166     }
167
168     /**
169      * Handle a participantRegister Ack message.
170      *
171      * @param participantRegisterAckMsg the participantRegisterAck message
172      */
173     @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
174     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
175         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
176                 participantRegisterAckMsg.getResponseTo());
177         publisher.sendParticipantStatus(makeHeartbeat(false));
178     }
179
180     /**
181      * Method to send ParticipantDeregister message to automation composition runtime.
182      */
183     public void sendParticipantDeregister() {
184         var participantDeregister = new ParticipantDeregister();
185         participantDeregister.setParticipantId(participantId);
186         publisher.sendParticipantDeregister(participantDeregister);
187         automationCompositionHandler.undeployInstances();
188     }
189
190     /**
191      * Handle a participantDeregister Ack message.
192      *
193      * @param participantDeregisterAckMsg the participantDeregisterAck message
194      */
195     @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
196     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
197         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
198                 participantDeregisterAckMsg.getResponseTo());
199     }
200
201     /**
202      * Handle a ParticipantPrime message.
203      *
204      * @param participantPrimeMsg the ParticipantPrime message
205      */
206     @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
207     public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
208         LOGGER.debug("ParticipantPrime message received for participantId {}",
209                 participantPrimeMsg.getParticipantId());
210
211         acElementDefsMap.putIfAbsent(participantPrimeMsg.getCompositionId(), new ArrayList<>());
212         if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) {
213             // This message is to commission the automation composition
214             for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) {
215                 if (participantDefinition.getParticipantId().equals(participantId)) {
216                     acElementDefsMap.get(participantPrimeMsg.getCompositionId())
217                             .addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
218                     break;
219                 }
220             }
221         } else {
222             // This message is to decommission the automation composition
223             acElementDefsMap.get(participantPrimeMsg.getCompositionId()).clear();
224         }
225         sendParticipantPrimeAck(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId());
226     }
227
228     /**
229      * Method to send ParticipantPrimeAck message to automation composition runtime.
230      */
231     private void sendParticipantPrimeAck(UUID messageId, UUID compositionId) {
232         var participantPrimeAck = new ParticipantPrimeAck();
233         participantPrimeAck.setResponseTo(messageId);
234         participantPrimeAck.setCompositionId(compositionId);
235         participantPrimeAck.setMessage("Participant Prime Ack message");
236         participantPrimeAck.setResult(true);
237         participantPrimeAck.setParticipantId(participantId);
238         participantPrimeAck.setState(ParticipantState.ON_LINE);
239         publisher.sendParticipantPrimeAck(participantPrimeAck);
240     }
241
242     /**
243      * Dispatch a heartbeat for this participant.
244      */
245     public void sendHeartbeat() {
246         publisher.sendHeartbeat(makeHeartbeat(false));
247     }
248
249     /**
250      * Method to send heartbeat to automation composition runtime.
251      */
252     public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
253         var heartbeat = new ParticipantStatus();
254         heartbeat.setParticipantId(participantId);
255         heartbeat.setState(ParticipantState.ON_LINE);
256         heartbeat.setAutomationCompositionInfoList(automationCompositionHandler.getAutomationCompositionInfoList());
257         heartbeat.setParticipantSupportedElementType(new ArrayList<>(this.supportedAcElementTypes));
258
259         if (responseToParticipantStatusReq) {
260             List<ParticipantDefinition> participantDefinitionList = new ArrayList<>(acElementDefsMap.size());
261             for (var acElementDefsOnThisParticipant : acElementDefsMap.values()) {
262                 var participantDefinition = new ParticipantDefinition();
263                 participantDefinition.setParticipantId(participantId);
264                 participantDefinition.setAutomationCompositionElementDefinitionList(acElementDefsOnThisParticipant);
265                 participantDefinitionList.add(participantDefinition);
266             }
267             heartbeat.setParticipantDefinitionUpdates(participantDefinitionList);
268         }
269
270         return heartbeat;
271     }
272 }