2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.kserve.handler;
23 import io.kubernetes.client.openapi.ApiException;
24 import java.io.IOException;
25 import java.lang.invoke.MethodHandles;
26 import java.util.HashMap;
28 import java.util.UUID;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import java.util.concurrent.Future;
33 import javax.validation.Validation;
34 import javax.validation.ValidationException;
35 import lombok.AccessLevel;
37 import lombok.RequiredArgsConstructor;
39 import org.apache.http.HttpStatus;
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.acm.participant.kserve.exception.KserveException;
43 import org.onap.policy.clamp.acm.participant.kserve.k8s.InferenceServiceValidator;
44 import org.onap.policy.clamp.acm.participant.kserve.k8s.KserveClient;
45 import org.onap.policy.clamp.acm.participant.kserve.models.ConfigurationEntity;
46 import org.onap.policy.clamp.acm.participant.kserve.models.KserveInferenceEntity;
47 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
48 import org.onap.policy.clamp.models.acm.concepts.DeployState;
49 import org.onap.policy.common.utils.coder.Coder;
50 import org.onap.policy.common.utils.coder.CoderException;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.models.base.PfModelException;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.stereotype.Component;
58 * This class handles implementation of automationCompositionElement updates.
61 @RequiredArgsConstructor
62 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
64 private static final Coder CODER = new StandardCoder();
66 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
68 private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
71 private ParticipantIntermediaryApi intermediaryApi;
73 private final KserveClient kserveClient;
75 @Getter(AccessLevel.PACKAGE)
76 private final Map<UUID, ConfigurationEntity> configRequestMap = new HashMap<>();
79 private static class ThreadConfig {
81 private int uninitializedToPassiveTimeout = 60;
82 private int statusCheckInterval = 30;
86 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
87 var configurationEntity = configRequestMap.get(automationCompositionElementId);
88 if (configurationEntity != null) {
90 for (KserveInferenceEntity kserveInferenceEntity : configurationEntity.getKserveInferenceEntities()) {
91 kserveClient.undeployInferenceService(kserveInferenceEntity.getNamespace(),
92 kserveInferenceEntity.getName());
94 configRequestMap.remove(automationCompositionElementId);
95 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
96 automationCompositionElementId, DeployState.UNDEPLOYED, null, "Undeployed");
97 } catch (IOException | ApiException exception) {
98 LOGGER.warn("Deletion of Inference service failed", exception);
104 * Callback method to handle an update on an automation composition element.
106 * @param automationCompositionId the ID of the automation composition
107 * @param element the information on the automation composition element
108 * @param properties properties Map
111 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
112 throws PfModelException {
114 var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
115 var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configurationEntity);
116 if (violations.isEmpty()) {
117 boolean isAllInferenceSvcDeployed = true;
118 var config = CODER.convert(properties, ThreadConfig.class);
119 for (KserveInferenceEntity kserveInferenceEntity : configurationEntity.getKserveInferenceEntities()) {
120 kserveClient.deployInferenceService(kserveInferenceEntity.getNamespace(),
121 kserveInferenceEntity.getPayload());
123 if (!checkInferenceServiceStatus(kserveInferenceEntity.getName(),
124 kserveInferenceEntity.getNamespace(), config.uninitializedToPassiveTimeout,
125 config.statusCheckInterval)) {
126 isAllInferenceSvcDeployed = false;
130 if (isAllInferenceSvcDeployed) {
131 configRequestMap.put(element.getId(), configurationEntity);
132 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
133 DeployState.DEPLOYED, null, "Deployed");
135 LOGGER.error("Inference Service deployment failed");
138 LOGGER.error("Violations found in the config request parameters: {}", violations);
139 throw new ValidationException("Constraint violations in the config request");
141 } catch (CoderException e) {
142 throw new KserveException(HttpStatus.SC_BAD_REQUEST, "Invalid inference service configuration", e);
143 } catch (InterruptedException e) {
144 Thread.currentThread().interrupt();
145 throw new KserveException("Interrupt in configuring the inference service", e);
146 } catch (IOException | ExecutionException | ApiException e) {
147 throw new KserveException("Failed to configure the inference service", e);
152 * Check the status of Inference Service.
154 * @param inferenceServiceName name of the inference service
155 * @param namespace kubernetes namespace
156 * @param timeout Inference service time check
157 * @param statusCheckInterval Status check time interval
158 * @return status of the inference service
159 * @throws ExecutionException Exception on execution
160 * @throws InterruptedException Exception on inference service status check
162 public boolean checkInferenceServiceStatus(String inferenceServiceName, String namespace, int timeout,
163 int statusCheckInterval) throws ExecutionException, InterruptedException {
164 // Invoke runnable thread to check pod status
165 Future<String> result = executor.submit(
166 new InferenceServiceValidator(inferenceServiceName, namespace, timeout, statusCheckInterval,
167 kserveClient), "Done");
168 return (!result.get().isEmpty()) && result.isDone();