161cf9278a7bbf2d658d03f7d64d6800e95aa7c6
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-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.http.main.handler;
22
23 import jakarta.validation.Validation;
24 import jakarta.ws.rs.core.Response.Status;
25 import java.lang.invoke.MethodHandles;
26 import java.util.Map;
27 import java.util.UUID;
28 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
29 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
30 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV1;
32 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
33 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
34 import org.onap.policy.clamp.models.acm.concepts.DeployState;
35 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.stereotype.Component;
43
44 /**
45  * This class handles implementation of automationCompositionElement updates.
46  */
47 @Component
48 public class AutomationCompositionElementHandler extends AcElementListenerV1 {
49
50     private static final Coder CODER = new StandardCoder();
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54     private final AcHttpClient acHttpClient;
55
56     public AutomationCompositionElementHandler(ParticipantIntermediaryApi intermediaryApi, AcHttpClient acHttpClient) {
57         super(intermediaryApi);
58         this.acHttpClient = acHttpClient;
59     }
60
61     /**
62      * Handle a automation composition element state change.
63      *
64      * @param automationCompositionElementId the ID of the automation composition element
65      */
66     @Override
67     public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
68         intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
69                 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
70     }
71
72     /**
73      * Callback method to handle an update on a automation composition element.
74      *
75      * @param automationCompositionId the automationComposition Id
76      * @param element the information on the automation composition element
77      * @param properties properties Map
78      */
79     @Override
80     public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties) {
81         try {
82             var configRequest = getConfigRequest(properties);
83             var restResponseMap = acHttpClient.run(configRequest);
84             var failedResponseStatus = restResponseMap.values().stream()
85                     .filter(response -> !HttpStatus.valueOf(response.getKey()).is2xxSuccessful())
86                     .toList();
87             if (failedResponseStatus.isEmpty()) {
88                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
89                         DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
90             } else {
91                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
92                         DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
93                         "Error on Invoking the http request: " + failedResponseStatus);
94             }
95         } catch (AutomationCompositionException e) {
96             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
97                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, e.getMessage());
98         }
99     }
100
101     private ConfigRequest getConfigRequest(Map<String, Object> properties) throws AutomationCompositionException {
102         try {
103             var configRequest = CODER.convert(properties, ConfigRequest.class);
104             try (var validatorFactory = Validation.buildDefaultValidatorFactory()) {
105                 var violations = validatorFactory.getValidator().validate(configRequest);
106                 if (!violations.isEmpty()) {
107                     LOGGER.error("Violations found in the config request parameters: {}", violations);
108                     throw new AutomationCompositionException(Status.BAD_REQUEST,
109                             "Constraint violations in the config request");
110                 }
111             }
112             return configRequest;
113         } catch (CoderException e) {
114             throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);
115         }
116     }
117 }