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