2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation.
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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import java.util.UUID;
29 import javax.ws.rs.core.Response;
30 import lombok.RequiredArgsConstructor;
31 import org.apache.commons.collections4.MapUtils;
32 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantStatusReqPublisher;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
34 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantInformation;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
37 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
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 ParticipantStatusReqPublisher participantStatusReqPublisher;
54 * Get all participants.
56 * @return A list of available participants
58 public List<ParticipantInformation> getAllParticipants() {
59 var participants = this.participantProvider.getParticipants();
61 List<ParticipantInformation> participantInformationList = new ArrayList<>();
62 participants.forEach(participant -> {
63 ParticipantInformation participantInformation = new ParticipantInformation();
64 participantInformation.setParticipant(participant);
65 participantInformation.setAcElementInstanceMap(getAutomationCompositionElementsForParticipant(participant
66 .getParticipantId()));
67 participantInformation.setAcNodeTemplateStateDefinitionMap(getNodeTemplateStatesForParticipant(participant
68 .getParticipantId()));
69 participantInformationList.add(participantInformation);
71 return participantInformationList;
77 * @param participantId The UUID of the participant to get
78 * @return The participant
80 public ParticipantInformation getParticipantById(UUID participantId) {
81 var participant = this.participantProvider.getParticipantById(participantId);
82 var participantInformation = new ParticipantInformation();
83 participantInformation.setParticipant(participant);
85 participantInformation.setAcElementInstanceMap(getAutomationCompositionElementsForParticipant(participantId));
86 participantInformation.setAcNodeTemplateStateDefinitionMap(getNodeTemplateStatesForParticipant(participantId));
88 return participantInformation;
92 * Send a participant status request.
94 * @param participantId The UUID of the participant to send request to
96 public void sendParticipantStatusRequest(UUID participantId) {
97 var participant = this.participantProvider.getParticipantById(participantId);
99 LOGGER.debug("Requesting Participant Status Now ParticipantStatusReq");
100 participantStatusReqPublisher.send(participantId);
101 participant.setParticipantState(ParticipantState.OFF_LINE);
102 participantProvider.updateParticipant(participant);
106 * Send status request to all participants.
109 public void sendAllParticipantStatusRequest() {
110 this.participantStatusReqPublisher.send((UUID) null);
114 * Verify Participant state.
116 * @param participantIds The list of UUIDs of the participants to get
117 * @throws PfModelRuntimeException in case the participant is offline
119 public void verifyParticipantState(Set<UUID> participantIds) {
120 for (UUID participantId : participantIds) {
121 var participant = this.participantProvider.getParticipantById(participantId);
122 if (! participant.getParticipantState().equals(ParticipantState.ON_LINE)) {
123 throw new PfModelRuntimeException(Response.Status.CONFLICT,
124 "Participant: " + participantId + " is OFFLINE");
129 private Map<UUID, AutomationCompositionElement> getAutomationCompositionElementsForParticipant(UUID participantId) {
130 var automationCompositionElements = participantProvider
131 .getAutomationCompositionElements(participantId);
132 Map<UUID, AutomationCompositionElement> map = new HashMap<>();
133 MapUtils.populateMap(map, automationCompositionElements, AutomationCompositionElement::getId);
138 private Map<UUID, NodeTemplateState> getNodeTemplateStatesForParticipant(UUID participantId) {
139 var acNodeTemplateStates = participantProvider.getAcNodeTemplateStates(participantId);
140 Map<UUID, NodeTemplateState> map = new HashMap<>();
141 MapUtils.populateMap(map, acNodeTemplateStates, NodeTemplateState::getNodeTemplateStateId);