4f654832dcbc67cdc700400cdea6664b4efe73a2
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.kubernetes.handler;
22
23
24 import java.io.IOException;
25 import java.lang.invoke.MethodHandles;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.UUID;
29 import lombok.AccessLevel;
30 import lombok.Getter;
31 import lombok.Setter;
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;
47
48 /**
49  * This class handles implementation of controlLoopElement updates.
50  */
51 @Component
52 public class ControlLoopElementHandler implements ControlLoopElementListener {
53     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55     @Autowired
56     private ChartService chartService;
57
58     @Setter
59     private ParticipantIntermediaryApi intermediaryApi;
60
61     // Map of CLElement Id and installed Helm charts
62     @Getter(AccessLevel.PACKAGE)
63     private final Map<UUID, ChartInfo> chartMap = new HashMap<>();
64
65     /**
66      * Callback method to handle a control loop element state change.
67      *
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
71      */
72     @Override
73     public synchronized void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState,
74             ControlLoopOrderedState newState) {
75         switch (newState) {
76             case UNINITIALISED:
77                 ChartInfo chart = chartMap.get(controlLoopElementId);
78                 if (chart != null) {
79                     LOGGER.info("Helm deployment to be deleted {} ", chart.getReleaseName());
80                     try {
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);
86                     }
87                 }
88                 break;
89             case PASSIVE:
90                 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.PASSIVE);
91                 break;
92             case RUNNING:
93                 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.RUNNING);
94                 break;
95             default:
96                 LOGGER.warn("cannot transition from state {} to state {}", currentState, newState);
97                 break;
98         }
99     }
100
101
102     /**
103      * Callback method to handle an update on a control loop element.
104      *
105      * @param element the information on the control loop element
106      * @param controlLoopDefinition toscaServiceTemplate
107      * @throws PfModelException in case of an exception
108      */
109     @Override
110     public synchronized void controlLoopElementUpdate(ControlLoopElement element,
111             ToscaServiceTemplate controlLoopDefinition) throws PfModelException {
112
113         for (Map.Entry<String, ToscaNodeTemplate> nodeTemplate : controlLoopDefinition.getToscaTopologyTemplate()
114                 .getNodeTemplates().entrySet()) {
115
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");
122
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")));
127                 try {
128                     var repositoryValue = chartData.get("repository");
129                     if (repositoryValue != null) {
130                         chart.setRepository(String.valueOf(repositoryValue));
131                     }
132                     chartService.installChart(chart);
133                     chartMap.put(element.getId(), chart);
134                 } catch (IOException | ServiceException ise) {
135                     LOGGER.warn("installation of Helm chart failed", ise);
136                 }
137             }
138         }
139     }
140
141     /**
142      * Overridden method.
143      *
144      * @param controlLoopElementId controlLoopElement id
145      * @throws PfModelException incase of error
146      */
147     @Override
148     public synchronized void handleStatistics(UUID controlLoopElementId) throws PfModelException {
149         // TODO Implement statistics functionality
150     }
151 }