079b9008e0e3931649c2eabe7fbd2857ce11811b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.participant.intermediary.handler;
23
24 import java.util.UUID;
25 import lombok.RequiredArgsConstructor;
26 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
27 import org.onap.policy.clamp.acm.participant.intermediary.handler.cache.CacheProvider;
28 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
29 import org.onap.policy.clamp.models.acm.concepts.LockState;
30 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
31 import org.onap.policy.clamp.models.acm.concepts.SubState;
32 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 @RequiredArgsConstructor
39 public class AcLockHandler {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(AcLockHandler.class);
42
43     private final CacheProvider cacheProvider;
44     private final ThreadHandler listener;
45
46     /**
47      * Handle a automation composition state change message.
48      *
49      * @param stateChangeMsg the state change message
50      */
51     public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
52         if (stateChangeMsg.getAutomationCompositionId() == null) {
53             return;
54         }
55
56         var automationComposition = cacheProvider.getAutomationComposition(stateChangeMsg.getAutomationCompositionId());
57
58         if (automationComposition == null) {
59             LOGGER.debug("Automation composition {} does not use this participant",
60                     stateChangeMsg.getAutomationCompositionId());
61             return;
62         }
63
64         switch (stateChangeMsg.getLockOrderedState()) {
65             case LOCK -> handleLockState(stateChangeMsg.getMessageId(), automationComposition,
66                     stateChangeMsg.getStartPhase());
67             case UNLOCK -> handleUnlockState(stateChangeMsg.getMessageId(), automationComposition,
68                     stateChangeMsg.getStartPhase());
69             default -> LOGGER.error("StateChange message has no lock order {}", automationComposition.getKey());
70         }
71     }
72
73     private void handleLockState(UUID messageId, final AutomationComposition automationComposition,
74                                  Integer startPhaseMsg) {
75         automationComposition.setLockState(LockState.LOCKING);
76         for (var element : automationComposition.getElements().values()) {
77             var compositionInProperties = cacheProvider
78                     .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition());
79             int startPhase = ParticipantUtils.findStartPhase(compositionInProperties);
80             if (startPhaseMsg.equals(startPhase)) {
81                 element.setLockState(LockState.LOCKING);
82                 element.setSubState(SubState.NONE);
83                 var compositionElement = cacheProvider.createCompositionElementDto(
84                         automationComposition.getCompositionId(), element);
85                 var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(),
86                         element.getProperties(), element.getOutProperties());
87                 listener.lock(messageId, compositionElement, instanceElement);
88             }
89         }
90     }
91
92     private void handleUnlockState(UUID messageId, final AutomationComposition automationComposition,
93                                    Integer startPhaseMsg) {
94         automationComposition.setLockState(LockState.UNLOCKING);
95         for (var element : automationComposition.getElements().values()) {
96             var compositionInProperties = cacheProvider
97                     .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition());
98             int startPhase = ParticipantUtils.findStartPhase(compositionInProperties);
99             if (startPhaseMsg.equals(startPhase)) {
100                 element.setLockState(LockState.UNLOCKING);
101                 element.setSubState(SubState.NONE);
102                 var compositionElement = cacheProvider.createCompositionElementDto(
103                         automationComposition.getCompositionId(), element);
104                 var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(),
105                         element.getProperties(), element.getOutProperties());
106                 listener.unlock(messageId, compositionElement, instanceElement);
107             }
108         }
109     }
110 }