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
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.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;
47 @RequiredArgsConstructor
48 public class AutomationCompositionOutHandler {
49 private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionOutHandler.class);
51 private final ParticipantMessagePublisher publisher;
52 private final CacheProvider cacheProvider;
55 * Handle a automation composition element state change message.
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
64 public void updateAutomationCompositionElementState(UUID automationCompositionId, UUID elementId,
65 DeployState deployState, LockState lockState, StateChangeResult stateChangeResult, String message) {
67 if (automationCompositionId == null || elementId == null) {
68 LOGGER.error("Cannot update Automation composition element state, id is null");
72 if ((deployState != null && lockState != null) || (deployState == null && lockState == null)) {
73 LOGGER.error("state error {} and {} cannot be handled", deployState, lockState);
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);
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);
91 if (deployState != null) {
92 handleDeployState(automationComposition, element, deployState);
94 if (lockState != null) {
95 handleLockState(automationComposition, element, lockState);
98 var automationCompositionStateChangeAck =
99 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
100 automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId());
101 automationCompositionStateChangeAck.setMessage(message);
102 automationCompositionStateChangeAck.setResponseTo(cacheProvider.getMsgIdentification().get(element.getId()));
103 automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult);
104 automationCompositionStateChangeAck.setAutomationCompositionId(automationCompositionId);
105 automationCompositionStateChangeAck.getAutomationCompositionResultMap().put(element.getId(),
106 new AcElementDeployAck(element.getDeployState(), element.getLockState(), element.getOperationalState(),
107 element.getUseState(), element.getOutProperties(), true, message));
108 LOGGER.debug("Automation composition element {} state changed to {}", elementId, deployState);
109 automationCompositionStateChangeAck.setResult(true);
110 publisher.sendAutomationCompositionAck(automationCompositionStateChangeAck);
111 cacheProvider.getMsgIdentification().remove(element.getId());
114 private void handleDeployState(AutomationComposition automationComposition, AutomationCompositionElement element,
115 DeployState deployState) {
116 element.setDeployState(deployState);
117 element.setLockState(DeployState.DEPLOYED.equals(element.getDeployState()) ? LockState.LOCKED : LockState.NONE);
118 var checkOpt = automationComposition.getElements().values().stream()
119 .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny();
120 if (checkOpt.isEmpty()) {
121 automationComposition.setDeployState(deployState);
122 automationComposition.setLockState(element.getLockState());
124 if (DeployState.DELETED.equals(deployState)) {
125 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
130 private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element,
131 LockState lockState) {
132 element.setLockState(lockState);
133 var checkOpt = automationComposition.getElements().values().stream()
134 .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
135 if (checkOpt.isEmpty()) {
136 automationComposition.setLockState(lockState);
141 * Send Ac Element Info.
143 * @param automationCompositionId the automationComposition Id
144 * @param elementId the automationComposition Element id
145 * @param useState the use State
146 * @param operationalState the operational State
147 * @param outProperties the output Properties Map
149 public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState,
150 String operationalState, Map<String, Object> outProperties) {
152 if (automationCompositionId == null || elementId == null) {
153 LOGGER.error("Cannot update Automation composition element state, id is null");
157 var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
158 if (automationComposition == null) {
159 LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
160 automationComposition);
164 var element = automationComposition.getElements().get(elementId);
165 if (element == null) {
166 var msg = "Cannot update Automation composition element state, AC Element id {} not present";
167 LOGGER.error(msg, automationComposition);
170 element.setOperationalState(operationalState);
171 element.setUseState(useState);
172 element.setOutProperties(outProperties);
174 var statusMsg = new ParticipantStatus();
175 statusMsg.setParticipantId(cacheProvider.getParticipantId());
176 statusMsg.setState(ParticipantState.ON_LINE);
177 statusMsg.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
178 var acInfo = new AutomationCompositionInfo();
179 acInfo.setAutomationCompositionId(automationCompositionId);
180 acInfo.setDeployState(automationComposition.getDeployState());
181 acInfo.setLockState(automationComposition.getLockState());
182 acInfo.setElements(List.of(getAutomationCompositionElementInfo(element)));
183 statusMsg.setAutomationCompositionInfoList(List.of(acInfo));
184 publisher.sendParticipantStatus(statusMsg);
188 * Get AutomationCompositionElementInfo from AutomationCompositionElement.
190 * @param element the AutomationCompositionElement
191 * @return the AutomationCompositionElementInfo
193 public AutomationCompositionElementInfo getAutomationCompositionElementInfo(AutomationCompositionElement element) {
194 var elementInfo = new AutomationCompositionElementInfo();
195 elementInfo.setAutomationCompositionElementId(element.getId());
196 elementInfo.setDeployState(element.getDeployState());
197 elementInfo.setLockState(element.getLockState());
198 elementInfo.setOperationalState(element.getOperationalState());
199 elementInfo.setUseState(element.getUseState());
200 elementInfo.setOutProperties(element.getOutProperties());
205 * Update Composition State for prime and deprime.
207 * @param compositionId the composition id
208 * @param state the Composition State
209 * @param stateChangeResult the indicator if error occurs
210 * @param message the message
212 public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
214 var participantPrimeAck = new ParticipantPrimeAck();
215 participantPrimeAck.setCompositionId(compositionId);
216 participantPrimeAck.setMessage(message);
217 participantPrimeAck.setResult(true);
218 participantPrimeAck.setResponseTo(cacheProvider.getMsgIdentification().get(compositionId));
219 participantPrimeAck.setCompositionState(state);
220 participantPrimeAck.setStateChangeResult(stateChangeResult);
221 participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
222 participantPrimeAck.setState(ParticipantState.ON_LINE);
223 publisher.sendParticipantPrimeAck(participantPrimeAck);
224 cacheProvider.getMsgIdentification().remove(compositionId);