2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.runtime.participants;
23 import jakarta.ws.rs.core.Response;
24 import java.util.List;
26 import java.util.UUID;
27 import java.util.function.Function;
28 import java.util.stream.Collectors;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionParticipantHandler;
31 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantStatusReqPublisher;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
33 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
34 import org.onap.policy.clamp.models.acm.concepts.Participant;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantInformation;
36 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.data.domain.Pageable;
41 import org.springframework.stereotype.Service;
42 import org.springframework.transaction.annotation.Transactional;
46 @RequiredArgsConstructor
47 public class AcmParticipantProvider {
49 private static final Logger LOGGER = LoggerFactory.getLogger(AcmParticipantProvider.class);
50 private final ParticipantProvider participantProvider;
51 private final SupervisionParticipantHandler supervisionParticipantHandler;
52 private final ParticipantStatusReqPublisher participantStatusReqPublisher;
55 * Get all participants.
57 * @param pageable the Pageable
58 * @return A list of available participants
60 public List<ParticipantInformation> getAllParticipants(final Pageable pageable) {
61 var participants = this.participantProvider.getParticipants();
62 return participants.stream().map(participant -> createParticipantInformation(participant, pageable)).toList();
65 private ParticipantInformation createParticipantInformation(Participant participant, Pageable pageable) {
66 var participantInformation = new ParticipantInformation();
67 participantInformation.setParticipant(participant);
68 participantInformation.setAcElementInstanceMap(
69 getAcElementsForParticipant(participant.getParticipantId(), pageable));
70 participantInformation.setAcNodeTemplateStateDefinitionMap(
71 getNodeTemplateStatesForParticipant(participant.getParticipantId(), pageable));
72 return participantInformation;
78 * @param participantId The UUID of the participant to get
79 * @param pageable the Pageable
80 * @return The participant
82 public ParticipantInformation getParticipantById(final UUID participantId, final Pageable pageable) {
83 var participant = this.participantProvider.getParticipantById(participantId);
84 return createParticipantInformation(participant, pageable);
88 * Send a participant status request.
90 * @param participantId The UUID of the participant to send request to
92 public void sendParticipantStatusRequest(UUID participantId) {
93 // check if participant is present
94 this.participantProvider.getParticipantById(participantId);
96 LOGGER.debug("Requesting Participant Status Now ParticipantStatusReq");
97 participantStatusReqPublisher.send(participantId);
101 * Send status request to all participants.
104 public void sendAllParticipantStatusRequest() {
105 this.participantStatusReqPublisher.send((UUID) null);
108 private Map<UUID, AutomationCompositionElement> getAcElementsForParticipant(UUID participantId, Pageable pageable) {
109 var automationCompositionElements =
110 participantProvider.getAutomationCompositionElements(participantId, pageable);
111 return automationCompositionElements
112 .stream().collect(Collectors.toMap(AutomationCompositionElement::getId, Function.identity()));
115 private Map<UUID, NodeTemplateState> getNodeTemplateStatesForParticipant(UUID participantId, Pageable pageable) {
116 var acNodeTemplateStates = participantProvider.getAcNodeTemplateStates(participantId, pageable);
117 return acNodeTemplateStates
118 .stream().collect(Collectors.toMap(NodeTemplateState::getNodeTemplateStateId, Function.identity()));
122 * Restart specific participant.
124 * @param participantId ID of participant to restart
126 public void restartParticipant(UUID participantId) {
127 if (participantProvider.findParticipant(participantId).isEmpty()) {
128 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
129 "Participant Not Found with ID: " + participantId);
131 supervisionParticipantHandler.handleRestart(participantId, null);
132 LOGGER.debug("Restarting participant with ID: {}", participantId);
137 * Restart all participants.
139 public void restartAllParticipants() {
140 supervisionParticipantHandler.handleRestartOfAllParticipants();
141 LOGGER.debug("Restarting all participants");