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 java.util.UUID;
24 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
25 import org.onap.policy.clamp.acm.runtime.main.utils.EncryptionUtils;
26 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
27 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
28 import org.onap.policy.clamp.models.acm.concepts.DeployState;
29 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
30 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
31 import org.onap.policy.clamp.models.acm.concepts.SubState;
32 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
33 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
34 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public abstract class AbstractScanner {
40 protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
42 protected final long maxOperationWaitMs;
44 protected final AutomationCompositionProvider acProvider;
45 private final ParticipantSyncPublisher participantSyncPublisher;
46 private final EncryptionUtils encryptionUtils;
48 protected AbstractScanner(final AutomationCompositionProvider acProvider,
49 final ParticipantSyncPublisher participantSyncPublisher,
50 final AcRuntimeParameterGroup acRuntimeParameterGroup, final EncryptionUtils encryptionUtils) {
51 this.acProvider = acProvider;
52 this.participantSyncPublisher = participantSyncPublisher;
53 this.maxOperationWaitMs = acRuntimeParameterGroup.getParticipantParameters().getMaxOperationWaitMs();
54 this.encryptionUtils = encryptionUtils;
57 protected void complete(final AutomationComposition automationComposition, UpdateSync updateSync) {
58 LOGGER.debug("automation composition scan: transition state {} {} {} completed",
59 automationComposition.getDeployState(), automationComposition.getLockState(),
60 automationComposition.getSubState());
62 var deployState = automationComposition.getDeployState();
63 if (DeployState.MIGRATING.equals(automationComposition.getDeployState())
64 || DeployState.MIGRATION_REVERTING.equals(automationComposition.getDeployState())) {
66 automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
67 automationComposition.setCompositionTargetId(null);
69 automationComposition.setDeployState(AcmUtils.deployCompleted(deployState));
70 automationComposition.setLockState(AcmUtils.lockCompleted(deployState, automationComposition.getLockState()));
71 automationComposition.setPhase(null);
72 automationComposition.setSubState(SubState.NONE);
73 automationComposition.setPrecheck(null);
74 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
75 automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
77 if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
78 updateSync.setToBeDelete(true);
79 updateSync.setUpdated(false);
81 updateSync.setUpdated(true);
83 updateSync.setToBeSync(true);
84 saveAndSync(automationComposition, updateSync);
87 protected void savePhase(AutomationComposition automationComposition, int startPhase) {
88 automationComposition.setLastMsg(TimestampHelper.now());
89 automationComposition.setPhase(startPhase);
92 protected void handleTimeout(AutomationComposition automationComposition, UpdateSync updateSync) {
93 LOGGER.debug("automation composition scan: transition from state {} to {} {} not completed",
94 automationComposition.getDeployState(), automationComposition.getLockState(),
95 automationComposition.getSubState());
97 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
98 LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
99 saveAndSync(automationComposition, updateSync);
102 var name = ParticipantUtils.getOpName(automationComposition.getDeployState());
103 var element = automationComposition.getElements().values().stream()
104 .filter(el -> automationComposition.getDeployState().equals(el.getDeployState())).findFirst();
105 var maxWaitMs = element.map(automationCompositionElement -> ParticipantUtils.getTimeout(
106 automationCompositionElement.getProperties(), name, maxOperationWaitMs)).orElse(maxOperationWaitMs);
107 var now = TimestampHelper.nowEpochMilli();
108 var lastMsg = TimestampHelper.toEpochMilli(automationComposition.getLastMsg());
109 if ((now - lastMsg) > maxWaitMs) {
110 LOGGER.debug("Report timeout for the ac instance {}", automationComposition.getInstanceId());
111 automationComposition.setStateChangeResult(StateChangeResult.TIMEOUT);
112 updateSync.setUpdated(true);
113 updateSync.setToBeSync(true);
115 saveAndSync(automationComposition, updateSync);
119 * Save AutomationComposition and Sync.
121 * @param automationComposition the AutomationComposition
122 * @param updateSync the update/sync information
124 public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
125 if (updateSync.isUpdated()) {
126 if (updateSync.isToBeSync()) {
127 automationComposition.setRevisionId(UUID.randomUUID());
129 acProvider.updateAutomationComposition(automationComposition);
131 if (updateSync.isToBeDelete()) {
132 acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
134 if (updateSync.isToBeSync()) {
135 decryptInstanceProperties(automationComposition);
136 participantSyncPublisher.sendSync(automationComposition);
140 protected void decryptInstanceProperties(AutomationComposition automationComposition) {
141 encryptionUtils.decryptInstanceProperties(automationComposition);