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.ParticipantUtils;
29 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
30 import org.onap.policy.clamp.models.acm.concepts.SubState;
31 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
32 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
33 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public abstract class AbstractScanner {
39 protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
41 protected final long maxOperationWaitMs;
43 protected final AutomationCompositionProvider acProvider;
44 private final ParticipantSyncPublisher participantSyncPublisher;
45 private final EncryptionUtils encryptionUtils;
47 protected AbstractScanner(final AutomationCompositionProvider acProvider,
48 final ParticipantSyncPublisher participantSyncPublisher,
49 final AcRuntimeParameterGroup acRuntimeParameterGroup, final EncryptionUtils encryptionUtils) {
50 this.acProvider = acProvider;
51 this.participantSyncPublisher = participantSyncPublisher;
52 this.maxOperationWaitMs = acRuntimeParameterGroup.getParticipantParameters().getMaxOperationWaitMs();
53 this.encryptionUtils = encryptionUtils;
56 protected void complete(final AutomationComposition automationComposition, UpdateSync updateSync) {
57 LOGGER.debug("automation composition scan: transition state {} {} {} completed",
58 automationComposition.getDeployState(), automationComposition.getLockState(),
59 automationComposition.getSubState());
61 var deployState = automationComposition.getDeployState();
62 if (DeployState.MIGRATING.equals(automationComposition.getDeployState())
63 || DeployState.MIGRATION_REVERTING.equals(automationComposition.getDeployState())) {
65 automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
66 automationComposition.setCompositionTargetId(null);
68 automationComposition.setDeployState(AcmUtils.deployCompleted(deployState));
69 automationComposition.setLockState(AcmUtils.lockCompleted(deployState, automationComposition.getLockState()));
70 automationComposition.setPhase(null);
71 automationComposition.setSubState(SubState.NONE);
72 automationComposition.setPrecheck(null);
73 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
74 automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
76 if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
77 updateSync.setToBeDelete(true);
78 updateSync.setUpdated(false);
80 updateSync.setUpdated(true);
82 updateSync.setToBeSync(true);
83 saveAndSync(automationComposition, updateSync);
86 protected void savePhase(AutomationComposition automationComposition, int startPhase) {
87 automationComposition.setLastMsg(TimestampHelper.now());
88 automationComposition.setPhase(startPhase);
91 protected void handleTimeout(AutomationComposition automationComposition, UpdateSync updateSync) {
92 LOGGER.debug("automation composition scan: transition from state {} to {} {} not completed",
93 automationComposition.getDeployState(), automationComposition.getLockState(),
94 automationComposition.getSubState());
96 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
97 LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
98 saveAndSync(automationComposition, updateSync);
101 var name = ParticipantUtils.getOpName(automationComposition.getDeployState());
102 var element = automationComposition.getElements().values().stream()
103 .filter(el -> automationComposition.getDeployState().equals(el.getDeployState())).findFirst();
104 var maxWaitMs = element.map(automationCompositionElement -> ParticipantUtils.getTimeout(
105 automationCompositionElement.getProperties(), name, maxOperationWaitMs)).orElse(maxOperationWaitMs);
106 var now = TimestampHelper.nowEpochMilli();
107 var lastMsg = TimestampHelper.toEpochMilli(automationComposition.getLastMsg());
108 if ((now - lastMsg) > maxWaitMs) {
109 LOGGER.debug("Report timeout for the ac instance {}", automationComposition.getInstanceId());
110 automationComposition.setStateChangeResult(StateChangeResult.TIMEOUT);
111 updateSync.setUpdated(true);
112 updateSync.setToBeSync(true);
114 saveAndSync(automationComposition, updateSync);
118 * Save AutomationComposition and Sync.
120 * @param automationComposition the AutomationComposition
121 * @param updateSync the update/sync information
123 public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
124 if (updateSync.isUpdated()) {
125 acProvider.updateAutomationComposition(automationComposition);
127 if (updateSync.isToBeDelete()) {
128 acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
130 if (updateSync.isToBeSync()) {
131 decryptInstanceProperties(automationComposition);
132 participantSyncPublisher.sendSync(automationComposition);
136 protected void decryptInstanceProperties(AutomationComposition automationComposition) {
137 if (encryptionUtils.encryptionEnabled()) {
138 encryptionUtils.findAndDecryptSensitiveData(automationComposition);