27a05f3f89b53760d2c73b599b6eb5b84e6b17dc
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.supervision.scanner;
22
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;
37
38 public abstract class AbstractScanner {
39
40     protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
41
42     protected final long maxOperationWaitMs;
43
44     protected final AutomationCompositionProvider acProvider;
45     private final ParticipantSyncPublisher participantSyncPublisher;
46     private final EncryptionUtils encryptionUtils;
47
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;
55     }
56
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());
61
62         var deployState = automationComposition.getDeployState();
63         if (DeployState.MIGRATING.equals(automationComposition.getDeployState())
64                 || DeployState.MIGRATION_REVERTING.equals(automationComposition.getDeployState())) {
65             // migration scenario
66             automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
67             automationComposition.setCompositionTargetId(null);
68         }
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);
76         }
77         if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
78             updateSync.setToBeDelete(true);
79             updateSync.setUpdated(false);
80         } else {
81             updateSync.setUpdated(true);
82         }
83         updateSync.setToBeSync(true);
84         saveAndSync(automationComposition, updateSync);
85     }
86
87     protected void savePhase(AutomationComposition automationComposition, int startPhase) {
88         automationComposition.setLastMsg(TimestampHelper.now());
89         automationComposition.setPhase(startPhase);
90     }
91
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());
96
97         if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
98             LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
99             saveAndSync(automationComposition, updateSync);
100             return;
101         }
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);
114         }
115         saveAndSync(automationComposition, updateSync);
116     }
117
118     /**
119      * Save AutomationComposition and Sync.
120      *
121      * @param automationComposition the AutomationComposition
122      * @param updateSync the update/sync information
123      */
124     public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
125         if (updateSync.isUpdated()) {
126             if (updateSync.isToBeSync()) {
127                 automationComposition.setRevisionId(UUID.randomUUID());
128             }
129             acProvider.updateAutomationComposition(automationComposition);
130         }
131         if (updateSync.isToBeDelete()) {
132             acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
133         }
134         if (updateSync.isToBeSync()) {
135             decryptInstanceProperties(automationComposition);
136             participantSyncPublisher.sendSync(automationComposition);
137         }
138     }
139
140     protected void decryptInstanceProperties(AutomationComposition automationComposition) {
141         encryptionUtils.decryptInstanceProperties(automationComposition);
142     }
143 }