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