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