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;
27 import java.util.UUID;
28 import lombok.RequiredArgsConstructor;
29 import org.apache.commons.collections4.MapUtils;
30 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantStatusReqPublisher;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
32 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantInformation;
34 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
35 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
39 import org.springframework.transaction.annotation.Transactional;
43 @RequiredArgsConstructor
44 public class AcmParticipantProvider {
46 private static final Logger LOGGER = LoggerFactory.getLogger(AcmParticipantProvider.class);
47 private final ParticipantProvider participantProvider;
48 private final ParticipantStatusReqPublisher participantStatusReqPublisher;
51 * Get all participants.
53 * @return A list of available participants
55 public List<ParticipantInformation> getAllParticipants() {
56 var participants = this.participantProvider.getParticipants();
58 List<ParticipantInformation> participantInformationList = new ArrayList<>();
59 participants.forEach(participant -> {
60 ParticipantInformation participantInformation = new ParticipantInformation();
61 participantInformation.setParticipant(participant);
62 participantInformation.setAcElementInstanceMap(getAutomationCompositionElementsForParticipant(participant
63 .getParticipantId()));
64 participantInformation.setAcNodeTemplateStateDefinitionMap(getNodeTemplateStatesForParticipant(participant
65 .getParticipantId()));
66 participantInformationList.add(participantInformation);
68 return participantInformationList;
74 * @param participantId The UUID of the participant to get
75 * @return The participant
77 public ParticipantInformation getParticipantById(UUID participantId) {
78 var participant = this.participantProvider.getParticipantById(participantId);
79 var participantInformation = new ParticipantInformation();
80 participantInformation.setParticipant(participant);
82 participantInformation.setAcElementInstanceMap(getAutomationCompositionElementsForParticipant(participantId));
83 participantInformation.setAcNodeTemplateStateDefinitionMap(getNodeTemplateStatesForParticipant(participantId));
85 return participantInformation;
89 * Send a participant status request.
91 * @param participantId The UUID of the participant to send request to
93 public void sendParticipantStatusRequest(UUID participantId) {
94 var participant = this.participantProvider.getParticipantById(participantId);
96 LOGGER.debug("Requesting Participant Status Now ParticipantStatusReq");
97 participantStatusReqPublisher.send(participantId);
98 participant.setParticipantState(ParticipantState.OFF_LINE);
99 participantProvider.updateParticipant(participant);
103 * Send status request to all participants.
106 public void sendAllParticipantStatusRequest() {
107 this.participantStatusReqPublisher.send((UUID) null);
110 private Map<UUID, AutomationCompositionElement> getAutomationCompositionElementsForParticipant(UUID participantId) {
111 var automationCompositionElements = participantProvider
112 .getAutomationCompositionElements(participantId);
113 Map<UUID, AutomationCompositionElement> map = new HashMap<>();
114 MapUtils.populateMap(map, automationCompositionElements, AutomationCompositionElement::getId);
119 private Map<UUID, NodeTemplateState> getNodeTemplateStatesForParticipant(UUID participantId) {
120 var acNodeTemplateStates = participantProvider.getAcNodeTemplateStates(participantId);
121 Map<UUID, NodeTemplateState> map = new HashMap<>();
122 MapUtils.populateMap(map, acNodeTemplateStates, NodeTemplateState::getNodeTemplateStateId);