b0006f7111cdf4000043caea0db7ba2a8ba9f89f
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.sim.main.handler;
22
23 import java.util.ArrayList;
24 import java.util.Map;
25 import java.util.UUID;
26 import lombok.Getter;
27 import lombok.RequiredArgsConstructor;
28 import lombok.Setter;
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;
44
45 /**
46  * This class handles implementation of Simulator Service.
47  */
48 @Service
49 @RequiredArgsConstructor
50 public class SimulatorService {
51
52     private final ParticipantIntermediaryApi intermediaryApi;
53
54     private static final Logger LOGGER = LoggerFactory.getLogger(SimulatorService.class);
55
56     @Getter
57     @Setter
58     private SimConfig config = new SimConfig();
59
60     /**
61      * Get AutomationComposition.
62      *
63      * @return the AutomationCompositions
64      */
65     public AutomationCompositions getAutomationCompositions() {
66         var result = new AutomationCompositions();
67         result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
68         return result;
69     }
70
71     public AutomationComposition getAutomationComposition(UUID instanceId) {
72         return intermediaryApi.getAutomationComposition(instanceId);
73     }
74
75     /**
76      * Set OutProperties.
77      *
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
83      */
84     public void setOutProperties(UUID instanceId, UUID elementId, String useState, String operationalState,
85                                  Map<String, Object> outProperties) {
86         intermediaryApi.sendAcElementInfo(instanceId, elementId, useState, operationalState,
87                 outProperties);
88     }
89
90     /**
91      * Get Instance Data List.
92      *
93      * @return the InternalDatas
94      */
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);
109             }
110         }
111         return result;
112     }
113
114     /**
115      * Get Composition Data List.
116      *
117      * @return the InternalDatas
118      */
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);
131             }
132         }
133         return internalDatas;
134     }
135
136     public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
137                                             Map<String, Object> outProperties) {
138         intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);
139
140     }
141
142     private boolean execution(int timeMs, String msg, UUID elementId) {
143         long endTime = System.currentTimeMillis() + timeMs;
144         while (System.currentTimeMillis() < endTime) {
145             try {
146                 if (Thread.currentThread().isInterrupted()) {
147                     LOGGER.debug(msg, elementId);
148                     return false;
149                 }
150                 Thread.sleep(10L);
151             } catch (InterruptedException e) {
152                 LOGGER.debug(msg, elementId);
153                 Thread.currentThread().interrupt();
154                 return false;
155             }
156         }
157         return true;
158     }
159
160     /**
161      * Handle a deploy on a automation composition element.
162      *
163      * @param instanceId the instanceId
164      * @param elementId the elementId
165      */
166     public void deploy(UUID instanceId, UUID elementId) {
167         if (!execution(getConfig().getDeployTimerMs(),
168                 "Current Thread deploy is Interrupted during execution {}", elementId)) {
169             return;
170         }
171
172         if (getConfig().isDeploySuccess()) {
173             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
174                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
175         } else {
176             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
177                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
178         }
179     }
180
181     /**
182      * Handle an udeploy on a automation composition element.
183      *
184      * @param instanceId the instanceId
185      * @param elementId the elementId
186      */
187     public void undeploy(UUID instanceId, UUID elementId) {
188         if (!execution(getConfig().getUndeployTimerMs(),
189                 "Current Thread undeploy is Interrupted during execution {}", elementId)) {
190             return;
191         }
192
193         if (getConfig().isUndeploySuccess()) {
194             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
195                     DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
196         } else {
197             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
198                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Undeploy failed!");
199         }
200     }
201
202     /**
203      * Handle a lock on a automation composition element.
204      *
205      * @param instanceId the instanceId
206      * @param elementId the elementId
207      */
208     public void lock(UUID instanceId, UUID elementId) {
209         if (!execution(getConfig().getLockTimerMs(),
210                 "Current Thread lock is Interrupted during execution {}", elementId)) {
211             return;
212         }
213
214         if (getConfig().isLockSuccess()) {
215             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
216                     null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
217         } else {
218             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
219                     null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
220         }
221     }
222
223     /**
224      * Handle an unlock on a automation composition element.
225      *
226      * @param instanceId the instanceId
227      * @param elementId the elementId
228      */
229     public void unlock(UUID instanceId, UUID elementId) {
230         if (!execution(getConfig().getUnlockTimerMs(),
231                 "Current Thread unlock is Interrupted during execution {}", elementId)) {
232             return;
233         }
234
235         if (getConfig().isUnlockSuccess()) {
236             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
237                     null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
238         } else {
239             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
240                     null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
241         }
242     }
243
244     /**
245      * Handle a delete on a automation composition element.
246      *
247      * @param instanceId the instanceId
248      * @param elementId the elementId
249      */
250     public void delete(UUID instanceId, UUID elementId) {
251         if (!execution(getConfig().getDeleteTimerMs(),
252                 "Current Thread delete is Interrupted during execution {}", elementId)) {
253             return;
254         }
255
256         if (getConfig().isDeleteSuccess()) {
257             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
258                     DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
259         } else {
260             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
261                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Delete failed!");
262         }
263     }
264
265     /**
266      * Handle an update on a automation composition element.
267      *
268      * @param instanceId the instanceId
269      * @param elementId the elementId
270      */
271     public void update(UUID instanceId, UUID elementId) {
272         if (!execution(getConfig().getUpdateTimerMs(),
273                 "Current Thread update is Interrupted during execution {}", elementId)) {
274             return;
275         }
276
277         if (getConfig().isUpdateSuccess()) {
278             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
279                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
280         } else {
281             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
282                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
283         }
284     }
285
286     /**
287      * Handle a prime on a automation composition definition.
288      *
289      * @param compositionId the compositionId
290      */
291     public void prime(UUID compositionId) {
292         if (!execution(getConfig().getPrimeTimerMs(),
293                 "Current Thread prime is Interrupted during execution {}", compositionId)) {
294             return;
295         }
296
297         if (getConfig().isPrimeSuccess()) {
298             intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
299                     "Primed");
300         } else {
301             intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
302                     "Prime failed!");
303         }
304     }
305
306     /**
307      * Handle a deprime on a automation composition definition.
308      *
309      * @param compositionId the compositionId
310      */
311     public void deprime(UUID compositionId) {
312         if (!execution(getConfig().getDeprimeTimerMs(),
313                 "Current Thread deprime is Interrupted during execution {}", compositionId)) {
314             return;
315         }
316
317         if (getConfig().isDeprimeSuccess()) {
318             intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
319                     "Deprimed");
320         } else {
321             intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
322                     "Deprime failed!");
323         }
324     }
325
326     /**
327      * Handle a migrate on a automation composition element.
328      *
329      * @param instanceId the instanceId
330      * @param elementId the elementId
331      * @param stage the stage
332      */
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)) {
336             return;
337         }
338
339         if (config.isMigrateSuccess()) {
340             var stageSet = ParticipantUtils.findStageSet(compositionInProperties);
341             var nextStage = 1000;
342             for (var s : stageSet) {
343                 if (s > stage) {
344                     nextStage = Math.min(s, nextStage);
345                 }
346             }
347             if (nextStage == 1000) {
348                 intermediaryApi.updateAutomationCompositionElementState(
349                         instanceId, elementId,
350                         DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
351             } else {
352                 intermediaryApi.updateAutomationCompositionElementStage(
353                         instanceId, elementId,
354                         StateChangeResult.NO_ERROR, nextStage, "stage " + stage + " Migrated");
355             }
356         } else {
357             intermediaryApi.updateAutomationCompositionElementState(
358                     instanceId, elementId,
359                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
360         }
361     }
362
363     /**
364      * Handle a Migrate Precheck on a automation composition element.
365      *
366      * @param instanceId the instanceId
367      * @param elementId the elementId
368      */
369     public void migratePrecheck(UUID instanceId, UUID elementId) {
370         if (!execution(config.getMigratePrecheckTimerMs(),
371                 "Current Thread migrate precheck is Interrupted during execution {}", elementId)) {
372             return;
373         }
374
375         if (config.isMigratePrecheck()) {
376             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
377                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migration precheck completed");
378         } else {
379             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
380                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migration precheck failed");
381         }
382     }
383
384     /**
385      * Handle a Prepare on a automation composition element.
386      *
387      * @param instanceId the instanceId
388      * @param elementId the elementId
389      */
390     public void prepare(UUID instanceId, UUID elementId) {
391         if (!execution(config.getPrepareTimerMs(),
392                 "Current Thread prepare is Interrupted during execution {}", elementId)) {
393             return;
394         }
395
396         if (config.isPrepare()) {
397             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
398                     DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Prepare completed");
399         } else {
400             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
401                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Prepare failed");
402         }
403     }
404
405     /**
406      * Handle a Review on a automation composition element.
407      *
408      * @param instanceId the instanceId
409      * @param elementId the elementId
410      */
411     public void review(UUID instanceId, UUID elementId) {
412         if (!execution(config.getReviewTimerMs(),
413                 "Current Thread review is Interrupted during execution {}", elementId)) {
414             return;
415         }
416
417         if (config.isReview()) {
418             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
419                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Review completed");
420         } else {
421             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
422                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Review failed");
423         }
424     }
425 }