2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.kubernetes.handler;
23 import java.io.IOException;
24 import java.lang.invoke.MethodHandles;
25 import java.util.HashMap;
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.concurrent.Future;
33 import lombok.AccessLevel;
36 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
38 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
39 import org.onap.policy.clamp.acm.participant.kubernetes.helm.PodStatusValidator;
40 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
41 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.models.base.PfModelException;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.stereotype.Component;
58 * This class handles implementation of automationCompositionElement updates.
61 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
62 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
64 private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
66 // Map of helm installation and the status of corresponding pods
68 private static Map<String, Map<String, String>> podStatusMap = new ConcurrentHashMap<>();
69 private static final Coder CODER = new StandardCoder();
72 private ChartService chartService;
75 private ParticipantIntermediaryApi intermediaryApi;
77 // Map of acElement Id and installed Helm charts
78 @Getter(AccessLevel.PACKAGE)
79 private final Map<UUID, ChartInfo> chartMap = new HashMap<>();
81 // Default thread config values
82 private static class ThreadConfig {
83 private int uninitializedToPassiveTimeout = 60;
84 private int podStatusCheckInterval = 30;
88 * Callback method to handle a automation composition element state change.
90 * @param automationCompositionElementId the ID of the automation composition element
91 * @param currentState the current state of the automation composition element
92 * @param newState the state to which the automation composition element is changing to
95 public synchronized void automationCompositionElementStateChange(ToscaConceptIdentifier automationCompositionId,
96 UUID automationCompositionElementId, AutomationCompositionState currentState,
97 AutomationCompositionOrderedState newState) {
100 ChartInfo chart = chartMap.get(automationCompositionElementId);
102 LOGGER.info("Helm deployment to be deleted {} ", chart.getReleaseName());
104 chartService.uninstallChart(chart);
105 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
106 automationCompositionElementId, newState, AutomationCompositionState.UNINITIALISED,
107 ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
108 chartMap.remove(automationCompositionElementId);
109 podStatusMap.remove(chart.getReleaseName());
110 } catch (ServiceException se) {
111 LOGGER.warn("Deletion of Helm deployment failed", se);
116 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
117 automationCompositionElementId, newState, AutomationCompositionState.PASSIVE,
118 ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
121 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
122 automationCompositionElementId, newState, AutomationCompositionState.RUNNING,
123 ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
126 LOGGER.warn("Cannot transition from state {} to state {}", currentState, newState);
132 * Callback method to handle an update on a automation composition element.
134 * @param element the information on the automation composition element
135 * @param nodeTemplate toscaNodeTemplate
136 * @throws PfModelException in case of an exception
139 public synchronized void automationCompositionElementUpdate(ToscaConceptIdentifier automationCompositionId,
140 AutomationCompositionElement element, ToscaNodeTemplate nodeTemplate) throws PfModelException {
141 @SuppressWarnings("unchecked")
142 Map<String, Object> chartData = (Map<String, Object>) nodeTemplate.getProperties().get("chart");
144 LOGGER.info("Installation request received for the Helm Chart {} ", chartData);
146 var chartInfo = CODER.convert(chartData, ChartInfo.class);
147 if (chartService.installChart(chartInfo)) {
148 chartMap.put(element.getId(), chartInfo);
150 var config = CODER.convert(nodeTemplate.getProperties(),
152 checkPodStatus(automationCompositionId, element.getId(), chartInfo,
153 config.uninitializedToPassiveTimeout, config.podStatusCheckInterval);
155 } catch (ServiceException | CoderException | IOException | ExecutionException
156 | InterruptedException e) {
157 LOGGER.warn("Installation of Helm chart failed", e);
162 * Invoke a new thread to check the status of deployed pods.
164 * @param chart ChartInfo
166 public void checkPodStatus(ToscaConceptIdentifier automationCompositionId, UUID elementId,
167 ChartInfo chart, int timeout, int podStatusCheckInterval) throws ExecutionException, InterruptedException {
168 // Invoke runnable thread to check pod status
169 Future<String> result = executor.submit(new PodStatusValidator(chart, timeout,
170 podStatusCheckInterval), "Done");
171 if (!result.get().isEmpty()) {
172 LOGGER.info("Pod Status Validator Completed: {}", result.isDone());
173 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, elementId,
174 AutomationCompositionOrderedState.PASSIVE, AutomationCompositionState.PASSIVE,
175 ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);