2c34652c820a84e107bbde96f8450ace8dd4f6b4
[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.AcTypeState;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementInfo;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
34 import org.onap.policy.clamp.models.acm.concepts.DeployState;
35 import org.onap.policy.clamp.models.acm.concepts.LockState;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
37 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrimeAck;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.stereotype.Component;
45
46 @Component
47 @RequiredArgsConstructor
48 public class AutomationCompositionOutHandler {
49     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionOutHandler.class);
50
51     private final ParticipantMessagePublisher publisher;
52     private final CacheProvider cacheProvider;
53
54     /**
55      * Handle a automation composition element state change message.
56      *
57      * @param automationCompositionId the automationComposition Id
58      * @param elementId the automationComposition Element Id
59      * @param deployState the DeployState state
60      * @param lockState the LockState state
61      * @param message the message
62      * @param stateChangeResult the indicator if error occurs
63      */
64     public void updateAutomationCompositionElementState(UUID automationCompositionId, UUID elementId,
65             DeployState deployState, LockState lockState, StateChangeResult stateChangeResult, String message) {
66
67         if (automationCompositionId == null || elementId == null) {
68             LOGGER.error("Cannot update Automation composition element state, id is null");
69             return;
70         }
71
72         if ((deployState != null && lockState != null) || (deployState == null && lockState == null)) {
73             LOGGER.error("state error {} and {} cannot be handled", deployState, lockState);
74             return;
75         }
76
77         var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
78         if (automationComposition == null) {
79             LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
80                     automationCompositionId);
81             return;
82         }
83
84         var element = automationComposition.getElements().get(elementId);
85         if (element == null) {
86             var msg = "Cannot update Automation composition element state, AC Element id {} not present";
87             LOGGER.error(msg, elementId);
88             return;
89         }
90
91         if (deployState != null) {
92             handleDeployState(automationComposition, element, deployState);
93         }
94         if (lockState != null) {
95             handleLockState(automationComposition, element, lockState);
96         }
97
98         var automationCompositionStateChangeAck =
99                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
100         automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId());
101         automationCompositionStateChangeAck.setMessage(message);
102         automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult);
103         automationCompositionStateChangeAck.setAutomationCompositionId(automationCompositionId);
104         automationCompositionStateChangeAck.getAutomationCompositionResultMap().put(element.getId(),
105                 new AcElementDeployAck(element.getDeployState(), element.getLockState(), element.getOperationalState(),
106                         element.getUseState(), element.getOutProperties(), true, message));
107         LOGGER.debug("Automation composition element {} state changed to {}", elementId, deployState);
108         automationCompositionStateChangeAck.setResult(true);
109         publisher.sendAutomationCompositionAck(automationCompositionStateChangeAck);
110     }
111
112     private void handleDeployState(AutomationComposition automationComposition, AutomationCompositionElement element,
113             DeployState deployState) {
114         element.setDeployState(deployState);
115         element.setLockState(DeployState.DEPLOYED.equals(element.getDeployState()) ? LockState.LOCKED : LockState.NONE);
116         var checkOpt = automationComposition.getElements().values().stream()
117                 .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny();
118         if (checkOpt.isEmpty()) {
119             automationComposition.setDeployState(deployState);
120             automationComposition.setLockState(element.getLockState());
121
122             if (DeployState.DELETED.equals(deployState)) {
123                 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
124             }
125         }
126     }
127
128     private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element,
129             LockState lockState) {
130         element.setLockState(lockState);
131         var checkOpt = automationComposition.getElements().values().stream()
132                 .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
133         if (checkOpt.isEmpty()) {
134             automationComposition.setLockState(lockState);
135         }
136     }
137
138     /**
139      * Send Ac Element Info.
140      *
141      * @param automationCompositionId the automationComposition Id
142      * @param elementId the automationComposition Element id
143      * @param useState the use State
144      * @param operationalState the operational State
145      * @param outProperties the output Properties Map
146      */
147     public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState,
148             String operationalState, Map<String, Object> outProperties) {
149
150         if (automationCompositionId == null || elementId == null) {
151             LOGGER.error("Cannot update Automation composition element state, id is null");
152             return;
153         }
154
155         var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
156         if (automationComposition == null) {
157             LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
158                     automationComposition);
159             return;
160         }
161
162         var element = automationComposition.getElements().get(elementId);
163         if (element == null) {
164             var msg = "Cannot update Automation composition element state, AC Element id {} not present";
165             LOGGER.error(msg, automationComposition);
166             return;
167         }
168         element.setOperationalState(operationalState);
169         element.setUseState(useState);
170         element.setOutProperties(outProperties);
171
172         var statusMsg = new ParticipantStatus();
173         statusMsg.setParticipantId(cacheProvider.getParticipantId());
174         statusMsg.setState(ParticipantState.ON_LINE);
175         statusMsg.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
176         var acInfo = new AutomationCompositionInfo();
177         acInfo.setAutomationCompositionId(automationCompositionId);
178         acInfo.setDeployState(automationComposition.getDeployState());
179         acInfo.setLockState(automationComposition.getLockState());
180         acInfo.setElements(List.of(getAutomationCompositionElementInfo(element)));
181         statusMsg.setAutomationCompositionInfoList(List.of(acInfo));
182         publisher.sendParticipantStatus(statusMsg);
183     }
184
185     /**
186      * Get AutomationCompositionElementInfo from AutomationCompositionElement.
187      *
188      * @param element the AutomationCompositionElement
189      * @return the AutomationCompositionElementInfo
190      */
191     public AutomationCompositionElementInfo getAutomationCompositionElementInfo(AutomationCompositionElement element) {
192         var elementInfo = new AutomationCompositionElementInfo();
193         elementInfo.setAutomationCompositionElementId(element.getId());
194         elementInfo.setDeployState(element.getDeployState());
195         elementInfo.setLockState(element.getLockState());
196         elementInfo.setOperationalState(element.getOperationalState());
197         elementInfo.setUseState(element.getUseState());
198         elementInfo.setOutProperties(element.getOutProperties());
199         return elementInfo;
200     }
201
202     /**
203      * Update Composition State for prime and deprime.
204      *
205      * @param compositionId the composition id
206      * @param state the Composition State
207      * @param stateChangeResult the indicator if error occurs
208      * @param message the message
209      */
210     public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
211             String message) {
212         var participantPrimeAck = new ParticipantPrimeAck();
213         participantPrimeAck.setCompositionId(compositionId);
214         participantPrimeAck.setMessage(message);
215         participantPrimeAck.setResult(true);
216         participantPrimeAck.setCompositionState(state);
217         participantPrimeAck.setStateChangeResult(stateChangeResult);
218         participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
219         participantPrimeAck.setState(ParticipantState.ON_LINE);
220         publisher.sendParticipantPrimeAck(participantPrimeAck);
221     }
222 }