ed02314cd8bac877475e41fa04df6ec34d1e0bbf
[policy/clamp.git] /
1 /*-
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
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.kserve.handler;
22
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;
28 import java.util.Map;
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;
37 import lombok.Getter;
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;
60
61 /**
62  * This class handles implementation of automationCompositionElement updates.
63  */
64 @Component
65 @RequiredArgsConstructor
66 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
67
68     private static final Coder CODER = new StandardCoder();
69
70     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
71
72     private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
73
74     private final ParticipantIntermediaryApi intermediaryApi;
75
76     private final KserveClient kserveClient;
77
78     @Getter(AccessLevel.PACKAGE)
79     private final Map<UUID, ConfigurationEntity> configRequestMap = new HashMap<>();
80
81     private static class ThreadConfig {
82
83         private int uninitializedToPassiveTimeout = 60;
84         private int statusCheckInterval = 30;
85     }
86
87     @Override
88     public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
89         var configurationEntity = configRequestMap.get(automationCompositionElementId);
90         if (configurationEntity != null) {
91             try {
92                 for (KserveInferenceEntity kserveInferenceEntity : configurationEntity.getKserveInferenceEntities()) {
93                     kserveClient.undeployInferenceService(kserveInferenceEntity.getNamespace(),
94                             kserveInferenceEntity.getName());
95                 }
96                 configRequestMap.remove(automationCompositionElementId);
97                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
98                         automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
99                         "Undeployed");
100             } catch (IOException | ApiException exception) {
101                 LOGGER.warn("Deletion of Inference service failed", exception);
102             }
103         }
104     }
105
106     /**
107      * Callback method to handle an update on an automation composition element.
108      *
109      * @param automationCompositionId the ID of the automation composition
110      * @param element the information on the automation composition element
111      * @param properties properties Map
112      */
113     @Override
114     public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
115             throws PfModelException {
116         try {
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());
125
126                     if (!checkInferenceServiceStatus(kserveInferenceEntity.getName(),
127                             kserveInferenceEntity.getNamespace(), config.uninitializedToPassiveTimeout,
128                             config.statusCheckInterval)) {
129                         isAllInferenceSvcDeployed = false;
130                         break;
131                     }
132                 }
133                 if (isAllInferenceSvcDeployed) {
134                     configRequestMap.put(element.getId(), configurationEntity);
135                     intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
136                             DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
137                 } else {
138                     LOGGER.error("Inference Service deployment failed");
139                 }
140             } else {
141                 LOGGER.error("Violations found in the config request parameters: {}", violations);
142                 throw new ValidationException("Constraint violations in the config request");
143             }
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);
151         }
152     }
153
154     /**
155      * Check the status of Inference Service.
156      *
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
164      */
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();
171     }
172
173     @Override
174     public void lock(UUID instanceId, UUID elementId) throws PfModelException {
175         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
176                 StateChangeResult.NO_ERROR, "Locked");
177     }
178
179     @Override
180     public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
181         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
182                 StateChangeResult.NO_ERROR, "Unlocked");
183     }
184
185     @Override
186     public void delete(UUID instanceId, UUID elementId) throws PfModelException {
187         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
188                 StateChangeResult.NO_ERROR, "Deleted");
189     }
190
191     @Override
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");
196     }
197
198     @Override
199     public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
200             throws PfModelException {
201         intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
202     }
203
204     @Override
205     public void deprime(UUID compositionId) throws PfModelException {
206         intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
207                 "Deprimed");
208     }
209 }