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.sim.main.handler;
23 import java.lang.invoke.MethodHandles;
24 import java.util.ArrayList;
25 import java.util.List;
27 import java.util.UUID;
29 import lombok.RequiredArgsConstructor;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
33 import org.onap.policy.clamp.acm.participant.sim.model.InternalData;
34 import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas;
35 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.clamp.models.acm.concepts.LockState;
42 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
43 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.stereotype.Component;
51 * This class handles implementation of automationCompositionElement updates.
54 @RequiredArgsConstructor
55 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
57 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 private final ParticipantIntermediaryApi intermediaryApi;
63 private SimConfig config = new SimConfig();
66 * Callback method to handle an update on a automation composition element.
68 * @param automationCompositionId the automationComposition Id
69 * @param element the information on the automation composition element
70 * @param properties properties Map
71 * @throws PfModelException in case of a exception
74 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
75 throws PfModelException {
76 LOGGER.debug("deploy call");
78 if (!execution(config.getDeployTimerMs(), "Current Thread deploy is Interrupted during execution {}",
83 if (config.isDeploySuccess()) {
84 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
85 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
87 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
88 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
92 private boolean execution(int timeMs, String msg, UUID elementId) {
93 long endTime = System.currentTimeMillis() + timeMs;
94 while (System.currentTimeMillis() < endTime) {
96 if (Thread.currentThread().isInterrupted()) {
97 LOGGER.debug(msg, elementId);
101 } catch (InterruptedException e) {
102 LOGGER.debug(msg, elementId);
103 Thread.currentThread().interrupt();
111 * Handle a automation composition element state change.
113 * @param automationCompositionElementId the ID of the automation composition element
116 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
117 LOGGER.debug("undeploy call");
119 if (!execution(config.getUndeployTimerMs(), "Current Thread undeploy is Interrupted during execution {}",
120 automationCompositionElementId)) {
124 if (config.isUndeploySuccess()) {
125 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
126 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
129 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
130 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.FAILED,
136 public void lock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
137 LOGGER.debug("lock call");
139 if (!execution(config.getLockTimerMs(), "Current Thread lock is Interrupted during execution {}",
140 automationCompositionElementId)) {
144 if (config.isLockSuccess()) {
145 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
146 automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
148 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
149 automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
154 public void unlock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
155 LOGGER.debug("unlock call");
157 if (!execution(config.getUnlockTimerMs(), "Current Thread unlock is Interrupted during execution {}",
158 automationCompositionElementId)) {
162 if (config.isUnlockSuccess()) {
163 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
164 automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
166 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
167 automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
172 public void delete(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
173 LOGGER.debug("delete call");
175 if (!execution(config.getDeleteTimerMs(), "Current Thread delete is Interrupted during execution {}",
176 automationCompositionElementId)) {
180 if (config.isDeleteSuccess()) {
181 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
182 automationCompositionElementId, DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
184 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
185 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
191 public void update(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
192 throws PfModelException {
193 LOGGER.debug("update call");
195 if (!execution(config.getUpdateTimerMs(), "Current Thread update is Interrupted during execution {}",
200 if (config.isUpdateSuccess()) {
201 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
202 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
204 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
205 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
210 * Get AutomationComposition.
212 * @return the AutomationCompositions
214 public AutomationCompositions getAutomationCompositions() {
215 var result = new AutomationCompositions();
216 result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
223 * @param automationCompositionId the automationComposition Id
224 * @param elementId the automationComposition Element Id
225 * @param useState the useState
226 * @param operationalState the operationalState
227 * @param outProperties the outProperties
229 public void setOutProperties(UUID automationCompositionId, UUID elementId, String useState, String operationalState,
230 Map<String, Object> outProperties) {
231 intermediaryApi.sendAcElementInfo(automationCompositionId, elementId, useState, operationalState,
236 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
237 throws PfModelException {
238 LOGGER.debug("prime call");
240 if (!execution(config.getPrimeTimerMs(), "Current Thread prime is Interrupted during execution {}",
245 if (config.isPrimeSuccess()) {
246 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
249 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
255 public void deprime(UUID compositionId) throws PfModelException {
256 LOGGER.debug("deprime call");
258 if (!execution(config.getDeprimeTimerMs(), "Current Thread deprime is Interrupted during execution {}",
263 if (config.isDeprimeSuccess()) {
264 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
267 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
273 * Get Instance Data List.
275 * @return the InternalDatas
277 public InternalDatas getDataList() {
278 var result = new InternalDatas();
279 var map = intermediaryApi.getAutomationCompositions();
280 for (var instance : map.values()) {
281 for (var element : instance.getElements().values()) {
282 var data = new InternalData();
283 data.setAutomationCompositionId(instance.getInstanceId());
284 data.setAutomationCompositionElementId(element.getId());
285 data.setIntProperties(element.getProperties());
286 data.setOperationalState(element.getOperationalState());
287 data.setUseState(element.getUseState());
288 data.setOutProperties(element.getOutProperties());
289 result.getList().add(data);
296 public void handleRestartComposition(UUID compositionId,
297 List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state)
298 throws PfModelException {
299 LOGGER.debug("restart composition definition call");
302 prime(compositionId, elementDefinitionList);
306 deprime(compositionId);
310 intermediaryApi.updateCompositionState(compositionId, state, StateChangeResult.NO_ERROR, "Restarted");
315 public void handleRestartInstance(UUID automationCompositionId, AcElementDeploy element,
316 Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
317 LOGGER.debug("restart instance call");
318 if (!AcmUtils.isInTransitionalState(deployState, lockState)) {
319 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
320 deployState, lockState, StateChangeResult.NO_ERROR, "Restarted");
323 if (DeployState.DEPLOYING.equals(deployState)) {
324 deploy(automationCompositionId, element, properties);
327 if (DeployState.UNDEPLOYING.equals(deployState)) {
328 undeploy(automationCompositionId, element.getId());
331 if (DeployState.UPDATING.equals(deployState)) {
332 update(automationCompositionId, element, properties);
335 if (DeployState.DELETING.equals(deployState)) {
336 delete(automationCompositionId, element.getId());
339 if (LockState.LOCKING.equals(lockState)) {
340 lock(automationCompositionId, element.getId());
343 if (LockState.UNLOCKING.equals(lockState)) {
344 unlock(automationCompositionId, element.getId());
349 * Get Composition Data List.
351 * @return the InternalDatas
353 public InternalDatas getCompositionDataList() {
354 var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions();
355 var internalDatas = new InternalDatas();
356 for (var entry : acElementsDefinitions.entrySet()) {
357 for (var acElementsDefinition : entry.getValue().values()) {
358 var internalData = new InternalData();
359 internalData.setCompositionId(entry.getKey());
360 internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId());
361 internalData.setIntProperties(
362 acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties());
363 internalData.setOutProperties(acElementsDefinition.getOutProperties());
364 internalDatas.getList().add(internalData);
367 return internalDatas;
370 public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
371 Map<String, Object> outProperties) {
372 intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);