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.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.ParticipantRegister;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRestart;
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);
191 automationCompositionHandler.prime(participantPrimeMsg.getMessageId(),
192 participantPrimeMsg.getCompositionId(), list);
195 cacheProvider.removeElementDefinition(participantPrimeMsg.getCompositionId());
196 automationCompositionHandler.deprime(participantPrimeMsg.getMessageId(),
197 participantPrimeMsg.getCompositionId());
202 * Handle a ParticipantRestart message.
204 * @param participantRestartMsg the participantRestart message
206 @Timed(value = "listener.participant_restart", description = "PARTICIPANT_RESTART messages received")
207 public void handleParticipantRestart(ParticipantRestart participantRestartMsg) {
208 LOGGER.debug("ParticipantRestart message received for participantId {}",
209 participantRestartMsg.getParticipantId());
210 List<AutomationCompositionElementDefinition> list = new ArrayList<>();
211 for (var participantDefinition : participantRestartMsg.getParticipantDefinitionUpdates()) {
212 list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
214 if (!AcTypeState.COMMISSIONED.equals(participantRestartMsg.getState())) {
215 cacheProvider.addElementDefinition(participantRestartMsg.getCompositionId(), list);
217 automationCompositionHandler.restarted(participantRestartMsg.getMessageId(),
218 participantRestartMsg.getCompositionId(), list, participantRestartMsg.getState(),
219 participantRestartMsg.getAutomationcompositionList());
223 * Dispatch a heartbeat for this participant.
225 public void sendHeartbeat() {
226 if (publisher.isActive()) {
227 publisher.sendHeartbeat(makeHeartbeat(false));
232 * Method to send heartbeat to automation composition runtime.
234 public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
235 var heartbeat = new ParticipantStatus();
236 heartbeat.setParticipantId(cacheProvider.getParticipantId());
237 heartbeat.setState(ParticipantState.ON_LINE);
238 heartbeat.setAutomationCompositionInfoList(getAutomationCompositionInfoList());
239 heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
241 if (responseToParticipantStatusReq) {
242 var acElementDefsMap = cacheProvider.getAcElementsDefinitions();
243 List<ParticipantDefinition> participantDefinitionList = new ArrayList<>(acElementDefsMap.size());
244 for (var acElementDefs : acElementDefsMap.values()) {
245 var participantDefinition = new ParticipantDefinition();
246 participantDefinition.setParticipantId(cacheProvider.getParticipantId());
247 participantDefinition
248 .setAutomationCompositionElementDefinitionList(new ArrayList<>(acElementDefs.values()));
249 participantDefinitionList.add(participantDefinition);
251 heartbeat.setParticipantDefinitionUpdates(participantDefinitionList);
258 * get AutomationComposition Info List.
260 * @return list of AutomationCompositionInfo
262 private List<AutomationCompositionInfo> getAutomationCompositionInfoList() {
263 List<AutomationCompositionInfo> automationCompositionInfoList = new ArrayList<>();
264 for (var entry : cacheProvider.getAutomationCompositions().entrySet()) {
265 var acInfo = new AutomationCompositionInfo();
266 acInfo.setAutomationCompositionId(entry.getKey());
267 acInfo.setDeployState(entry.getValue().getDeployState());
268 acInfo.setLockState(entry.getValue().getLockState());
269 for (var element : entry.getValue().getElements().values()) {
270 acInfo.getElements().add(automationCompositionOutHandler.getAutomationCompositionElementInfo(element));
272 automationCompositionInfoList.add(acInfo);
274 return automationCompositionInfoList;