2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.controlloop.participant.kubernetes.handler;
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.ConcurrentHashMap;
30 import lombok.AccessLevel;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
39 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
40 import org.onap.policy.clamp.controlloop.participant.kubernetes.helm.PodStatusValidator;
41 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
42 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.stereotype.Component;
55 * This class handles implementation of controlLoopElement updates.
58 public class ControlLoopElementHandler implements ControlLoopElementListener {
59 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61 // Map of helm installation and the status of corresponding pods
63 private static Map<String, Map<String, String>> podStatusMap = new ConcurrentHashMap<>();
64 private static final Coder CODER = new StandardCoder();
67 private ChartService chartService;
70 private ParticipantIntermediaryApi intermediaryApi;
72 // Map of CLElement Id and installed Helm charts
73 @Getter(AccessLevel.PACKAGE)
74 private final Map<UUID, ChartInfo> chartMap = new HashMap<>();
76 // Default thread config values
77 private static class ThreadConfig {
78 private int uninitializedToPassiveTimeout = 60;
79 private int podStatusCheckInterval = 30;
83 * Callback method to handle a control loop element state change.
85 * @param controlLoopElementId the ID of the control loop element
86 * @param currentState the current state of the control loop element
87 * @param newState the state to which the control loop element is changing to
90 public synchronized void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState,
91 ControlLoopOrderedState newState) {
94 ChartInfo chart = chartMap.get(controlLoopElementId);
96 LOGGER.info("Helm deployment to be deleted {} ", chart.getReleaseName());
98 chartService.uninstallChart(chart);
99 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState,
100 ControlLoopState.UNINITIALISED, ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
101 chartMap.remove(controlLoopElementId);
102 podStatusMap.remove(chart.getReleaseName());
103 } catch (ServiceException se) {
104 LOGGER.warn("deletion of Helm deployment failed", se);
109 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.PASSIVE,
110 ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
113 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.RUNNING,
114 ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
117 LOGGER.warn("cannot transition from state {} to state {}", currentState, newState);
124 * Callback method to handle an update on a control loop element.
126 * @param element the information on the control loop element
127 * @param nodeTemplate toscaNodeTemplate
128 * @throws PfModelException in case of an exception
131 public synchronized void controlLoopElementUpdate(ControlLoopElement element,
132 ToscaNodeTemplate nodeTemplate) throws PfModelException {
133 @SuppressWarnings("unchecked")
134 Map<String, Object> chartData =
135 (Map<String, Object>) nodeTemplate.getProperties().get("chart");
137 LOGGER.info("Installation request received for the Helm Chart {} ", chartData);
139 var chartInfo = CODER.convert(chartData, ChartInfo.class);
140 chartService.installChart(chartInfo);
141 chartMap.put(element.getId(), chartInfo);
143 var config = CODER.convert(nodeTemplate.getProperties(), ThreadConfig.class);
144 checkPodStatus(chartInfo, config.uninitializedToPassiveTimeout, config.podStatusCheckInterval);
146 } catch (ServiceException | CoderException | IOException e) {
147 LOGGER.warn("Installation of Helm chart failed", e);
152 * Invoke a new thread to check the status of deployed pods.
153 * @param chart ChartInfo
155 public void checkPodStatus(ChartInfo chart, int timeout, int podStatusCheckInterval) {
156 // Invoke runnable thread to check pod status
157 var runnableThread = new Thread(new PodStatusValidator(chart, timeout, podStatusCheckInterval));
158 runnableThread.start();
164 * @param controlLoopElementId controlLoopElement id
165 * @throws PfModelException incase of error
168 public synchronized void handleStatistics(UUID controlLoopElementId) throws PfModelException {
169 // TODO Implement statistics functionality