7554c0b3c9ccc53db06fa1f97c3d3311eb7f7c60
[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.AutomationCompositionElementDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.clamp.models.acm.concepts.LockState;
42 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
43 import org.onap.policy.models.base.PfModelException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Component;
47
48 /**
49  * This class handles implementation of automationCompositionElement updates.
50  */
51 @Component
52 @RequiredArgsConstructor
53 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56
57     private final ParticipantIntermediaryApi intermediaryApi;
58
59     @Getter
60     @Setter
61     private SimConfig config = new SimConfig();
62
63     /**
64      * Callback method to handle an update on a automation composition element.
65      *
66      * @param automationCompositionId the automationComposition Id
67      * @param element the information on the automation composition element
68      * @param properties properties Map
69      * @throws PfModelException in case of a exception
70      */
71     @Override
72     public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
73             throws PfModelException {
74         LOGGER.debug("deploy call");
75
76         if (!execution(config.getDeployTimerMs(), "Current Thread deploy is Interrupted during execution {}",
77                 element.getId())) {
78             return;
79         }
80
81         if (config.isDeploySuccess()) {
82             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
83                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
84         } else {
85             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
86                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
87         }
88     }
89
90     private boolean execution(int timeMs, String msg, UUID elementId) {
91         long endTime = System.currentTimeMillis() + timeMs;
92         while (System.currentTimeMillis() < endTime) {
93             try {
94                 if (Thread.currentThread().isInterrupted()) {
95                     LOGGER.debug(msg, elementId);
96                     return false;
97                 }
98                 Thread.sleep(10L);
99             } catch (InterruptedException e) {
100                 LOGGER.debug(msg, elementId);
101                 Thread.currentThread().interrupt();
102                 return false;
103             }
104         }
105         return true;
106     }
107
108     /**
109      * Handle a automation composition element state change.
110      *
111      * @param automationCompositionElementId the ID of the automation composition element
112      */
113     @Override
114     public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
115         LOGGER.debug("undeploy call");
116
117         if (!execution(config.getUndeployTimerMs(), "Current Thread undeploy is Interrupted during execution {}",
118                 automationCompositionElementId)) {
119             return;
120         }
121
122         if (config.isUndeploySuccess()) {
123             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
124                     automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
125                     "Undeployed");
126         } else {
127             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
128                     automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.FAILED,
129                     "Undeploy failed!");
130         }
131     }
132
133     @Override
134     public void lock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
135         LOGGER.debug("lock call");
136
137         if (!execution(config.getLockTimerMs(), "Current Thread lock is Interrupted during execution {}",
138                 automationCompositionElementId)) {
139             return;
140         }
141
142         if (config.isLockSuccess()) {
143             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
144                     automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
145         } else {
146             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
147                     automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
148         }
149     }
150
151     @Override
152     public void unlock(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
153         LOGGER.debug("unlock call");
154
155         if (!execution(config.getUnlockTimerMs(), "Current Thread unlock is Interrupted during execution {}",
156                 automationCompositionElementId)) {
157             return;
158         }
159
160         if (config.isUnlockSuccess()) {
161             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
162                     automationCompositionElementId, null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
163         } else {
164             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
165                     automationCompositionElementId, null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
166         }
167     }
168
169     @Override
170     public void delete(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
171         LOGGER.debug("delete call");
172
173         if (!execution(config.getDeleteTimerMs(), "Current Thread delete is Interrupted during execution {}",
174                 automationCompositionElementId)) {
175             return;
176         }
177
178         if (config.isDeleteSuccess()) {
179             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
180                     automationCompositionElementId, DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
181         } else {
182             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
183                     automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
184                     "Delete failed!");
185         }
186     }
187
188     @Override
189     public void update(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
190             throws PfModelException {
191         LOGGER.debug("updat call");
192
193         if (!execution(config.getUpdateTimerMs(), "Current Thread update is Interrupted during execution {}",
194                 element.getId())) {
195             return;
196         }
197
198         if (config.isUpdateSuccess()) {
199             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
200                     DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
201         } else {
202             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
203                     DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
204         }
205     }
206
207     /**
208      * Get AutomationComposition.
209      *
210      * @return the AutomationCompositions
211      */
212     public AutomationCompositions getAutomationCompositions() {
213         var result = new AutomationCompositions();
214         result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values()));
215         return result;
216     }
217
218     /**
219      * Set OutProperties.
220      *
221      * @param automationCompositionId the automationComposition Id
222      * @param elementId the automationComposition Element Id
223      * @param useState the useState
224      * @param operationalState the operationalState
225      * @param outProperties the outProperties
226      */
227     public void setOutProperties(UUID automationCompositionId, UUID elementId, String useState, String operationalState,
228             Map<String, Object> outProperties) {
229         intermediaryApi.sendAcElementInfo(automationCompositionId, elementId, useState, operationalState,
230                 outProperties);
231     }
232
233     @Override
234     public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
235             throws PfModelException {
236
237         if (!execution(config.getPrimeTimerMs(), "Current Thread prime is Interrupted during execution {}",
238                 compositionId)) {
239             return;
240         }
241
242         if (config.isPrimeSuccess()) {
243             intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
244                     "Primed");
245         } else {
246             intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED,
247                     "Prime failed!");
248         }
249     }
250
251     @Override
252     public void deprime(UUID compositionId) throws PfModelException {
253
254         if (!execution(config.getDeprimeTimerMs(), "Current Thread deprime is Interrupted during execution {}",
255                 compositionId)) {
256             return;
257         }
258
259         if (config.isDeprimeSuccess()) {
260             intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
261                     "Deprimed");
262         } else {
263             intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
264                     "Deprime failed!");
265         }
266     }
267
268     /**
269      * Get Data List.
270      *
271      * @return the InternalDatas
272      */
273     public InternalDatas getDataList() {
274         var result = new InternalDatas();
275         var map = intermediaryApi.getAutomationCompositions();
276         for (var instance : map.values()) {
277             for (var element : instance.getElements().values()) {
278                 var data = new InternalData();
279                 data.setAutomationCompositionId(instance.getInstanceId());
280                 data.setAutomationCompositionElementId(element.getId());
281                 data.setIntProperties(element.getProperties());
282                 data.setOperationalState(element.getOperationalState());
283                 data.setUseState(element.getUseState());
284                 data.setOutProperties(element.getOutProperties());
285                 result.getList().add(data);
286             }
287         }
288         return result;
289     }
290 }