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 java.lang.invoke.MethodHandles;
24 import java.util.List;
26 import java.util.UUID;
27 import java.util.stream.Collectors;
28 import javax.validation.Validation;
29 import javax.ws.rs.core.Response.Status;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
32 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
35 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
41 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
42 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.base.PfModelException;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.stereotype.Component;
53 * This class handles implementation of automationCompositionElement updates.
56 @RequiredArgsConstructor
57 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
59 private static final Coder CODER = new StandardCoder();
61 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
63 private final ParticipantIntermediaryApi intermediaryApi;
65 private final AcHttpClient acHttpClient;
68 * Handle a automation composition element state change.
70 * @param automationCompositionElementId the ID of the automation composition element
73 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
74 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
75 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
79 * Callback method to handle an update on a automation composition element.
81 * @param automationCompositionId the automationComposition Id
82 * @param element the information on the automation composition element
83 * @param properties properties Map
84 * @throws PfModelException in case of a exception
87 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
88 throws PfModelException {
90 var configRequest = getConfigRequest(properties);
91 var restResponseMap = acHttpClient.run(configRequest);
92 var failedResponseStatus = restResponseMap.values().stream()
93 .filter(response -> !HttpStatus.valueOf(response.getKey()).is2xxSuccessful())
94 .collect(Collectors.toList());
95 if (failedResponseStatus.isEmpty()) {
96 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
97 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
99 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
100 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
101 "Error on Invoking the http request: " + failedResponseStatus);
103 } catch (AutomationCompositionException e) {
104 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
105 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, e.getMessage());
109 private ConfigRequest getConfigRequest(Map<String, Object> properties) throws AutomationCompositionException {
111 var configRequest = CODER.convert(properties, ConfigRequest.class);
112 var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configRequest);
113 if (!violations.isEmpty()) {
114 LOGGER.error("Violations found in the config request parameters: {}", violations);
115 throw new AutomationCompositionException(Status.BAD_REQUEST,
116 "Constraint violations in the config request");
118 return configRequest;
119 } catch (CoderException e) {
120 throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);
125 public void lock(UUID instanceId, UUID elementId) throws PfModelException {
126 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
127 StateChangeResult.NO_ERROR, "Locked");
131 public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
132 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
133 StateChangeResult.NO_ERROR, "Unlocked");
137 public void delete(UUID instanceId, UUID elementId) throws PfModelException {
138 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
139 StateChangeResult.NO_ERROR, "Deleted");
143 public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
144 throws PfModelException {
145 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null,
146 StateChangeResult.NO_ERROR, "Update not supported");
150 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
151 throws PfModelException {
152 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
156 public void deprime(UUID compositionId) throws PfModelException {
157 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
162 public void handleRestartComposition(UUID compositionId,
163 List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state)
164 throws PfModelException {
165 var finalState = AcTypeState.PRIMED.equals(state) || AcTypeState.PRIMING.equals(state) ? AcTypeState.PRIMED
166 : AcTypeState.COMMISSIONED;
167 intermediaryApi.updateCompositionState(compositionId, finalState, StateChangeResult.NO_ERROR, "Restarted");
171 public void handleRestartInstance(UUID automationCompositionId, AcElementDeploy element,
172 Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
173 if (DeployState.DEPLOYING.equals(deployState)) {
174 deploy(automationCompositionId, element, properties);
177 deployState = AcmUtils.deployCompleted(deployState);
178 lockState = AcmUtils.lockCompleted(deployState, lockState);
179 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(), deployState,
180 lockState, StateChangeResult.NO_ERROR, "Restarted");