3023cddda9905f4b8e032b2e65d028d42be8ceaa
[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.AutomationCompositionElementDefinition;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementInfo;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
35 import org.onap.policy.clamp.models.acm.concepts.DeployState;
36 import org.onap.policy.clamp.models.acm.concepts.LockState;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
39 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrimeAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
48
49 @Component
50 @RequiredArgsConstructor
51 public class AutomationCompositionOutHandler {
52     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionOutHandler.class);
53
54     private final ParticipantMessagePublisher publisher;
55     private final CacheProvider cacheProvider;
56
57     /**
58      * Handle a automation composition element state change message.
59      *
60      * @param automationCompositionId the automationComposition Id
61      * @param elementId the automationComposition Element Id
62      * @param deployState the DeployState state
63      * @param lockState the LockState state
64      * @param message the message
65      * @param stateChangeResult the indicator if error occurs
66      */
67     public void updateAutomationCompositionElementState(UUID automationCompositionId, UUID elementId,
68             DeployState deployState, LockState lockState, StateChangeResult stateChangeResult, String message) {
69
70         if (automationCompositionId == null || elementId == null) {
71             LOGGER.error("Cannot update Automation composition element state, id is null");
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 ((element.getRestarting() == null)
90                 && ((deployState != null && lockState != null) || (deployState == null && lockState == null))) {
91             LOGGER.error("state error {} and {} cannot be handled", deployState, lockState);
92             return;
93         }
94         element.setRestarting(null);
95
96         if (deployState != null) {
97             handleDeployState(automationComposition, element, deployState);
98         }
99         if (lockState != null) {
100             handleLockState(automationComposition, element, lockState);
101         }
102
103         var automationCompositionStateChangeAck =
104                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
105         automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId());
106         automationCompositionStateChangeAck.setMessage(message);
107         automationCompositionStateChangeAck.setResponseTo(cacheProvider.getMsgIdentification().get(element.getId()));
108         automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult);
109         automationCompositionStateChangeAck.setAutomationCompositionId(automationCompositionId);
110         automationCompositionStateChangeAck.getAutomationCompositionResultMap().put(element.getId(),
111                 new AcElementDeployAck(element.getDeployState(), element.getLockState(), element.getOperationalState(),
112                         element.getUseState(), element.getOutProperties(), true, message));
113         LOGGER.debug("Automation composition element {} state changed to {}", elementId, deployState);
114         automationCompositionStateChangeAck.setResult(true);
115         publisher.sendAutomationCompositionAck(automationCompositionStateChangeAck);
116         cacheProvider.getMsgIdentification().remove(element.getId());
117     }
118
119     private void handleDeployState(AutomationComposition automationComposition, AutomationCompositionElement element,
120             DeployState deployState) {
121         element.setDeployState(deployState);
122         element.setLockState(DeployState.DEPLOYED.equals(element.getDeployState()) ? LockState.LOCKED : LockState.NONE);
123         var checkOpt = automationComposition.getElements().values().stream()
124                 .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny();
125         if (checkOpt.isEmpty()) {
126             automationComposition.setDeployState(deployState);
127             automationComposition.setLockState(element.getLockState());
128
129             if (DeployState.DELETED.equals(deployState)) {
130                 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
131             }
132         }
133     }
134
135     private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element,
136             LockState lockState) {
137         element.setLockState(lockState);
138         var checkOpt = automationComposition.getElements().values().stream()
139                 .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
140         if (checkOpt.isEmpty()) {
141             automationComposition.setLockState(lockState);
142         }
143     }
144
145     /**
146      * Send Ac Element Info.
147      *
148      * @param automationCompositionId the automationComposition Id
149      * @param elementId the automationComposition Element id
150      * @param useState the use State
151      * @param operationalState the operational State
152      * @param outProperties the output Properties Map
153      */
154     public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState,
155             String operationalState, Map<String, Object> outProperties) {
156
157         if (automationCompositionId == null || elementId == null) {
158             LOGGER.error("Cannot update Automation composition element state, id is null");
159             return;
160         }
161
162         var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
163         if (automationComposition == null) {
164             LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
165                     automationCompositionId);
166             return;
167         }
168
169         var element = automationComposition.getElements().get(elementId);
170         if (element == null) {
171             var msg = "Cannot update Automation composition element state, AC Element id {} not present";
172             LOGGER.error(msg, elementId);
173             return;
174         }
175         element.setOperationalState(operationalState);
176         element.setUseState(useState);
177         element.setOutProperties(outProperties);
178
179         var acInfo = new AutomationCompositionInfo();
180         acInfo.setAutomationCompositionId(automationCompositionId);
181         acInfo.setDeployState(automationComposition.getDeployState());
182         acInfo.setLockState(automationComposition.getLockState());
183         acInfo.setElements(List.of(getAutomationCompositionElementInfo(element)));
184         var statusMsg = createParticipantStatus();
185         statusMsg.setCompositionId(automationComposition.getCompositionId());
186         statusMsg.setAutomationCompositionInfoList(List.of(acInfo));
187         publisher.sendParticipantStatus(statusMsg);
188     }
189
190     /**
191      * Get AutomationCompositionElementInfo from AutomationCompositionElement.
192      *
193      * @param element the AutomationCompositionElement
194      * @return the AutomationCompositionElementInfo
195      */
196     public AutomationCompositionElementInfo getAutomationCompositionElementInfo(AutomationCompositionElement element) {
197         var elementInfo = new AutomationCompositionElementInfo();
198         elementInfo.setAutomationCompositionElementId(element.getId());
199         elementInfo.setDeployState(element.getDeployState());
200         elementInfo.setLockState(element.getLockState());
201         elementInfo.setOperationalState(element.getOperationalState());
202         elementInfo.setUseState(element.getUseState());
203         elementInfo.setOutProperties(element.getOutProperties());
204         return elementInfo;
205     }
206
207     /**
208      * Update Composition State for prime and deprime.
209      *
210      * @param compositionId the composition id
211      * @param state the Composition State
212      * @param stateChangeResult the indicator if error occurs
213      * @param message the message
214      */
215     public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
216             String message) {
217         var participantPrimeAck = new ParticipantPrimeAck();
218         participantPrimeAck.setCompositionId(compositionId);
219         participantPrimeAck.setMessage(message);
220         participantPrimeAck.setResult(true);
221         participantPrimeAck.setResponseTo(cacheProvider.getMsgIdentification().get(compositionId));
222         participantPrimeAck.setCompositionState(state);
223         participantPrimeAck.setStateChangeResult(stateChangeResult);
224         participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
225         participantPrimeAck.setState(ParticipantState.ON_LINE);
226         publisher.sendParticipantPrimeAck(participantPrimeAck);
227         cacheProvider.getMsgIdentification().remove(compositionId);
228     }
229
230     /**
231      * Send Composition Definition Info.
232      *
233      * @param compositionId the composition id
234      * @param elementId the Composition Definition Element id
235      * @param outProperties the output Properties Map
236      */
237     public void sendAcDefinitionInfo(UUID compositionId, ToscaConceptIdentifier elementId,
238             Map<String, Object> outProperties) {
239         if (compositionId == null) {
240             LOGGER.error("Cannot send Composition outProperties, id is null");
241             return;
242         }
243         var statusMsg = createParticipantStatus();
244         statusMsg.setCompositionId(compositionId);
245         var acElementDefsMap = cacheProvider.getAcElementsDefinitions();
246         var acElementsDefinitions = acElementDefsMap.get(compositionId);
247         if (acElementsDefinitions == null) {
248             LOGGER.error("Cannot send Composition outProperties, id {} is null", compositionId);
249             return;
250         }
251         var acElementDefinition = getAutomationCompositionElementDefinition(acElementsDefinitions, elementId);
252         if (acElementDefinition == null) {
253             LOGGER.error("Cannot send Composition outProperties, elementId {} not present", elementId);
254             return;
255         }
256         acElementDefinition.setOutProperties(outProperties);
257         var participantDefinition = new ParticipantDefinition();
258         participantDefinition.setParticipantId(cacheProvider.getParticipantId());
259         participantDefinition.setAutomationCompositionElementDefinitionList(List.of(acElementDefinition));
260         statusMsg.setParticipantDefinitionUpdates(List.of(participantDefinition));
261         publisher.sendHeartbeat(statusMsg);
262     }
263
264     private AutomationCompositionElementDefinition getAutomationCompositionElementDefinition(
265             Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> acElementsDefinition,
266             ToscaConceptIdentifier elementId) {
267
268         if (elementId == null) {
269             if (acElementsDefinition.size() == 1) {
270                 return acElementsDefinition.values().iterator().next();
271             }
272             return null;
273         }
274         return acElementsDefinition.get(elementId);
275     }
276
277     private ParticipantStatus createParticipantStatus() {
278         var statusMsg = new ParticipantStatus();
279         statusMsg.setParticipantId(cacheProvider.getParticipantId());
280         statusMsg.setState(ParticipantState.ON_LINE);
281         statusMsg.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
282         return statusMsg;
283     }
284 }