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.ParticipantSync;
43 import org.onap.policy.clamp.models.acm.messages.kafka.participant.PropertiesUpdate;
44 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
50 * This class is responsible for managing the state of a participant.
53 @RequiredArgsConstructor
54 public class ParticipantHandler {
55 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
57 private final AutomationCompositionHandler automationCompositionHandler;
58 private final AcLockHandler acLockHandler;
59 private final AcDefinitionHandler acDefinitionHandler;
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) {
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 if (DeployOrder.NONE.equals(stateChangeMsg.getDeployOrderedState())) {
95 acLockHandler.handleAutomationCompositionStateChange(stateChangeMsg);
97 automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg);
102 * Handle a automation composition migration message.
104 * @param migrationMsg the migration message
107 value = "listener.automation_composition_migration",
108 description = "AUTOMATION_COMPOSITION_MIGRATION messages received")
109 public void handleAutomationCompositionMigration(AutomationCompositionMigration migrationMsg) {
110 automationCompositionHandler.handleAutomationCompositionMigration(migrationMsg);
114 * Handle a automation composition property update message.
116 * @param propertyUpdateMsg the property update message
118 @Timed(value = "listener.properties_update", description = "PROPERTIES_UPDATE message received")
119 public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
120 automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg);
124 * Check if a participant message applies to this participant handler.
126 * @param participantMsg the message to check
127 * @return true if it applies, false otherwise
129 public boolean appliesTo(ParticipantMessage participantMsg) {
130 return participantMsg.appliesTo(cacheProvider.getParticipantId(), cacheProvider.getReplicaId());
134 * Check if a participant message applies to this participant handler.
136 * @param participantMsg the message to check
137 * @return true if it applies, false otherwise
139 public boolean appliesTo(ParticipantAckMessage participantMsg) {
140 return participantMsg.appliesTo(cacheProvider.getParticipantId(), cacheProvider.getReplicaId());
144 * Method to send ParticipantRegister message to automation composition runtime.
146 public void sendParticipantRegister() {
147 var participantRegister = new ParticipantRegister();
148 participantRegister.setParticipantId(cacheProvider.getParticipantId());
149 participantRegister.setReplicaId(cacheProvider.getReplicaId());
150 participantRegister.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
152 publisher.sendParticipantRegister(participantRegister);
156 * Handle a participantRegister Ack message.
158 * @param participantRegisterAckMsg the participantRegisterAck message
160 @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
161 public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
162 LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
163 participantRegisterAckMsg.getResponseTo());
164 cacheProvider.setRegistered(true);
165 publisher.sendParticipantStatus(makeHeartbeat());
169 * Method to send ParticipantDeregister message to automation composition runtime.
171 public void sendParticipantDeregister() {
172 var participantDeregister = new ParticipantDeregister();
173 participantDeregister.setParticipantId(cacheProvider.getParticipantId());
174 participantDeregister.setReplicaId(cacheProvider.getReplicaId());
175 publisher.sendParticipantDeregister(participantDeregister);
179 * Handle a participantDeregister Ack message.
181 * @param participantDeregisterAckMsg the participantDeregisterAck message
183 @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
184 public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
185 LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
186 participantDeregisterAckMsg.getResponseTo());
190 * Handle a ParticipantPrime message.
192 * @param participantPrimeMsg the ParticipantPrime message
194 @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
195 public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
196 LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId());
197 acDefinitionHandler.handlePrime(participantPrimeMsg);
201 * Handle a ParticipantRestart message.
203 * @param participantRestartMsg the participantRestart message
205 @Timed(value = "listener.participant_restart", description = "PARTICIPANT_RESTART messages received")
206 public void handleParticipantRestart(ParticipantRestart participantRestartMsg) {
207 LOGGER.debug("ParticipantRestart message received for participantId {}",
208 participantRestartMsg.getParticipantId());
209 acDefinitionHandler.handleParticipantRestart(participantRestartMsg);
213 * Handle a ParticipantSync message.
215 * @param participantSyncMsg the participantSync message
217 @Timed(value = "listener.participant_sync_msg", description = "PARTICIPANT_SYNC messages received")
218 public void handleParticipantSync(ParticipantSync participantSyncMsg) {
219 LOGGER.debug("ParticipantSync message received for participantId {}",
220 participantSyncMsg.getParticipantId());
224 * Dispatch a heartbeat for this participant.
226 public void sendHeartbeat() {
227 if (publisher.isActive()) {
228 if (!cacheProvider.isRegistered()) {
229 sendParticipantRegister();
231 publisher.sendParticipantStatus(makeHeartbeat());
237 * Method to send heartbeat to automation composition runtime.
239 private ParticipantStatus makeHeartbeat() {
240 var heartbeat = new ParticipantStatus();
241 heartbeat.setParticipantId(cacheProvider.getParticipantId());
242 heartbeat.setReplicaId(cacheProvider.getReplicaId());
243 heartbeat.setState(ParticipantState.ON_LINE);
244 heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());