aboutsummaryrefslogtreecommitdiffstats
path: root/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java
blob: 5a2079b0dbac13a2e5322417fa1951dbe4c938a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.acm.runtime.supervision;

import io.micrometer.core.annotation.Timed;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import lombok.AllArgsConstructor;
import org.onap.policy.clamp.acm.runtime.supervision.comm.AcElementPropertiesPublisher;
import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionDeployPublisher;
import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionStateChangePublisher;
import org.onap.policy.clamp.models.acm.concepts.AcElementDeployAck;
import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
import org.onap.policy.clamp.models.acm.concepts.DeployState;
import org.onap.policy.clamp.models.acm.concepts.LockState;
import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
import org.onap.policy.clamp.models.acm.utils.AcmUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * This class handles supervision of automation composition instances, so only one object of this type should be built
 * at a time.
 */
@Component
@AllArgsConstructor
public class SupervisionAcHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionAcHandler.class);

    private final AutomationCompositionProvider automationCompositionProvider;

    // Publishers for participant communication
    private final AutomationCompositionDeployPublisher automationCompositionDeployPublisher;
    private final AutomationCompositionStateChangePublisher automationCompositionStateChangePublisher;
    private final AcElementPropertiesPublisher acElementPropertiesPublisher;

    /**
     * Handle Deploy an AutomationComposition instance.
     *
     * @param automationComposition the AutomationComposition
     * @param acDefinition the AutomationCompositionDefinition
     */
    public void deploy(AutomationComposition automationComposition, AutomationCompositionDefinition acDefinition) {
        if (StateChangeResult.FAILED.equals(automationComposition.getStateChangeResult())) {
            automationComposition.setDeployState(DeployState.DEPLOYING);
            for (var element : automationComposition.getElements().values()) {
                if (!DeployState.DEPLOYED.equals(element.getDeployState())) {
                    element.setDeployState(DeployState.DEPLOYING);
                }
            }
        } else {
            AcmUtils.setCascadedState(automationComposition, DeployState.DEPLOYING, LockState.NONE);
        }
        automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
        automationCompositionProvider.updateAutomationComposition(automationComposition);
        var startPhase = ParticipantUtils.getFirstStartPhase(automationComposition, acDefinition.getServiceTemplate());
        automationCompositionDeployPublisher.send(automationComposition, acDefinition.getServiceTemplate(), startPhase,
                true);
    }

    /**
     * Handle Undeploy an AutomationComposition instance.
     *
     * @param automationComposition the AutomationComposition
     * @param acDefinition the AutomationCompositionDefinition
     */
    public void undeploy(AutomationComposition automationComposition, AutomationCompositionDefinition acDefinition) {
        if (StateChangeResult.FAILED.equals(automationComposition.getStateChangeResult())) {
            automationComposition.setDeployState(DeployState.UNDEPLOYING);
            for (var element : automationComposition.getElements().values()) {
                if (!DeployState.UNDEPLOYED.equals(element.getDeployState())) {
                    element.setDeployState(DeployState.UNDEPLOYING);
                }
            }
        } else {
            AcmUtils.setCascadedState(automationComposition, DeployState.UNDEPLOYING, LockState.NONE);
        }
        automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
        automationCompositionProvider.updateAutomationComposition(automationComposition);
        var startPhase = ParticipantUtils.getFirstStartPhase(automationComposition, acDefinition.getServiceTemplate());
        automationCompositionStateChangePublisher.send(automationComposition, startPhase, true);
    }

    /**
     * Handle Unlock an AutomationComposition instance.
     *
     * @param automationComposition the AutomationComposition
     * @param acDefinition the AutomationCompositionDefinition
     */
    public void unlock(AutomationComposition automationComposition, AutomationCompositionDefinition acDefinition) {
        if (StateChangeResult.FAILED.equals(automationComposition.getStateChangeResult())) {
            automationComposition.setLockState(LockState.UNLOCKING);
            for (var element : automationComposition.getElements().values()) {
                if (!LockState.UNLOCKED.equals(element.getLockState())) {
                    element.setLockState(LockState.UNLOCKING);
                }
            }
        } else {
            AcmUtils.setCascadedState(automationComposition, DeployState.DEPLOYED, LockState.UNLOCKING);
        }
        automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
        automationCompositionProvider.updateAutomationComposition(automationComposition);
        var startPhase = ParticipantUtils.getFirstStartPhase(automationComposition, acDefinition.getServiceTemplate());
        automationCompositionStateChangePublisher.send(automationComposition, startPhase, true);
    }

    /**
     * Handle Lock an AutomationComposition instance.
     *
     * @param automationComposition the AutomationComposition
     * @param acDefinition the AutomationCompositionDefinition
     */
    public void lock(AutomationComposition automationComposition, AutomationCompositionDefinition acDefinition) {
        if (StateChangeResult.FAILED.equals(automationComposition.getStateChangeResult())) {
            automationComposition.setLockState(LockState.LOCKING);
            for (var element : automationComposition.getElements().values()) {
                if (!LockState.LOCKED.equals(element.getLockState())) {
                    element.setLockState(LockState.LOCKING);
                }
            }
        } else {
            AcmUtils.setCascadedState(automationComposition, DeployState.DEPLOYED, LockState.LOCKING);
        }
        automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
        automationCompositionProvider.updateAutomationComposition(automationComposition);
        var startPhase = ParticipantUtils.getFirstStartPhase(automationComposition, acDefinition.getServiceTemplate());
        automationCompositionStateChangePublisher.send(automationComposition, startPhase, true);
    }

    /**
     * Handle Element property update on a deployed instance.
     *
     * @param automationComposition the AutomationComposition
     */
    public void update(AutomationComposition automationComposition) {
        AcmUtils.setCascadedState(automationComposition, DeployState.UPDATING, automationComposition.getLockState());
        automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
        acElementPropertiesPublisher.send(automationComposition);
    }

    /**
     * Handle Delete an AutomationComposition instance.
     *
     * @param automationComposition the AutomationComposition
     * @param acDefinition the AutomationCompositionDefinition
     */
    public void delete(AutomationComposition automationComposition, AutomationCompositionDefinition acDefinition) {
        AcmUtils.setCascadedState(automationComposition, DeployState.DELETING, LockState.NONE);
        automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
        automationCompositionProvider.updateAutomationComposition(automationComposition);
        var startPhase = ParticipantUtils.getFirstStartPhase(automationComposition, acDefinition.getServiceTemplate());
        automationCompositionStateChangePublisher.send(automationComposition, startPhase, true);
    }

    /**
     * Handle a AutomationComposition deploy acknowledge message from a participant.
     *
     * @param automationCompositionAckMessage the AutomationCompositionAck message received from a participant
     */
    @MessageIntercept
    @Timed(
            value = "listener.automation_composition_deploy_ack",
            description = "AUTOMATION_COMPOSITION_DEPLOY_ACK messages received")
    public void handleAutomationCompositionUpdateAckMessage(
            AutomationCompositionDeployAck automationCompositionAckMessage) {
        LOGGER.debug("AutomationComposition Update Ack message received {}", automationCompositionAckMessage);
        setAcElementStateInDb(automationCompositionAckMessage);
    }

    /**
     * Handle a AutomationComposition statechange acknowledge message from a participant.
     *
     * @param automationCompositionAckMessage the AutomationCompositionAck message received from a participant
     */
    @MessageIntercept
    @Timed(
            value = "listener.automation_composition_statechange_ack",
            description = "AUTOMATION_COMPOSITION_STATECHANGE_ACK messages received")
    public void handleAutomationCompositionStateChangeAckMessage(
            AutomationCompositionDeployAck automationCompositionAckMessage) {
        LOGGER.debug("AutomationComposition StateChange Ack message received {}", automationCompositionAckMessage);
        setAcElementStateInDb(automationCompositionAckMessage);
    }

    private void setAcElementStateInDb(AutomationCompositionDeployAck automationCompositionAckMessage) {
        var automationCompositionOpt = automationCompositionProvider
                .findAutomationComposition(automationCompositionAckMessage.getAutomationCompositionId());
        if (automationCompositionOpt.isEmpty()) {
            LOGGER.warn("AutomationComposition not found in database {}",
                    automationCompositionAckMessage.getAutomationCompositionId());
            return;
        }
        var automationComposition = automationCompositionOpt.get();

        if (automationCompositionAckMessage.getAutomationCompositionResultMap() == null
                || automationCompositionAckMessage.getAutomationCompositionResultMap().isEmpty()) {
            if (DeployState.DELETING.equals(automationComposition.getDeployState())) {
                // scenario when Automation Composition instance has never been deployed
                for (var element : automationComposition.getElements().values()) {
                    if (element.getParticipantId().equals(automationCompositionAckMessage.getParticipantId())) {
                        element.setDeployState(DeployState.DELETED);
                    }
                }
                automationCompositionProvider.updateAutomationComposition(automationComposition);
            } else {
                LOGGER.warn("Empty AutomationCompositionResultMap  {} {}",
                        automationCompositionAckMessage.getAutomationCompositionId(),
                        automationCompositionAckMessage.getMessage());
            }
            return;
        }

        var updated = updateState(automationComposition,
                automationCompositionAckMessage.getAutomationCompositionResultMap().entrySet(),
                automationCompositionAckMessage.getStateChangeResult());
        if (updated) {
            automationCompositionProvider.updateAutomationComposition(automationComposition);
        }
    }

    private boolean updateState(AutomationComposition automationComposition,
            Set<Map.Entry<UUID, AcElementDeployAck>> automationCompositionResultSet,
            StateChangeResult stateChangeResult) {
        var updated = false;
        var elementInErrors = StateChangeResult.FAILED.equals(stateChangeResult);
        boolean inProgress = !StateChangeResult.FAILED.equals(automationComposition.getStateChangeResult());
        if (elementInErrors && inProgress) {
            automationComposition.setStateChangeResult(StateChangeResult.FAILED);
        }

        for (var acElementAck : automationCompositionResultSet) {
            var element = automationComposition.getElements().get(acElementAck.getKey());
            if (element != null) {
                element.setMessage(acElementAck.getValue().getMessage());
                element.setOutProperties(acElementAck.getValue().getOutProperties());
                element.setOperationalState(acElementAck.getValue().getOperationalState());
                element.setUseState(acElementAck.getValue().getUseState());
                element.setDeployState(acElementAck.getValue().getDeployState());
                element.setLockState(acElementAck.getValue().getLockState());
                element.setRestarting(null);
                updated = true;
            }
        }

        if (automationComposition.getRestarting() != null) {
            var restarting = automationComposition.getElements().values().stream()
                    .map(AutomationCompositionElement::getRestarting).filter(Objects::nonNull).findAny();
            if (restarting.isEmpty()) {
                automationComposition.setRestarting(null);
            }
        }

        return updated;
    }
}