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 lombok.AccessLevel;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
37 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
38 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
39 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
49 * This class handles implementation of controlLoopElement updates.
52 public class ControlLoopElementHandler implements ControlLoopElementListener {
53 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56 private ChartService chartService;
59 private ParticipantIntermediaryApi intermediaryApi;
61 // Map of CLElement Id and installed Helm charts
62 @Getter(AccessLevel.PACKAGE)
63 private final Map<UUID, ChartInfo> chartMap = new HashMap<>();
66 * Callback method to handle a control loop element state change.
68 * @param controlLoopElementId the ID of the control loop element
69 * @param currentState the current state of the control loop element
70 * @param newState the state to which the control loop element is changing to
73 public synchronized void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState,
74 ControlLoopOrderedState newState) {
77 ChartInfo chart = chartMap.get(controlLoopElementId);
79 LOGGER.info("Helm deployment to be deleted {} ", chart.getReleaseName());
81 chartService.uninstallChart(chart);
82 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState,
83 ControlLoopState.UNINITIALISED);
84 } catch (ServiceException se) {
85 LOGGER.warn("deletion of Helm deployment failed", se);
90 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.PASSIVE);
93 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.RUNNING);
96 LOGGER.warn("cannot transition from state {} to state {}", currentState, newState);
103 * Callback method to handle an update on a control loop element.
105 * @param element the information on the control loop element
106 * @param controlLoopDefinition toscaServiceTemplate
107 * @throws PfModelException in case of an exception
110 public synchronized void controlLoopElementUpdate(ControlLoopElement element,
111 ToscaServiceTemplate controlLoopDefinition) throws PfModelException {
113 for (Map.Entry<String, ToscaNodeTemplate> nodeTemplate : controlLoopDefinition.getToscaTopologyTemplate()
114 .getNodeTemplates().entrySet()) {
116 // Fetching the node template of corresponding CL element
117 if (element.getDefinition().getName().equals(nodeTemplate.getKey())
118 && nodeTemplate.getValue().getProperties().containsKey("chart")) {
119 @SuppressWarnings("unchecked")
120 Map<String, Object> chartData =
121 (Map<String, Object>) nodeTemplate.getValue().getProperties().get("chart");
123 LOGGER.info("Installation request received for the Helm Chart {} ", chartData);
124 var chart = new ChartInfo(String.valueOf(chartData.get("release_name")),
125 String.valueOf(chartData.get("chart_name")), String.valueOf(chartData.get("version")),
126 String.valueOf(chartData.get("namespace")));
128 var repositoryValue = chartData.get("repository");
129 if (repositoryValue != null) {
130 chart.setRepository(String.valueOf(repositoryValue));
132 chartService.installChart(chart);
133 chartMap.put(element.getId(), chart);
134 } catch (IOException | ServiceException ise) {
135 LOGGER.warn("installation of Helm chart failed", ise);
144 * @param controlLoopElementId controlLoopElement id
145 * @throws PfModelException incase of error
148 public synchronized void handleStatistics(UUID controlLoopElementId) throws PfModelException {
149 // TODO Implement statistics functionality