d37edf761d7fcbd8f5a60de08752a9d4960d64e4
[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.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;
43
44 /**
45  * This class handles implementation of Simulator Service.
46  */
47 @Service
48 @RequiredArgsConstructor
49 public class SimulatorService {
50
51     private final ParticipantIntermediaryApi intermediaryApi;
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(SimulatorService.class);
54
55     @Getter
56     @Setter
57     private SimConfig config = new SimConfig();
58
59     /**
60      * Get AutomationComposition.
61      *
62      * @return the AutomationCompositions
63      */
64     public AutomationCompositions getAutomationCompositions() {
65         var result = new AutomationCompositions();
66         result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
67         return result;
68     }
69
70     public AutomationComposition getAutomationComposition(UUID instanceId) {
71         return intermediaryApi.getAutomationComposition(instanceId);
72     }
73
74     /**
75      * Set OutProperties.
76      *
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
82      */
83     public void setOutProperties(UUID instanceId, UUID elementId, String useState, String operationalState,
84                                  Map<String, Object> outProperties) {
85         intermediaryApi.sendAcElementInfo(instanceId, elementId, useState, operationalState,
86                 outProperties);
87     }
88
89     /**
90      * Get Instance Data List.
91      *
92      * @return the InternalDatas
93      */
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);
108             }
109         }
110         return result;
111     }
112
113     /**
114      * Get Composition Data List.
115      *
116      * @return the InternalDatas
117      */
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);
130             }
131         }
132         return internalDatas;
133     }
134
135     public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
136                                             Map<String, Object> outProperties) {
137         intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);
138
139     }
140
141     private boolean execution(int timeMs, String msg, UUID elementId) {
142         long endTime = System.currentTimeMillis() + timeMs;
143         while (System.currentTimeMillis() < endTime) {
144             try {
145                 if (Thread.currentThread().isInterrupted()) {
146                     LOGGER.debug(msg, elementId);
147                     return false;
148                 }
149                 Thread.sleep(10L);
150             } catch (InterruptedException e) {
151                 LOGGER.debug(msg, elementId);
152                 Thread.currentThread().interrupt();
153                 return false;
154             }
155         }
156         return true;
157     }
158
159     /**
160      * Handle a deploy on a automation composition element.
161      *
162      * @param instanceId the instanceId
163      * @param elementId the elementId
164      */
165     public void deploy(UUID instanceId, UUID elementId) {
166         if (!execution(getConfig().getDeployTimerMs(),
167                 "Current Thread deploy is Interrupted during execution {}", elementId)) {
168             return;
169         }
170
171         if (getConfig().isDeploySuccess()) {
172             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
173                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
174         } else {
175             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
176                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
177         }
178     }
179
180     /**
181      * Handle an udeploy on a automation composition element.
182      *
183      * @param instanceId the instanceId
184      * @param elementId the elementId
185      */
186     public void undeploy(UUID instanceId, UUID elementId) {
187         if (!execution(getConfig().getUndeployTimerMs(),
188                 "Current Thread undeploy is Interrupted during execution {}", elementId)) {
189             return;
190         }
191
192         if (getConfig().isUndeploySuccess()) {
193             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
194                     DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
195         } else {
196             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
197                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Undeploy failed!");
198         }
199     }
200
201     /**
202      * Handle a lock on a automation composition element.
203      *
204      * @param instanceId the instanceId
205      * @param elementId the elementId
206      */
207     public void lock(UUID instanceId, UUID elementId) {
208         if (!execution(getConfig().getLockTimerMs(),
209                 "Current Thread lock is Interrupted during execution {}", elementId)) {
210             return;
211         }
212
213         if (getConfig().isLockSuccess()) {
214             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
215                     null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
216         } else {
217             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
218                     null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
219         }
220     }
221
222     /**
223      * Handle an unlock on a automation composition element.
224      *
225      * @param instanceId the instanceId
226      * @param elementId the elementId
227      */
228     public void unlock(UUID instanceId, UUID elementId) {
229         if (!execution(getConfig().getUnlockTimerMs(),
230                 "Current Thread unlock is Interrupted during execution {}", elementId)) {
231             return;
232         }
233
234         if (getConfig().isUnlockSuccess()) {
235             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
236                     null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
237         } else {
238             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
239                     null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
240         }
241     }
242
243     /**
244      * Handle a delete on a automation composition element.
245      *
246      * @param instanceId the instanceId
247      * @param elementId the elementId
248      */
249     public void delete(UUID instanceId, UUID elementId) {
250         if (!execution(getConfig().getDeleteTimerMs(),
251                 "Current Thread delete is Interrupted during execution {}", elementId)) {
252             return;
253         }
254
255         if (getConfig().isDeleteSuccess()) {
256             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
257                     DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
258         } else {
259             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
260                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Delete failed!");
261         }
262     }
263
264     /**
265      * Handle an update on a automation composition element.
266      *
267      * @param instanceId the instanceId
268      * @param elementId the elementId
269      */
270     public void update(UUID instanceId, UUID elementId) {
271         if (!execution(getConfig().getUpdateTimerMs(),
272                 "Current Thread update is Interrupted during execution {}", elementId)) {
273             return;
274         }
275
276         if (getConfig().isUpdateSuccess()) {
277             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
278                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
279         } else {
280             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
281                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
282         }
283     }
284
285     /**
286      * Handle a prime on a automation composition definition.
287      *
288      * @param compositionId the compositionId
289      */
290     public void prime(UUID compositionId) {
291         if (!execution(getConfig().getPrimeTimerMs(),
292                 "Current Thread prime is Interrupted during execution {}", compositionId)) {
293             return;
294         }
295
296         if (getConfig().isPrimeSuccess()) {
297             intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
298                     "Primed");
299         } else {
300             intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
301                     "Prime failed!");
302         }
303     }
304
305     /**
306      * Handle a deprime on a automation composition definition.
307      *
308      * @param compositionId the compositionId
309      */
310     public void deprime(UUID compositionId) {
311         if (!execution(getConfig().getDeprimeTimerMs(),
312                 "Current Thread deprime is Interrupted during execution {}", compositionId)) {
313             return;
314         }
315
316         if (getConfig().isDeprimeSuccess()) {
317             intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
318                     "Deprimed");
319         } else {
320             intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
321                     "Deprime failed!");
322         }
323     }
324
325     /**
326      * Handle a migrate on a automation composition element.
327      *
328      * @param instanceId the instanceId
329      * @param elementId the elementId
330      */
331     public void migrate(UUID instanceId, UUID elementId) {
332         if (!execution(getConfig().getMigrateTimerMs(),
333                 "Current Thread migrate is Interrupted during execution {}", elementId)) {
334             return;
335         }
336
337         if (getConfig().isMigrateSuccess()) {
338             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
339                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
340         } else {
341             intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId,
342                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
343         }
344     }
345 }