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