2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.acm.participant.policy.main.handler;
25 import jakarta.ws.rs.core.Response.Status;
26 import java.util.ArrayList;
27 import java.util.List;
29 import java.util.UUID;
30 import java.util.concurrent.ConcurrentHashMap;
31 import org.apache.http.HttpStatus;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV1;
34 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
35 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
37 import org.onap.policy.clamp.models.acm.concepts.DeployState;
38 import org.onap.policy.clamp.models.acm.concepts.LockState;
39 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
40 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
50 * This class handles implementation of automationCompositionElement updates.
53 public class AutomationCompositionElementHandler extends AcElementListenerV1 {
55 private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandler.class);
57 private final Map<UUID, ToscaServiceTemplate> serviceTemplateMap = new ConcurrentHashMap<>();
59 private final PolicyApiHttpClient apiHttpClient;
60 private final PolicyPapHttpClient papHttpClient;
65 * @param apiHttpClient the PolicyApi Http Client
66 * @param papHttpClient the Policy Pap Http Client
67 * @param intermediaryApi the Participant Intermediary Api
69 public AutomationCompositionElementHandler(PolicyApiHttpClient apiHttpClient, PolicyPapHttpClient papHttpClient,
70 ParticipantIntermediaryApi intermediaryApi) {
71 super(intermediaryApi);
72 this.apiHttpClient = apiHttpClient;
73 this.papHttpClient = papHttpClient;
77 * Callback method to handle a automation composition element state change.
79 * @param automationCompositionId the ID of the automation composition
80 * @param automationCompositionElementId the ID of the automation composition element
83 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
84 var automationCompositionDefinition = serviceTemplateMap.get(automationCompositionElementId);
85 if (automationCompositionDefinition == null) {
86 LOGGER.debug("No policies to undeploy to {}", automationCompositionElementId);
87 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
88 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
92 var policyList = getPolicyList(automationCompositionDefinition);
93 undeployPolicies(policyList, automationCompositionElementId);
94 var policyTypeList = getPolicyTypeList(automationCompositionDefinition);
95 deletePolicyData(policyTypeList, policyList);
96 serviceTemplateMap.remove(automationCompositionElementId);
97 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
98 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
101 private void deletePolicyData(List<ToscaConceptIdentifier> policyTypeList,
102 List<ToscaConceptIdentifier> policyList) {
103 // Delete all policies of this automationComposition from policy framework
104 for (var policy : policyList) {
105 apiHttpClient.deletePolicy(policy.getName(), policy.getVersion());
107 // Delete all policy types of this automation composition from policy framework
108 for (var policyType : policyTypeList) {
109 apiHttpClient.deletePolicyType(policyType.getName(), policyType.getVersion());
113 private void deployPolicies(List<ToscaConceptIdentifier> policyList, UUID automationCompositionId,
114 UUID automationCompositionElementId) throws PfModelException {
115 var deployFailure = false;
116 // Deploy all policies of this automationComposition from Policy Framework
117 if (!policyList.isEmpty()) {
118 for (var policy : policyList) {
119 var deployPolicyResp = papHttpClient.handlePolicyDeployOrUndeploy(policy.getName(), policy.getVersion(),
120 DeploymentSubGroup.Action.POST).getStatus();
121 if (deployPolicyResp != HttpStatus.SC_ACCEPTED) {
122 deployFailure = true;
125 LOGGER.info("Policies deployed to {} successfully", automationCompositionElementId);
127 LOGGER.debug("No policies to deploy to {}", automationCompositionElementId);
129 if (!deployFailure) {
130 // Update the AC element state
131 intermediaryApi.sendAcElementInfo(automationCompositionId, automationCompositionElementId, "IDLE",
132 "ENABLED", Map.of());
133 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
134 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
136 throw new PfModelException(Status.BAD_REQUEST, "Deploy of Policy failed.");
140 private void undeployPolicies(List<ToscaConceptIdentifier> policyList, UUID automationCompositionElementId) {
141 // Undeploy all policies of this automation composition from Policy Framework
142 if (!policyList.isEmpty()) {
143 for (var policy : policyList) {
144 papHttpClient.handlePolicyDeployOrUndeploy(policy.getName(), policy.getVersion(),
145 DeploymentSubGroup.Action.DELETE);
147 LOGGER.debug("Undeployed policies from {} successfully", automationCompositionElementId);
149 LOGGER.debug("No policies are deployed to {}", automationCompositionElementId);
154 * Callback method to handle an update on automation composition element.
156 * @param automationCompositionId the automationComposition Id
157 * @param element the information on the automation composition element
158 * @param properties properties Map
159 * @throws PfModelException in case of an exception
162 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
163 throws PfModelException {
164 var createPolicyTypeResp = HttpStatus.SC_OK;
165 var createPolicyResp = HttpStatus.SC_OK;
167 var automationCompositionDefinition = element.getToscaServiceTemplateFragment();
168 if (automationCompositionDefinition.getToscaTopologyTemplate() == null) {
169 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
170 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "ToscaTopologyTemplate not defined");
173 serviceTemplateMap.put(element.getId(), automationCompositionDefinition);
174 if (automationCompositionDefinition.getPolicyTypes() != null) {
175 LOGGER.info("Found Policy Types in automation composition definition: {} , Creating Policy Types",
176 automationCompositionDefinition.getName());
177 try (var response = apiHttpClient.createPolicyType(automationCompositionDefinition)) {
178 createPolicyTypeResp = response.getStatus();
181 if (automationCompositionDefinition.getToscaTopologyTemplate().getPolicies() != null) {
182 LOGGER.info("Found Policies in automation composition definition: {} , Creating Policies",
183 automationCompositionDefinition.getName());
184 try (var response = apiHttpClient.createPolicy(automationCompositionDefinition)) {
185 createPolicyResp = response.getStatus();
188 if (createPolicyTypeResp == HttpStatus.SC_OK && createPolicyResp == HttpStatus.SC_OK) {
190 "PolicyTypes/Policies for the automation composition element : {} are created " + "successfully",
192 var policyList = getPolicyList(automationCompositionDefinition);
193 deployPolicies(policyList, automationCompositionId, element.getId());
195 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
196 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
197 "Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
201 private List<ToscaConceptIdentifier> getPolicyTypeList(ToscaServiceTemplate serviceTemplate) {
202 List<ToscaConceptIdentifier> policyTypeList = new ArrayList<>();
203 if (serviceTemplate.getPolicyTypes() != null) {
204 for (var policyType : serviceTemplate.getPolicyTypes().values()) {
205 policyTypeList.add(policyType.getKey().asIdentifier());
209 return policyTypeList;
212 private List<ToscaConceptIdentifier> getPolicyList(ToscaServiceTemplate serviceTemplate) {
213 List<ToscaConceptIdentifier> policyList = new ArrayList<>();
214 if (serviceTemplate.getToscaTopologyTemplate().getPolicies() != null) {
215 for (var gotPolicyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
216 for (var policy : gotPolicyMap.values()) {
217 policyList.add(policy.getKey().asIdentifier());
226 public void handleRestartInstance(UUID automationCompositionId, AcElementDeploy element,
227 Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
228 if (DeployState.DEPLOYING.equals(deployState)) {
229 deploy(automationCompositionId, element, properties);
232 if (DeployState.UNDEPLOYING.equals(deployState) || DeployState.DEPLOYED.equals(deployState)
233 || DeployState.UPDATING.equals(deployState)) {
234 var automationCompositionDefinition = element.getToscaServiceTemplateFragment();
235 serviceTemplateMap.put(element.getId(), automationCompositionDefinition);
237 if (DeployState.UNDEPLOYING.equals(deployState)) {
238 undeploy(automationCompositionId, element.getId());
241 deployState = AcmUtils.deployCompleted(deployState);
242 lockState = AcmUtils.lockCompleted(deployState, lockState);
243 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(), deployState,
244 lockState, StateChangeResult.NO_ERROR, "Restarted");