c4ddbd011ba432de7ce0429ea29e8de99e898e8b
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.participants;
22
23 import jakarta.ws.rs.core.Response;
24 import java.util.List;
25 import java.util.Map;
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;
43
44 @Service
45 @Transactional
46 @RequiredArgsConstructor
47 public class AcmParticipantProvider {
48
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;
53
54     /**
55      * Get all participants.
56      *
57      * @param pageable the Pageable
58      * @return A list of available participants
59      */
60     public List<ParticipantInformation> getAllParticipants(final Pageable pageable) {
61         var participants = this.participantProvider.getParticipants();
62         return participants.stream().map(participant -> createParticipantInformation(participant, pageable)).toList();
63     }
64
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;
73     }
74
75     /**
76      * Get a participant.
77      *
78      * @param participantId The UUID of the participant to get
79      * @param pageable the Pageable
80      * @return The participant
81      */
82     public ParticipantInformation getParticipantById(final UUID participantId, final Pageable pageable) {
83         var participant = this.participantProvider.getParticipantById(participantId);
84         return createParticipantInformation(participant, pageable);
85     }
86
87     /**
88      * Send a participant status request.
89      *
90      * @param participantId The UUID of the participant to send request to
91      */
92     public void sendParticipantStatusRequest(UUID participantId) {
93         // check if participant is present
94         this.participantProvider.getParticipantById(participantId);
95
96         LOGGER.debug("Requesting Participant Status Now ParticipantStatusReq");
97         participantStatusReqPublisher.send(participantId);
98     }
99
100     /**
101      * Send status request to all participants.
102      *
103      */
104     public void sendAllParticipantStatusRequest() {
105         this.participantStatusReqPublisher.send((UUID) null);
106     }
107
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()));
113     }
114
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()));
119     }
120
121     /**
122      * Restart specific participant.
123      *
124      * @param participantId     ID of participant to restart
125      */
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);
130         }
131         supervisionParticipantHandler.handleRestart(participantId, null);
132         LOGGER.debug("Restarting participant with ID: {}", participantId);
133     }
134
135
136     /**
137      * Restart all participants.
138      */
139     public void restartAllParticipants() {
140         supervisionParticipantHandler.handleRestartOfAllParticipants();
141         LOGGER.debug("Restarting all participants");
142     }
143 }