2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
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.main.utils.EncryptionUtils;
25 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
26 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
27 import org.onap.policy.clamp.models.acm.concepts.DeployState;
28 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
29 import org.onap.policy.clamp.models.acm.concepts.SubState;
30 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
31 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
32 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public abstract class AbstractScanner {
38 protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
40 protected final long maxOperationWaitMs;
42 protected final AutomationCompositionProvider acProvider;
43 private final ParticipantSyncPublisher participantSyncPublisher;
44 private final EncryptionUtils encryptionUtils;
46 protected AbstractScanner(final AutomationCompositionProvider acProvider,
47 final ParticipantSyncPublisher participantSyncPublisher,
48 final AcRuntimeParameterGroup acRuntimeParameterGroup, final EncryptionUtils encryptionUtils) {
49 this.acProvider = acProvider;
50 this.participantSyncPublisher = participantSyncPublisher;
51 this.maxOperationWaitMs = acRuntimeParameterGroup.getParticipantParameters().getMaxOperationWaitMs();
52 this.encryptionUtils = encryptionUtils;
55 protected void complete(final AutomationComposition automationComposition, UpdateSync updateSync) {
56 LOGGER.debug("automation composition scan: transition state {} {} {} completed",
57 automationComposition.getDeployState(), automationComposition.getLockState(),
58 automationComposition.getSubState());
60 var deployState = automationComposition.getDeployState();
61 if (DeployState.MIGRATING.equals(automationComposition.getDeployState())) {
63 automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
64 automationComposition.setCompositionTargetId(null);
66 automationComposition.setDeployState(AcmUtils.deployCompleted(deployState));
67 automationComposition.setLockState(AcmUtils.lockCompleted(deployState, automationComposition.getLockState()));
68 automationComposition.setPhase(null);
69 automationComposition.setSubState(SubState.NONE);
70 automationComposition.setPrecheck(null);
71 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
72 automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
74 if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
75 updateSync.setToBeDelete(true);
76 updateSync.setUpdated(false);
78 updateSync.setUpdated(true);
80 updateSync.setToBeSync(true);
81 saveAndSync(automationComposition, updateSync);
84 protected void savePhase(AutomationComposition automationComposition, int startPhase) {
85 automationComposition.setLastMsg(TimestampHelper.now());
86 automationComposition.setPhase(startPhase);
89 protected void handleTimeout(AutomationComposition automationComposition, UpdateSync updateSync) {
90 LOGGER.debug("automation composition scan: transition from state {} to {} {} not completed",
91 automationComposition.getDeployState(), automationComposition.getLockState(),
92 automationComposition.getSubState());
94 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
95 LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
96 saveAndSync(automationComposition, updateSync);
99 var now = TimestampHelper.nowEpochMilli();
100 var lastMsg = TimestampHelper.toEpochMilli(automationComposition.getLastMsg());
101 if ((now - lastMsg) > maxOperationWaitMs) {
102 LOGGER.debug("Report timeout for the ac instance {}", automationComposition.getInstanceId());
103 automationComposition.setStateChangeResult(StateChangeResult.TIMEOUT);
104 updateSync.setUpdated(true);
105 updateSync.setToBeSync(true);
107 saveAndSync(automationComposition, updateSync);
111 * Save AutomationComposition and Sync.
113 * @param automationComposition the AutomationComposition
114 * @param updateSync the update/sync information
116 public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
117 if (updateSync.isUpdated()) {
118 acProvider.updateAutomationComposition(automationComposition);
120 if (updateSync.isToBeDelete()) {
121 acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
123 if (updateSync.isToBeSync()) {
124 decryptInstanceProperties(automationComposition);
125 participantSyncPublisher.sendSync(automationComposition);
129 protected void decryptInstanceProperties(AutomationComposition automationComposition) {
130 if (encryptionUtils.encryptionEnabled()) {
131 encryptionUtils.findAndDecryptSensitiveData(automationComposition);