2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 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.HashMap;
28 import org.apache.http.HttpStatus;
29 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
30 import org.onap.policy.clamp.acm.participant.a1pms.models.ConfigurationEntity;
31 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
35 import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV2;
36 import org.onap.policy.clamp.models.acm.concepts.DeployState;
37 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
38 import org.onap.policy.common.utils.coder.Coder;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Component;
46 * This class handles implementation of automationCompositionElement updates.
49 public class AutomationCompositionElementHandler extends AcElementListenerV2 {
51 private static final Coder CODER = new StandardCoder();
53 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
55 private final AcA1PmsClient acA1PmsClient;
57 public AutomationCompositionElementHandler(ParticipantIntermediaryApi intermediaryApi,
58 AcA1PmsClient acA1PmsClient) {
59 super(intermediaryApi);
60 this.acA1PmsClient = acA1PmsClient;
64 * Handle a automation composition element state change.
66 * @param compositionElement the information of the Automation Composition Definition Element
67 * @param instanceElement the information of the Automation Composition Instance Element
68 * @throws A1PolicyServiceException in case of a model exception
71 public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
72 throws A1PolicyServiceException {
73 Map<String, Object> properties = new HashMap<>(compositionElement.inProperties());
74 properties.putAll(instanceElement.inProperties());
75 var configurationEntity = getConfigurationEntity(properties);
76 if (configurationEntity != null && acA1PmsClient.isPmsHealthy()) {
77 acA1PmsClient.deleteService(configurationEntity.getPolicyServiceEntities());
78 intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
79 instanceElement.elementId(), DeployState.UNDEPLOYED, null,
80 StateChangeResult.NO_ERROR, "Undeployed");
82 LOGGER.warn("Failed to connect with A1PMS. Service configuration is: {}", configurationEntity);
83 throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Unable to connect with A1PMS");
88 * Callback method to handle an update on an automation composition element.
90 * @param compositionElement the information of the Automation Composition Definition Element
91 * @param instanceElement the information of the Automation Composition Instance Element
92 * @throws A1PolicyServiceException in case of a model exception
95 public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
96 throws A1PolicyServiceException {
97 Map<String, Object> properties = new HashMap<>(compositionElement.inProperties());
98 properties.putAll(instanceElement.inProperties());
100 var configurationEntity = getConfigurationEntity(properties);
101 if (acA1PmsClient.isPmsHealthy()) {
102 acA1PmsClient.createService(configurationEntity.getPolicyServiceEntities());
104 intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
105 instanceElement.elementId(), DeployState.DEPLOYED, null,
106 StateChangeResult.NO_ERROR, "Deployed");
108 LOGGER.error("Failed to connect with A1PMS");
109 throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Unable to connect with A1PMS");
111 } catch (ValidationException | A1PolicyServiceException e) {
112 throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
116 private ConfigurationEntity getConfigurationEntity(Map<String, Object> properties) throws A1PolicyServiceException {
118 var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
119 try (var validatorFactory = Validation.buildDefaultValidatorFactory()) {
120 var violations = validatorFactory.getValidator().validate(configurationEntity);
121 if (!violations.isEmpty()) {
122 LOGGER.error("Violations found in the config request parameters: {}", violations);
123 throw new ValidationException("Constraint violations in the config request");
126 return configurationEntity;
127 } catch (CoderException e) {
128 throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);