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