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.AutomationComposition;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
41 import org.onap.policy.clamp.models.acm.concepts.DeployState;
42 import org.onap.policy.clamp.models.acm.concepts.LockState;
43 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
44 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Component;
52 * This class handles implementation of automationCompositionElement updates.
55 @RequiredArgsConstructor
56 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
58 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60 private final ParticipantIntermediaryApi intermediaryApi;
64 private SimConfig config = new SimConfig();
67 * Callback method to handle an update on a automation composition element.
69 * @param automationCompositionId the automationComposition Id
70 * @param element the information on the automation composition element
71 * @param properties properties Map
72 * @throws PfModelException in case of a exception
75 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
76 throws PfModelException {
77 LOGGER.debug("deploy call");
79 if (!execution(config.getDeployTimerMs(), "Current Thread deploy is Interrupted during execution {}",
84 if (config.isDeploySuccess()) {
85 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
86 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
88 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
89 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
93 private boolean execution(int timeMs, String msg, UUID elementId) {
94 long endTime = System.currentTimeMillis() + timeMs;
95 while (System.currentTimeMillis() < endTime) {
97 if (Thread.currentThread().isInterrupted()) {
98 LOGGER.debug(msg, elementId);
102 } catch (InterruptedException e) {
103 LOGGER.debug(msg, elementId);
104 Thread.currentThread().interrupt();
112 * Handle a automation composition element state change.
114 * @param automationCompositionElementId the ID of the automation composition element
117 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
118 LOGGER.debug("undeploy call");
120 if (!execution(config.getUndeployTimerMs(), "Current Thread undeploy is Interrupted during execution {}",
121 automationCompositionElementId)) {
125 if (config.isUndeploySuccess()) {
126 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
127 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
130 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
131 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.FAILED,
137 public void lock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
138 LOGGER.debug("lock call");
140 if (!execution(config.getLockTimerMs(), "Current Thread lock is Interrupted during execution {}",
141 automationCompositionElementId)) {
145 if (config.isLockSuccess()) {
146 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
147 automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
149 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
150 automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
155 public void unlock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
156 LOGGER.debug("unlock call");
158 if (!execution(config.getUnlockTimerMs(), "Current Thread unlock is Interrupted during execution {}",
159 automationCompositionElementId)) {
163 if (config.isUnlockSuccess()) {
164 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
165 automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
167 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
168 automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
173 public void delete(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
174 LOGGER.debug("delete call");
176 if (!execution(config.getDeleteTimerMs(), "Current Thread delete is Interrupted during execution {}",
177 automationCompositionElementId)) {
181 if (config.isDeleteSuccess()) {
182 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
183 automationCompositionElementId, DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
185 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
186 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
192 public void update(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
193 throws PfModelException {
194 LOGGER.debug("update call");
196 if (!execution(config.getUpdateTimerMs(), "Current Thread update is Interrupted during execution {}",
201 if (config.isUpdateSuccess()) {
202 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
203 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
205 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
206 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
211 * Get AutomationComposition.
213 * @return the AutomationCompositions
215 public AutomationCompositions getAutomationCompositions() {
216 var result = new AutomationCompositions();
217 result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
221 public AutomationComposition getAutomationComposition(UUID instanceId) {
222 return intermediaryApi.getAutomationComposition(instanceId);
228 * @param automationCompositionId the automationComposition Id
229 * @param elementId the automationComposition Element Id
230 * @param useState the useState
231 * @param operationalState the operationalState
232 * @param outProperties the outProperties
234 public void setOutProperties(UUID automationCompositionId, UUID elementId, String useState, String operationalState,
235 Map<String, Object> outProperties) {
236 intermediaryApi.sendAcElementInfo(automationCompositionId, elementId, useState, operationalState,
241 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
242 throws PfModelException {
243 LOGGER.debug("prime call");
245 if (!execution(config.getPrimeTimerMs(), "Current Thread prime is Interrupted during execution {}",
250 if (config.isPrimeSuccess()) {
251 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
254 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
260 public void deprime(UUID compositionId) throws PfModelException {
261 LOGGER.debug("deprime call");
263 if (!execution(config.getDeprimeTimerMs(), "Current Thread deprime is Interrupted during execution {}",
268 if (config.isDeprimeSuccess()) {
269 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
272 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
278 * Get Instance Data List.
280 * @return the InternalDatas
282 public InternalDatas getDataList() {
283 var result = new InternalDatas();
284 var map = intermediaryApi.getAutomationCompositions();
285 for (var instance : map.values()) {
286 for (var element : instance.getElements().values()) {
287 var data = new InternalData();
288 data.setCompositionId(instance.getCompositionId());
289 data.setAutomationCompositionId(instance.getInstanceId());
290 data.setAutomationCompositionElementId(element.getId());
291 data.setIntProperties(element.getProperties());
292 data.setOperationalState(element.getOperationalState());
293 data.setUseState(element.getUseState());
294 data.setOutProperties(element.getOutProperties());
295 result.getList().add(data);
302 public void handleRestartComposition(UUID compositionId,
303 List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state)
304 throws PfModelException {
305 LOGGER.debug("restart composition definition call");
308 prime(compositionId, elementDefinitionList);
312 deprime(compositionId);
316 intermediaryApi.updateCompositionState(compositionId, state, StateChangeResult.NO_ERROR, "Restarted");
321 public void handleRestartInstance(UUID automationCompositionId, AcElementDeploy element,
322 Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
323 LOGGER.debug("restart instance call");
324 if (!AcmUtils.isInTransitionalState(deployState, lockState)) {
325 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
326 deployState, lockState, StateChangeResult.NO_ERROR, "Restarted");
329 if (DeployState.DEPLOYING.equals(deployState)) {
330 deploy(automationCompositionId, element, properties);
333 if (DeployState.UNDEPLOYING.equals(deployState)) {
334 undeploy(automationCompositionId, element.getId());
337 if (DeployState.UPDATING.equals(deployState)) {
338 update(automationCompositionId, element, properties);
341 if (DeployState.DELETING.equals(deployState)) {
342 delete(automationCompositionId, element.getId());
345 if (LockState.LOCKING.equals(lockState)) {
346 lock(automationCompositionId, element.getId());
349 if (LockState.UNLOCKING.equals(lockState)) {
350 unlock(automationCompositionId, element.getId());
355 * Get Composition Data List.
357 * @return the InternalDatas
359 public InternalDatas getCompositionDataList() {
360 var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions();
361 var internalDatas = new InternalDatas();
362 for (var entry : acElementsDefinitions.entrySet()) {
363 for (var acElementsDefinition : entry.getValue().values()) {
364 var internalData = new InternalData();
365 internalData.setCompositionId(entry.getKey());
366 internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId());
367 internalData.setIntProperties(
368 acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties());
369 internalData.setOutProperties(acElementsDefinition.getOutProperties());
370 internalDatas.getList().add(internalData);
373 return internalDatas;
376 public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
377 Map<String, Object> outProperties) {
378 intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);
383 public void migrate(UUID automationCompositionId, AcElementDeploy element, UUID compositionTargetId,
384 Map<String, Object> properties) throws PfModelException {
385 LOGGER.debug("migrate call");
387 if (!execution(config.getMigrateTimerMs(), "Current Thread migrate is Interrupted during execution {}",
392 if (config.isMigrateSuccess()) {
393 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
394 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
396 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
397 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");