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
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.http.main.handler;
23 import jakarta.validation.Validation;
24 import jakarta.ws.rs.core.Response.Status;
25 import java.lang.invoke.MethodHandles;
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;
46 * This class handles implementation of automationCompositionElement updates.
49 public class AutomationCompositionElementHandler extends AcElementListenerV1 {
51 private static final Coder CODER = new StandardCoder();
53 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
55 private final AcHttpClient acHttpClient;
57 public AutomationCompositionElementHandler(ParticipantIntermediaryApi intermediaryApi, AcHttpClient acHttpClient) {
58 super(intermediaryApi);
59 this.acHttpClient = acHttpClient;
63 * Handle a automation composition element state change.
65 * @param automationCompositionElementId the ID of the automation composition element
68 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
69 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
70 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
74 * Callback method to handle an update on a automation composition element.
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
82 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
83 throws PfModelException {
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())
90 if (failedResponseStatus.isEmpty()) {
91 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
92 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
94 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
95 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
96 "Error on Invoking the http request: " + failedResponseStatus);
98 } catch (AutomationCompositionException e) {
99 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
100 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, e.getMessage());
104 private ConfigRequest getConfigRequest(Map<String, Object> properties) throws AutomationCompositionException {
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");
113 return configRequest;
114 } catch (CoderException e) {
115 throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);