2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 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.util.ArrayList;
24 import java.util.List;
26 import java.util.UUID;
28 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
31 import org.onap.policy.clamp.acm.participant.sim.model.InternalData;
32 import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas;
33 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
34 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
37 import org.onap.policy.clamp.models.acm.concepts.DeployState;
38 import org.onap.policy.clamp.models.acm.concepts.LockState;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
40 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.stereotype.Service;
47 * This class handles implementation of Simulator Service.
50 @RequiredArgsConstructor
51 public class SimulatorService {
53 private final ParticipantIntermediaryApi intermediaryApi;
55 private static final Logger LOGGER = LoggerFactory.getLogger(SimulatorService.class);
59 private SimConfig config = new SimConfig();
62 * Get AutomationComposition.
64 * @return the AutomationCompositions
66 public AutomationCompositions getAutomationCompositions() {
67 var result = new AutomationCompositions();
68 result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
72 public AutomationComposition getAutomationComposition(UUID instanceId) {
73 return intermediaryApi.getAutomationComposition(instanceId);
79 * @param instanceId the automationComposition Id
80 * @param elementId the automationComposition Element Id
81 * @param useState the useState
82 * @param operationalState the operationalState
83 * @param outProperties the outProperties
85 public void setOutProperties(UUID instanceId, UUID elementId, String useState, String operationalState,
86 Map<String, Object> outProperties) {
87 intermediaryApi.sendAcElementInfo(instanceId, elementId, useState, operationalState,
92 * Get Instance Data List.
94 * @return the InternalDatas
96 public InternalDatas getDataList() {
97 var result = new InternalDatas();
98 var map = intermediaryApi.getAutomationCompositions();
99 for (var instance : map.values()) {
100 for (var element : instance.getElements().values()) {
101 var data = new InternalData();
102 data.setCompositionId(instance.getCompositionId());
103 data.setAutomationCompositionId(instance.getInstanceId());
104 data.setAutomationCompositionElementId(element.getId());
105 data.setIntProperties(element.getProperties());
106 data.setOperationalState(element.getOperationalState());
107 data.setUseState(element.getUseState());
108 data.setOutProperties(element.getOutProperties());
109 result.getList().add(data);
116 * Get Composition Data List.
118 * @return the InternalDatas
120 public InternalDatas getCompositionDataList() {
121 var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions();
122 var internalDatas = new InternalDatas();
123 for (var entry : acElementsDefinitions.entrySet()) {
124 for (var acElementsDefinition : entry.getValue().values()) {
125 var internalData = new InternalData();
126 internalData.setCompositionId(entry.getKey());
127 internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId());
128 internalData.setIntProperties(
129 acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties());
130 internalData.setOutProperties(acElementsDefinition.getOutProperties());
131 internalDatas.getList().add(internalData);
134 return internalDatas;
137 public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
138 Map<String, Object> outProperties) {
139 intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);
143 private boolean execution(int timeMs, String msg, UUID elementId) {
144 long endTime = System.currentTimeMillis() + timeMs;
145 while (System.currentTimeMillis() < endTime) {
147 if (Thread.currentThread().isInterrupted()) {
148 LOGGER.debug(msg, elementId);
152 } catch (InterruptedException e) {
153 LOGGER.debug(msg, elementId);
154 Thread.currentThread().interrupt();
162 * Handle a deploy on a automation composition element.
164 * @param instanceId the instanceId
165 * @param elementId the elementId
167 public void deploy(UUID instanceId, UUID elementId) {
168 if (!execution(getConfig().getDeployTimerMs(),
169 "Current Thread deploy is Interrupted during execution {}", elementId)) {
173 if (getConfig().isDeploySuccess()) {
174 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
175 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
177 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
178 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
183 * Handle an udeploy on a automation composition element.
185 * @param instanceId the instanceId
186 * @param elementId the elementId
188 public void undeploy(UUID instanceId, UUID elementId) {
189 if (!execution(getConfig().getUndeployTimerMs(),
190 "Current Thread undeploy is Interrupted during execution {}", elementId)) {
194 if (getConfig().isUndeploySuccess()) {
195 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
196 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
198 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
199 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Undeploy failed!");
204 * Handle a lock on a automation composition element.
206 * @param instanceId the instanceId
207 * @param elementId the elementId
209 public void lock(UUID instanceId, UUID elementId) {
210 if (!execution(getConfig().getLockTimerMs(),
211 "Current Thread lock is Interrupted during execution {}", elementId)) {
215 if (getConfig().isLockSuccess()) {
216 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
217 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
219 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
220 null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
225 * Handle an unlock on a automation composition element.
227 * @param instanceId the instanceId
228 * @param elementId the elementId
230 public void unlock(UUID instanceId, UUID elementId) {
231 if (!execution(getConfig().getUnlockTimerMs(),
232 "Current Thread unlock is Interrupted during execution {}", elementId)) {
236 if (getConfig().isUnlockSuccess()) {
237 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
238 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
240 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
241 null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
246 * Handle a delete on a automation composition element.
248 * @param instanceId the instanceId
249 * @param elementId the elementId
251 public void delete(UUID instanceId, UUID elementId) {
252 if (!execution(getConfig().getDeleteTimerMs(),
253 "Current Thread delete is Interrupted during execution {}", elementId)) {
257 if (getConfig().isDeleteSuccess()) {
258 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
259 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
261 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
262 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Delete failed!");
267 * Handle an update on a automation composition element.
269 * @param instanceId the instanceId
270 * @param elementId the elementId
272 public void update(UUID instanceId, UUID elementId) {
273 if (!execution(getConfig().getUpdateTimerMs(),
274 "Current Thread update is Interrupted during execution {}", elementId)) {
278 if (getConfig().isUpdateSuccess()) {
279 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
280 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
282 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
283 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
288 * Handle a prime on a automation composition definition.
290 * @param compositionId the compositionId
292 public void prime(UUID compositionId) {
293 if (!execution(getConfig().getPrimeTimerMs(),
294 "Current Thread prime is Interrupted during execution {}", compositionId)) {
298 if (getConfig().isPrimeSuccess()) {
299 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
302 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
308 * Handle a deprime on a automation composition definition.
310 * @param compositionId the compositionId
312 public void deprime(UUID compositionId) {
313 if (!execution(getConfig().getDeprimeTimerMs(),
314 "Current Thread deprime is Interrupted during execution {}", compositionId)) {
318 if (getConfig().isDeprimeSuccess()) {
319 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
322 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
328 * Handle a migrate on a automation composition element.
330 * @param instanceId the instanceId
331 * @param elementId the elementId
332 * @param stage the stage
334 public void migrate(UUID instanceId, UUID elementId, int stage, Map<String, Object> compositionInProperties,
335 Map<String, Object> instanceOutProperties) {
336 if (!execution(getConfig().getMigrateTimerMs(),
337 "Current Thread migrate is Interrupted during execution {}", elementId)) {
341 if (config.isMigrateSuccess()) {
342 var stageSet = ParticipantUtils.findStageSet(compositionInProperties);
343 var nextStage = 1000;
344 for (var s : stageSet) {
346 nextStage = Math.min(s, nextStage);
349 instanceOutProperties.putIfAbsent("stage", new ArrayList<>());
350 @SuppressWarnings("unchecked")
351 var stageList = (List<Integer>) instanceOutProperties.get("stage");
352 stageList.add(stage);
353 intermediaryApi.sendAcElementInfo(instanceId, elementId, null, null, instanceOutProperties);
354 if (nextStage == 1000) {
355 intermediaryApi.updateAutomationCompositionElementState(
356 instanceId, elementId,
357 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
359 intermediaryApi.updateAutomationCompositionElementStage(
360 instanceId, elementId,
361 StateChangeResult.NO_ERROR, nextStage, "stage " + stage + " Migrated");
364 intermediaryApi.updateAutomationCompositionElementState(
365 instanceId, elementId,
366 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
371 * Handle a Migrate Precheck on a automation composition element.
373 * @param instanceId the instanceId
374 * @param elementId the elementId
376 public void migratePrecheck(UUID instanceId, UUID elementId) {
377 if (!execution(config.getMigratePrecheckTimerMs(),
378 "Current Thread migrate precheck is Interrupted during execution {}", elementId)) {
382 if (config.isMigratePrecheck()) {
383 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
384 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migration precheck completed");
386 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
387 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migration precheck failed");
392 * Handle a Prepare on a automation composition element.
394 * @param instanceId the instanceId
395 * @param elementId the elementId
397 public void prepare(UUID instanceId, UUID elementId) {
398 if (!execution(config.getPrepareTimerMs(),
399 "Current Thread prepare is Interrupted during execution {}", elementId)) {
403 if (config.isPrepare()) {
404 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
405 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Prepare completed");
407 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
408 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Prepare failed");
413 * Handle a Review on a automation composition element.
415 * @param instanceId the instanceId
416 * @param elementId the elementId
418 public void review(UUID instanceId, UUID elementId) {
419 if (!execution(config.getReviewTimerMs(),
420 "Current Thread review is Interrupted during execution {}", elementId)) {
424 if (config.isReview()) {
425 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
426 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Review completed");
428 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
429 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Review failed");