2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021,2022 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.time.Instant;
26 import java.util.LinkedHashMap;
28 import java.util.Map.Entry;
29 import java.util.UUID;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
33 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
34 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
35 import org.onap.policy.clamp.models.acm.concepts.AcElementStatistics;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
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.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.stereotype.Component;
53 * This class handles implementation of automationCompositionElement updates.
56 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
58 private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandler.class);
59 private final Map<String, String> policyTypeMap = new LinkedHashMap<>();
60 private final Map<String, String> policyMap = new LinkedHashMap<>();
62 private final PolicyApiHttpClient apiHttpClient;
63 private final PolicyPapHttpClient papHttpClient;
66 private ParticipantIntermediaryApi intermediaryApi;
71 * @param apiHttpClient the Policy Api Http Client
72 * @param papHttpClient the Policy Pap Http Client
74 public AutomationCompositionElementHandler(PolicyApiHttpClient apiHttpClient, PolicyPapHttpClient papHttpClient) {
75 this.papHttpClient = papHttpClient;
76 this.apiHttpClient = apiHttpClient;
80 * Callback method to handle a automation composition element state change.
82 * @param automationCompositionId the ID of the automation composition
83 * @param automationCompositionElementId the ID of the automation composition element
84 * @param currentState the current state of the automation composition element
85 * @param orderedState the state to which the automation composition element is changing to
88 public void automationCompositionElementStateChange(ToscaConceptIdentifier automationCompositionId,
89 UUID automationCompositionElementId,
90 AutomationCompositionState currentState,
91 AutomationCompositionOrderedState orderedState) {
92 switch (orderedState) {
95 undeployPolicies(automationCompositionElementId);
96 deletePolicyData(automationCompositionId, automationCompositionElementId, orderedState);
97 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
98 automationCompositionElementId, orderedState, AutomationCompositionState.UNINITIALISED,
99 ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
100 } catch (PfModelRuntimeException e) {
101 LOGGER.debug("Undeploying/Deleting policy failed {}", automationCompositionElementId, e);
106 undeployPolicies(automationCompositionElementId);
107 } catch (PfModelRuntimeException e) {
108 LOGGER.debug("Undeploying policies failed - no policies to undeploy {}",
109 automationCompositionElementId);
111 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
112 automationCompositionElementId, orderedState, AutomationCompositionState.PASSIVE,
113 ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
116 LOGGER.info("Running state is not supported");
119 LOGGER.debug("Unknown orderedstate {}", orderedState);
124 private void deletePolicyData(ToscaConceptIdentifier automationCompositionId,
125 UUID automationCompositionElementId, AutomationCompositionOrderedState newState) {
126 // Delete all policies of this automationComposition from policy framework
127 for (Entry<String, String> policy : policyMap.entrySet()) {
128 apiHttpClient.deletePolicy(policy.getKey(), policy.getValue());
131 // Delete all policy types of this automation composition from policy framework
132 for (Entry<String, String> policyType : policyTypeMap.entrySet()) {
133 apiHttpClient.deletePolicyType(policyType.getKey(), policyType.getValue());
135 policyTypeMap.clear();
136 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
137 automationCompositionElementId, newState, AutomationCompositionState.UNINITIALISED,
138 ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
141 private void deployPolicies(ToscaConceptIdentifier automationCompositionId, UUID automationCompositionElementId,
142 AutomationCompositionOrderedState newState) {
143 // Deploy all policies of this automationComposition from Policy Framework
144 if (!policyMap.entrySet().isEmpty()) {
145 for (Entry<String, String> policy : policyMap.entrySet()) {
146 papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(),
147 DeploymentSubGroup.Action.POST);
149 LOGGER.debug("Policies deployed to {} successfully", automationCompositionElementId);
151 LOGGER.debug("No policies to deploy to {}", automationCompositionElementId);
153 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
154 automationCompositionElementId, newState, AutomationCompositionState.PASSIVE,
155 ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
158 private void undeployPolicies(UUID automationCompositionElementId) {
159 // Undeploy all policies of this automation composition from Policy Framework
160 if (!policyMap.entrySet().isEmpty()) {
161 for (Entry<String, String> policy : policyMap.entrySet()) {
162 papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(),
163 DeploymentSubGroup.Action.DELETE);
165 LOGGER.debug("Undeployed policies from {} successfully", automationCompositionElementId);
167 LOGGER.debug("No policies are deployed to {}", automationCompositionElementId);
172 * Callback method to handle an update on a automation composition element.
174 * @param element the information on the automation composition element
175 * @param acElementDefinition toscaNodeTemplate
176 * @throws PfModelException in case of an exception
179 public void automationCompositionElementUpdate(ToscaConceptIdentifier automationCompositionId,
180 AutomationCompositionElement element,
181 ToscaNodeTemplate acElementDefinition)
182 throws PfModelException {
183 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
184 element.getOrderedState(),
185 AutomationCompositionState.PASSIVE, ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
186 ToscaServiceTemplate automationCompositionDefinition = element.getToscaServiceTemplateFragment();
187 if (automationCompositionDefinition.getToscaTopologyTemplate() != null) {
188 if (automationCompositionDefinition.getPolicyTypes() != null) {
189 for (ToscaPolicyType policyType : automationCompositionDefinition.getPolicyTypes().values()) {
190 policyTypeMap.put(policyType.getName(), policyType.getVersion());
192 LOGGER.debug("Found Policy Types in automation composition definition: {} , Creating Policy Types",
193 automationCompositionDefinition.getName());
194 apiHttpClient.createPolicyType(automationCompositionDefinition);
196 if (automationCompositionDefinition.getToscaTopologyTemplate().getPolicies() != null) {
197 for (Map<String, ToscaPolicy> gotPolicyMap : automationCompositionDefinition.getToscaTopologyTemplate()
199 for (ToscaPolicy policy : gotPolicyMap.values()) {
200 policyMap.put(policy.getName(), policy.getVersion());
203 LOGGER.debug("Found Policies in automation composition definition: {} , Creating Policies",
204 automationCompositionDefinition.getName());
205 apiHttpClient.createPolicy(automationCompositionDefinition);
208 deployPolicies(automationCompositionId, element.getId(), element.getOrderedState());
212 * Handle automationCompositionElement statistics.
214 * @param automationCompositionElementId automation composition element id
217 public void handleStatistics(UUID automationCompositionElementId) {
218 var acElement = intermediaryApi.getAutomationCompositionElement(automationCompositionElementId);
219 if (acElement != null) {
220 var acElementStatistics = new AcElementStatistics();
221 acElementStatistics.setState(acElement.getState());
222 acElementStatistics.setTimeStamp(Instant.now());
223 intermediaryApi.updateAutomationCompositionElementStatistics(automationCompositionElementId,
224 acElementStatistics);