2d845d4c871db6e5e4a389ece1e57ef0a287ffe2
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.ArrayList;
25 import java.util.HashMap;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30 import lombok.Getter;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
32 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
33 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
35 import org.onap.policy.clamp.models.acm.concepts.AcElementDeployAck;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
42 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
47 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
48 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
49 import org.onap.policy.clamp.models.acm.persistence.provider.AcInstanceStateResolver;
50 import org.onap.policy.models.base.PfModelException;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.stereotype.Component;
56
57 /*
58  * This class is responsible for managing the state of all automation compositions in the participant.
59  */
60 @Component
61 public class AutomationCompositionHandler {
62     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionHandler.class);
63
64     private final UUID participantId;
65     private final ParticipantMessagePublisher publisher;
66     private final AcInstanceStateResolver acInstanceStateResolver;
67
68     @Getter
69     private final Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
70
71     @Getter
72     private final Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
73
74     @Getter
75     private final List<AutomationCompositionElementListener> listeners = new ArrayList<>();
76
77     /**
78      * Constructor, set the participant ID and messageSender.
79      *
80      * @param parameters the parameters of the participant
81      * @param publisher the ParticipantMessage Publisher
82      */
83     public AutomationCompositionHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher) {
84         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
85         this.publisher = publisher;
86         this.acInstanceStateResolver = new AcInstanceStateResolver();
87     }
88
89     public void registerAutomationCompositionElementListener(AutomationCompositionElementListener listener) {
90         listeners.add(listener);
91     }
92
93     /**
94      * Handle a automation composition element state change message.
95      *
96      * @param automationCompositionId the automationComposition Id
97      * @param id the automationComposition UUID
98      * @param deployState the DeployState state
99      */
100     public void updateAutomationCompositionElementState(UUID automationCompositionId, UUID id, DeployState deployState,
101             LockState lockState) {
102
103         if (id == null) {
104             LOGGER.warn("Cannot update Automation composition element state, id is null");
105             return;
106         }
107
108         // Update states of AutomationCompositionElement in automationCompositionMap
109         for (var automationComposition : automationCompositionMap.values()) {
110             var element = automationComposition.getElements().get(id);
111             if (element != null) {
112                 element.setDeployState(deployState);
113                 element.setLockState(lockState);
114             }
115             var checkOpt = automationComposition.getElements().values().stream()
116                     .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny();
117             if (checkOpt.isEmpty()) {
118                 automationComposition.setDeployState(deployState);
119             }
120             checkOpt = automationComposition.getElements().values().stream()
121                     .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
122             if (checkOpt.isEmpty()) {
123                 automationComposition.setLockState(lockState);
124             }
125         }
126
127         // Update states of AutomationCompositionElement in elementsOnThisParticipant
128         var acElement = elementsOnThisParticipant.get(id);
129         if (acElement != null) {
130             var automationCompositionStateChangeAck =
131                     new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
132             automationCompositionStateChangeAck.setParticipantId(participantId);
133             automationCompositionStateChangeAck.setAutomationCompositionId(automationCompositionId);
134             acElement.setDeployState(deployState);
135             acElement.setLockState(lockState);
136             automationCompositionStateChangeAck.getAutomationCompositionResultMap().put(acElement.getId(),
137                     new AcElementDeployAck(deployState, lockState, true,
138                             "Automation composition element {} state changed to {}\", id, newState)"));
139             LOGGER.debug("Automation composition element {} state changed to {}", id, deployState);
140             automationCompositionStateChangeAck
141                     .setMessage("AutomationCompositionElement state changed to {} " + deployState);
142             automationCompositionStateChangeAck.setResult(true);
143             publisher.sendAutomationCompositionAck(automationCompositionStateChangeAck);
144         }
145     }
146
147     /**
148      * Handle a automation composition state change message.
149      *
150      * @param stateChangeMsg the state change message
151      * @param acElementDefinitions the list of AutomationCompositionElementDefinition
152      */
153     public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg,
154             List<AutomationCompositionElementDefinition> acElementDefinitions) {
155         if (stateChangeMsg.getAutomationCompositionId() == null) {
156             return;
157         }
158
159         var automationComposition = automationCompositionMap.get(stateChangeMsg.getAutomationCompositionId());
160
161         if (automationComposition == null) {
162             var automationCompositionAck =
163                     new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
164             automationCompositionAck.setParticipantId(participantId);
165             automationCompositionAck.setMessage("Automation composition " + stateChangeMsg.getAutomationCompositionId()
166                     + " does not use this participant " + participantId);
167             automationCompositionAck.setResult(false);
168             automationCompositionAck.setResponseTo(stateChangeMsg.getMessageId());
169             automationCompositionAck.setAutomationCompositionId(stateChangeMsg.getAutomationCompositionId());
170             publisher.sendAutomationCompositionAck(automationCompositionAck);
171             LOGGER.debug("Automation composition {} does not use this participant",
172                     stateChangeMsg.getAutomationCompositionId());
173             return;
174         }
175
176         if (!checkConsistantOrderState(automationComposition, stateChangeMsg.getDeployOrderedState(),
177                 stateChangeMsg.getLockOrderedState())) {
178             var automationCompositionAck =
179                     new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
180             automationCompositionAck.setParticipantId(participantId);
181             automationCompositionAck.setMessage("Automation composition is already in state "
182                     + stateChangeMsg.getDeployOrderedState() + " and " + stateChangeMsg.getLockOrderedState());
183             automationCompositionAck.setResult(false);
184             automationCompositionAck.setAutomationCompositionId(automationComposition.getInstanceId());
185             publisher.sendAutomationCompositionAck(automationCompositionAck);
186             return;
187         }
188
189         if (DeployOrder.NONE.equals(stateChangeMsg.getDeployOrderedState())) {
190             handleLockOrderState(automationComposition, stateChangeMsg.getLockOrderedState(),
191                     stateChangeMsg.getStartPhase(), acElementDefinitions);
192         } else {
193             handleDeployOrderState(automationComposition, stateChangeMsg.getDeployOrderedState(),
194                     stateChangeMsg.getStartPhase(), acElementDefinitions);
195         }
196     }
197
198     private boolean checkConsistantOrderState(AutomationComposition automationComposition, DeployOrder deployOrder,
199             LockOrder lockOrder) {
200         return acInstanceStateResolver.resolve(deployOrder, lockOrder, automationComposition.getDeployState(),
201                 automationComposition.getLockState()) != null;
202     }
203
204     /**
205      * Method to handle state changes.
206      *
207      * @param automationComposition participant response
208      * @param orderedState automation composition ordered state
209      * @param startPhaseMsg startPhase from message
210      * @param acElementDefinitions the list of AutomationCompositionElementDefinition
211      */
212     private void handleDeployOrderState(final AutomationComposition automationComposition, DeployOrder orderedState,
213             Integer startPhaseMsg, List<AutomationCompositionElementDefinition> acElementDefinitions) {
214
215         if (DeployOrder.UNDEPLOY.equals(orderedState)) {
216             handleUndeployState(automationComposition, startPhaseMsg, acElementDefinitions);
217         } else {
218             LOGGER.debug("StateChange message has no state, state is null {}", automationComposition.getKey());
219         }
220     }
221
222     /**
223      * Method to handle state changes.
224      *
225      * @param automationComposition participant response
226      * @param orderedState automation composition ordered state
227      * @param startPhaseMsg startPhase from message
228      * @param acElementDefinitions the list of AutomationCompositionElementDefinition
229      */
230     private void handleLockOrderState(final AutomationComposition automationComposition, LockOrder orderedState,
231             Integer startPhaseMsg, List<AutomationCompositionElementDefinition> acElementDefinitions) {
232
233         switch (orderedState) {
234             case LOCK:
235                 handleLockState(automationComposition, startPhaseMsg, acElementDefinitions);
236                 break;
237             case UNLOCK:
238                 handleUnlockState(automationComposition, startPhaseMsg, acElementDefinitions);
239                 break;
240             default:
241                 LOGGER.debug("StateChange message has no state, state is null {}", automationComposition.getKey());
242                 break;
243         }
244     }
245
246     /**
247      * Handle a automation composition Deploy message.
248      *
249      * @param updateMsg the Deploy message
250      * @param acElementDefinitions the list of AutomationCompositionElementDefinition
251      */
252     public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg,
253             List<AutomationCompositionElementDefinition> acElementDefinitions) {
254
255         if (updateMsg.getParticipantUpdatesList().isEmpty()) {
256             LOGGER.warn("No AutomationCompositionElement updates in message {}",
257                     updateMsg.getAutomationCompositionId());
258             return;
259         }
260
261         for (var participantDeploy : updateMsg.getParticipantUpdatesList()) {
262             if (participantId.equals(participantDeploy.getParticipantId())) {
263                 if (updateMsg.isFirstStartPhase()) {
264                     initializeDeploy(updateMsg.getMessageId(), updateMsg.getAutomationCompositionId(),
265                             participantDeploy);
266                 }
267                 callParticipanDeploy(participantDeploy.getAcElementList(), acElementDefinitions,
268                         updateMsg.getStartPhase(), updateMsg.getAutomationCompositionId());
269             }
270         }
271     }
272
273     private void initializeDeploy(UUID messageId, UUID instanceId, ParticipantDeploy participantDeploy) {
274         var automationComposition = automationCompositionMap.get(instanceId);
275
276         if (automationComposition != null) {
277             var automationCompositionUpdateAck =
278                     new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY_ACK);
279             automationCompositionUpdateAck.setParticipantId(participantId);
280
281             automationCompositionUpdateAck.setMessage(
282                     "Automation composition " + instanceId + " already defined on participant " + participantId);
283             automationCompositionUpdateAck.setResult(false);
284             automationCompositionUpdateAck.setResponseTo(messageId);
285             automationCompositionUpdateAck.setAutomationCompositionId(instanceId);
286             publisher.sendAutomationCompositionAck(automationCompositionUpdateAck);
287             return;
288         }
289
290         automationComposition = new AutomationComposition();
291         automationComposition.setInstanceId(instanceId);
292         var acElements = storeElementsOnThisParticipant(participantDeploy);
293         automationComposition.setElements(prepareAcElementMap(acElements));
294         automationCompositionMap.put(instanceId, automationComposition);
295     }
296
297     private void callParticipanDeploy(List<AcElementDeploy> acElements,
298             List<AutomationCompositionElementDefinition> acElementDefinitions, Integer startPhaseMsg,
299             UUID automationCompositionId) {
300         try {
301             for (var element : acElements) {
302                 var acElementNodeTemplate = getAcElementNodeTemplate(acElementDefinitions, element.getDefinition());
303                 if (acElementNodeTemplate != null) {
304                     int startPhase = ParticipantUtils.findStartPhase(acElementNodeTemplate.getProperties());
305                     if (startPhaseMsg.equals(startPhase)) {
306                         for (var acElementListener : listeners) {
307                             var map = new HashMap<>(acElementNodeTemplate.getProperties());
308                             map.putAll(element.getProperties());
309                             acElementListener.deploy(automationCompositionId, element, map);
310                         }
311                     }
312                 }
313             }
314         } catch (PfModelException e) {
315             LOGGER.debug("Automation composition element update failed {}", automationCompositionId);
316         }
317
318     }
319
320     private ToscaNodeTemplate getAcElementNodeTemplate(
321             List<AutomationCompositionElementDefinition> acElementDefinitions, ToscaConceptIdentifier acElementDefId) {
322
323         for (var acElementDefinition : acElementDefinitions) {
324             if (acElementDefId.getName().contains(acElementDefinition.getAcElementDefinitionId().getName())) {
325                 return acElementDefinition.getAutomationCompositionElementToscaNodeTemplate();
326             }
327         }
328         return null;
329     }
330
331     private List<AutomationCompositionElement> storeElementsOnThisParticipant(ParticipantDeploy participantDeploy) {
332         List<AutomationCompositionElement> acElementList = new ArrayList<>();
333         for (var element : participantDeploy.getAcElementList()) {
334             var acElement = new AutomationCompositionElement();
335             acElement.setId(element.getId());
336             acElement.setDefinition(element.getDefinition());
337             acElement.setDeployState(DeployState.DEPLOYING);
338             acElement.setLockState(LockState.NONE);
339             elementsOnThisParticipant.put(element.getId(), acElement);
340             acElementList.add(acElement);
341         }
342         return acElementList;
343     }
344
345     private Map<UUID, AutomationCompositionElement> prepareAcElementMap(List<AutomationCompositionElement> acElements) {
346         Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
347         for (var element : acElements) {
348             acElementMap.put(element.getId(), element);
349         }
350         return acElementMap;
351     }
352
353     /**
354      * Method to handle when the new state from participant is UNINITIALISED state.
355      *
356      * @param automationComposition participant response
357      * @param startPhaseMsg startPhase from message
358      * @param acElementDefinitions the list of AutomationCompositionElementDefinition
359      */
360     private void handleUndeployState(final AutomationComposition automationComposition, Integer startPhaseMsg,
361             List<AutomationCompositionElementDefinition> acElementDefinitions) {
362
363         automationComposition.getElements().values().stream()
364                 .forEach(acElement -> automationCompositionElementUndeploy(automationComposition.getInstanceId(),
365                         acElement, startPhaseMsg, acElementDefinitions));
366
367         boolean isAllUninitialised = automationComposition.getElements().values().stream()
368                 .filter(element -> !DeployState.UNDEPLOYED.equals(element.getDeployState())).findAny().isEmpty();
369         if (isAllUninitialised) {
370             automationCompositionMap.remove(automationComposition.getInstanceId());
371             automationComposition.getElements().values()
372                     .forEach(element -> elementsOnThisParticipant.remove(element.getId()));
373         }
374     }
375
376     /**
377      * Method to handle when the new state from participant is PASSIVE state.
378      *
379      * @param automationComposition participant response
380      * @param startPhaseMsg startPhase from message
381      * @param acElementDefinitions the list of AutomationCompositionElementDefinition
382      */
383     private void handleLockState(final AutomationComposition automationComposition, Integer startPhaseMsg,
384             List<AutomationCompositionElementDefinition> acElementDefinitions) {
385         automationComposition.getElements().values().stream()
386                 .forEach(acElement -> automationCompositionElementLock(automationComposition.getInstanceId(), acElement,
387                         startPhaseMsg, acElementDefinitions));
388     }
389
390     /**
391      * Method to handle when the new state from participant is RUNNING state.
392      *
393      * @param automationComposition participant response
394      * @param startPhaseMsg startPhase from message
395      * @param acElementDefinitions the list of AutomationCompositionElementDefinition
396      */
397     private void handleUnlockState(final AutomationComposition automationComposition, Integer startPhaseMsg,
398             List<AutomationCompositionElementDefinition> acElementDefinitions) {
399         automationComposition.getElements().values().stream()
400                 .forEach(acElement -> automationCompositionElementUnlock(automationComposition.getInstanceId(),
401                         acElement, startPhaseMsg, acElementDefinitions));
402     }
403
404     private void automationCompositionElementLock(UUID instanceId, AutomationCompositionElement acElement,
405             Integer startPhaseMsg, List<AutomationCompositionElementDefinition> acElementDefinitions) {
406         var acElementNodeTemplate = getAcElementNodeTemplate(acElementDefinitions, acElement.getDefinition());
407         if (acElementNodeTemplate != null) {
408             int startPhase = ParticipantUtils.findStartPhase(acElementNodeTemplate.getProperties());
409             if (startPhaseMsg.equals(startPhase)) {
410                 updateAutomationCompositionElementState(instanceId, acElement.getId(), DeployState.DEPLOYED,
411                         LockState.LOCKED);
412             }
413         }
414     }
415
416     private void automationCompositionElementUnlock(UUID instanceId, AutomationCompositionElement acElement,
417             Integer startPhaseMsg, List<AutomationCompositionElementDefinition> acElementDefinitions) {
418         var acElementNodeTemplate = getAcElementNodeTemplate(acElementDefinitions, acElement.getDefinition());
419         if (acElementNodeTemplate != null) {
420             int startPhase = ParticipantUtils.findStartPhase(acElementNodeTemplate.getProperties());
421             if (startPhaseMsg.equals(startPhase)) {
422                 updateAutomationCompositionElementState(instanceId, acElement.getId(), DeployState.DEPLOYED,
423                         LockState.UNLOCKED);
424             }
425         }
426     }
427
428     private void automationCompositionElementUndeploy(UUID instanceId, AutomationCompositionElement acElement,
429             Integer startPhaseMsg, List<AutomationCompositionElementDefinition> acElementDefinitions) {
430         var acElementNodeTemplate = getAcElementNodeTemplate(acElementDefinitions, acElement.getDefinition());
431         if (acElementNodeTemplate != null) {
432             int startPhase = ParticipantUtils.findStartPhase(acElementNodeTemplate.getProperties());
433             if (startPhaseMsg.equals(startPhase)) {
434                 for (var acElementListener : listeners) {
435                     try {
436                         acElementListener.undeploy(instanceId, acElement.getId());
437                     } catch (PfModelException e) {
438                         LOGGER.debug("Automation composition element update failed {}", instanceId);
439                     }
440                 }
441             }
442         }
443     }
444 }