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.io.Closeable;
24 import java.io.IOException;
25 import java.lang.invoke.MethodHandles;
27 import java.util.UUID;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import java.util.stream.Collectors;
33 import javax.validation.Validation;
34 import javax.ws.rs.core.Response.Status;
35 import lombok.RequiredArgsConstructor;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
39 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
40 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
41 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
42 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
43 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
44 import org.onap.policy.clamp.models.acm.concepts.DeployState;
45 import org.onap.policy.common.utils.coder.Coder;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.models.base.PfModelException;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.http.HttpStatus;
53 import org.springframework.stereotype.Component;
56 * This class handles implementation of automationCompositionElement updates.
59 @RequiredArgsConstructor
60 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener, Closeable {
62 private static final Coder CODER = new StandardCoder();
64 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
66 private final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
69 private ParticipantIntermediaryApi intermediaryApi;
71 private final AcHttpClient acHttpClient;
74 * Handle a automation composition element state change.
76 * @param automationCompositionElementId the ID of the automation composition element
79 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
80 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
81 DeployState.UNDEPLOYED, null, "");
85 * Callback method to handle an update on a automation composition element.
87 * @param automationCompositionId the automationComposition Id
88 * @param element the information on the automation composition element
89 * @param properties properties Map
90 * @throws PfModelException in case of a exception
93 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
94 throws PfModelException {
96 var configRequest = getConfigRequest(properties);
97 var restResponseMap = invokeHttpClient(configRequest);
98 var failedResponseStatus = restResponseMap.values().stream()
99 .filter(response -> !HttpStatus.valueOf(response.getKey()).is2xxSuccessful())
100 .collect(Collectors.toList());
101 if (failedResponseStatus.isEmpty()) {
102 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
103 DeployState.DEPLOYED, null, "Deployed");
105 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
106 DeployState.UNDEPLOYED, null, "Error on Invoking the http request: " + failedResponseStatus);
108 } catch (AutomationCompositionException e) {
109 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
110 DeployState.UNDEPLOYED, null, e.getMessage());
114 private ConfigRequest getConfigRequest(Map<String, Object> properties) throws AutomationCompositionException {
116 var configRequest = CODER.convert(properties, ConfigRequest.class);
117 var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configRequest);
118 if (!violations.isEmpty()) {
119 LOGGER.error("Violations found in the config request parameters: {}", violations);
120 throw new AutomationCompositionException(Status.BAD_REQUEST,
121 "Constraint violations in the config request");
123 return configRequest;
124 } catch (CoderException e) {
125 throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);
130 * Invoke a runnable thread to execute http requests.
132 * @param configRequest ConfigRequest
134 private Map<ToscaConceptIdentifier, Pair<Integer, String>> invokeHttpClient(ConfigRequest configRequest)
135 throws PfModelException {
137 Map<ToscaConceptIdentifier, Pair<Integer, String>> restResponseMap = new ConcurrentHashMap<>();
138 // Invoke runnable thread to execute https requests of all config entities
139 var result = executor.submit(() -> acHttpClient.run(configRequest, restResponseMap), restResponseMap);
140 if (!result.get().isEmpty()) {
141 LOGGER.debug("Http Request Completed: {}", result.isDone());
143 return restResponseMap;
144 } catch (InterruptedException e) {
145 Thread.currentThread().interrupt();
146 throw new PfModelException(Status.BAD_REQUEST, "Error invoking ExecutorService ", e);
147 } catch (ExecutionException e) {
148 throw new PfModelException(Status.BAD_REQUEST, "Error invoking the http request for the config ", e);
153 * Closes this stream and releases any system resources associated
154 * with it. If the stream is already closed then invoking this
155 * method has no effect.
157 * @throws IOException if an I/O error occurs
160 public void close() throws IOException {