Re-enable formatting and fix formatting issues
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / k8s-connection-plugin / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / k8s / definition / K8sPluginDefinitionApi.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  * Modifications Copyright © 2020 Orange.
5  * Modifications Copyright © 2020 Deutsche Telekom AG.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition
21
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.profile.K8sProfile
24 import com.fasterxml.jackson.module.kotlin.readValue
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template.K8sTemplate
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.slf4j.LoggerFactory
31 import org.springframework.http.HttpMethod.DELETE
32 import org.springframework.http.HttpMethod.GET
33 import org.springframework.http.HttpMethod.POST
34 import org.springframework.http.HttpMethod.PUT
35 import java.nio.file.Path
36
37 class K8sPluginDefinitionApi(
38     private val k8sConfiguration: K8sConnectionPluginConfiguration
39 ) {
40     private val log = LoggerFactory.getLogger(K8sPluginDefinitionApi::class.java)!!
41     private val objectMapper = ObjectMapper()
42
43     fun hasDefinition(definition: String, definitionVersion: String): Boolean {
44         val rbDefinitionService = K8sDefinitionRestClient(
45             k8sConfiguration,
46             definition,
47             definitionVersion
48         )
49         try {
50             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
51                 GET.name,
52                 "",
53                 ""
54             )
55             log.debug(result.toString())
56             return result.status in 200..299
57         } catch (e: Exception) {
58             log.error("Caught exception trying to get k8s rb definition")
59             throw BluePrintProcessorException("${e.message}")
60         }
61     }
62
63     fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
64         val rbDefinitionService = K8sDefinitionRestClient(
65             k8sConfiguration,
66             definition,
67             definitionVersion
68         )
69         try {
70             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
71                 GET.name,
72                 "/profile/$profileName",
73                 ""
74             )
75             log.debug(result.toString())
76             return result.status in 200..299
77         } catch (e: Exception) {
78             log.error("Caught exception trying to get k8s rb profile")
79             throw BluePrintProcessorException("${e.message}")
80         }
81     }
82
83     fun getProfile(definition: String, definitionVersion: String, profileName: String): K8sProfile? {
84         val rbDefinitionService = K8sDefinitionRestClient(
85             k8sConfiguration,
86             definition,
87             definitionVersion
88         )
89         try {
90             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
91                 GET.name,
92                 "/profile/$profileName",
93                 ""
94             )
95             log.debug(result.toString())
96             return if (result.status in 200..299) {
97                 val parsedObject: K8sProfile? = JacksonUtils.readValue(result.body, K8sProfile::class.java)
98                 parsedObject
99             } else if (result.status == 500 && result.body.contains("Error finding master table"))
100                 null
101             else
102                 throw BluePrintProcessorException(result.body)
103         } catch (e: Exception) {
104             log.error("Caught exception trying to get k8s rb profile")
105             throw BluePrintProcessorException("${e.message}")
106         }
107     }
108
109     fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
110         val rbDefinitionService = K8sDefinitionRestClient(
111             k8sConfiguration,
112             definition,
113             definitionVersion
114         )
115         val profileJsonString: String = objectMapper.writeValueAsString(profile)
116         try {
117             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
118                 POST.name,
119                 "/profile",
120                 profileJsonString
121             )
122             if (result.status !in 200..299) {
123                 throw Exception(result.body)
124             }
125         } catch (e: Exception) {
126             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
127             throw BluePrintProcessorException("${e.message}")
128         }
129     }
130
131     fun updateProfile(profile: K8sProfile) {
132         val rbDefinitionService = K8sDefinitionRestClient(
133             k8sConfiguration,
134             profile.rbName!!,
135             profile.rbVersion!!
136         )
137         val profileJsonString: String = objectMapper.writeValueAsString(profile)
138         try {
139             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
140                 PUT.name,
141                 "/profile/${profile.profileName}",
142                 profileJsonString
143             )
144             if (result.status !in 200..299) {
145                 throw Exception(result.body)
146             }
147         } catch (e: Exception) {
148             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
149             throw BluePrintProcessorException("${e.message}")
150         }
151     }
152
153     fun deleteProfile(definition: String, definitionVersion: String, profileName: String) {
154         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
155         try {
156             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
157                 DELETE.name,
158                 "/profile/$profileName",
159                 ""
160             )
161             log.debug(result.toString())
162             if (result.status !in 200..299) {
163                 throw Exception(result.body)
164             }
165         } catch (e: Exception) {
166             log.error("Caught exception during get template")
167             throw BluePrintProcessorException("${e.message}")
168         }
169     }
170
171     fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
172         val fileUploadService = K8sUploadFileRestClientService(
173             k8sConfiguration,
174             definition,
175             definitionVersion
176         )
177         try {
178             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
179                 "/profile/${profile.profileName}/content",
180                 filePath
181             )
182             log.debug(result.toString())
183             if (result.status !in 200..299) {
184                 throw Exception(result.body)
185             }
186             log.debug("Profile uploaded properly")
187         } catch (e: Exception) {
188             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
189             throw BluePrintProcessorException("${e.message}")
190         }
191     }
192
193     fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) {
194         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
195         val templateJsonString: String = objectMapper.writeValueAsString(template)
196         try {
197             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
198                 POST.name,
199                 "/config-template",
200                 templateJsonString
201             )
202             log.debug(result.toString())
203             if (result.status !in 200..299) {
204                 throw Exception(result.body)
205             }
206         } catch (e: Exception) {
207             log.error("Caught exception during create template")
208             throw BluePrintProcessorException("${e.message}")
209         }
210     }
211
212     fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
213         val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
214         try {
215             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
216                 "/config-template/${template.templateName}/content",
217                 filePath
218             )
219             log.debug(result.toString())
220             if (result.status !in 200..299) {
221                 throw Exception(result.body)
222             }
223         } catch (e: Exception) {
224             log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
225             throw BluePrintProcessorException("${e.message}")
226         }
227     }
228
229     fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
230         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
231         try {
232             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
233                 DELETE.name,
234                 "/config-template/$templateName",
235                 ""
236             )
237             log.debug(result.toString())
238             if (result.status !in 200..299) {
239                 throw Exception(result.body)
240             }
241         } catch (e: Exception) {
242             log.error("Caught exception during get template")
243             throw BluePrintProcessorException("${e.message}")
244         }
245     }
246
247     fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
248         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
249         try {
250             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
251             log.debug(result.toString())
252             return objectMapper.readValue(result.body)
253         } catch (e: Exception) {
254             log.error("Caught exception during get template")
255             throw BluePrintProcessorException("${e.message}")
256         }
257     }
258
259     fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
260         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
261         try {
262             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
263             log.debug(result.toString())
264             return result.status in 200..299
265         } catch (e: Exception) {
266             log.error("Caught exception during get template")
267             throw BluePrintProcessorException("${e.message}")
268         }
269     }
270
271     private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
272         return rbDefinitionService.exchangeResource(
273             GET.name,
274             "/config-template/$templateName",
275             ""
276         )
277     }
278 }