18a8e8a26e98c7d795c6eac7839be5bc314fa3fd
[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.List;
25 import java.util.UUID;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantStatusReqPublisher;
28 import org.onap.policy.clamp.models.acm.concepts.ParticipantInformation;
29 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
30 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.stereotype.Service;
34 import org.springframework.transaction.annotation.Transactional;
35
36 @Service
37 @Transactional
38 @RequiredArgsConstructor
39 public class AcmParticipantProvider {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(AcmParticipantProvider.class);
42     private final ParticipantProvider participantProvider;
43     private final ParticipantStatusReqPublisher participantStatusReqPublisher;
44
45     /**
46      * Get all participants.
47      *
48      * @return A list of available participants
49      */
50     public List<ParticipantInformation> getAllParticipants() {
51         var participants = this.participantProvider.getParticipants();
52
53         List<ParticipantInformation> participantInformationList = new ArrayList<>();
54         participants.forEach(participant -> {
55             ParticipantInformation participantInformation = new ParticipantInformation();
56             participantInformation.setParticipant(participant);
57             participantInformationList.add(participantInformation);
58         });
59         return participantInformationList;
60     }
61
62     /**
63      * Get a participant.
64      *
65      * @param participantId The UUID of the participant to get
66      * @return The participant
67      */
68     public ParticipantInformation getParticipantById(UUID participantId) {
69         var participant = this.participantProvider.getParticipantById(participantId);
70         var participantInformation = new ParticipantInformation();
71         participantInformation.setParticipant(participant);
72         return participantInformation;
73     }
74
75     /**
76      * Send a participant status request.
77      *
78      * @param participantId The UUID of the participant to send request to
79      */
80     public void sendParticipantStatusRequest(UUID participantId) {
81         var participant = this.participantProvider.getParticipantById(participantId);
82
83         LOGGER.debug("Requesting Participant Status Now ParticipantStatusReq");
84         participantStatusReqPublisher.send(participantId);
85         participant.setParticipantState(ParticipantState.OFF_LINE);
86         participantProvider.updateParticipant(participant);
87     }
88
89     /**
90      * Send status request to all participants.
91      *
92      */
93     public void sendAllParticipantStatusRequest() {
94         this.participantStatusReqPublisher.send((UUID) null);
95     }
96 }