2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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;
26 import java.util.List;
28 import java.util.UUID;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
31 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
34 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
35 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
36 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
38 import org.onap.policy.clamp.models.acm.concepts.DeployState;
39 import org.onap.policy.clamp.models.acm.concepts.LockState;
40 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
41 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
42 import org.onap.policy.common.utils.coder.Coder;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.models.base.PfModelException;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.stereotype.Component;
52 * This class handles implementation of automationCompositionElement updates.
55 @RequiredArgsConstructor
56 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
58 private static final Coder CODER = new StandardCoder();
60 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62 private final ParticipantIntermediaryApi intermediaryApi;
64 private final AcHttpClient acHttpClient;
67 * Handle a automation composition element state change.
69 * @param automationCompositionElementId the ID of the automation composition element
72 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
73 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
74 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
78 * Callback method to handle an update on a automation composition element.
80 * @param automationCompositionId the automationComposition Id
81 * @param element the information on the automation composition element
82 * @param properties properties Map
83 * @throws PfModelException in case of a exception
86 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
87 throws PfModelException {
89 var configRequest = getConfigRequest(properties);
90 var restResponseMap = acHttpClient.run(configRequest);
91 var failedResponseStatus = restResponseMap.values().stream()
92 .filter(response -> !HttpStatus.valueOf(response.getKey()).is2xxSuccessful())
94 if (failedResponseStatus.isEmpty()) {
95 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
96 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
98 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
99 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
100 "Error on Invoking the http request: " + failedResponseStatus);
102 } catch (AutomationCompositionException e) {
103 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
104 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, e.getMessage());
108 private ConfigRequest getConfigRequest(Map<String, Object> properties) throws AutomationCompositionException {
110 var configRequest = CODER.convert(properties, ConfigRequest.class);
111 var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configRequest);
112 if (!violations.isEmpty()) {
113 LOGGER.error("Violations found in the config request parameters: {}", violations);
114 throw new AutomationCompositionException(Status.BAD_REQUEST,
115 "Constraint violations in the config request");
117 return configRequest;
118 } catch (CoderException e) {
119 throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);
124 public void lock(UUID instanceId, UUID elementId) throws PfModelException {
125 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
126 StateChangeResult.NO_ERROR, "Locked");
130 public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
131 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
132 StateChangeResult.NO_ERROR, "Unlocked");
136 public void delete(UUID instanceId, UUID elementId) throws PfModelException {
137 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
138 StateChangeResult.NO_ERROR, "Deleted");
142 public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
143 throws PfModelException {
144 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null,
145 StateChangeResult.NO_ERROR, "Update not supported");
149 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
150 throws PfModelException {
151 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
155 public void deprime(UUID compositionId) throws PfModelException {
156 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
161 public void handleRestartComposition(UUID compositionId,
162 List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state)
163 throws PfModelException {
164 var finalState = AcTypeState.PRIMED.equals(state) || AcTypeState.PRIMING.equals(state) ? AcTypeState.PRIMED
165 : AcTypeState.COMMISSIONED;
166 intermediaryApi.updateCompositionState(compositionId, finalState, StateChangeResult.NO_ERROR, "Restarted");
170 public void handleRestartInstance(UUID automationCompositionId, AcElementDeploy element,
171 Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
172 if (DeployState.DEPLOYING.equals(deployState)) {
173 deploy(automationCompositionId, element, properties);
176 deployState = AcmUtils.deployCompleted(deployState);
177 lockState = AcmUtils.lockCompleted(deployState, lockState);
178 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(), deployState,
179 lockState, StateChangeResult.NO_ERROR, "Restarted");