6257c3d19b0d4182ca3bcdda3bdf4a763be24561
[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.Setter;
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;
45
46 /**
47  * This class handles implementation of controlLoopElement updates.
48  */
49 @Component
50 public class ControlLoopElementHandler implements ControlLoopElementListener {
51     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52
53     @Autowired
54     private ChartService chartService;
55
56     @Setter
57     private ParticipantIntermediaryApi intermediaryApi;
58
59     // Map of CLElement Id and installed Helm charts
60     private final Map<UUID, ChartInfo> chartMap = new HashMap<>();
61
62     /**
63      * Callback method to handle a control loop element state change.
64      *
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
68      */
69     @Override
70     public synchronized void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState,
71             ControlLoopOrderedState newState) {
72         switch (newState) {
73             case UNINITIALISED:
74                 ChartInfo chart = chartMap.get(controlLoopElementId);
75                 if (chart != null) {
76                     LOGGER.info("Helm deployment to be deleted {} ", chart.getReleaseName());
77                     try {
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);
83                     }
84                 }
85                 break;
86             case PASSIVE:
87                 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.PASSIVE);
88                 break;
89             case RUNNING:
90                 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.RUNNING);
91                 break;
92             default:
93                 LOGGER.warn("cannot transition from state {} to state {}", currentState, newState);
94                 break;
95         }
96     }
97
98
99     /**
100      * Callback method to handle an update on a control loop element.
101      *
102      * @param element the information on the control loop element
103      * @param controlLoopDefinition toscaServiceTemplate
104      * @throws PfModelException in case of an exception
105      */
106     @Override
107     public synchronized void controlLoopElementUpdate(ControlLoopElement element,
108             ToscaServiceTemplate controlLoopDefinition) throws PfModelException {
109
110         for (Map.Entry<String, ToscaNodeTemplate> nodeTemplate : controlLoopDefinition.getToscaTopologyTemplate()
111                 .getNodeTemplates().entrySet()) {
112
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");
119
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")));
124                 try {
125                     var repositoryValue = chartData.get("repository");
126                     if (repositoryValue != null) {
127                         chart.setRepository(String.valueOf(repositoryValue));
128                     }
129                     chartService.installChart(chart);
130                     chartMap.put(element.getId(), chart);
131                 } catch (IOException | ServiceException ise) {
132                     LOGGER.warn("installation of Helm chart failed", ise);
133                 }
134             }
135         }
136     }
137
138     /**
139      * Overridden method.
140      *
141      * @param controlLoopElementId controlLoopElement id
142      * @throws PfModelException incase of error
143      */
144     @Override
145     public synchronized void handleStatistics(UUID controlLoopElementId) throws PfModelException {
146         // TODO Implement statistics functionality
147     }
148 }