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
9 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
19 package com.oransc.rappmanager.acm.service;
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;
49 @RequiredArgsConstructor
50 public class AcmDeployer implements RappDeployer {
52 Logger logger = LoggerFactory.getLogger(AcmDeployer.class);
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;
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());
67 public void primeACMComposition(UUID compositionId, PrimeOrder primeOrder) {
68 AcTypeStateUpdate acTypeStateUpdate = new AcTypeStateUpdate();
69 acTypeStateUpdate.setPrimeOrder(primeOrder);
70 automationCompositionDefinitionApiClient.compositionDefinitionPriming(compositionId, UUID.randomUUID(),
74 public CommissioningResponse createComposition(String compositionPayload) {
75 CommissioningResponse commissioningResponse = null;
77 commissioningResponse =
78 automationCompositionDefinitionApiClient.createCompositionDefinitions(compositionPayload,
80 } catch (Exception e) {
81 logger.warn("Error in creating composition", e);
83 return commissioningResponse;
86 public CommissioningResponse deleteComposition(UUID compositionId) {
88 return automationCompositionDefinitionApiClient.deleteCompositionDefinition(compositionId,
90 } catch (Exception e) {
91 logger.warn("Error in deleting composition {}", compositionId, e);
96 boolean isCompositionInstanceStateEquals(UUID compositionId, UUID compositionIntanceId, DeployState deployState) {
97 return automationCompositionInstanceApiClient.getCompositionInstance(compositionId, compositionIntanceId,
98 UUID.randomUUID()).getDeployState().equals(deployState);
101 boolean waitForCompositionInstanceTargetState(UUID compositionId, RappInstance rappInstance,
102 DeployState deployState) {
103 boolean targetInstanceStateTransition = false;
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(),
109 sendRappInstanceStateEvent(rappInstance, deployState);
110 logger.info("Composition instance {} state is {}", rappInstance.getAcm().getAcmInstanceId(),
112 targetInstanceStateTransition = true;
115 TimeUnit.SECONDS.sleep(acmConfiguration.getRetryInterval());
118 } catch (Exception e) {
119 logger.warn("Unable to get composition instance state for composition {}", compositionId, e);
120 Thread.currentThread().interrupt();
122 return targetInstanceStateTransition;
126 public boolean deployRappInstance(Rapp rapp, RappInstance rappInstance) {
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);
139 } catch (Exception e) {
140 logger.warn("Error in deploying Rapp", e);
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(),
157 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.ACMUNDEPLOYED);
165 public boolean primeRapp(Rapp rapp) {
166 logger.info("Priming rapp {}", rapp.getName());
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);
176 logger.error("Failed to create automation composition");
178 } catch (Exception e) {
179 logger.error("Failed to create automation composition", e);
185 public boolean deprimeRapp(Rapp rapp) {
187 primeACMComposition(rapp.getCompositionId(), PrimeOrder.DEPRIME);
188 CommissioningResponse commissioningResponse = deleteComposition(rapp.getCompositionId());
189 if (commissioningResponse != null) {
192 } catch (Exception e) {
193 logger.error("Failed deprime automation composition", e);
198 public void syncRappInstanceStatus(UUID compositionId, RappInstance rappInstance) {
199 if (rappInstance.getAcm().getAcmInstanceId() != null) {
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());
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);