752b8d938f3965500f3035910688a1e7ee83e224
[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.onap.policy.models.base.PfModelException;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.stereotype.Component;
44
45 /**
46  * This class handles implementation of automationCompositionElement updates.
47  */
48 @Component
49 public class AutomationCompositionElementHandler extends AcElementListenerV1 {
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 AcHttpClient acHttpClient;
56
57     public AutomationCompositionElementHandler(ParticipantIntermediaryApi intermediaryApi, AcHttpClient acHttpClient) {
58         super(intermediaryApi);
59         this.acHttpClient = acHttpClient;
60     }
61
62     /**
63      * Handle a automation composition element state change.
64      *
65      * @param automationCompositionElementId the ID of the automation composition element
66      */
67     @Override
68     public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
69         intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
70                 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
71     }
72
73     /**
74      * Callback method to handle an update on a automation composition element.
75      *
76      * @param automationCompositionId the automationComposition Id
77      * @param element the information on the automation composition element
78      * @param properties properties Map
79      * @throws PfModelException in case of a exception
80      */
81     @Override
82     public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
83             throws PfModelException {
84         try {
85             var configRequest = getConfigRequest(properties);
86             var restResponseMap = acHttpClient.run(configRequest);
87             var failedResponseStatus = restResponseMap.values().stream()
88                     .filter(response -> !HttpStatus.valueOf(response.getKey()).is2xxSuccessful())
89                     .toList();
90             if (failedResponseStatus.isEmpty()) {
91                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
92                         DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
93             } else {
94                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
95                         DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
96                         "Error on Invoking the http request: " + failedResponseStatus);
97             }
98         } catch (AutomationCompositionException e) {
99             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
100                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, e.getMessage());
101         }
102     }
103
104     private ConfigRequest getConfigRequest(Map<String, Object> properties) throws AutomationCompositionException {
105         try {
106             var configRequest = CODER.convert(properties, ConfigRequest.class);
107             var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configRequest);
108             if (!violations.isEmpty()) {
109                 LOGGER.error("Violations found in the config request parameters: {}", violations);
110                 throw new AutomationCompositionException(Status.BAD_REQUEST,
111                         "Constraint violations in the config request");
112             }
113             return configRequest;
114         } catch (CoderException e) {
115             throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);
116         }
117     }
118 }