2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation.
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.a1pms.handler;
23 import jakarta.validation.Validation;
24 import jakarta.validation.ValidationException;
25 import java.lang.invoke.MethodHandles;
26 import java.util.List;
28 import java.util.UUID;
29 import java.util.concurrent.ConcurrentHashMap;
30 import lombok.AccessLevel;
32 import lombok.RequiredArgsConstructor;
33 import org.apache.http.HttpStatus;
34 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
35 import org.onap.policy.clamp.acm.participant.a1pms.models.ConfigurationEntity;
36 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
38 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
39 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
40 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
42 import org.onap.policy.clamp.models.acm.concepts.DeployState;
43 import org.onap.policy.clamp.models.acm.concepts.LockState;
44 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
45 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.models.base.PfModelException;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.stereotype.Component;
55 * This class handles implementation of automationCompositionElement updates.
58 @RequiredArgsConstructor
59 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
61 private static final Coder CODER = new StandardCoder();
63 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
65 private final ParticipantIntermediaryApi intermediaryApi;
67 private final AcA1PmsClient acA1PmsClient;
69 // Map of acElement Id and A1PMS services
70 @Getter(AccessLevel.PACKAGE)
71 private final Map<UUID, ConfigurationEntity> configRequestMap = new ConcurrentHashMap<>();
74 * Handle a automation composition element state change.
76 * @param automationCompositionId the ID of the automation composition
77 * @param automationCompositionElementId the ID of the automation composition element
78 * @throws A1PolicyServiceException in case of a model exception
81 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId)
82 throws A1PolicyServiceException {
83 var configurationEntity = configRequestMap.get(automationCompositionElementId);
84 if (configurationEntity != null && acA1PmsClient.isPmsHealthy()) {
85 acA1PmsClient.deleteService(configurationEntity.getPolicyServiceEntities());
86 configRequestMap.remove(automationCompositionElementId);
87 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
88 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
91 LOGGER.warn("Failed to connect with A1PMS. Service configuration is: {}", configurationEntity);
92 throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Unable to connect with A1PMS");
97 * Callback method to handle an update on an automation composition element.
99 * @param automationCompositionId the ID of the automation composition
100 * @param element the information on the automation composition element
101 * @param properties properties Map
104 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
105 throws A1PolicyServiceException {
107 var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
108 var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configurationEntity);
109 if (violations.isEmpty()) {
110 if (acA1PmsClient.isPmsHealthy()) {
111 acA1PmsClient.createService(configurationEntity.getPolicyServiceEntities());
112 configRequestMap.put(element.getId(), configurationEntity);
114 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
115 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
117 LOGGER.error("Failed to connect with A1PMS");
118 throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE,
119 "Unable to connect with A1PMS");
122 LOGGER.error("Violations found in the config request parameters: {}", violations);
123 throw new ValidationException("Constraint violations in the config request");
125 } catch (ValidationException | CoderException | A1PolicyServiceException e) {
126 throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
131 public void lock(UUID instanceId, UUID elementId) throws PfModelException {
132 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
133 StateChangeResult.NO_ERROR, "Locked");
137 public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
138 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
139 StateChangeResult.NO_ERROR, "Unlocked");
143 public void delete(UUID instanceId, UUID elementId) throws PfModelException {
144 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
145 StateChangeResult.NO_ERROR, "Deleted");
149 public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
150 throws PfModelException {
151 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null,
152 StateChangeResult.NO_ERROR, "Update not supported");
156 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
157 throws PfModelException {
158 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
162 public void deprime(UUID compositionId) throws PfModelException {
163 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
168 public void handleRestartComposition(UUID compositionId,
169 List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state)
170 throws PfModelException {
171 var finalState = AcTypeState.PRIMED.equals(state) || AcTypeState.PRIMING.equals(state) ? AcTypeState.PRIMED
172 : AcTypeState.COMMISSIONED;
173 intermediaryApi.updateCompositionState(compositionId, finalState, StateChangeResult.NO_ERROR, "Restarted");
177 public void handleRestartInstance(UUID automationCompositionId, AcElementDeploy element,
178 Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
179 if (DeployState.DEPLOYING.equals(deployState)) {
180 deploy(automationCompositionId, element, properties);
183 if (DeployState.UNDEPLOYING.equals(deployState) || DeployState.DEPLOYED.equals(deployState)
184 || DeployState.UPDATING.equals(deployState)) {
186 var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
187 configRequestMap.put(element.getId(), configurationEntity);
188 } catch (ValidationException | CoderException e) {
189 throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
192 if (DeployState.UNDEPLOYING.equals(deployState)) {
193 undeploy(automationCompositionId, element.getId());
196 deployState = AcmUtils.deployCompleted(deployState);
197 lockState = AcmUtils.lockCompleted(deployState, lockState);
198 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(), deployState,
199 lockState, StateChangeResult.NO_ERROR, "Restarted");
203 public void migrate(UUID automationCompositionId, AcElementDeploy element, UUID compositionTargetId,
204 Map<String, Object> properties) throws PfModelException {
205 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
206 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");