b396e629c5fca32d55d1f4ca3759fd117b404d1a
[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.supervision;
22
23 import java.util.UUID;
24 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
25 import org.onap.policy.clamp.models.acm.concepts.Participant;
26 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
27 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.stereotype.Component;
31
32 /**
33  * This class is used to scan the automation compositions in the database and check if they are in the correct state.
34  */
35 @Component
36 public class SupervisionPartecipantScanner {
37     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionPartecipantScanner.class);
38
39     private final HandleCounter<UUID> participantStatusCounter = new HandleCounter<>();
40
41     private final ParticipantProvider participantProvider;
42
43     /**
44      * Constructor for instantiating SupervisionPartecipantScanner.
45      *
46      * @param participantProvider the Participant Provider
47      * @param acRuntimeParameterGroup the parameters for the automation composition runtime
48      */
49     public SupervisionPartecipantScanner(
50                               final ParticipantProvider participantProvider,
51                               final AcRuntimeParameterGroup acRuntimeParameterGroup) {
52         this.participantProvider = participantProvider;
53
54         participantStatusCounter.setMaxRetryCount(
55             acRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxRetryCount());
56         participantStatusCounter.setMaxWaitMs(acRuntimeParameterGroup.getParticipantParameters().getMaxStatusWaitMs());
57     }
58
59     /**
60      * Run Scanning.
61      */
62     public void run() {
63         LOGGER.debug("Scanning participans in the database . . .");
64
65         for (var participant : participantProvider.getParticipants()) {
66             scanParticipantStatus(participant);
67         }
68
69         LOGGER.debug("Participans scan complete . . .");
70     }
71
72     private void scanParticipantStatus(Participant participant) {
73         var id = participant.getParticipantId();
74         if (participantStatusCounter.isFault(id)) {
75             LOGGER.debug("report Participant fault");
76             return;
77         }
78         if (participantStatusCounter.getDuration(id) > participantStatusCounter.getMaxWaitMs()
79             && !participantStatusCounter.count(id)) {
80             LOGGER.debug("report Participant fault");
81             participantStatusCounter.setFault(id);
82             participant.setParticipantState(ParticipantState.OFF_LINE);
83             participantProvider.updateParticipant(participant);
84         }
85     }
86
87     /**
88      * handle participant Status message.
89      */
90     public void handleParticipantStatus(UUID id) {
91         participantStatusCounter.clear(id);
92     }
93 }