334d6a89d4147366d266d5d56a0a9aec7a3c36c3
[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         var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
73         if (automationComposition == null) {
74             LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
75                     automationCompositionId);
76             return;
77         }
78
79         var element = automationComposition.getElements().get(elementId);
80         if (element == null) {
81             var msg = "Cannot update Automation composition element state, AC Element id {} not present";
82             LOGGER.error(msg, elementId);
83             return;
84         }
85
86         if ((element.getRestarting() != null)
87                 && ((deployState != null && lockState != null) || (deployState == null && lockState == null))) {
88             LOGGER.error("state error {} and {} cannot be handled", deployState, lockState);
89             return;
90         }
91         element.setRestarting(null);
92
93         if (deployState != null) {
94             handleDeployState(automationComposition, element, deployState);
95         }
96         if (lockState != null) {
97             handleLockState(automationComposition, element, lockState);
98         }
99
100         var automationCompositionStateChangeAck =
101                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
102         automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId());
103         automationCompositionStateChangeAck.setMessage(message);
104         automationCompositionStateChangeAck.setResponseTo(cacheProvider.getMsgIdentification().get(element.getId()));
105         automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult);
106         automationCompositionStateChangeAck.setAutomationCompositionId(automationCompositionId);
107         automationCompositionStateChangeAck.getAutomationCompositionResultMap().put(element.getId(),
108                 new AcElementDeployAck(element.getDeployState(), element.getLockState(), element.getOperationalState(),
109                         element.getUseState(), element.getOutProperties(), true, message));
110         LOGGER.debug("Automation composition element {} state changed to {}", elementId, deployState);
111         automationCompositionStateChangeAck.setResult(true);
112         publisher.sendAutomationCompositionAck(automationCompositionStateChangeAck);
113         cacheProvider.getMsgIdentification().remove(element.getId());
114     }
115
116     private void handleDeployState(AutomationComposition automationComposition, AutomationCompositionElement element,
117             DeployState deployState) {
118         element.setDeployState(deployState);
119         element.setLockState(DeployState.DEPLOYED.equals(element.getDeployState()) ? LockState.LOCKED : LockState.NONE);
120         var checkOpt = automationComposition.getElements().values().stream()
121                 .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny();
122         if (checkOpt.isEmpty()) {
123             automationComposition.setDeployState(deployState);
124             automationComposition.setLockState(element.getLockState());
125
126             if (DeployState.DELETED.equals(deployState)) {
127                 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
128             }
129         }
130     }
131
132     private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element,
133             LockState lockState) {
134         element.setLockState(lockState);
135         var checkOpt = automationComposition.getElements().values().stream()
136                 .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
137         if (checkOpt.isEmpty()) {
138             automationComposition.setLockState(lockState);
139         }
140     }
141
142     /**
143      * Send Ac Element Info.
144      *
145      * @param automationCompositionId the automationComposition Id
146      * @param elementId the automationComposition Element id
147      * @param useState the use State
148      * @param operationalState the operational State
149      * @param outProperties the output Properties Map
150      */
151     public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState,
152             String operationalState, Map<String, Object> outProperties) {
153
154         if (automationCompositionId == null || elementId == null) {
155             LOGGER.error("Cannot update Automation composition element state, id is null");
156             return;
157         }
158
159         var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
160         if (automationComposition == null) {
161             LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
162                     automationComposition);
163             return;
164         }
165
166         var element = automationComposition.getElements().get(elementId);
167         if (element == null) {
168             var msg = "Cannot update Automation composition element state, AC Element id {} not present";
169             LOGGER.error(msg, automationComposition);
170             return;
171         }
172         element.setOperationalState(operationalState);
173         element.setUseState(useState);
174         element.setOutProperties(outProperties);
175
176         var statusMsg = new ParticipantStatus();
177         statusMsg.setParticipantId(cacheProvider.getParticipantId());
178         statusMsg.setState(ParticipantState.ON_LINE);
179         statusMsg.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
180         var acInfo = new AutomationCompositionInfo();
181         acInfo.setAutomationCompositionId(automationCompositionId);
182         acInfo.setDeployState(automationComposition.getDeployState());
183         acInfo.setLockState(automationComposition.getLockState());
184         acInfo.setElements(List.of(getAutomationCompositionElementInfo(element)));
185         statusMsg.setAutomationCompositionInfoList(List.of(acInfo));
186         publisher.sendParticipantStatus(statusMsg);
187     }
188
189     /**
190      * Get AutomationCompositionElementInfo from AutomationCompositionElement.
191      *
192      * @param element the AutomationCompositionElement
193      * @return the AutomationCompositionElementInfo
194      */
195     public AutomationCompositionElementInfo getAutomationCompositionElementInfo(AutomationCompositionElement element) {
196         var elementInfo = new AutomationCompositionElementInfo();
197         elementInfo.setAutomationCompositionElementId(element.getId());
198         elementInfo.setDeployState(element.getDeployState());
199         elementInfo.setLockState(element.getLockState());
200         elementInfo.setOperationalState(element.getOperationalState());
201         elementInfo.setUseState(element.getUseState());
202         elementInfo.setOutProperties(element.getOutProperties());
203         return elementInfo;
204     }
205
206     /**
207      * Update Composition State for prime and deprime.
208      *
209      * @param compositionId the composition id
210      * @param state the Composition State
211      * @param stateChangeResult the indicator if error occurs
212      * @param message the message
213      */
214     public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
215             String message) {
216         var participantPrimeAck = new ParticipantPrimeAck();
217         participantPrimeAck.setCompositionId(compositionId);
218         participantPrimeAck.setMessage(message);
219         participantPrimeAck.setResult(true);
220         participantPrimeAck.setResponseTo(cacheProvider.getMsgIdentification().get(compositionId));
221         participantPrimeAck.setCompositionState(state);
222         participantPrimeAck.setStateChangeResult(stateChangeResult);
223         participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
224         participantPrimeAck.setState(ParticipantState.ON_LINE);
225         publisher.sendParticipantPrimeAck(participantPrimeAck);
226         cacheProvider.getMsgIdentification().remove(compositionId);
227     }
228 }