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.StateChangeResult;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Service;
45 * This class handles implementation of Simulator Service.
48 @RequiredArgsConstructor
49 public class SimulatorService {
51 private final ParticipantIntermediaryApi intermediaryApi;
53 private static final Logger LOGGER = LoggerFactory.getLogger(SimulatorService.class);
57 private SimConfig config = new SimConfig();
60 * Get AutomationComposition.
62 * @return the AutomationCompositions
64 public AutomationCompositions getAutomationCompositions() {
65 var result = new AutomationCompositions();
66 result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
70 public AutomationComposition getAutomationComposition(UUID instanceId) {
71 return intermediaryApi.getAutomationComposition(instanceId);
77 * @param instanceId the automationComposition Id
78 * @param elementId the automationComposition Element Id
79 * @param useState the useState
80 * @param operationalState the operationalState
81 * @param outProperties the outProperties
83 public void setOutProperties(UUID instanceId, UUID elementId, String useState, String operationalState,
84 Map<String, Object> outProperties) {
85 intermediaryApi.sendAcElementInfo(instanceId, elementId, useState, operationalState,
90 * Get Instance Data List.
92 * @return the InternalDatas
94 public InternalDatas getDataList() {
95 var result = new InternalDatas();
96 var map = intermediaryApi.getAutomationCompositions();
97 for (var instance : map.values()) {
98 for (var element : instance.getElements().values()) {
99 var data = new InternalData();
100 data.setCompositionId(instance.getCompositionId());
101 data.setAutomationCompositionId(instance.getInstanceId());
102 data.setAutomationCompositionElementId(element.getId());
103 data.setIntProperties(element.getProperties());
104 data.setOperationalState(element.getOperationalState());
105 data.setUseState(element.getUseState());
106 data.setOutProperties(element.getOutProperties());
107 result.getList().add(data);
114 * Get Composition Data List.
116 * @return the InternalDatas
118 public InternalDatas getCompositionDataList() {
119 var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions();
120 var internalDatas = new InternalDatas();
121 for (var entry : acElementsDefinitions.entrySet()) {
122 for (var acElementsDefinition : entry.getValue().values()) {
123 var internalData = new InternalData();
124 internalData.setCompositionId(entry.getKey());
125 internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId());
126 internalData.setIntProperties(
127 acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties());
128 internalData.setOutProperties(acElementsDefinition.getOutProperties());
129 internalDatas.getList().add(internalData);
132 return internalDatas;
135 public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
136 Map<String, Object> outProperties) {
137 intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);
141 private boolean execution(int timeMs, String msg, UUID elementId) {
142 long endTime = System.currentTimeMillis() + timeMs;
143 while (System.currentTimeMillis() < endTime) {
145 if (Thread.currentThread().isInterrupted()) {
146 LOGGER.debug(msg, elementId);
150 } catch (InterruptedException e) {
151 LOGGER.debug(msg, elementId);
152 Thread.currentThread().interrupt();
160 * Handle a deploy on a automation composition element.
162 * @param instanceId the instanceId
163 * @param elementId the elementId
165 public void deploy(UUID instanceId, UUID elementId) {
166 if (!execution(getConfig().getDeployTimerMs(),
167 "Current Thread deploy is Interrupted during execution {}", elementId)) {
171 if (getConfig().isDeploySuccess()) {
172 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
173 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
175 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
176 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
181 * Handle an udeploy on a automation composition element.
183 * @param instanceId the instanceId
184 * @param elementId the elementId
186 public void undeploy(UUID instanceId, UUID elementId) {
187 if (!execution(getConfig().getUndeployTimerMs(),
188 "Current Thread undeploy is Interrupted during execution {}", elementId)) {
192 if (getConfig().isUndeploySuccess()) {
193 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
194 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
196 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
197 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Undeploy failed!");
202 * Handle a lock on a automation composition element.
204 * @param instanceId the instanceId
205 * @param elementId the elementId
207 public void lock(UUID instanceId, UUID elementId) {
208 if (!execution(getConfig().getLockTimerMs(),
209 "Current Thread lock is Interrupted during execution {}", elementId)) {
213 if (getConfig().isLockSuccess()) {
214 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
215 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
217 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
218 null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
223 * Handle an unlock on a automation composition element.
225 * @param instanceId the instanceId
226 * @param elementId the elementId
228 public void unlock(UUID instanceId, UUID elementId) {
229 if (!execution(getConfig().getUnlockTimerMs(),
230 "Current Thread unlock is Interrupted during execution {}", elementId)) {
234 if (getConfig().isUnlockSuccess()) {
235 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
236 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
238 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
239 null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
244 * Handle a delete on a automation composition element.
246 * @param instanceId the instanceId
247 * @param elementId the elementId
249 public void delete(UUID instanceId, UUID elementId) {
250 if (!execution(getConfig().getDeleteTimerMs(),
251 "Current Thread delete is Interrupted during execution {}", elementId)) {
255 if (getConfig().isDeleteSuccess()) {
256 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
257 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
259 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
260 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Delete failed!");
265 * Handle an update on a automation composition element.
267 * @param instanceId the instanceId
268 * @param elementId the elementId
270 public void update(UUID instanceId, UUID elementId) {
271 if (!execution(getConfig().getUpdateTimerMs(),
272 "Current Thread update is Interrupted during execution {}", elementId)) {
276 if (getConfig().isUpdateSuccess()) {
277 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
278 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
280 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
281 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
286 * Handle a prime on a automation composition definition.
288 * @param compositionId the compositionId
290 public void prime(UUID compositionId) {
291 if (!execution(getConfig().getPrimeTimerMs(),
292 "Current Thread prime is Interrupted during execution {}", compositionId)) {
296 if (getConfig().isPrimeSuccess()) {
297 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
300 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
306 * Handle a deprime on a automation composition definition.
308 * @param compositionId the compositionId
310 public void deprime(UUID compositionId) {
311 if (!execution(getConfig().getDeprimeTimerMs(),
312 "Current Thread deprime is Interrupted during execution {}", compositionId)) {
316 if (getConfig().isDeprimeSuccess()) {
317 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
320 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
326 * Handle a migrate on a automation composition element.
328 * @param instanceId the instanceId
329 * @param elementId the elementId
331 public void migrate(UUID instanceId, UUID elementId) {
332 if (!execution(getConfig().getMigrateTimerMs(),
333 "Current Thread migrate is Interrupted during execution {}", elementId)) {
337 if (getConfig().isMigrateSuccess()) {
338 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
339 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
341 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
342 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");