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;
27 import java.util.List;
29 import java.util.UUID;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.Future;
34 import javax.validation.Validation;
35 import javax.validation.ValidationException;
36 import lombok.AccessLevel;
38 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.AcTypeState;
49 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
50 import org.onap.policy.clamp.models.acm.concepts.DeployState;
51 import org.onap.policy.clamp.models.acm.concepts.LockState;
52 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
53 import org.onap.policy.common.utils.coder.Coder;
54 import org.onap.policy.common.utils.coder.CoderException;
55 import org.onap.policy.common.utils.coder.StandardCoder;
56 import org.onap.policy.models.base.PfModelException;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.springframework.stereotype.Component;
62 * This class handles implementation of automationCompositionElement updates.
65 @RequiredArgsConstructor
66 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
68 private static final Coder CODER = new StandardCoder();
70 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
72 private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
74 private final ParticipantIntermediaryApi intermediaryApi;
76 private final KserveClient kserveClient;
78 @Getter(AccessLevel.PACKAGE)
79 private final Map<UUID, ConfigurationEntity> configRequestMap = new HashMap<>();
81 private static class ThreadConfig {
83 private int uninitializedToPassiveTimeout = 60;
84 private int statusCheckInterval = 30;
88 public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
89 var configurationEntity = configRequestMap.get(automationCompositionElementId);
90 if (configurationEntity != null) {
92 for (KserveInferenceEntity kserveInferenceEntity : configurationEntity.getKserveInferenceEntities()) {
93 kserveClient.undeployInferenceService(kserveInferenceEntity.getNamespace(),
94 kserveInferenceEntity.getName());
96 configRequestMap.remove(automationCompositionElementId);
97 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
98 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
100 } catch (IOException | ApiException exception) {
101 LOGGER.warn("Deletion of Inference service failed", exception);
107 * Callback method to handle an update on an automation composition element.
109 * @param automationCompositionId the ID of the automation composition
110 * @param element the information on the automation composition element
111 * @param properties properties Map
114 public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
115 throws PfModelException {
117 var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
118 var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configurationEntity);
119 if (violations.isEmpty()) {
120 boolean isAllInferenceSvcDeployed = true;
121 var config = CODER.convert(properties, ThreadConfig.class);
122 for (KserveInferenceEntity kserveInferenceEntity : configurationEntity.getKserveInferenceEntities()) {
123 kserveClient.deployInferenceService(kserveInferenceEntity.getNamespace(),
124 kserveInferenceEntity.getPayload());
126 if (!checkInferenceServiceStatus(kserveInferenceEntity.getName(),
127 kserveInferenceEntity.getNamespace(), config.uninitializedToPassiveTimeout,
128 config.statusCheckInterval)) {
129 isAllInferenceSvcDeployed = false;
133 if (isAllInferenceSvcDeployed) {
134 configRequestMap.put(element.getId(), configurationEntity);
135 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
136 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
138 LOGGER.error("Inference Service deployment failed");
141 LOGGER.error("Violations found in the config request parameters: {}", violations);
142 throw new ValidationException("Constraint violations in the config request");
144 } catch (CoderException e) {
145 throw new KserveException(HttpStatus.SC_BAD_REQUEST, "Invalid inference service configuration", e);
146 } catch (InterruptedException e) {
147 Thread.currentThread().interrupt();
148 throw new KserveException("Interrupt in configuring the inference service", e);
149 } catch (IOException | ExecutionException | ApiException e) {
150 throw new KserveException("Failed to configure the inference service", e);
155 * Check the status of Inference Service.
157 * @param inferenceServiceName name of the inference service
158 * @param namespace kubernetes namespace
159 * @param timeout Inference service time check
160 * @param statusCheckInterval Status check time interval
161 * @return status of the inference service
162 * @throws ExecutionException Exception on execution
163 * @throws InterruptedException Exception on inference service status check
165 public boolean checkInferenceServiceStatus(String inferenceServiceName, String namespace, int timeout,
166 int statusCheckInterval) throws ExecutionException, InterruptedException {
167 // Invoke runnable thread to check pod status
168 Future<String> result = executor.submit(new InferenceServiceValidator(inferenceServiceName, namespace, timeout,
169 statusCheckInterval, kserveClient), "Done");
170 return (!result.get().isEmpty()) && result.isDone();
174 public void lock(UUID instanceId, UUID elementId) throws PfModelException {
175 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
176 StateChangeResult.NO_ERROR, "Locked");
180 public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
181 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
182 StateChangeResult.NO_ERROR, "Unlocked");
186 public void delete(UUID instanceId, UUID elementId) throws PfModelException {
187 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
188 StateChangeResult.NO_ERROR, "Deleted");
192 public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
193 throws PfModelException {
194 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null,
195 StateChangeResult.NO_ERROR, "Update not supported");
199 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
200 throws PfModelException {
201 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
205 public void deprime(UUID compositionId) throws PfModelException {
206 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,