2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.participant.intermediary.handler;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.UUID;
27 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
28 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
29 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
32 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
34 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
35 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
41 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
42 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
43 import org.onap.policy.clamp.models.acm.persistence.provider.AcInstanceStateResolver;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Component;
49 * This class is responsible for managing the state of all automation compositions in the participant.
52 public class AutomationCompositionHandler {
53 private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionHandler.class);
55 private final CacheProvider cacheProvider;
56 private final ParticipantMessagePublisher publisher;
57 private final ThreadHandler listener;
58 private final AcInstanceStateResolver acInstanceStateResolver;
61 * Constructor, set the participant ID and messageSender.
63 * @param cacheProvider the Cache Provider
64 * @param publisher the ParticipantMessage Publisher
65 * @param listener the ThreadHandler Listener
67 public AutomationCompositionHandler(CacheProvider cacheProvider, ParticipantMessagePublisher publisher,
68 ThreadHandler listener) {
69 this.cacheProvider = cacheProvider;
70 this.publisher = publisher;
71 this.listener = listener;
72 this.acInstanceStateResolver = new AcInstanceStateResolver();
76 * Handle a automation composition state change message.
78 * @param stateChangeMsg the state change message
80 public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
81 if (stateChangeMsg.getAutomationCompositionId() == null) {
85 var automationComposition = cacheProvider.getAutomationComposition(stateChangeMsg.getAutomationCompositionId());
87 if (automationComposition == null) {
88 if (DeployOrder.DELETE.equals(stateChangeMsg.getDeployOrderedState())) {
89 var automationCompositionAck = new AutomationCompositionDeployAck(
90 ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
91 automationCompositionAck.setParticipantId(cacheProvider.getParticipantId());
92 automationCompositionAck.setMessage("Already deleted or never used");
93 automationCompositionAck.setResult(true);
94 automationCompositionAck.setStateChangeResult(StateChangeResult.NO_ERROR);
95 automationCompositionAck.setResponseTo(stateChangeMsg.getMessageId());
96 automationCompositionAck.setAutomationCompositionId(stateChangeMsg.getAutomationCompositionId());
97 publisher.sendAutomationCompositionAck(automationCompositionAck);
99 LOGGER.debug("Automation composition {} does not use this participant",
100 stateChangeMsg.getAutomationCompositionId());
105 if (!checkConsistantOrderState(automationComposition, stateChangeMsg.getDeployOrderedState(),
106 stateChangeMsg.getLockOrderedState())) {
107 LOGGER.warn("Not Consistant OrderState Automation composition {}",
108 stateChangeMsg.getAutomationCompositionId());
112 if (DeployOrder.NONE.equals(stateChangeMsg.getDeployOrderedState())) {
113 handleLockOrderState(stateChangeMsg.getMessageId(), automationComposition,
114 stateChangeMsg.getLockOrderedState(), stateChangeMsg.getStartPhase());
116 handleDeployOrderState(stateChangeMsg.getMessageId(), automationComposition,
117 stateChangeMsg.getDeployOrderedState(), stateChangeMsg.getStartPhase());
121 private boolean checkConsistantOrderState(AutomationComposition automationComposition, DeployOrder deployOrder,
122 LockOrder lockOrder) {
123 if (DeployOrder.UPDATE.equals(deployOrder)) {
126 return acInstanceStateResolver.resolve(deployOrder, lockOrder, automationComposition.getDeployState(),
127 automationComposition.getLockState(), automationComposition.getStateChangeResult()) != null;
131 * Method to handle state changes.
133 * @param messageId the messageId
134 * @param automationComposition participant response
135 * @param orderedState automation composition ordered state
136 * @param startPhaseMsg startPhase from message
138 private void handleDeployOrderState(UUID messageId, final AutomationComposition automationComposition,
139 DeployOrder orderedState, Integer startPhaseMsg) {
141 switch (orderedState) {
143 handleUndeployState(messageId, automationComposition, startPhaseMsg);
146 handleDeleteState(messageId, automationComposition, startPhaseMsg);
150 LOGGER.error("StateChange message has no state, state is null {}", automationComposition.getKey());
156 * Method to handle state changes.
158 * @param messageId the messageId
159 * @param automationComposition participant response
160 * @param orderedState automation composition ordered state
161 * @param startPhaseMsg startPhase from message
163 private void handleLockOrderState(UUID messageId, final AutomationComposition automationComposition,
164 LockOrder orderedState, Integer startPhaseMsg) {
166 switch (orderedState) {
168 handleLockState(messageId, automationComposition, startPhaseMsg);
171 handleUnlockState(messageId, automationComposition, startPhaseMsg);
174 LOGGER.error("StateChange message has no state, state is null {}", automationComposition.getKey());
180 * Handle a automation composition properties update message.
182 * @param updateMsg the properties update message
184 public void handleAcPropertyUpdate(PropertiesUpdate updateMsg) {
186 if (updateMsg.getParticipantUpdatesList().isEmpty()) {
187 LOGGER.warn("No AutomationCompositionElement updates in message {}",
188 updateMsg.getAutomationCompositionId());
192 for (var participantDeploy : updateMsg.getParticipantUpdatesList()) {
193 if (cacheProvider.getParticipantId().equals(participantDeploy.getParticipantId())) {
195 updateExistingElementsOnThisParticipant(updateMsg.getAutomationCompositionId(), participantDeploy);
197 callParticipantUpdateProperty(updateMsg.getMessageId(), participantDeploy.getAcElementList(),
198 updateMsg.getAutomationCompositionId());
204 * Handle a automation composition Deploy message.
206 * @param deployMsg the Deploy message
208 public void handleAutomationCompositionDeploy(AutomationCompositionDeploy deployMsg) {
210 if (deployMsg.getParticipantUpdatesList().isEmpty()) {
211 LOGGER.warn("No AutomationCompositionElement deploy in message {}", deployMsg.getAutomationCompositionId());
215 for (var participantDeploy : deployMsg.getParticipantUpdatesList()) {
216 if (cacheProvider.getParticipantId().equals(participantDeploy.getParticipantId())) {
217 if (deployMsg.isFirstStartPhase()) {
218 cacheProvider.initializeAutomationComposition(deployMsg.getCompositionId(),
219 deployMsg.getAutomationCompositionId(), participantDeploy);
221 callParticipanDeploy(deployMsg.getMessageId(), participantDeploy.getAcElementList(),
222 deployMsg.getStartPhase(), deployMsg.getAutomationCompositionId());
227 private void callParticipanDeploy(UUID messageId, List<AcElementDeploy> acElements, Integer startPhaseMsg,
229 for (var element : acElements) {
230 var commonProperties = cacheProvider.getCommonProperties(instanceId, element.getId());
231 int startPhase = ParticipantUtils.findStartPhase(commonProperties);
232 if (startPhaseMsg.equals(startPhase)) {
233 var map = new HashMap<>(commonProperties);
234 map.putAll(element.getProperties());
235 listener.deploy(messageId, instanceId, element, map);
240 private void callParticipantUpdateProperty(UUID messageId, List<AcElementDeploy> acElements, UUID instanceId) {
241 for (var element : acElements) {
242 listener.update(messageId, instanceId, element, element.getProperties());
246 private void updateExistingElementsOnThisParticipant(UUID instanceId, ParticipantDeploy participantDeploy) {
247 var acElementList = cacheProvider.getAutomationComposition(instanceId).getElements();
248 for (var element : participantDeploy.getAcElementList()) {
249 var acElement = acElementList.get(element.getId());
250 acElement.getProperties().putAll(element.getProperties());
255 * Method to handle when the new state from participant is UNINITIALISED state.
257 * @param messageId the messageId
258 * @param automationComposition participant response
259 * @param startPhaseMsg startPhase from message
261 private void handleUndeployState(UUID messageId, final AutomationComposition automationComposition,
262 Integer startPhaseMsg) {
263 for (var acElement : automationComposition.getElements().values()) {
264 int startPhase = ParticipantUtils.findStartPhase(
265 cacheProvider.getCommonProperties(automationComposition.getInstanceId(), acElement.getId()));
266 if (startPhaseMsg.equals(startPhase)) {
267 listener.undeploy(messageId, automationComposition.getInstanceId(), acElement.getId());
272 private void handleDeleteState(UUID messageId, final AutomationComposition automationComposition,
273 Integer startPhaseMsg) {
274 for (var acElement : automationComposition.getElements().values()) {
275 int startPhase = ParticipantUtils.findStartPhase(
276 cacheProvider.getCommonProperties(automationComposition.getInstanceId(), acElement.getId()));
277 if (startPhaseMsg.equals(startPhase)) {
278 listener.delete(messageId, automationComposition.getInstanceId(), acElement.getId());
284 * Method to handle when the new state from participant is PASSIVE state.
286 * @param messageId the messageId
287 * @param automationComposition participant response
288 * @param startPhaseMsg startPhase from message
290 private void handleLockState(UUID messageId, final AutomationComposition automationComposition,
291 Integer startPhaseMsg) {
292 for (var acElement : automationComposition.getElements().values()) {
293 int startPhase = ParticipantUtils.findStartPhase(
294 cacheProvider.getCommonProperties(automationComposition.getInstanceId(), acElement.getId()));
295 if (startPhaseMsg.equals(startPhase)) {
296 listener.lock(messageId, automationComposition.getInstanceId(), acElement.getId());
302 * Method to handle when the new state from participant is RUNNING state.
304 * @param messageId the messageId
305 * @param automationComposition participant response
306 * @param startPhaseMsg startPhase from message
308 private void handleUnlockState(UUID messageId, final AutomationComposition automationComposition,
309 Integer startPhaseMsg) {
310 for (var acElement : automationComposition.getElements().values()) {
311 int startPhase = ParticipantUtils.findStartPhase(
312 cacheProvider.getCommonProperties(automationComposition.getInstanceId(), acElement.getId()));
313 if (startPhaseMsg.equals(startPhase)) {
314 listener.unlock(messageId, automationComposition.getInstanceId(), acElement.getId());
320 * Handles prime a Composition Definition.
322 * @param messageId the messageId
323 * @param compositionId the compositionId
324 * @param list the list of AutomationCompositionElementDefinition
326 public void prime(UUID messageId, UUID compositionId, List<AutomationCompositionElementDefinition> list) {
327 listener.prime(messageId, compositionId, list);
331 * Handles deprime a Composition Definition.
333 * @param messageId the messageId
334 * @param compositionId the compositionId
336 public void deprime(UUID messageId, UUID compositionId) {
337 listener.deprime(messageId, compositionId);
341 * Handles restarted scenario.
343 * @param messageId the messageId
344 * @param compositionId the compositionId
345 * @param list the list of AutomationCompositionElementDefinition
346 * @param state the state of the composition
347 * @param automationCompositionList list of ParticipantRestartAc
349 public void restarted(UUID messageId, UUID compositionId, List<AutomationCompositionElementDefinition> list,
350 AcTypeState state, List<ParticipantRestartAc> automationCompositionList) {
352 for (var automationcomposition : automationCompositionList) {
353 cacheProvider.initializeAutomationComposition(compositionId, automationcomposition);
355 listener.restarted(messageId, compositionId, list, state, automationCompositionList);