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 java.lang.invoke.MethodHandles;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.UUID;
28 import javax.validation.Validation;
29 import javax.validation.ValidationException;
30 import lombok.AccessLevel;
32 import lombok.RequiredArgsConstructor;
34 import org.apache.http.HttpStatus;
35 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
36 import org.onap.policy.clamp.acm.participant.a1pms.models.ConfigurationEntity;
37 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
38 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
39 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
40 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
41 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
43 import org.onap.policy.clamp.models.acm.concepts.DeployState;
44 import org.onap.policy.clamp.models.acm.concepts.LockState;
45 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
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());
66 private ParticipantIntermediaryApi intermediaryApi;
68 private final AcA1PmsClient acA1PmsClient;
70 // Map of acElement Id and A1PMS services
71 @Getter(AccessLevel.PACKAGE)
72 private final Map<UUID, ConfigurationEntity> configRequestMap = new HashMap<>();
75 * Handle a automation composition element state change.
77 * @param automationCompositionId the ID of the automation composition
78 * @param automationCompositionElementId the ID of the automation composition element
79 * @throws PfModelException in case of a model exception
82 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId)
83 throws A1PolicyServiceException {
84 var configurationEntity = configRequestMap.get(automationCompositionElementId);
85 if (configurationEntity != null && acA1PmsClient.isPmsHealthy()) {
86 acA1PmsClient.deleteService(configurationEntity.getPolicyServiceEntities());
87 configRequestMap.remove(automationCompositionElementId);
88 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
89 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
92 LOGGER.warn("Failed to connect with A1PMS. Service configuration is: {}", configurationEntity);
93 throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Unable to connect with A1PMS");
98 * Callback method to handle an update on an automation composition element.
100 * @param automationCompositionId the ID of the automation composition
101 * @param element the information on the automation composition element
102 * @param properties properties Map
105 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
106 throws A1PolicyServiceException {
108 var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
109 var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configurationEntity);
110 if (violations.isEmpty()) {
111 if (acA1PmsClient.isPmsHealthy()) {
112 acA1PmsClient.createService(configurationEntity.getPolicyServiceEntities());
113 configRequestMap.put(element.getId(), configurationEntity);
115 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
116 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
118 LOGGER.error("Failed to connect with A1PMS");
119 throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE,
120 "Unable to connect with A1PMS");
123 LOGGER.error("Violations found in the config request parameters: {}", violations);
124 throw new ValidationException("Constraint violations in the config request");
126 } catch (ValidationException | CoderException | A1PolicyServiceException e) {
127 throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
132 public void lock(UUID instanceId, UUID elementId) throws PfModelException {
133 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
134 StateChangeResult.NO_ERROR, "Locked");
138 public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
139 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
140 StateChangeResult.NO_ERROR, "Unlocked");
144 public void delete(UUID instanceId, UUID elementId) throws PfModelException {
145 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
146 StateChangeResult.NO_ERROR, "Deleted");
150 public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
151 throws PfModelException {
152 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null,
153 StateChangeResult.NO_ERROR, "Update not supported");
157 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
158 throws PfModelException {
159 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
163 public void deprime(UUID compositionId) throws PfModelException {
164 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,