b1412fe1972889423d0edfd14e6a6b7acd329f64
[policy/clamp.git] /
1 /*-
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
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.List;
27 import java.util.Map;
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;
50
51 /**
52  * This class handles implementation of automationCompositionElement updates.
53  */
54 @Component
55 @RequiredArgsConstructor
56 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
57
58     private static final Coder CODER = new StandardCoder();
59
60     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62     private final ParticipantIntermediaryApi intermediaryApi;
63
64     private final AcHttpClient acHttpClient;
65
66     /**
67      * Handle a automation composition element state change.
68      *
69      * @param automationCompositionElementId the ID of the automation composition element
70      */
71     @Override
72     public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
73         intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
74                 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
75     }
76
77     /**
78      * Callback method to handle an update on a automation composition element.
79      *
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
84      */
85     @Override
86     public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
87             throws PfModelException {
88         try {
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())
93                     .toList();
94             if (failedResponseStatus.isEmpty()) {
95                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
96                         DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
97             } else {
98                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
99                         DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
100                         "Error on Invoking the http request: " + failedResponseStatus);
101             }
102         } catch (AutomationCompositionException e) {
103             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
104                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, e.getMessage());
105         }
106     }
107
108     private ConfigRequest getConfigRequest(Map<String, Object> properties) throws AutomationCompositionException {
109         try {
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");
116             }
117             return configRequest;
118         } catch (CoderException e) {
119             throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);
120         }
121     }
122
123     @Override
124     public void lock(UUID instanceId, UUID elementId) throws PfModelException {
125         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
126                 StateChangeResult.NO_ERROR, "Locked");
127     }
128
129     @Override
130     public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
131         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
132                 StateChangeResult.NO_ERROR, "Unlocked");
133     }
134
135     @Override
136     public void delete(UUID instanceId, UUID elementId) throws PfModelException {
137         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
138                 StateChangeResult.NO_ERROR, "Deleted");
139     }
140
141     @Override
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");
146     }
147
148     @Override
149     public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
150             throws PfModelException {
151         intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
152     }
153
154     @Override
155     public void deprime(UUID compositionId) throws PfModelException {
156         intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
157                 "Deprimed");
158     }
159
160     @Override
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");
167     }
168
169     @Override
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);
174             return;
175         }
176         deployState = AcmUtils.deployCompleted(deployState);
177         lockState = AcmUtils.lockCompleted(deployState, lockState);
178         intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(), deployState,
179                 lockState, StateChangeResult.NO_ERROR, "Restarted");
180     }
181 }