5ad8ea9a44966d61dcfa37b1ec305d6470f936e6
[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.ParticipantUtils;
31 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
32 import org.onap.policy.clamp.models.acm.concepts.SubState;
33 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
34 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
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                 || DeployState.MIGRATION_REVERTING.equals(automationComposition.getDeployState())) {
66             // migration scenario
67             automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
68             automationComposition.setCompositionTargetId(null);
69
70             for (var acElement : automationComposition.getElements().values()) {
71                 if (MigrationState.NEW.equals(acElement.getMigrationState())) {
72                     acElement.setMigrationState(MigrationState.DEFAULT);
73                 }
74             }
75         }
76         automationComposition.setDeployState(AcmUtils.deployCompleted(deployState));
77         automationComposition.setLockState(AcmUtils.lockCompleted(deployState, automationComposition.getLockState()));
78         automationComposition.setPhase(null);
79         automationComposition.setSubState(SubState.NONE);
80         automationComposition.setPrecheck(null);
81         if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
82             automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
83         }
84         if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
85             updateSync.setToBeDelete(true);
86             updateSync.setUpdated(false);
87         } else {
88             updateSync.setUpdated(true);
89         }
90         updateSync.setToBeSync(true);
91         saveAndSync(automationComposition, updateSync);
92     }
93
94     protected void savePhase(AutomationComposition automationComposition, int startPhase) {
95         automationComposition.setLastMsg(TimestampHelper.now());
96         automationComposition.setPhase(startPhase);
97     }
98
99     protected void handleTimeout(AutomationComposition automationComposition, UpdateSync updateSync) {
100         LOGGER.debug("automation composition scan: transition from state {} to {} {} not completed",
101                 automationComposition.getDeployState(), automationComposition.getLockState(),
102                 automationComposition.getSubState());
103
104         if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
105             LOGGER.debug("The ac instance is in timeout {}", automationComposition.getInstanceId());
106             saveAndSync(automationComposition, updateSync);
107             return;
108         }
109         var name = ParticipantUtils.getOpName(automationComposition.getDeployState());
110         var element = automationComposition.getElements().values().stream()
111                 .filter(el -> automationComposition.getDeployState().equals(el.getDeployState())).findFirst();
112         var maxWaitMs = element.map(automationCompositionElement -> ParticipantUtils.getTimeout(
113                 automationCompositionElement.getProperties(), name, maxOperationWaitMs)).orElse(maxOperationWaitMs);
114         var now = TimestampHelper.nowEpochMilli();
115         var lastMsg = TimestampHelper.toEpochMilli(automationComposition.getLastMsg());
116         if ((now - lastMsg) > maxWaitMs) {
117             LOGGER.debug("Report timeout for the ac instance {}", automationComposition.getInstanceId());
118             automationComposition.setStateChangeResult(StateChangeResult.TIMEOUT);
119             updateSync.setUpdated(true);
120             updateSync.setToBeSync(true);
121         }
122         saveAndSync(automationComposition, updateSync);
123     }
124
125     /**
126      * Save AutomationComposition and Sync.
127      *
128      * @param automationComposition the AutomationComposition
129      * @param updateSync the update/sync information
130      */
131     public void saveAndSync(AutomationComposition automationComposition, UpdateSync updateSync) {
132         if (updateSync.isUpdated()) {
133             if (updateSync.isToBeSync()) {
134                 automationComposition.setRevisionId(UUID.randomUUID());
135             }
136             acProvider.updateAutomationComposition(automationComposition);
137         }
138         if (updateSync.isToBeDelete()) {
139             acProvider.deleteAutomationComposition(automationComposition.getInstanceId());
140         }
141         if (updateSync.isToBeSync()) {
142             decryptInstanceProperties(automationComposition);
143             participantSyncPublisher.sendSync(automationComposition);
144         }
145     }
146
147     protected void decryptInstanceProperties(AutomationComposition automationComposition) {
148         encryptionUtils.decryptInstanceProperties(automationComposition);
149     }
150 }