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 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.ParticipantState;
33 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
34 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
35 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRestart;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.stereotype.Component;
51 * This class is responsible for managing the state of a participant.
54 @RequiredArgsConstructor
55 public class ParticipantHandler {
56 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
58 private final AutomationCompositionHandler automationCompositionHandler;
59 private final AutomationCompositionOutHandler automationCompositionOutHandler;
60 private final ParticipantMessagePublisher publisher;
61 private final CacheProvider cacheProvider;
64 * Method which handles a participant health check event from clamp.
66 * @param participantStatusReqMsg participant participantStatusReq message
68 @Timed(value = "listener.participant_status_req", description = "PARTICIPANT_STATUS_REQ messages received")
69 public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
70 publisher.sendParticipantStatus(makeHeartbeat());
74 * Handle a automation composition update message.
76 * @param updateMsg the update message
79 value = "listener.automation_composition_update",
80 description = "AUTOMATION_COMPOSITION_UPDATE messages received")
81 public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg) {
82 automationCompositionHandler.handleAutomationCompositionDeploy(updateMsg);
86 * Handle a automation composition state change message.
88 * @param stateChangeMsg the state change message
91 value = "listener.automation_composition_state_change",
92 description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received")
93 public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
94 automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg);
98 * Handle a automation composition property update message.
100 * @param propertyUpdateMsg the property update message
102 @Timed(value = "listener.properties_update", description = "PROPERTIES_UPDATE message received")
103 public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
104 automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg);
108 * Check if a participant message applies to this participant handler.
110 * @param participantMsg the message to check
111 * @return true if it applies, false otherwise
113 public boolean appliesTo(ParticipantMessage participantMsg) {
114 return participantMsg.appliesTo(cacheProvider.getParticipantId());
118 * Check if a participant message applies to this participant handler.
120 * @param participantMsg the message to check
121 * @return true if it applies, false otherwise
123 public boolean appliesTo(ParticipantAckMessage participantMsg) {
124 return participantMsg.appliesTo(cacheProvider.getParticipantId());
128 * Method to send ParticipantRegister message to automation composition runtime.
130 public void sendParticipantRegister() {
131 var participantRegister = new ParticipantRegister();
132 participantRegister.setParticipantId(cacheProvider.getParticipantId());
133 participantRegister.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
135 publisher.sendParticipantRegister(participantRegister);
139 * Handle a participantRegister Ack message.
141 * @param participantRegisterAckMsg the participantRegisterAck message
143 @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
144 public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
145 LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
146 participantRegisterAckMsg.getResponseTo());
147 publisher.sendParticipantStatus(makeHeartbeat());
151 * Method to send ParticipantDeregister message to automation composition runtime.
153 public void sendParticipantDeregister() {
154 var participantDeregister = new ParticipantDeregister();
155 participantDeregister.setParticipantId(cacheProvider.getParticipantId());
156 publisher.sendParticipantDeregister(participantDeregister);
160 * Handle a participantDeregister Ack message.
162 * @param participantDeregisterAckMsg the participantDeregisterAck message
164 @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
165 public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
166 LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
167 participantDeregisterAckMsg.getResponseTo());
171 * Handle a ParticipantPrime message.
173 * @param participantPrimeMsg the ParticipantPrime message
175 @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
176 public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
177 LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId());
179 if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) {
181 List<AutomationCompositionElementDefinition> list = new ArrayList<>();
182 for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) {
183 if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) {
184 list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
187 cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list);
188 automationCompositionHandler.prime(participantPrimeMsg.getMessageId(),
189 participantPrimeMsg.getCompositionId(), list);
192 automationCompositionHandler.deprime(participantPrimeMsg.getMessageId(),
193 participantPrimeMsg.getCompositionId());
198 * Handle a ParticipantRestart message.
200 * @param participantRestartMsg the participantRestart message
202 @Timed(value = "listener.participant_restart", description = "PARTICIPANT_RESTART messages received")
203 public void handleParticipantRestart(ParticipantRestart participantRestartMsg) {
204 LOGGER.debug("ParticipantRestart message received for participantId {}",
205 participantRestartMsg.getParticipantId());
206 List<AutomationCompositionElementDefinition> list = new ArrayList<>();
207 for (var participantDefinition : participantRestartMsg.getParticipantDefinitionUpdates()) {
208 list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
210 if (!AcTypeState.COMMISSIONED.equals(participantRestartMsg.getState())) {
211 cacheProvider.addElementDefinition(participantRestartMsg.getCompositionId(), list);
213 automationCompositionHandler.restarted(participantRestartMsg.getMessageId(),
214 participantRestartMsg.getCompositionId(), list, participantRestartMsg.getState(),
215 participantRestartMsg.getAutomationcompositionList());
219 * Dispatch a heartbeat for this participant.
221 public void sendHeartbeat() {
222 if (publisher.isActive()) {
223 publisher.sendHeartbeat(makeHeartbeat());
228 * Method to send heartbeat to automation composition runtime.
230 private ParticipantStatus makeHeartbeat() {
231 var heartbeat = new ParticipantStatus();
232 heartbeat.setParticipantId(cacheProvider.getParticipantId());
233 heartbeat.setState(ParticipantState.ON_LINE);
234 heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());