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 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);
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);
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);
91 element.setRestarting(null);
93 if (deployState != null) {
94 handleDeployState(automationComposition, element, deployState);
96 if (lockState != null) {
97 handleLockState(automationComposition, element, lockState);
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());
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());
126 if (DeployState.DELETED.equals(deployState)) {
127 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
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);
143 * Send Ac Element Info.
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
151 public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState,
152 String operationalState, Map<String, Object> outProperties) {
154 if (automationCompositionId == null || elementId == null) {
155 LOGGER.error("Cannot update Automation composition element state, id is null");
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);
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);
172 element.setOperationalState(operationalState);
173 element.setUseState(useState);
174 element.setOutProperties(outProperties);
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);
190 * Get AutomationCompositionElementInfo from AutomationCompositionElement.
192 * @param element the AutomationCompositionElement
193 * @return the AutomationCompositionElementInfo
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());
207 * Update Composition State for prime and deprime.
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
214 public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
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);