62ba7b017656188668ee52dafa794e3cfdf13e9c
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023-2024 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
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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
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.persistence.provider.ParticipantProvider;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.stereotype.Service;
38 import org.springframework.transaction.annotation.Transactional;
39
40 @Service
41 @Transactional
42 @RequiredArgsConstructor
43 public class AcmParticipantProvider {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(AcmParticipantProvider.class);
46     private final ParticipantProvider participantProvider;
47     private final ParticipantStatusReqPublisher participantStatusReqPublisher;
48
49     /**
50      * Get all participants.
51      *
52      * @return A list of available participants
53      */
54     public List<ParticipantInformation> getAllParticipants() {
55         var participants = this.participantProvider.getParticipants();
56
57         List<ParticipantInformation> participantInformationList = new ArrayList<>();
58         participants.forEach(participant -> {
59             ParticipantInformation participantInformation = new ParticipantInformation();
60             participantInformation.setParticipant(participant);
61             participantInformation.setAcElementInstanceMap(getAutomationCompositionElementsForParticipant(participant
62                 .getParticipantId()));
63             participantInformation.setAcNodeTemplateStateDefinitionMap(getNodeTemplateStatesForParticipant(participant
64                 .getParticipantId()));
65             participantInformationList.add(participantInformation);
66         });
67         return participantInformationList;
68     }
69
70     /**
71      * Get a participant.
72      *
73      * @param participantId The UUID of the participant to get
74      * @return The participant
75      */
76     public ParticipantInformation getParticipantById(UUID participantId) {
77         var participant = this.participantProvider.getParticipantById(participantId);
78         var participantInformation = new ParticipantInformation();
79         participantInformation.setParticipant(participant);
80
81         participantInformation.setAcElementInstanceMap(getAutomationCompositionElementsForParticipant(participantId));
82         participantInformation.setAcNodeTemplateStateDefinitionMap(getNodeTemplateStatesForParticipant(participantId));
83
84         return participantInformation;
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> getAutomationCompositionElementsForParticipant(UUID participantId) {
109         var automationCompositionElements = participantProvider
110             .getAutomationCompositionElements(participantId);
111         Map<UUID, AutomationCompositionElement> map = new HashMap<>();
112         MapUtils.populateMap(map, automationCompositionElements, AutomationCompositionElement::getId);
113
114         return map;
115     }
116
117     private Map<UUID, NodeTemplateState> getNodeTemplateStatesForParticipant(UUID participantId) {
118         var acNodeTemplateStates = participantProvider.getAcNodeTemplateStates(participantId);
119         Map<UUID, NodeTemplateState> map = new HashMap<>();
120         MapUtils.populateMap(map, acNodeTemplateStates, NodeTemplateState::getNodeTemplateStateId);
121
122         return map;
123     }
124 }