2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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 java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.UUID;
30 import javax.ws.rs.core.Response.Status;
31 import lombok.RequiredArgsConstructor;
33 import org.apache.http.HttpStatus;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
35 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
36 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
37 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
38 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
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 @RequiredArgsConstructor
54 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
56 private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandler.class);
58 private final Map<UUID, ToscaServiceTemplate> serviceTemplateMap = new LinkedHashMap<>();
60 private final PolicyApiHttpClient apiHttpClient;
61 private final PolicyPapHttpClient papHttpClient;
64 private ParticipantIntermediaryApi intermediaryApi;
67 * Callback method to handle a automation composition element state change.
69 * @param automationCompositionId the ID of the automation composition
70 * @param automationCompositionElementId the ID of the automation composition element
73 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
74 var automationCompositionDefinition = serviceTemplateMap.get(automationCompositionElementId);
75 if (automationCompositionDefinition == null) {
76 LOGGER.debug("No policies to undeploy to {}", automationCompositionElementId);
77 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
78 automationCompositionElementId, DeployState.UNDEPLOYED, LockState.NONE);
81 var policyList = getPolicyList(automationCompositionDefinition);
82 undeployPolicies(policyList, automationCompositionElementId);
83 var policyTypeList = getPolicyTypeList(automationCompositionDefinition);
84 deletePolicyData(policyTypeList, policyList);
85 serviceTemplateMap.remove(automationCompositionElementId);
86 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
87 automationCompositionElementId, DeployState.UNDEPLOYED, LockState.NONE);
90 private void deletePolicyData(List<ToscaConceptIdentifier> policyTypeList,
91 List<ToscaConceptIdentifier> policyList) {
92 // Delete all policies of this automationComposition from policy framework
93 for (var policy : policyList) {
94 apiHttpClient.deletePolicy(policy.getName(), policy.getVersion());
96 // Delete all policy types of this automation composition from policy framework
97 for (var policyType : policyTypeList) {
98 apiHttpClient.deletePolicyType(policyType.getName(), policyType.getVersion());
102 private void deployPolicies(List<ToscaConceptIdentifier> policyList, UUID automationCompositionId,
103 UUID automationCompositionElementId) throws PfModelException {
104 var deployFailure = false;
105 // Deploy all policies of this automationComposition from Policy Framework
106 if (!policyList.isEmpty()) {
107 for (var policy : policyList) {
108 var deployPolicyResp = papHttpClient.handlePolicyDeployOrUndeploy(policy.getName(), policy.getVersion(),
109 DeploymentSubGroup.Action.POST).getStatus();
110 if (deployPolicyResp != HttpStatus.SC_ACCEPTED) {
111 deployFailure = true;
114 LOGGER.info("Policies deployed to {} successfully", automationCompositionElementId);
116 LOGGER.debug("No policies to deploy to {}", automationCompositionElementId);
118 if (!deployFailure) {
119 // Update the AC element state
120 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
121 automationCompositionElementId, DeployState.DEPLOYED, LockState.LOCKED);
123 throw new PfModelException(Status.BAD_REQUEST, "Deploy of Policy failed.");
127 private void undeployPolicies(List<ToscaConceptIdentifier> policyList, UUID automationCompositionElementId) {
128 // Undeploy all policies of this automation composition from Policy Framework
129 if (!policyList.isEmpty()) {
130 for (var policy : policyList) {
131 papHttpClient.handlePolicyDeployOrUndeploy(policy.getName(), policy.getVersion(),
132 DeploymentSubGroup.Action.DELETE);
134 LOGGER.debug("Undeployed policies from {} successfully", automationCompositionElementId);
136 LOGGER.debug("No policies are deployed to {}", automationCompositionElementId);
141 * Callback method to handle an update on automation composition element.
143 * @param automationCompositionId the automationComposition Id
144 * @param element the information on the automation composition element
145 * @param properties properties Map
146 * @throws PfModelException in case of an exception
149 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
150 throws PfModelException {
151 var createPolicyTypeResp = HttpStatus.SC_OK;
152 var createPolicyResp = HttpStatus.SC_OK;
154 var automationCompositionDefinition = element.getToscaServiceTemplateFragment();
155 if (automationCompositionDefinition.getToscaTopologyTemplate() == null) {
156 throw new PfModelException(Status.BAD_REQUEST, "ToscaTopologyTemplate not defined");
158 serviceTemplateMap.put(element.getId(), automationCompositionDefinition);
159 if (automationCompositionDefinition.getPolicyTypes() != null) {
160 LOGGER.info("Found Policy Types in automation composition definition: {} , Creating Policy Types",
161 automationCompositionDefinition.getName());
162 createPolicyTypeResp = apiHttpClient.createPolicyType(automationCompositionDefinition).getStatus();
164 if (automationCompositionDefinition.getToscaTopologyTemplate().getPolicies() != null) {
165 LOGGER.info("Found Policies in automation composition definition: {} , Creating Policies",
166 automationCompositionDefinition.getName());
167 createPolicyResp = apiHttpClient.createPolicy(automationCompositionDefinition).getStatus();
169 if (createPolicyTypeResp == HttpStatus.SC_OK && createPolicyResp == HttpStatus.SC_OK) {
171 "PolicyTypes/Policies for the automation composition element : {} are created " + "successfully",
173 var policyList = getPolicyList(automationCompositionDefinition);
174 deployPolicies(policyList, automationCompositionId, element.getId());
176 throw new PfModelException(Status.BAD_REQUEST,
177 "Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
181 private List<ToscaConceptIdentifier> getPolicyTypeList(ToscaServiceTemplate serviceTemplate) {
182 List<ToscaConceptIdentifier> policyTypeList = new ArrayList<>();
183 if (serviceTemplate.getPolicyTypes() != null) {
184 for (var policyType : serviceTemplate.getPolicyTypes().values()) {
185 policyTypeList.add(policyType.getKey().asIdentifier());
189 return policyTypeList;
192 private List<ToscaConceptIdentifier> getPolicyList(ToscaServiceTemplate serviceTemplate) {
193 List<ToscaConceptIdentifier> policyList = new ArrayList<>();
194 if (serviceTemplate.getToscaTopologyTemplate().getPolicies() != null) {
195 for (var gotPolicyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
196 for (var policy : gotPolicyMap.values()) {
197 policyList.add(policy.getKey().asIdentifier());
206 public String getUseState(UUID automationCompositionId, UUID automationCompositionElementId)
207 throws PfModelException {
212 public String getOperationalState(UUID automationCompositionId, UUID automationCompositionElementId)
213 throws PfModelException {