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.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.stereotype.Component;
45 * This class handles implementation of automationCompositionElement updates.
48 public class AutomationCompositionElementHandler extends AcElementListenerV1 {
50 private static final Coder CODER = new StandardCoder();
52 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54 private final AcHttpClient acHttpClient;
56 public AutomationCompositionElementHandler(ParticipantIntermediaryApi intermediaryApi, AcHttpClient acHttpClient) {
57 super(intermediaryApi);
58 this.acHttpClient = acHttpClient;
62 * Handle a automation composition element state change.
64 * @param automationCompositionElementId the ID of the automation composition element
67 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
68 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
69 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
73 * Callback method to handle an update on a automation composition element.
75 * @param automationCompositionId the automationComposition Id
76 * @param element the information on the automation composition element
77 * @param properties properties Map
80 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties) {
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())
87 if (failedResponseStatus.isEmpty()) {
88 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
89 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
91 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
92 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
93 "Error on Invoking the http request: " + failedResponseStatus);
95 } catch (AutomationCompositionException e) {
96 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
97 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, e.getMessage());
101 private ConfigRequest getConfigRequest(Map<String, Object> properties) throws AutomationCompositionException {
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");
112 return configRequest;
113 } catch (CoderException e) {
114 throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);