2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2024 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
23 import java.util.List;
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.kafka.participant.AutomationCompositionDeployAck;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
43 import org.onap.policy.clamp.models.acm.messages.kafka.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;
50 @RequiredArgsConstructor
51 public class AutomationCompositionOutHandler {
52 private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionOutHandler.class);
54 private final ParticipantMessagePublisher publisher;
55 private final CacheProvider cacheProvider;
58 * Handle a automation composition element state change message.
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
67 public void updateAutomationCompositionElementState(UUID automationCompositionId, UUID elementId,
68 DeployState deployState, LockState lockState, StateChangeResult stateChangeResult, String message) {
70 if (automationCompositionId == null || elementId == null) {
71 LOGGER.error("Cannot update Automation composition element state, id is null");
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);
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);
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);
94 element.setRestarting(null);
96 if (deployState != null) {
97 handleDeployState(automationComposition, element, deployState);
99 if (lockState != null) {
100 handleLockState(automationComposition, element, lockState);
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());
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 if (DeployState.DEPLOYED.equals(automationComposition.getDeployState())
127 && automationComposition.getCompositionTargetId() != null) {
128 // migration scenario
129 automationComposition.setCompositionId(automationComposition.getCompositionTargetId());
130 automationComposition.setCompositionTargetId(null);
132 automationComposition.setDeployState(deployState);
133 automationComposition.setLockState(element.getLockState());
135 if (DeployState.DELETED.equals(deployState)) {
136 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
141 private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element,
142 LockState lockState) {
143 element.setLockState(lockState);
144 var checkOpt = automationComposition.getElements().values().stream()
145 .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
146 if (checkOpt.isEmpty()) {
147 automationComposition.setLockState(lockState);
152 * Send Ac Element Info.
154 * @param automationCompositionId the automationComposition Id
155 * @param elementId the automationComposition Element id
156 * @param useState the use State
157 * @param operationalState the operational State
158 * @param outProperties the output Properties Map
160 public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState,
161 String operationalState, Map<String, Object> outProperties) {
163 if (automationCompositionId == null || elementId == null) {
164 LOGGER.error("Cannot update Automation composition element state, id is null");
168 var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
169 if (automationComposition == null) {
170 LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
171 automationCompositionId);
175 var element = automationComposition.getElements().get(elementId);
176 if (element == null) {
177 var msg = "Cannot update Automation composition element state, AC Element id {} not present";
178 LOGGER.error(msg, elementId);
181 element.setOperationalState(operationalState);
182 element.setUseState(useState);
183 element.setOutProperties(outProperties);
185 var acInfo = new AutomationCompositionInfo();
186 acInfo.setAutomationCompositionId(automationCompositionId);
187 acInfo.setDeployState(automationComposition.getDeployState());
188 acInfo.setLockState(automationComposition.getLockState());
189 acInfo.setElements(List.of(getAutomationCompositionElementInfo(element)));
190 var statusMsg = createParticipantStatus();
191 statusMsg.setCompositionId(automationComposition.getCompositionId());
192 statusMsg.setAutomationCompositionInfoList(List.of(acInfo));
193 publisher.sendParticipantStatus(statusMsg);
197 * Get AutomationCompositionElementInfo from AutomationCompositionElement.
199 * @param element the AutomationCompositionElement
200 * @return the AutomationCompositionElementInfo
202 public AutomationCompositionElementInfo getAutomationCompositionElementInfo(AutomationCompositionElement element) {
203 var elementInfo = new AutomationCompositionElementInfo();
204 elementInfo.setAutomationCompositionElementId(element.getId());
205 elementInfo.setDeployState(element.getDeployState());
206 elementInfo.setLockState(element.getLockState());
207 elementInfo.setOperationalState(element.getOperationalState());
208 elementInfo.setUseState(element.getUseState());
209 elementInfo.setOutProperties(element.getOutProperties());
214 * Update Composition State for prime and deprime.
216 * @param compositionId the composition id
217 * @param state the Composition State
218 * @param stateChangeResult the indicator if error occurs
219 * @param message the message
221 public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
223 var participantPrimeAck = new ParticipantPrimeAck();
224 participantPrimeAck.setCompositionId(compositionId);
225 participantPrimeAck.setMessage(message);
226 participantPrimeAck.setResult(true);
227 participantPrimeAck.setResponseTo(cacheProvider.getMsgIdentification().get(compositionId));
228 participantPrimeAck.setCompositionState(state);
229 participantPrimeAck.setStateChangeResult(stateChangeResult);
230 participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
231 participantPrimeAck.setState(ParticipantState.ON_LINE);
232 publisher.sendParticipantPrimeAck(participantPrimeAck);
233 cacheProvider.getMsgIdentification().remove(compositionId);
234 if (AcTypeState.COMMISSIONED.equals(state) && StateChangeResult.NO_ERROR.equals(stateChangeResult)) {
235 cacheProvider.removeElementDefinition(compositionId);
240 * Send Composition Definition Info.
242 * @param compositionId the composition id
243 * @param elementId the Composition Definition Element id
244 * @param outProperties the output Properties Map
246 public void sendAcDefinitionInfo(UUID compositionId, ToscaConceptIdentifier elementId,
247 Map<String, Object> outProperties) {
248 if (compositionId == null) {
249 LOGGER.error("Cannot send Composition outProperties, id is null");
252 var statusMsg = createParticipantStatus();
253 statusMsg.setCompositionId(compositionId);
254 var acElementDefsMap = cacheProvider.getAcElementsDefinitions();
255 var acElementsDefinitions = acElementDefsMap.get(compositionId);
256 if (acElementsDefinitions == null) {
257 LOGGER.error("Cannot send Composition outProperties, id {} is null", compositionId);
260 var acElementDefinition = getAutomationCompositionElementDefinition(acElementsDefinitions, elementId);
261 if (acElementDefinition == null) {
262 LOGGER.error("Cannot send Composition outProperties, elementId {} not present", elementId);
265 acElementDefinition.setOutProperties(outProperties);
266 var participantDefinition = new ParticipantDefinition();
267 participantDefinition.setParticipantId(cacheProvider.getParticipantId());
268 participantDefinition.setAutomationCompositionElementDefinitionList(List.of(acElementDefinition));
269 statusMsg.setParticipantDefinitionUpdates(List.of(participantDefinition));
270 publisher.sendParticipantStatus(statusMsg);
273 private AutomationCompositionElementDefinition getAutomationCompositionElementDefinition(
274 Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> acElementsDefinition,
275 ToscaConceptIdentifier elementId) {
277 if (elementId == null) {
278 if (acElementsDefinition.size() == 1) {
279 return acElementsDefinition.values().iterator().next();
283 return acElementsDefinition.get(elementId);
286 private ParticipantStatus createParticipantStatus() {
287 var statusMsg = new ParticipantStatus();
288 statusMsg.setParticipantId(cacheProvider.getParticipantId());
289 statusMsg.setState(ParticipantState.ON_LINE);
290 statusMsg.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());