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