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