47870ea398dee47a70ae70a32261c070cd276983
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.a1pms.handler;
22
23 import jakarta.validation.Validation;
24 import jakarta.validation.ValidationException;
25 import java.lang.invoke.MethodHandles;
26 import java.util.HashMap;
27 import java.util.Map;
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;
44
45 /**
46  * This class handles implementation of automationCompositionElement updates.
47  */
48 @Component
49 public class AutomationCompositionElementHandler extends AcElementListenerV2 {
50
51     private static final Coder CODER = new StandardCoder();
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55     private final AcA1PmsClient acA1PmsClient;
56
57     public AutomationCompositionElementHandler(ParticipantIntermediaryApi intermediaryApi,
58         AcA1PmsClient acA1PmsClient) {
59         super(intermediaryApi);
60         this.acA1PmsClient = acA1PmsClient;
61     }
62
63     /**
64      * Handle a automation composition element state change.
65      *
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
69      */
70     @Override
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");
81         } else {
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");
84         }
85     }
86
87     /**
88      * Callback method to handle an update on an automation composition element.
89      *
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
93      */
94     @Override
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());
99         try {
100             var configurationEntity = getConfigurationEntity(properties);
101             if (acA1PmsClient.isPmsHealthy()) {
102                 acA1PmsClient.createService(configurationEntity.getPolicyServiceEntities());
103
104                 intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
105                         instanceElement.elementId(), DeployState.DEPLOYED, null,
106                         StateChangeResult.NO_ERROR, "Deployed");
107             } else {
108                 LOGGER.error("Failed to connect with A1PMS");
109                 throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Unable to connect with A1PMS");
110             }
111         } catch (ValidationException | A1PolicyServiceException e) {
112             throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
113         }
114     }
115
116     private ConfigurationEntity getConfigurationEntity(Map<String, Object> properties) throws A1PolicyServiceException {
117         try {
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");
124                 }
125             }
126             return  configurationEntity;
127         } catch (CoderException e) {
128             throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
129         }
130     }
131 }