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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.runtime.supervision.scanner;
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;
35 public abstract class AbstractScanner {
37 protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
39 protected final long maxStatusWaitMs;
41 protected final AutomationCompositionProvider acProvider;
42 private final ParticipantSyncPublisher participantSyncPublisher;
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();
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());
57 var deployState = automationComposition.getDeployState();
58 if (DeployState.MIGRATING.equals(automationComposition.getDeployState())) {
60 automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
61 automationComposition.setCompositionTargetId(null);
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);
71 if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
72 updateSync.setToBeDelete(true);
73 updateSync.setUpdated(false);
75 updateSync.setUpdated(true);
77 updateSync.setToBeSync(true);
78 saveAndSync(automationComposition, updateSync);
81 protected void savePhase(AutomationComposition automationComposition, int startPhase) {
82 automationComposition.setLastMsg(TimestampHelper.now());
83 automationComposition.setPhase(startPhase);
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());
91 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
92 LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
93 saveAndSync(automationComposition, updateSync);
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);
104 saveAndSync(automationComposition, updateSync);
108 * Save AutomationComposition and Sync.
110 * @param automationComposition the AutomationComposition
111 * @param updateSync the update/sync information
113 public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
114 if (updateSync.isUpdated()) {
115 acProvider.updateAutomationComposition(automationComposition);
117 if (updateSync.isToBeDelete()) {
118 acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
120 if (updateSync.isToBeSync()) {
121 participantSyncPublisher.sendSync(automationComposition);