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