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