85f12b7a74ef7752fe6452640b8c96a83dcd62bf
[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.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;
38
39 public abstract class AbstractScanner {
40
41     protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractScanner.class);
42
43     protected final long maxOperationWaitMs;
44
45     protected final AutomationCompositionProvider acProvider;
46     private final ParticipantSyncPublisher participantSyncPublisher;
47     private final EncryptionUtils encryptionUtils;
48
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;
56     }
57
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());
62
63         var deployState = automationComposition.getDeployState();
64         if (DeployState.MIGRATING.equals(automationComposition.getDeployState())) {
65             // migration completed
66             automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
67         }
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);
74                 }
75             }
76         }
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);
85         }
86         if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
87             updateSync.setToBeDelete(true);
88             updateSync.setUpdated(false);
89         } else {
90             updateSync.setUpdated(true);
91         }
92         updateSync.setToBeSync(true);
93         saveAndSync(automationComposition, updateSync);
94     }
95
96     protected void savePhase(AutomationComposition automationComposition, int startPhase) {
97         automationComposition.setLastMsg(TimestampHelper.now());
98         automationComposition.setPhase(startPhase);
99     }
100
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());
105
106         if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
107             LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
108             saveAndSync(automationComposition, updateSync);
109             return;
110         }
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);
123         }
124         saveAndSync(automationComposition, updateSync);
125     }
126
127     /**
128      * Save AutomationComposition and Sync.
129      *
130      * @param automationComposition the AutomationComposition
131      * @param updateSync the update/sync information
132      */
133     public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
134         if (updateSync.isUpdated()) {
135             if (updateSync.isToBeSync()) {
136                 automationComposition.setRevisionId(UUID.randomUUID());
137             }
138             acProvider.updateAutomationComposition(automationComposition);
139         }
140         if (updateSync.isToBeDelete()) {
141             acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
142         }
143         if (updateSync.isToBeSync()) {
144             decryptInstanceProperties(automationComposition);
145             participantSyncPublisher.sendSync(automationComposition);
146         }
147     }
148
149     protected void decryptInstanceProperties(AutomationComposition automationComposition) {
150         encryptionUtils.decryptInstanceProperties(automationComposition);
151     }
152 }