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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.acm.participant.intermediary.handler;
25 import io.micrometer.core.annotation.Timed;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
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.ParticipantPrimeAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
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;
53 * This class is responsible for managing the state of a participant.
56 @RequiredArgsConstructor
57 public class ParticipantHandler {
58 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
60 private final AutomationCompositionHandler automationCompositionHandler;
61 private final AutomationCompositionOutHandler automationCompositionOutHandler;
62 private final ParticipantMessagePublisher publisher;
63 private final CacheProvider cacheProvider;
66 * Method which handles a participant health check event from clamp.
68 * @param participantStatusReqMsg participant participantStatusReq message
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);
77 * Handle a automation composition update message.
79 * @param updateMsg the update message
82 value = "listener.automation_composition_update",
83 description = "AUTOMATION_COMPOSITION_UPDATE messages received")
84 public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg) {
85 automationCompositionHandler.handleAutomationCompositionDeploy(updateMsg);
89 * Handle a automation composition state change message.
91 * @param stateChangeMsg the state change message
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);
101 * Handle a automation composition property update message.
103 * @param propertyUpdateMsg the property update message
105 @Timed(value = "listener.properties_update", description = "PROPERTIES_UPDATE message received")
106 public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
107 automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg);
111 * Check if a participant message applies to this participant handler.
113 * @param participantMsg the message to check
114 * @return true if it applies, false otherwise
116 public boolean appliesTo(ParticipantMessage participantMsg) {
117 return participantMsg.appliesTo(cacheProvider.getParticipantId());
121 * Check if a participant message applies to this participant handler.
123 * @param participantMsg the message to check
124 * @return true if it applies, false otherwise
126 public boolean appliesTo(ParticipantAckMessage participantMsg) {
127 return participantMsg.appliesTo(cacheProvider.getParticipantId());
131 * Method to send ParticipantRegister message to automation composition runtime.
133 public void sendParticipantRegister() {
134 var participantRegister = new ParticipantRegister();
135 participantRegister.setParticipantId(cacheProvider.getParticipantId());
136 participantRegister.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
138 publisher.sendParticipantRegister(participantRegister);
142 * Handle a participantRegister Ack message.
144 * @param participantRegisterAckMsg the participantRegisterAck message
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));
154 * Method to send ParticipantDeregister message to automation composition runtime.
156 public void sendParticipantDeregister() {
157 var participantDeregister = new ParticipantDeregister();
158 participantDeregister.setParticipantId(cacheProvider.getParticipantId());
159 publisher.sendParticipantDeregister(participantDeregister);
163 * Handle a participantDeregister Ack message.
165 * @param participantDeregisterAckMsg the participantDeregisterAck message
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());
174 * Handle a ParticipantPrime message.
176 * @param participantPrimeMsg the ParticipantPrime message
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());
182 if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) {
184 List<AutomationCompositionElementDefinition> list = new ArrayList<>();
185 for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) {
186 if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) {
187 list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
190 cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list);
193 cacheProvider.removeElementDefinition(participantPrimeMsg.getCompositionId());
195 sendParticipantPrimeAck(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId());
199 * Method to send ParticipantPrimeAck message to automation composition runtime.
201 private void sendParticipantPrimeAck(UUID messageId, UUID compositionId) {
202 var participantPrimeAck = new ParticipantPrimeAck();
203 participantPrimeAck.setResponseTo(messageId);
204 participantPrimeAck.setCompositionId(compositionId);
205 participantPrimeAck.setMessage("Participant Prime Ack message");
206 participantPrimeAck.setResult(true);
207 participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
208 participantPrimeAck.setState(ParticipantState.ON_LINE);
209 publisher.sendParticipantPrimeAck(participantPrimeAck);
213 * Dispatch a heartbeat for this participant.
215 public void sendHeartbeat() {
216 publisher.sendHeartbeat(makeHeartbeat(false));
220 * Method to send heartbeat to automation composition runtime.
222 public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
223 var heartbeat = new ParticipantStatus();
224 heartbeat.setParticipantId(cacheProvider.getParticipantId());
225 heartbeat.setState(ParticipantState.ON_LINE);
226 heartbeat.setAutomationCompositionInfoList(getAutomationCompositionInfoList());
227 heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
229 if (responseToParticipantStatusReq) {
230 var acElementDefsMap = cacheProvider.getAcElementsDefinitions();
231 List<ParticipantDefinition> participantDefinitionList = new ArrayList<>(acElementDefsMap.size());
232 for (var acElementDefs : acElementDefsMap.values()) {
233 var participantDefinition = new ParticipantDefinition();
234 participantDefinition.setParticipantId(cacheProvider.getParticipantId());
235 participantDefinition
236 .setAutomationCompositionElementDefinitionList(new ArrayList<>(acElementDefs.values()));
237 participantDefinitionList.add(participantDefinition);
239 heartbeat.setParticipantDefinitionUpdates(participantDefinitionList);
246 * get AutomationComposition Info List.
248 * @return list of AutomationCompositionInfo
250 private List<AutomationCompositionInfo> getAutomationCompositionInfoList() {
251 List<AutomationCompositionInfo> automationCompositionInfoList = new ArrayList<>();
252 for (var entry : cacheProvider.getAutomationCompositions().entrySet()) {
253 var acInfo = new AutomationCompositionInfo();
254 acInfo.setAutomationCompositionId(entry.getKey());
255 acInfo.setDeployState(entry.getValue().getDeployState());
256 acInfo.setLockState(entry.getValue().getLockState());
257 for (var element : entry.getValue().getElements().values()) {
258 acInfo.getElements().add(automationCompositionOutHandler.getAutomationCompositionElementInfo(element));
260 automationCompositionInfoList.add(acInfo);
262 return automationCompositionInfoList;