2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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.util.ArrayList;
24 import java.util.List;
26 import java.util.UUID;
27 import java.util.concurrent.locks.LockSupport;
29 import lombok.RequiredArgsConstructor;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
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.AcTypeState;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
42 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Service;
49 * This class handles implementation of Simulator Service.
52 @RequiredArgsConstructor
53 public class SimulatorService {
55 private final ParticipantIntermediaryApi intermediaryApi;
57 private static final Logger LOGGER = LoggerFactory.getLogger(SimulatorService.class);
58 private static final String INTERNAL_STATE = "InternalState";
59 private static final String MIGRATION_PROPERTY = "stage";
60 private static final String ROLLBACK_PROPERTY = "rollbackStage";
61 private static final String PREPARE_PROPERTY = "prepareStage";
62 private static final String STAGE_MSG = "stage %d %s";
66 private SimConfig config = new SimConfig();
69 * Get AutomationComposition.
71 * @return the AutomationCompositions
73 public AutomationCompositions getAutomationCompositions() {
74 var result = new AutomationCompositions();
75 result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
79 public AutomationComposition getAutomationComposition(UUID instanceId) {
80 return intermediaryApi.getAutomationComposition(instanceId);
86 * @param instanceId the automationComposition Id
87 * @param elementId the automationComposition Element Id
88 * @param useState the useState
89 * @param operationalState the operationalState
90 * @param outProperties the outProperties
92 public void setOutProperties(UUID instanceId, UUID elementId, String useState, String operationalState,
93 Map<String, Object> outProperties) {
94 intermediaryApi.sendAcElementInfo(instanceId, elementId, useState, operationalState,
99 * Get Instance Data List.
101 * @return the InternalDatas
103 public InternalDatas getDataList() {
104 var result = new InternalDatas();
105 var map = intermediaryApi.getAutomationCompositions();
106 for (var instance : map.values()) {
107 for (var element : instance.getElements().values()) {
108 var data = new InternalData();
109 data.setCompositionId(instance.getCompositionId());
110 data.setAutomationCompositionId(instance.getInstanceId());
111 data.setAutomationCompositionElementId(element.getId());
112 data.setIntProperties(element.getProperties());
113 data.setOperationalState(element.getOperationalState());
114 data.setUseState(element.getUseState());
115 data.setOutProperties(element.getOutProperties());
116 result.getList().add(data);
123 * Get Composition Data List.
125 * @return the InternalDatas
127 public InternalDatas getCompositionDataList() {
128 var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions();
129 var internalDatas = new InternalDatas();
130 for (var entry : acElementsDefinitions.entrySet()) {
131 for (var acElementsDefinition : entry.getValue().values()) {
132 var internalData = new InternalData();
133 internalData.setCompositionId(entry.getKey());
134 internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId());
135 internalData.setIntProperties(
136 acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties());
137 internalData.setOutProperties(acElementsDefinition.getOutProperties());
138 internalDatas.getList().add(internalData);
141 return internalDatas;
144 public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
145 Map<String, Object> outProperties) {
146 intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);
150 protected boolean isInterrupted(int timeMs, String msg, UUID elementId) {
151 long endTime = System.nanoTime() + (timeMs * 1_000_000L);
152 while (System.nanoTime() < endTime) {
153 if (Thread.interrupted()) {
154 LOGGER.debug(msg, elementId);
157 LockSupport.parkNanos(10_000_000L);
162 private void sendAcInternalState(UUID instanceId, UUID elementId, Map<String, Object> outProperties,
163 DeployState deployState) {
164 outProperties.put(INTERNAL_STATE, deployState.name());
165 intermediaryApi.sendAcElementInfo(instanceId, elementId, null, null, outProperties);
169 * Handle deploying an automation composition element.
171 * @param instanceId the instanceId
172 * @param elementId the elementId
173 * @param outProperties the outProperties
175 public void deploy(UUID instanceId, UUID elementId, Map<String, Object> outProperties) {
176 sendAcInternalState(instanceId, elementId, outProperties, DeployState.DEPLOYING);
178 if (isInterrupted(getConfig().getDeployTimerMs(),
179 "Current Thread deploy is Interrupted during execution {}", elementId)) {
183 if (getConfig().isDeploySuccess()) {
184 sendAcInternalState(instanceId, elementId, outProperties, DeployState.DEPLOYED);
186 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
187 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
189 sendAcInternalState(instanceId, elementId, outProperties, DeployState.UNDEPLOYED);
191 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
192 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
197 * Handle undeploying an automation composition element.
199 * @param instanceId the instanceId
200 * @param elementId the elementId
201 * @param outProperties the outProperties
203 public void undeploy(UUID instanceId, UUID elementId, Map<String, Object> outProperties) {
204 sendAcInternalState(instanceId, elementId, outProperties, DeployState.UNDEPLOYING);
206 if (isInterrupted(getConfig().getUndeployTimerMs(),
207 "Current Thread undeploy is Interrupted during execution {}", elementId)) {
211 if (getConfig().isUndeploySuccess()) {
212 sendAcInternalState(instanceId, elementId, outProperties, DeployState.UNDEPLOYED);
214 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
215 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
217 sendAcInternalState(instanceId, elementId, outProperties, DeployState.DEPLOYED);
219 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
220 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Undeploy failed!");
225 * Handle locking an automation composition element.
227 * @param instanceId the instanceId
228 * @param elementId the elementId
230 public void lock(UUID instanceId, UUID elementId) {
231 if (isInterrupted(getConfig().getLockTimerMs(),
232 "Current Thread lock is Interrupted during execution {}", elementId)) {
236 if (getConfig().isLockSuccess()) {
237 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
238 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
240 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
241 null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
246 * Handle unlocking an automation composition element.
248 * @param instanceId the instanceId
249 * @param elementId the elementId
251 public void unlock(UUID instanceId, UUID elementId) {
252 if (isInterrupted(getConfig().getUnlockTimerMs(),
253 "Current Thread unlock is Interrupted during execution {}", elementId)) {
257 if (getConfig().isUnlockSuccess()) {
258 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
259 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
261 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
262 null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
267 * Handle deleting an automation composition element.
269 * @param instanceId the instanceId
270 * @param elementId the elementId
272 public void delete(UUID instanceId, UUID elementId) {
273 if (isInterrupted(getConfig().getDeleteTimerMs(),
274 "Current Thread delete is Interrupted during execution {}", elementId)) {
278 if (getConfig().isDeleteSuccess()) {
279 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
280 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
282 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
283 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Delete failed!");
288 * Handle deleting an automation composition element in migration.
290 * @param instanceId the instanceId
291 * @param elementId the elementId
293 public void deleteInMigration(UUID instanceId, UUID elementId) {
294 if (getConfig().isMigrateSuccess()) {
295 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
296 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
298 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
299 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Delete failed!");
304 * Handle an update on an automation composition element.
306 * @param instanceId the instanceId
307 * @param elementId the elementId
309 public void update(UUID instanceId, UUID elementId) {
310 if (isInterrupted(getConfig().getUpdateTimerMs(),
311 "Current Thread update is Interrupted during execution {}", elementId)) {
315 if (getConfig().isUpdateSuccess()) {
316 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
317 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
319 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
320 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
325 * Handle a prime on an automation composition definition.
327 * @param composition the information of the Automation Composition Definition
329 public void prime(CompositionDto composition) {
330 if (isInterrupted(getConfig().getPrimeTimerMs(),
331 "Current Thread prime is Interrupted during execution {}", composition.compositionId())) {
335 if (getConfig().isPrimeSuccess()) {
336 sendOutProperties(composition, AcTypeState.PRIMED.name());
337 intermediaryApi.updateCompositionState(composition.compositionId(), AcTypeState.PRIMED,
338 StateChangeResult.NO_ERROR, "Primed");
340 sendOutProperties(composition, AcTypeState.COMMISSIONED.name());
341 intermediaryApi.updateCompositionState(composition.compositionId(), AcTypeState.COMMISSIONED,
342 StateChangeResult.FAILED, "Prime failed!");
346 private void sendOutProperties(CompositionDto composition, String data) {
347 for (var elementEntry : composition.outPropertiesMap().entrySet()) {
348 elementEntry.getValue().put(INTERNAL_STATE, data);
349 intermediaryApi.sendAcDefinitionInfo(
350 composition.compositionId(), elementEntry.getKey(), elementEntry.getValue());
355 * Handle a deprime on an automation composition definition.
357 * @param composition the information of the Automation Composition Definition
359 public void deprime(CompositionDto composition) {
360 if (isInterrupted(getConfig().getDeprimeTimerMs(),
361 "Current Thread deprime is Interrupted during execution {}", composition.compositionId())) {
365 if (getConfig().isDeprimeSuccess()) {
366 sendOutProperties(composition, AcTypeState.COMMISSIONED.name());
367 intermediaryApi.updateCompositionState(composition.compositionId(), AcTypeState.COMMISSIONED,
368 StateChangeResult.NO_ERROR, "Deprimed");
370 sendOutProperties(composition, AcTypeState.PRIMED.name());
371 intermediaryApi.updateCompositionState(composition.compositionId(), AcTypeState.PRIMED,
372 StateChangeResult.FAILED, "Deprime failed!");
377 * Handle a migration on an automation composition element.
379 * @param instanceId the instanceId
380 * @param elementId the elementId
381 * @param stage the stage
382 * @param compositionInProperties in Properties from composition definition element
383 * @param instanceOutProperties in Properties from instance element
385 public void migrate(UUID instanceId, UUID elementId, int stage, Map<String, Object> compositionInProperties,
386 Map<String, Object> instanceOutProperties) {
387 if (isInterrupted(getConfig().getMigrateTimerMs(),
388 "Current Thread migrate is Interrupted during execution {}", elementId)) {
392 if (config.isMigrateSuccess()) {
393 var stageSet = ParticipantUtils.findStageSetMigrate(compositionInProperties);
394 var nextStage = 1000;
395 for (var s : stageSet) {
397 nextStage = Math.min(s, nextStage);
400 instanceOutProperties.putIfAbsent(MIGRATION_PROPERTY, new ArrayList<>());
401 @SuppressWarnings("unchecked")
402 var stageList = (List<Integer>) instanceOutProperties.get(MIGRATION_PROPERTY);
403 stageList.add(stage);
404 intermediaryApi.sendAcElementInfo(instanceId, elementId, null, null, instanceOutProperties);
405 if (nextStage == 1000) {
406 intermediaryApi.updateAutomationCompositionElementState(
407 instanceId, elementId,
408 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
410 intermediaryApi.updateAutomationCompositionElementStage(
411 instanceId, elementId,
412 StateChangeResult.NO_ERROR, nextStage, String.format(STAGE_MSG, stage, "Migrated"));
415 intermediaryApi.updateAutomationCompositionElementState(
416 instanceId, elementId,
417 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
422 * Handle a Migrate Precheck on an automation composition element.
424 * @param instanceId the instanceId
425 * @param elementId the elementId
427 public void migratePrecheck(UUID instanceId, UUID elementId) {
428 if (isInterrupted(config.getMigratePrecheckTimerMs(),
429 "Current Thread migrate precheck is Interrupted during execution {}", elementId)) {
433 if (config.isMigratePrecheck()) {
434 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
435 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migration precheck completed");
437 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
438 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migration precheck failed");
443 * Handle a Prepare on an automation composition element.
445 * @param instanceId the instanceId
446 * @param elementId the elementId
447 * @param stage the stage
448 * @param compositionInProperties in Properties from composition definition element
449 * @param instanceOutProperties in Properties from instance element
451 public void prepare(UUID instanceId, UUID elementId, int stage, Map<String, Object> compositionInProperties,
452 Map<String, Object> instanceOutProperties) {
453 if (isInterrupted(config.getPrepareTimerMs(),
454 "Current Thread prepare is Interrupted during execution {}", elementId)) {
458 if (config.isPrepare()) {
459 var stageSet = ParticipantUtils.findStageSetPrepare(compositionInProperties);
460 var nextStage = 1000;
461 for (var s : stageSet) {
463 nextStage = Math.min(s, nextStage);
466 instanceOutProperties.putIfAbsent(PREPARE_PROPERTY, new ArrayList<>());
467 @SuppressWarnings("unchecked")
468 var stageList = (List<Integer>) instanceOutProperties.get(PREPARE_PROPERTY);
469 stageList.add(stage);
470 intermediaryApi.sendAcElementInfo(instanceId, elementId, null, null, instanceOutProperties);
471 if (nextStage == 1000) {
472 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
473 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Prepare completed");
475 intermediaryApi.updateAutomationCompositionElementStage(
476 instanceId, elementId,
477 StateChangeResult.NO_ERROR, nextStage, String.format(STAGE_MSG, stage, "Prepared"));
480 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
481 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Prepare failed");
486 * Handle a Review on an automation composition element.
488 * @param instanceId the instanceId
489 * @param elementId the elementId
491 public void review(UUID instanceId, UUID elementId) {
492 if (isInterrupted(config.getReviewTimerMs(),
493 "Current Thread review is Interrupted during execution {}", elementId)) {
497 if (config.isReview()) {
498 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
499 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Review completed");
501 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
502 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Review failed");
507 * Handle rollback of an automation composition.
509 * @param instanceId the instanceId
510 * @param elementId the elementId
511 * @param stage the stage
512 * @param compositionInProperties in Properties from composition definition element
513 * @param instanceOutProperties in Properties from instance element
515 public void rollback(UUID instanceId, UUID elementId, int stage, Map<String, Object> compositionInProperties,
516 Map<String, Object> instanceOutProperties) {
517 if (isInterrupted(getConfig().getRollbackTimerMs(),
518 "Current Thread for rollback was Interrupted during execution {}", instanceId)) {
519 LOGGER.debug("Rollback interrupted");
523 if (config.isRollback()) {
524 var stageSet = ParticipantUtils.findStageSetMigrate(compositionInProperties);
525 var nextStage = 1000;
526 for (var s : stageSet) {
528 nextStage = Math.min(s, nextStage);
531 instanceOutProperties.putIfAbsent(ROLLBACK_PROPERTY, new ArrayList<>());
532 @SuppressWarnings("unchecked")
533 var stageList = (List<Integer>) instanceOutProperties.get(ROLLBACK_PROPERTY);
534 stageList.add(stage);
535 intermediaryApi.sendAcElementInfo(instanceId, elementId, null, null, instanceOutProperties);
536 if (nextStage == 1000) {
537 intermediaryApi.updateAutomationCompositionElementState(
538 instanceId, elementId,
539 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migration rollback done");
541 intermediaryApi.updateAutomationCompositionElementStage(
542 instanceId, elementId,
543 StateChangeResult.NO_ERROR, nextStage, String.format(STAGE_MSG, stage, "Migration rollback"));
546 intermediaryApi.updateAutomationCompositionElementState(
547 instanceId, elementId,
548 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migration rollback failed");