2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 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 lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
28 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
29 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeploy;
30 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionMigration;
31 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange;
32 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantAckMessage;
33 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
34 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck;
35 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessage;
36 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
37 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
38 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
39 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRestart;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.PropertiesUpdate;
43 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Component;
49 * This class is responsible for managing the state of a participant.
52 @RequiredArgsConstructor
53 public class ParticipantHandler {
54 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
56 private final AutomationCompositionHandler automationCompositionHandler;
57 private final AcLockHandler acLockHandler;
58 private final AcDefinitionHandler acDefinitionHandler;
59 private final ParticipantMessagePublisher publisher;
60 private final CacheProvider cacheProvider;
63 * Method which handles a participant health check event from clamp.
65 * @param participantStatusReqMsg participant participantStatusReq message
67 @Timed(value = "listener.participant_status_req", description = "PARTICIPANT_STATUS_REQ messages received")
68 public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
73 * Handle a automation composition update message.
75 * @param updateMsg the update message
78 value = "listener.automation_composition_update",
79 description = "AUTOMATION_COMPOSITION_UPDATE messages received")
80 public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg) {
81 automationCompositionHandler.handleAutomationCompositionDeploy(updateMsg);
85 * Handle a automation composition state change message.
87 * @param stateChangeMsg the state change message
90 value = "listener.automation_composition_state_change",
91 description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received")
92 public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
93 if (DeployOrder.NONE.equals(stateChangeMsg.getDeployOrderedState())) {
94 acLockHandler.handleAutomationCompositionStateChange(stateChangeMsg);
96 automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg);
101 * Handle a automation composition migration message.
103 * @param migrationMsg the migration message
106 value = "listener.automation_composition_migration",
107 description = "AUTOMATION_COMPOSITION_MIGRATION messages received")
108 public void handleAutomationCompositionMigration(AutomationCompositionMigration migrationMsg) {
109 automationCompositionHandler.handleAutomationCompositionMigration(migrationMsg);
113 * Handle a automation composition property update message.
115 * @param propertyUpdateMsg the property update message
117 @Timed(value = "listener.properties_update", description = "PROPERTIES_UPDATE message received")
118 public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
119 automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg);
123 * Check if a participant message applies to this participant handler.
125 * @param participantMsg the message to check
126 * @return true if it applies, false otherwise
128 public boolean appliesTo(ParticipantMessage participantMsg) {
129 return participantMsg.appliesTo(cacheProvider.getParticipantId());
133 * Check if a participant message applies to this participant handler.
135 * @param participantMsg the message to check
136 * @return true if it applies, false otherwise
138 public boolean appliesTo(ParticipantAckMessage participantMsg) {
139 return participantMsg.appliesTo(cacheProvider.getParticipantId());
143 * Method to send ParticipantRegister message to automation composition runtime.
145 public void sendParticipantRegister() {
146 var participantRegister = new ParticipantRegister();
147 participantRegister.setParticipantId(cacheProvider.getParticipantId());
148 participantRegister.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
150 publisher.sendParticipantRegister(participantRegister);
154 * Handle a participantRegister Ack message.
156 * @param participantRegisterAckMsg the participantRegisterAck message
158 @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
159 public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
160 LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
161 participantRegisterAckMsg.getResponseTo());
162 cacheProvider.setRegistered(true);
163 publisher.sendParticipantStatus(makeHeartbeat());
167 * Method to send ParticipantDeregister message to automation composition runtime.
169 public void sendParticipantDeregister() {
170 var participantDeregister = new ParticipantDeregister();
171 participantDeregister.setParticipantId(cacheProvider.getParticipantId());
172 publisher.sendParticipantDeregister(participantDeregister);
176 * Handle a participantDeregister Ack message.
178 * @param participantDeregisterAckMsg the participantDeregisterAck message
180 @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
181 public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
182 LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
183 participantDeregisterAckMsg.getResponseTo());
187 * Handle a ParticipantPrime message.
189 * @param participantPrimeMsg the ParticipantPrime message
191 @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
192 public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
193 LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId());
194 acDefinitionHandler.handlePrime(participantPrimeMsg);
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 acDefinitionHandler.handleParticipantRestart(participantRestartMsg);
210 * Dispatch a heartbeat for this participant.
212 public void sendHeartbeat() {
213 if (publisher.isActive()) {
214 if (!cacheProvider.isRegistered()) {
215 sendParticipantRegister();
217 publisher.sendParticipantStatus(makeHeartbeat());
223 * Method to send heartbeat to automation composition runtime.
225 private ParticipantStatus makeHeartbeat() {
226 var heartbeat = new ParticipantStatus();
227 heartbeat.setParticipantId(cacheProvider.getParticipantId());
228 heartbeat.setState(ParticipantState.ON_LINE);
229 heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());