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;
25 import java.util.UUID;
27 import lombok.RequiredArgsConstructor;
29 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
30 import org.onap.policy.clamp.acm.participant.sim.model.InternalData;
31 import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas;
32 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
33 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
36 import org.onap.policy.clamp.models.acm.concepts.DeployState;
37 import org.onap.policy.clamp.models.acm.concepts.LockState;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
39 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Service;
46 * This class handles implementation of Simulator Service.
49 @RequiredArgsConstructor
50 public class SimulatorService {
52 private final ParticipantIntermediaryApi intermediaryApi;
54 private static final Logger LOGGER = LoggerFactory.getLogger(SimulatorService.class);
58 private SimConfig config = new SimConfig();
61 * Get AutomationComposition.
63 * @return the AutomationCompositions
65 public AutomationCompositions getAutomationCompositions() {
66 var result = new AutomationCompositions();
67 result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
71 public AutomationComposition getAutomationComposition(UUID instanceId) {
72 return intermediaryApi.getAutomationComposition(instanceId);
78 * @param instanceId the automationComposition Id
79 * @param elementId the automationComposition Element Id
80 * @param useState the useState
81 * @param operationalState the operationalState
82 * @param outProperties the outProperties
84 public void setOutProperties(UUID instanceId, UUID elementId, String useState, String operationalState,
85 Map<String, Object> outProperties) {
86 intermediaryApi.sendAcElementInfo(instanceId, elementId, useState, operationalState,
91 * Get Instance Data List.
93 * @return the InternalDatas
95 public InternalDatas getDataList() {
96 var result = new InternalDatas();
97 var map = intermediaryApi.getAutomationCompositions();
98 for (var instance : map.values()) {
99 for (var element : instance.getElements().values()) {
100 var data = new InternalData();
101 data.setCompositionId(instance.getCompositionId());
102 data.setAutomationCompositionId(instance.getInstanceId());
103 data.setAutomationCompositionElementId(element.getId());
104 data.setIntProperties(element.getProperties());
105 data.setOperationalState(element.getOperationalState());
106 data.setUseState(element.getUseState());
107 data.setOutProperties(element.getOutProperties());
108 result.getList().add(data);
115 * Get Composition Data List.
117 * @return the InternalDatas
119 public InternalDatas getCompositionDataList() {
120 var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions();
121 var internalDatas = new InternalDatas();
122 for (var entry : acElementsDefinitions.entrySet()) {
123 for (var acElementsDefinition : entry.getValue().values()) {
124 var internalData = new InternalData();
125 internalData.setCompositionId(entry.getKey());
126 internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId());
127 internalData.setIntProperties(
128 acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties());
129 internalData.setOutProperties(acElementsDefinition.getOutProperties());
130 internalDatas.getList().add(internalData);
133 return internalDatas;
136 public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
137 Map<String, Object> outProperties) {
138 intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);
142 private boolean execution(int timeMs, String msg, UUID elementId) {
143 long endTime = System.currentTimeMillis() + timeMs;
144 while (System.currentTimeMillis() < endTime) {
146 if (Thread.currentThread().isInterrupted()) {
147 LOGGER.debug(msg, elementId);
151 } catch (InterruptedException e) {
152 LOGGER.debug(msg, elementId);
153 Thread.currentThread().interrupt();
161 * Handle a deploy on a automation composition element.
163 * @param instanceId the instanceId
164 * @param elementId the elementId
166 public void deploy(UUID instanceId, UUID elementId) {
167 if (!execution(getConfig().getDeployTimerMs(),
168 "Current Thread deploy is Interrupted during execution {}", elementId)) {
172 if (getConfig().isDeploySuccess()) {
173 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
174 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
176 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
177 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
182 * Handle an udeploy on a automation composition element.
184 * @param instanceId the instanceId
185 * @param elementId the elementId
187 public void undeploy(UUID instanceId, UUID elementId) {
188 if (!execution(getConfig().getUndeployTimerMs(),
189 "Current Thread undeploy is Interrupted during execution {}", elementId)) {
193 if (getConfig().isUndeploySuccess()) {
194 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
195 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
197 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
198 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Undeploy failed!");
203 * Handle a lock on a automation composition element.
205 * @param instanceId the instanceId
206 * @param elementId the elementId
208 public void lock(UUID instanceId, UUID elementId) {
209 if (!execution(getConfig().getLockTimerMs(),
210 "Current Thread lock is Interrupted during execution {}", elementId)) {
214 if (getConfig().isLockSuccess()) {
215 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
216 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
218 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
219 null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
224 * Handle an unlock on a automation composition element.
226 * @param instanceId the instanceId
227 * @param elementId the elementId
229 public void unlock(UUID instanceId, UUID elementId) {
230 if (!execution(getConfig().getUnlockTimerMs(),
231 "Current Thread unlock is Interrupted during execution {}", elementId)) {
235 if (getConfig().isUnlockSuccess()) {
236 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
237 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
239 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
240 null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
245 * Handle a delete on a automation composition element.
247 * @param instanceId the instanceId
248 * @param elementId the elementId
250 public void delete(UUID instanceId, UUID elementId) {
251 if (!execution(getConfig().getDeleteTimerMs(),
252 "Current Thread delete is Interrupted during execution {}", elementId)) {
256 if (getConfig().isDeleteSuccess()) {
257 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
258 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
260 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
261 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Delete failed!");
266 * Handle an update on a automation composition element.
268 * @param instanceId the instanceId
269 * @param elementId the elementId
271 public void update(UUID instanceId, UUID elementId) {
272 if (!execution(getConfig().getUpdateTimerMs(),
273 "Current Thread update is Interrupted during execution {}", elementId)) {
277 if (getConfig().isUpdateSuccess()) {
278 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
279 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
281 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
282 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
287 * Handle a prime on a automation composition definition.
289 * @param compositionId the compositionId
291 public void prime(UUID compositionId) {
292 if (!execution(getConfig().getPrimeTimerMs(),
293 "Current Thread prime is Interrupted during execution {}", compositionId)) {
297 if (getConfig().isPrimeSuccess()) {
298 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
301 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
307 * Handle a deprime on a automation composition definition.
309 * @param compositionId the compositionId
311 public void deprime(UUID compositionId) {
312 if (!execution(getConfig().getDeprimeTimerMs(),
313 "Current Thread deprime is Interrupted during execution {}", compositionId)) {
317 if (getConfig().isDeprimeSuccess()) {
318 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
321 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
327 * Handle a migrate on a automation composition element.
329 * @param instanceId the instanceId
330 * @param elementId the elementId
331 * @param stage the stage
333 public void migrate(UUID instanceId, UUID elementId, int stage, Map<String, Object> compositionInProperties) {
334 if (!execution(getConfig().getMigrateTimerMs(),
335 "Current Thread migrate is Interrupted during execution {}", elementId)) {
339 if (config.isMigrateSuccess()) {
340 var stageSet = ParticipantUtils.findStageSet(compositionInProperties);
341 var nextStage = 1000;
342 for (var s : stageSet) {
344 nextStage = Math.min(s, nextStage);
347 if (nextStage == 1000) {
348 intermediaryApi.updateAutomationCompositionElementState(
349 instanceId, elementId,
350 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
352 intermediaryApi.updateAutomationCompositionElementStage(
353 instanceId, elementId,
354 StateChangeResult.NO_ERROR, nextStage, "stage " + stage + " Migrated");
357 intermediaryApi.updateAutomationCompositionElementState(
358 instanceId, elementId,
359 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
364 * Handle a Migrate Precheck on a automation composition element.
366 * @param instanceId the instanceId
367 * @param elementId the elementId
369 public void migratePrecheck(UUID instanceId, UUID elementId) {
370 if (!execution(config.getMigratePrecheckTimerMs(),
371 "Current Thread migrate precheck is Interrupted during execution {}", elementId)) {
375 if (config.isMigratePrecheck()) {
376 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
377 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migration precheck completed");
379 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
380 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migration precheck failed");
385 * Handle a Prepare on a automation composition element.
387 * @param instanceId the instanceId
388 * @param elementId the elementId
390 public void prepare(UUID instanceId, UUID elementId) {
391 if (!execution(config.getPrepareTimerMs(),
392 "Current Thread prepare is Interrupted during execution {}", elementId)) {
396 if (config.isPrepare()) {
397 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
398 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Prepare completed");
400 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
401 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Prepare failed");
406 * Handle a Review on a automation composition element.
408 * @param instanceId the instanceId
409 * @param elementId the elementId
411 public void review(UUID instanceId, UUID elementId) {
412 if (!execution(config.getReviewTimerMs(),
413 "Current Thread review is Interrupted during execution {}", elementId)) {
417 if (config.isReview()) {
418 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
419 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Review completed");
421 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
422 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Review failed");