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.models.base.PfModelException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Component;
49 * This class handles implementation of automationCompositionElement updates.
52 @RequiredArgsConstructor
53 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
55 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57 private final ParticipantIntermediaryApi intermediaryApi;
61 private SimConfig config = new SimConfig();
64 * Callback method to handle an update on a automation composition element.
66 * @param automationCompositionId the automationComposition Id
67 * @param element the information on the automation composition element
68 * @param properties properties Map
69 * @throws PfModelException in case of a exception
72 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
73 throws PfModelException {
74 LOGGER.debug("deploy call");
76 if (!execution(config.getDeployTimerMs(), "Current Thread deploy is Interrupted during execution {}",
81 if (config.isDeploySuccess()) {
82 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
83 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
85 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
86 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
90 private boolean execution(int timeMs, String msg, UUID elementId) {
91 long endTime = System.currentTimeMillis() + timeMs;
92 while (System.currentTimeMillis() < endTime) {
94 if (Thread.currentThread().isInterrupted()) {
95 LOGGER.debug(msg, elementId);
99 } catch (InterruptedException e) {
100 LOGGER.debug(msg, elementId);
101 Thread.currentThread().interrupt();
109 * Handle a automation composition element state change.
111 * @param automationCompositionElementId the ID of the automation composition element
114 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
115 LOGGER.debug("undeploy call");
117 if (!execution(config.getUndeployTimerMs(), "Current Thread undeploy is Interrupted during execution {}",
118 automationCompositionElementId)) {
122 if (config.isUndeploySuccess()) {
123 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
124 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
127 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
128 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.FAILED,
134 public void lock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
135 LOGGER.debug("lock call");
137 if (!execution(config.getLockTimerMs(), "Current Thread lock is Interrupted during execution {}",
138 automationCompositionElementId)) {
142 if (config.isLockSuccess()) {
143 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
144 automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
146 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
147 automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
152 public void unlock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
153 LOGGER.debug("unlock call");
155 if (!execution(config.getUnlockTimerMs(), "Current Thread unlock is Interrupted during execution {}",
156 automationCompositionElementId)) {
160 if (config.isUnlockSuccess()) {
161 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
162 automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
164 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
165 automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
170 public void delete(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
171 LOGGER.debug("delete call");
173 if (!execution(config.getDeleteTimerMs(), "Current Thread delete is Interrupted during execution {}",
174 automationCompositionElementId)) {
178 if (config.isDeleteSuccess()) {
179 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
180 automationCompositionElementId, DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
182 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
183 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
189 public void update(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
190 throws PfModelException {
191 LOGGER.debug("updat call");
193 if (!execution(config.getUpdateTimerMs(), "Current Thread update is Interrupted during execution {}",
198 if (config.isUpdateSuccess()) {
199 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
200 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
202 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
203 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
208 * Get AutomationComposition.
210 * @return the AutomationCompositions
212 public AutomationCompositions getAutomationCompositions() {
213 var result = new AutomationCompositions();
214 result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
221 * @param automationCompositionId the automationComposition Id
222 * @param elementId the automationComposition Element Id
223 * @param useState the useState
224 * @param operationalState the operationalState
225 * @param outProperties the outProperties
227 public void setOutProperties(UUID automationCompositionId, UUID elementId, String useState, String operationalState,
228 Map<String, Object> outProperties) {
229 intermediaryApi.sendAcElementInfo(automationCompositionId, elementId, useState, operationalState,
234 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
235 throws PfModelException {
237 if (!execution(config.getPrimeTimerMs(), "Current Thread prime is Interrupted during execution {}",
242 if (config.isPrimeSuccess()) {
243 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
246 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
252 public void deprime(UUID compositionId) throws PfModelException {
254 if (!execution(config.getDeprimeTimerMs(), "Current Thread deprime is Interrupted during execution {}",
259 if (config.isDeprimeSuccess()) {
260 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
263 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
271 * @return the InternalDatas
273 public InternalDatas getDataList() {
274 var result = new InternalDatas();
275 var map = intermediaryApi.getAutomationCompositions();
276 for (var instance : map.values()) {
277 for (var element : instance.getElements().values()) {
278 var data = new InternalData();
279 data.setAutomationCompositionId(instance.getInstanceId());
280 data.setAutomationCompositionElementId(element.getId());
281 data.setIntProperties(element.getProperties());
282 data.setOperationalState(element.getOperationalState());
283 data.setUseState(element.getUseState());
284 data.setOutProperties(element.getOutProperties());
285 result.getList().add(data);