670ae53018f3713fa08765a087b7eef103eddb45
[policy/clamp.git] /
1 /*-
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
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.Set;
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;
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 ParticipantStatusReqPublisher participantStatusReqPublisher;
52
53     /**
54      * Get all participants.
55      *
56      * @return A list of available participants
57      */
58     public List<ParticipantInformation> getAllParticipants() {
59         var participants = this.participantProvider.getParticipants();
60
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);
70         });
71         return participantInformationList;
72     }
73
74     /**
75      * Get a participant.
76      *
77      * @param participantId The UUID of the participant to get
78      * @return The participant
79      */
80     public ParticipantInformation getParticipantById(UUID participantId) {
81         var participant = this.participantProvider.getParticipantById(participantId);
82         var participantInformation = new ParticipantInformation();
83         participantInformation.setParticipant(participant);
84
85         participantInformation.setAcElementInstanceMap(getAutomationCompositionElementsForParticipant(participantId));
86         participantInformation.setAcNodeTemplateStateDefinitionMap(getNodeTemplateStatesForParticipant(participantId));
87
88         return participantInformation;
89     }
90
91     /**
92      * Send a participant status request.
93      *
94      * @param participantId The UUID of the participant to send request to
95      */
96     public void sendParticipantStatusRequest(UUID participantId) {
97         var participant = this.participantProvider.getParticipantById(participantId);
98
99         LOGGER.debug("Requesting Participant Status Now ParticipantStatusReq");
100         participantStatusReqPublisher.send(participantId);
101         participant.setParticipantState(ParticipantState.OFF_LINE);
102         participantProvider.updateParticipant(participant);
103     }
104
105     /**
106      * Send status request to all participants.
107      *
108      */
109     public void sendAllParticipantStatusRequest() {
110         this.participantStatusReqPublisher.send((UUID) null);
111     }
112
113     /**
114      * Verify Participant state.
115      *
116      * @param participantIds The list of UUIDs of the participants to get
117      * @throws  PfModelRuntimeException in case the participant is offline
118      */
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");
125             }
126         }
127     }
128
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);
134
135         return map;
136     }
137
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);
142
143         return map;
144     }
145 }