665baee4270bb7f57ba6492647978b8250432b2a
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.UUID;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
28 import org.onap.policy.clamp.models.acm.concepts.AcElementDeployAck;
29 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementInfo;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
33 import org.onap.policy.clamp.models.acm.concepts.DeployState;
34 import org.onap.policy.clamp.models.acm.concepts.LockState;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
36 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 @RequiredArgsConstructor
46 public class AutomationCompositionOutHandler {
47     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionOutHandler.class);
48
49     private final ParticipantMessagePublisher publisher;
50     private final CacheProvider cacheProvider;
51
52     /**
53      * Handle a automation composition element state change message.
54      *
55      * @param automationCompositionId the automationComposition Id
56      * @param elementId the automationComposition Element Id
57      * @param deployState the DeployState state
58      * @param lockState the LockState state
59      * @param message the message
60      * @param stateChangeResult the indicator if error occurs
61      */
62     public void updateAutomationCompositionElementState(UUID automationCompositionId, UUID elementId,
63             DeployState deployState, LockState lockState, StateChangeResult stateChangeResult, String message) {
64
65         if (automationCompositionId == null || elementId == null) {
66             LOGGER.error("Cannot update Automation composition element state, id is null");
67             return;
68         }
69
70         if ((deployState != null && lockState != null) || (deployState == null && lockState == null)) {
71             LOGGER.error("state error {} and {} cannot be handled", deployState, lockState);
72             return;
73         }
74
75         var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
76         if (automationComposition == null) {
77             LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
78                     automationCompositionId);
79             return;
80         }
81
82         var element = automationComposition.getElements().get(elementId);
83         if (element == null) {
84             var msg = "Cannot update Automation composition element state, AC Element id {} not present";
85             LOGGER.error(msg, elementId);
86             return;
87         }
88
89         if (deployState != null) {
90             handleDeployState(automationComposition, element, deployState);
91         }
92         if (lockState != null) {
93             handleLockState(automationComposition, element, lockState);
94         }
95
96         var automationCompositionStateChangeAck =
97                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
98         automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId());
99         automationCompositionStateChangeAck.setMessage(message);
100         automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult);
101         automationCompositionStateChangeAck.setAutomationCompositionId(automationCompositionId);
102         automationCompositionStateChangeAck.getAutomationCompositionResultMap().put(element.getId(),
103                 new AcElementDeployAck(element.getDeployState(), element.getLockState(), element.getOperationalState(),
104                         element.getUseState(), element.getOutProperties(), true, message));
105         LOGGER.debug("Automation composition element {} state changed to {}", elementId, deployState);
106         automationCompositionStateChangeAck.setResult(true);
107         publisher.sendAutomationCompositionAck(automationCompositionStateChangeAck);
108     }
109
110     private void handleDeployState(AutomationComposition automationComposition, AutomationCompositionElement element,
111             DeployState deployState) {
112         element.setDeployState(deployState);
113         element.setLockState(DeployState.DEPLOYED.equals(element.getDeployState()) ? LockState.LOCKED : LockState.NONE);
114         var checkOpt = automationComposition.getElements().values().stream()
115                 .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny();
116         if (checkOpt.isEmpty()) {
117             automationComposition.setDeployState(deployState);
118             automationComposition.setLockState(element.getLockState());
119
120             if (DeployState.DELETED.equals(deployState)) {
121                 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
122             }
123         }
124     }
125
126     private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element,
127             LockState lockState) {
128         element.setLockState(lockState);
129         var checkOpt = automationComposition.getElements().values().stream()
130                 .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
131         if (checkOpt.isEmpty()) {
132             automationComposition.setLockState(lockState);
133         }
134     }
135
136     /**
137      * Send Ac Element Info.
138      *
139      * @param automationCompositionId the automationComposition Id
140      * @param elementId the automationComposition Element id
141      * @param useState the use State
142      * @param operationalState the operational State
143      * @param outProperties the output Properties Map
144      */
145     public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState,
146             String operationalState, Map<String, Object> outProperties) {
147
148         if (automationCompositionId == null || elementId == null) {
149             LOGGER.error("Cannot update Automation composition element state, id is null");
150             return;
151         }
152
153         var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
154         if (automationComposition == null) {
155             LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
156                     automationComposition);
157             return;
158         }
159
160         var element = automationComposition.getElements().get(elementId);
161         if (element == null) {
162             var msg = "Cannot update Automation composition element state, AC Element id {} not present";
163             LOGGER.error(msg, automationComposition);
164             return;
165         }
166         element.setOperationalState(operationalState);
167         element.setUseState(useState);
168         element.setOutProperties(outProperties);
169
170         var statusMsg = new ParticipantStatus();
171         statusMsg.setParticipantId(cacheProvider.getParticipantId());
172         statusMsg.setState(ParticipantState.ON_LINE);
173         statusMsg.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
174         var acInfo = new AutomationCompositionInfo();
175         acInfo.setAutomationCompositionId(automationCompositionId);
176         acInfo.setDeployState(automationComposition.getDeployState());
177         acInfo.setLockState(automationComposition.getLockState());
178         acInfo.setElements(List.of(getAutomationCompositionElementInfo(element)));
179         statusMsg.setAutomationCompositionInfoList(List.of(acInfo));
180         publisher.sendParticipantStatus(statusMsg);
181     }
182
183     /**
184      * Get AutomationCompositionElementInfo from AutomationCompositionElement.
185      *
186      * @param element the AutomationCompositionElement
187      * @return the AutomationCompositionElementInfo
188      */
189     public AutomationCompositionElementInfo getAutomationCompositionElementInfo(AutomationCompositionElement element) {
190         var elementInfo = new AutomationCompositionElementInfo();
191         elementInfo.setAutomationCompositionElementId(element.getId());
192         elementInfo.setDeployState(element.getDeployState());
193         elementInfo.setLockState(element.getLockState());
194         elementInfo.setOperationalState(element.getOperationalState());
195         elementInfo.setUseState(element.getUseState());
196         elementInfo.setOutProperties(element.getOutProperties());
197         return elementInfo;
198     }
199 }