Fix sonar code smells
[nonrtric/plt/rappmanager.git] / rapp-manager-acm / src / main / java / com / oransc / rappmanager / acm / service / AcmDeployer.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
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  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager.acm.service;
20
21 import com.oransc.rappmanager.acm.configuration.ACMConfiguration;
22 import com.oransc.rappmanager.acm.rest.AutomationCompositionDefinitionApiClient;
23 import com.oransc.rappmanager.acm.rest.AutomationCompositionInstanceApiClient;
24 import com.oransc.rappmanager.models.RappDeployer;
25 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
26 import com.oransc.rappmanager.models.rapp.Rapp;
27 import com.oransc.rappmanager.models.rapp.RappEvent;
28 import com.oransc.rappmanager.models.rapp.RappState;
29 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
30 import com.oransc.rappmanager.models.rappinstance.RappInstance;
31 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
32 import java.util.UUID;
33 import java.util.concurrent.TimeUnit;
34 import lombok.RequiredArgsConstructor;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
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.messages.rest.commissioning.AcTypeStateUpdate;
39 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
40 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.PrimeOrder;
41 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.AcInstanceStateUpdate;
42 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
43 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Service;
47 import org.springframework.web.client.RestClientException;
48
49 @Service
50 @RequiredArgsConstructor
51 public class AcmDeployer implements RappDeployer {
52
53     Logger logger = LoggerFactory.getLogger(AcmDeployer.class);
54
55     private final AutomationCompositionDefinitionApiClient automationCompositionDefinitionApiClient;
56     private final AutomationCompositionInstanceApiClient automationCompositionInstanceApiClient;
57     private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
58     private final RappInstanceStateMachine rappInstanceStateMachine;
59     private final ACMConfiguration acmConfiguration;
60
61     void updateACMInstanceState(UUID compositionId, RappACMInstance rappACMInstance, DeployOrder deployOrder) {
62         AcInstanceStateUpdate acInstanceStateUpdate = new AcInstanceStateUpdate();
63         acInstanceStateUpdate.setDeployOrder(deployOrder);
64         automationCompositionInstanceApiClient.compositionInstanceState(compositionId,
65                 rappACMInstance.getAcmInstanceId(), acInstanceStateUpdate, UUID.randomUUID());
66     }
67
68     public void primeACMComposition(UUID compositionId, PrimeOrder primeOrder) {
69         AcTypeStateUpdate acTypeStateUpdate = new AcTypeStateUpdate();
70         acTypeStateUpdate.setPrimeOrder(primeOrder);
71         automationCompositionDefinitionApiClient.compositionDefinitionPriming(compositionId, UUID.randomUUID(),
72                 acTypeStateUpdate);
73     }
74
75     public CommissioningResponse createComposition(String compositionPayload) {
76         CommissioningResponse commissioningResponse = null;
77         try {
78             commissioningResponse =
79                     automationCompositionDefinitionApiClient.createCompositionDefinitions(compositionPayload,
80                             UUID.randomUUID());
81         } catch (Exception e) {
82             logger.warn("Error in creating composition", e);
83         }
84         return commissioningResponse;
85     }
86
87     public CommissioningResponse deleteComposition(UUID compositionId) {
88         try {
89             return automationCompositionDefinitionApiClient.deleteCompositionDefinition(compositionId,
90                     UUID.randomUUID());
91         } catch (Exception e) {
92             logger.warn("Error in deleting composition {}", compositionId, e);
93         }
94         return null;
95     }
96
97     boolean isCompositionInstanceStateEquals(UUID compositionId, UUID compositionIntanceId, DeployState deployState) {
98         return automationCompositionInstanceApiClient.getCompositionInstance(compositionId, compositionIntanceId,
99                 UUID.randomUUID()).getDeployState().equals(deployState);
100     }
101
102     boolean waitForCompositionInstanceTargetState(UUID compositionId, RappInstance rappInstance,
103             DeployState deployState) {
104         boolean targetInstanceStateTransition = false;
105         try {
106             for (int i = 0; i < acmConfiguration.getMaxRetries(); i++) {
107                 logger.debug("Composition instance state check {}", i + 1);
108                 if (isCompositionInstanceStateEquals(compositionId, rappInstance.getAcm().getAcmInstanceId(),
109                         deployState)) {
110                     sendRappInstanceStateEvent(rappInstance, deployState);
111                     logger.info("Composition instance {} state is {}", rappInstance.getAcm().getAcmInstanceId(),
112                             deployState);
113                     targetInstanceStateTransition = true;
114                     break;
115                 } else {
116                     TimeUnit.SECONDS.sleep(acmConfiguration.getRetryInterval());
117                 }
118             }
119         } catch (Exception e) {
120             logger.warn("Unable to get composition instance state for composition {}", compositionId, e);
121             Thread.currentThread().interrupt();
122         }
123         return targetInstanceStateTransition;
124     }
125
126     @Override
127     public boolean deployRappInstance(Rapp rapp, RappInstance rappInstance) {
128         try {
129             String instantiationPayload =
130                     rappCsarConfigurationHandler.getInstantiationPayload(rapp, rappInstance.getAcm(),
131                             rapp.getCompositionId());
132             InstantiationResponse instantiationResponse =
133                     automationCompositionInstanceApiClient.createCompositionInstance(rapp.getCompositionId(),
134                             instantiationPayload, UUID.randomUUID());
135             if (instantiationResponse.getInstanceId() != null) {
136                 rappInstance.getAcm().setAcmInstanceId(instantiationResponse.getInstanceId());
137                 updateACMInstanceState(rapp.getCompositionId(), rappInstance.getAcm(), DeployOrder.DEPLOY);
138                 return true;
139             }
140         } catch (Exception e) {
141             logger.warn("Error in deploying Rapp", e);
142         }
143         return false;
144     }
145
146     @Override
147     public boolean undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
148         AutomationComposition automationComposition =
149                 automationCompositionInstanceApiClient.getCompositionInstance(rapp.getCompositionId(),
150                         rappInstance.getAcm().getAcmInstanceId(), UUID.randomUUID());
151         if (automationComposition.getDeployState().equals(DeployState.DEPLOYED) && automationComposition.getLockState()
152                                                                                            .equals(LockState.LOCKED)) {
153             updateACMInstanceState(rapp.getCompositionId(), rappInstance.getAcm(), DeployOrder.UNDEPLOY);
154             if (waitForCompositionInstanceTargetState(rapp.getCompositionId(), rappInstance, DeployState.UNDEPLOYED)) {
155                 automationCompositionInstanceApiClient.deleteCompositionInstance(
156                         automationComposition.getCompositionId(), automationComposition.getInstanceId(),
157                         UUID.randomUUID());
158                 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.ACMUNDEPLOYED);
159                 return true;
160             }
161         }
162         return false;
163     }
164
165     @Override
166     public boolean primeRapp(Rapp rapp) {
167         logger.info("Priming rapp {}", rapp.getName());
168         try {
169             String compositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
170             CommissioningResponse commissioningResponse = createComposition(compositionPayload);
171             if (commissioningResponse != null && commissioningResponse.getCompositionId() != null) {
172                 rapp.setCompositionId(commissioningResponse.getCompositionId());
173                 logger.info("Priming automation Composition");
174                 primeACMComposition(commissioningResponse.getCompositionId(), PrimeOrder.PRIME);
175                 rapp.setState(RappState.PRIMED);
176                 return true;
177             } else {
178                 logger.error("Failed to create automation composition");
179             }
180         } catch (Exception e) {
181             logger.error("Failed to create automation composition", e);
182         }
183         return false;
184     }
185
186     @Override
187     public boolean deprimeRapp(Rapp rapp) {
188         try {
189             primeACMComposition(rapp.getCompositionId(), PrimeOrder.DEPRIME);
190             CommissioningResponse commissioningResponse = deleteComposition(rapp.getCompositionId());
191             if (commissioningResponse != null) {
192                 rapp.setState(RappState.COMMISSIONED);
193                 return true;
194             }
195         } catch (Exception e) {
196             logger.error("Failed deprime automation composition", e);
197         }
198         return false;
199     }
200
201     public void syncRappInstanceStatus(UUID compositionId, RappInstance rappInstance) {
202         if (rappInstance.getAcm().getAcmInstanceId() != null) {
203             try {
204                 AutomationComposition compositionInstance =
205                         automationCompositionInstanceApiClient.getCompositionInstance(compositionId,
206                                 rappInstance.getAcm().getAcmInstanceId(), UUID.randomUUID());
207                 sendRappInstanceStateEvent(rappInstance, compositionInstance.getDeployState());
208             } catch (RestClientException exception) {
209                 logger.warn("Unable to get the ACM details for rapp instance {}", rappInstance.getRappInstanceId());
210             }
211         }
212     }
213
214     void sendRappInstanceStateEvent(RappInstance rappInstance, DeployState deployState) {
215         if (deployState.equals(DeployState.DEPLOYED)) {
216             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.ACMDEPLOYED);
217         } else if (deployState.equals(DeployState.UNDEPLOYED)) {
218             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.ACMUNDEPLOYED);
219         } else if (deployState.equals(DeployState.DEPLOYING)) {
220             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
221         } else if (deployState.equals(DeployState.UNDEPLOYING)) {
222             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.UNDEPLOYING);
223         }
224     }
225 }