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.MigrationState;
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.AcmStateUtils;
34 import org.onap.policy.clamp.models.acm.utils.AcmTimeoutUtils;
35 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public abstract class AbstractScanner {
41 protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
43 protected final long maxOperationWaitMs;
45 protected final AutomationCompositionProvider acProvider;
46 private final ParticipantSyncPublisher participantSyncPublisher;
47 private final EncryptionUtils encryptionUtils;
49 protected AbstractScanner(final AutomationCompositionProvider acProvider,
50 final ParticipantSyncPublisher participantSyncPublisher,
51 final AcRuntimeParameterGroup acRuntimeParameterGroup, final EncryptionUtils encryptionUtils) {
52 this.acProvider = acProvider;
53 this.participantSyncPublisher = participantSyncPublisher;
54 this.maxOperationWaitMs = acRuntimeParameterGroup.getParticipantParameters().getMaxOperationWaitMs();
55 this.encryptionUtils = encryptionUtils;
58 protected void complete(final AutomationComposition automationComposition, UpdateSync updateSync) {
59 LOGGER.debug("automation composition scan: transition state {} {} {} completed",
60 automationComposition.getDeployState(), automationComposition.getLockState(),
61 automationComposition.getSubState());
63 var deployState = automationComposition.getDeployState();
64 if (DeployState.MIGRATING.equals(automationComposition.getDeployState())) {
65 // migration completed
66 automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
68 if (DeployState.MIGRATING.equals(automationComposition.getDeployState())
69 || DeployState.MIGRATION_REVERTING.equals(automationComposition.getDeployState())) {
70 automationComposition.setCompositionTargetId(null);
71 for (var acElement : automationComposition.getElements().values()) {
72 if (MigrationState.NEW.equals(acElement.getMigrationState())) {
73 acElement.setMigrationState(MigrationState.DEFAULT);
77 automationComposition.setDeployState(AcmStateUtils.deployCompleted(deployState));
78 automationComposition.setLockState(
79 AcmStateUtils.lockCompleted(deployState, automationComposition.getLockState()));
80 automationComposition.setPhase(null);
81 automationComposition.setSubState(SubState.NONE);
82 automationComposition.setPrecheck(null);
83 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
84 automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
86 if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
87 updateSync.setToBeDelete(true);
88 updateSync.setUpdated(false);
90 updateSync.setUpdated(true);
92 updateSync.setToBeSync(true);
93 saveAndSync(automationComposition, updateSync);
96 protected void savePhase(AutomationComposition automationComposition, int startPhase) {
97 automationComposition.setLastMsg(TimestampHelper.now());
98 automationComposition.setPhase(startPhase);
101 protected void handleTimeout(AutomationComposition automationComposition, UpdateSync updateSync) {
102 LOGGER.debug("automation composition scan: transition from state {} to {} {} not completed",
103 automationComposition.getDeployState(), automationComposition.getLockState(),
104 automationComposition.getSubState());
106 if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
107 LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
108 saveAndSync(automationComposition, updateSync);
111 var name = AcmTimeoutUtils.getOpName(automationComposition.getDeployState());
112 var element = automationComposition.getElements().values().stream()
113 .filter(el -> automationComposition.getDeployState().equals(el.getDeployState())).findFirst();
114 var maxWaitMs = element.map(automationCompositionElement -> AcmTimeoutUtils.getTimeout(
115 automationCompositionElement.getProperties(), name, maxOperationWaitMs)).orElse(maxOperationWaitMs);
116 var now = TimestampHelper.nowEpochMilli();
117 var lastMsg = TimestampHelper.toEpochMilli(automationComposition.getLastMsg());
118 if ((now - lastMsg) > maxWaitMs) {
119 LOGGER.debug("Report timeout for the ac instance {}", automationComposition.getInstanceId());
120 automationComposition.setStateChangeResult(StateChangeResult.TIMEOUT);
121 updateSync.setUpdated(true);
122 updateSync.setToBeSync(true);
124 saveAndSync(automationComposition, updateSync);
128 * Save AutomationComposition and Sync.
130 * @param automationComposition the AutomationComposition
131 * @param updateSync the update/sync information
133 public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
134 if (updateSync.isUpdated()) {
135 if (updateSync.isToBeSync()) {
136 automationComposition.setRevisionId(UUID.randomUUID());
138 acProvider.updateAutomationComposition(automationComposition);
140 if (updateSync.isToBeDelete()) {
141 acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
143 if (updateSync.isToBeSync()) {
144 decryptInstanceProperties(automationComposition);
145 participantSyncPublisher.sendSync(automationComposition);
149 protected void decryptInstanceProperties(AutomationComposition automationComposition) {
150 encryptionUtils.decryptInstanceProperties(automationComposition);