7409c1137cd29dceca69d6a7899a147b9ffbf69c
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.lang.invoke.MethodHandles;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import lombok.Getter;
29 import lombok.RequiredArgsConstructor;
30 import lombok.Setter;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
33 import org.onap.policy.clamp.acm.participant.sim.model.InternalData;
34 import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas;
35 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
41 import org.onap.policy.clamp.models.acm.concepts.DeployState;
42 import org.onap.policy.clamp.models.acm.concepts.LockState;
43 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
44 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Component;
50
51 /**
52  * This class handles implementation of automationCompositionElement updates.
53  */
54 @Component
55 @RequiredArgsConstructor
56 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
57
58     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60     private final ParticipantIntermediaryApi intermediaryApi;
61
62     @Getter
63     @Setter
64     private SimConfig config = new SimConfig();
65
66     /**
67      * Callback method to handle an update on a automation composition element.
68      *
69      * @param automationCompositionId the automationComposition Id
70      * @param element the information on the automation composition element
71      * @param properties properties Map
72      * @throws PfModelException in case of a exception
73      */
74     @Override
75     public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
76             throws PfModelException {
77         LOGGER.debug("deploy call");
78
79         if (!execution(config.getDeployTimerMs(), "Current Thread deploy is Interrupted during execution {}",
80                 element.getId())) {
81             return;
82         }
83
84         if (config.isDeploySuccess()) {
85             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
86                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
87         } else {
88             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
89                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
90         }
91     }
92
93     private boolean execution(int timeMs, String msg, UUID elementId) {
94         long endTime = System.currentTimeMillis() + timeMs;
95         while (System.currentTimeMillis() < endTime) {
96             try {
97                 if (Thread.currentThread().isInterrupted()) {
98                     LOGGER.debug(msg, elementId);
99                     return false;
100                 }
101                 Thread.sleep(10L);
102             } catch (InterruptedException e) {
103                 LOGGER.debug(msg, elementId);
104                 Thread.currentThread().interrupt();
105                 return false;
106             }
107         }
108         return true;
109     }
110
111     /**
112      * Handle a automation composition element state change.
113      *
114      * @param automationCompositionElementId the ID of the automation composition element
115      */
116     @Override
117     public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
118         LOGGER.debug("undeploy call");
119
120         if (!execution(config.getUndeployTimerMs(), "Current Thread undeploy is Interrupted during execution {}",
121                 automationCompositionElementId)) {
122             return;
123         }
124
125         if (config.isUndeploySuccess()) {
126             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
127                     automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
128                     "Undeployed");
129         } else {
130             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
131                     automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.FAILED,
132                     "Undeploy failed!");
133         }
134     }
135
136     @Override
137     public void lock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
138         LOGGER.debug("lock call");
139
140         if (!execution(config.getLockTimerMs(), "Current Thread lock is Interrupted during execution {}",
141                 automationCompositionElementId)) {
142             return;
143         }
144
145         if (config.isLockSuccess()) {
146             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
147                     automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
148         } else {
149             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
150                     automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
151         }
152     }
153
154     @Override
155     public void unlock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
156         LOGGER.debug("unlock call");
157
158         if (!execution(config.getUnlockTimerMs(), "Current Thread unlock is Interrupted during execution {}",
159                 automationCompositionElementId)) {
160             return;
161         }
162
163         if (config.isUnlockSuccess()) {
164             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
165                     automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
166         } else {
167             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
168                     automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
169         }
170     }
171
172     @Override
173     public void delete(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
174         LOGGER.debug("delete call");
175
176         if (!execution(config.getDeleteTimerMs(), "Current Thread delete is Interrupted during execution {}",
177                 automationCompositionElementId)) {
178             return;
179         }
180
181         if (config.isDeleteSuccess()) {
182             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
183                     automationCompositionElementId, DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
184         } else {
185             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
186                     automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
187                     "Delete failed!");
188         }
189     }
190
191     @Override
192     public void update(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
193             throws PfModelException {
194         LOGGER.debug("update call");
195
196         if (!execution(config.getUpdateTimerMs(), "Current Thread update is Interrupted during execution {}",
197                 element.getId())) {
198             return;
199         }
200
201         if (config.isUpdateSuccess()) {
202             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
203                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
204         } else {
205             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
206                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
207         }
208     }
209
210     /**
211      * Get AutomationComposition.
212      *
213      * @return the AutomationCompositions
214      */
215     public AutomationCompositions getAutomationCompositions() {
216         var result = new AutomationCompositions();
217         result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
218         return result;
219     }
220
221     public AutomationComposition getAutomationComposition(UUID instanceId) {
222         return intermediaryApi.getAutomationComposition(instanceId);
223     }
224
225     /**
226      * Set OutProperties.
227      *
228      * @param automationCompositionId the automationComposition Id
229      * @param elementId the automationComposition Element Id
230      * @param useState the useState
231      * @param operationalState the operationalState
232      * @param outProperties the outProperties
233      */
234     public void setOutProperties(UUID automationCompositionId, UUID elementId, String useState, String operationalState,
235             Map<String, Object> outProperties) {
236         intermediaryApi.sendAcElementInfo(automationCompositionId, elementId, useState, operationalState,
237                 outProperties);
238     }
239
240     @Override
241     public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
242             throws PfModelException {
243         LOGGER.debug("prime call");
244
245         if (!execution(config.getPrimeTimerMs(), "Current Thread prime is Interrupted during execution {}",
246                 compositionId)) {
247             return;
248         }
249
250         if (config.isPrimeSuccess()) {
251             intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
252                     "Primed");
253         } else {
254             intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
255                     "Prime failed!");
256         }
257     }
258
259     @Override
260     public void deprime(UUID compositionId) throws PfModelException {
261         LOGGER.debug("deprime call");
262
263         if (!execution(config.getDeprimeTimerMs(), "Current Thread deprime is Interrupted during execution {}",
264                 compositionId)) {
265             return;
266         }
267
268         if (config.isDeprimeSuccess()) {
269             intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
270                     "Deprimed");
271         } else {
272             intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
273                     "Deprime failed!");
274         }
275     }
276
277     /**
278      * Get Instance Data List.
279      *
280      * @return the InternalDatas
281      */
282     public InternalDatas getDataList() {
283         var result = new InternalDatas();
284         var map = intermediaryApi.getAutomationCompositions();
285         for (var instance : map.values()) {
286             for (var element : instance.getElements().values()) {
287                 var data = new InternalData();
288                 data.setCompositionId(instance.getCompositionId());
289                 data.setAutomationCompositionId(instance.getInstanceId());
290                 data.setAutomationCompositionElementId(element.getId());
291                 data.setIntProperties(element.getProperties());
292                 data.setOperationalState(element.getOperationalState());
293                 data.setUseState(element.getUseState());
294                 data.setOutProperties(element.getOutProperties());
295                 result.getList().add(data);
296             }
297         }
298         return result;
299     }
300
301     @Override
302     public void handleRestartComposition(UUID compositionId,
303             List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state)
304             throws PfModelException {
305         LOGGER.debug("restart composition definition call");
306         switch (state) {
307             case PRIMING:
308                 prime(compositionId, elementDefinitionList);
309                 break;
310
311             case DEPRIMING:
312                 deprime(compositionId);
313                 break;
314
315             default:
316                 intermediaryApi.updateCompositionState(compositionId, state, StateChangeResult.NO_ERROR, "Restarted");
317         }
318     }
319
320     @Override
321     public void handleRestartInstance(UUID automationCompositionId, AcElementDeploy element,
322             Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
323         LOGGER.debug("restart instance call");
324         if (!AcmUtils.isInTransitionalState(deployState, lockState)) {
325             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
326                     deployState, lockState, StateChangeResult.NO_ERROR, "Restarted");
327             return;
328         }
329         if (DeployState.DEPLOYING.equals(deployState)) {
330             deploy(automationCompositionId, element, properties);
331             return;
332         }
333         if (DeployState.UNDEPLOYING.equals(deployState)) {
334             undeploy(automationCompositionId, element.getId());
335             return;
336         }
337         if (DeployState.UPDATING.equals(deployState)) {
338             update(automationCompositionId, element, properties);
339             return;
340         }
341         if (DeployState.DELETING.equals(deployState)) {
342             delete(automationCompositionId, element.getId());
343             return;
344         }
345         if (LockState.LOCKING.equals(lockState)) {
346             lock(automationCompositionId, element.getId());
347             return;
348         }
349         if (LockState.UNLOCKING.equals(lockState)) {
350             unlock(automationCompositionId, element.getId());
351         }
352     }
353
354     /**
355      * Get Composition Data List.
356      *
357      * @return the InternalDatas
358      */
359     public InternalDatas getCompositionDataList() {
360         var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions();
361         var internalDatas = new InternalDatas();
362         for (var entry : acElementsDefinitions.entrySet()) {
363             for (var acElementsDefinition : entry.getValue().values()) {
364                 var internalData = new InternalData();
365                 internalData.setCompositionId(entry.getKey());
366                 internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId());
367                 internalData.setIntProperties(
368                         acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties());
369                 internalData.setOutProperties(acElementsDefinition.getOutProperties());
370                 internalDatas.getList().add(internalData);
371             }
372         }
373         return internalDatas;
374     }
375
376     public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId,
377             Map<String, Object> outProperties) {
378         intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties);
379
380     }
381
382     @Override
383     public void migrate(UUID automationCompositionId, AcElementDeploy element, UUID compositionTargetId,
384             Map<String, Object> properties) throws PfModelException {
385         LOGGER.debug("migrate call");
386
387         if (!execution(config.getMigrateTimerMs(), "Current Thread migrate is Interrupted during execution {}",
388                 element.getId())) {
389             return;
390         }
391
392         if (config.isMigrateSuccess()) {
393             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
394                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
395         } else {
396             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
397                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
398         }
399     }
400 }