a4c05ce0fcf4fd13b0c4e1a6cdde57a73f9be5bc
[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 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;
35
36 public abstract class AbstractScanner {
37
38     protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
39
40     protected final long maxOperationWaitMs;
41
42     protected final AutomationCompositionProvider acProvider;
43     private final ParticipantSyncPublisher participantSyncPublisher;
44     private final EncryptionUtils encryptionUtils;
45
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;
53     }
54
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());
59
60         var deployState = automationComposition.getDeployState();
61         if (DeployState.MIGRATING.equals(automationComposition.getDeployState())) {
62             // migration scenario
63             automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
64             automationComposition.setCompositionTargetId(null);
65         }
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);
73         }
74         if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
75             updateSync.setToBeDelete(true);
76             updateSync.setUpdated(false);
77         } else {
78             updateSync.setUpdated(true);
79         }
80         updateSync.setToBeSync(true);
81         saveAndSync(automationComposition, updateSync);
82     }
83
84     protected void savePhase(AutomationComposition automationComposition, int startPhase) {
85         automationComposition.setLastMsg(TimestampHelper.now());
86         automationComposition.setPhase(startPhase);
87     }
88
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());
93
94         if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
95             LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
96             saveAndSync(automationComposition, updateSync);
97             return;
98         }
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);
106         }
107         saveAndSync(automationComposition, updateSync);
108     }
109
110     /**
111      * Save AutomationComposition and Sync.
112      *
113      * @param automationComposition the AutomationComposition
114      * @param updateSync the update/sync information
115      */
116     public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
117         if (updateSync.isUpdated()) {
118             acProvider.updateAutomationComposition(automationComposition);
119         }
120         if (updateSync.isToBeDelete()) {
121             acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
122         }
123         if (updateSync.isToBeSync()) {
124             decryptInstanceProperties(automationComposition);
125             participantSyncPublisher.sendSync(automationComposition);
126         }
127     }
128
129     protected void decryptInstanceProperties(AutomationComposition automationComposition) {
130         if (encryptionUtils.encryptionEnabled()) {
131             encryptionUtils.findAndDecryptSensitiveData(automationComposition);
132         }
133     }
134 }