136276e550e8cc4ab663787ba5d6066261d9c072
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2025 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.scanner;
22
23 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
24 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
25 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
26 import org.onap.policy.clamp.models.acm.concepts.DeployState;
27 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
28 import org.onap.policy.clamp.models.acm.concepts.SubState;
29 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
30 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
31 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class AbstractScanner {
36
37     protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
38
39     protected final long maxStatusWaitMs;
40
41     protected final AutomationCompositionProvider acProvider;
42     private final ParticipantSyncPublisher participantSyncPublisher;
43
44     protected AbstractScanner(final AutomationCompositionProvider acProvider,
45             final ParticipantSyncPublisher participantSyncPublisher,
46             final AcRuntimeParameterGroup acRuntimeParameterGroup) {
47         this.acProvider = acProvider;
48         this.participantSyncPublisher = participantSyncPublisher;
49         this.maxStatusWaitMs = acRuntimeParameterGroup.getParticipantParameters().getMaxStatusWaitMs();
50     }
51
52     protected void complete(final AutomationComposition automationComposition, UpdateSync updateSync) {
53         LOGGER.debug("automation composition scan: transition state {} {} {} completed",
54                 automationComposition.getDeployState(), automationComposition.getLockState(),
55                 automationComposition.getSubState());
56
57         var deployState = automationComposition.getDeployState();
58         if (DeployState.MIGRATING.equals(automationComposition.getDeployState())) {
59             // migration scenario
60             automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
61             automationComposition.setCompositionTargetId(null);
62         }
63         automationComposition.setDeployState(AcmUtils.deployCompleted(deployState));
64         automationComposition.setLockState(AcmUtils.lockCompleted(deployState, automationComposition.getLockState()));
65         automationComposition.setPhase(null);
66         automationComposition.setSubState(SubState.NONE);
67         automationComposition.setPrecheck(null);
68         if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
69             automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
70         }
71         if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
72             updateSync.setToBeDelete(true);
73             updateSync.setUpdated(false);
74         } else {
75             updateSync.setUpdated(true);
76         }
77         updateSync.setToBeSync(true);
78         saveAndSync(automationComposition, updateSync);
79     }
80
81     protected void savePhase(AutomationComposition automationComposition, int startPhase) {
82         automationComposition.setLastMsg(TimestampHelper.now());
83         automationComposition.setPhase(startPhase);
84     }
85
86     protected void handleTimeout(AutomationComposition automationComposition, UpdateSync updateSync) {
87         LOGGER.debug("automation composition scan: transition from state {} to {} {} not completed",
88                 automationComposition.getDeployState(), automationComposition.getLockState(),
89                 automationComposition.getSubState());
90
91         if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
92             LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
93             saveAndSync(automationComposition, updateSync);
94             return;
95         }
96         var now = TimestampHelper.nowEpochMilli();
97         var lastMsg = TimestampHelper.toEpochMilli(automationComposition.getLastMsg());
98         if ((now - lastMsg) > maxStatusWaitMs) {
99             LOGGER.debug("Report timeout for the ac instance {}", automationComposition.getInstanceId());
100             automationComposition.setStateChangeResult(StateChangeResult.TIMEOUT);
101             updateSync.setUpdated(true);
102             updateSync.setToBeSync(true);
103         }
104         saveAndSync(automationComposition, updateSync);
105     }
106
107     /**
108      * Save AutomationComposition and Sync.
109      *
110      * @param automationComposition the AutomationComposition
111      * @param updateSync the update/sync information
112      */
113     public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
114         if (updateSync.isUpdated()) {
115             acProvider.updateAutomationComposition(automationComposition);
116         }
117         if (updateSync.isToBeDelete()) {
118             acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
119         }
120         if (updateSync.isToBeSync()) {
121             participantSyncPublisher.sendSync(automationComposition);
122         }
123     }
124 }