K8sPlugin: support UAT functionality
[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.definition.K8sDefinitionRestClient.Companion.getK8sDefinitionRestClient
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template.K8sTemplate
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.slf4j.LoggerFactory
32 import org.springframework.http.HttpMethod.DELETE
33 import org.springframework.http.HttpMethod.GET
34 import org.springframework.http.HttpMethod.POST
35 import org.springframework.http.HttpMethod.PUT
36 import java.nio.file.Path
37
38 class K8sPluginDefinitionApi(
39     private val k8sConfiguration: K8sConnectionPluginConfiguration
40 ) {
41     private val log = LoggerFactory.getLogger(K8sPluginDefinitionApi::class.java)!!
42     private val objectMapper = ObjectMapper()
43
44     fun hasDefinition(definition: String, definitionVersion: String): Boolean {
45         try {
46             val rbDefinitionService = getK8sDefinitionRestClient(
47                 k8sConfiguration,
48                 definition,
49                 definitionVersion
50             )
51             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
52                 GET.name,
53                 "",
54                 ""
55             )
56             log.debug(result.toString())
57             return result.status in 200..299
58         } catch (e: Exception) {
59             log.error("Caught exception trying to get k8s rb definition")
60             throw BluePrintProcessorException("${e.message}")
61         }
62     }
63
64     fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
65         try {
66             val rbDefinitionService = getK8sDefinitionRestClient(
67                 k8sConfiguration,
68                 definition,
69                 definitionVersion
70             )
71             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
72                 GET.name,
73                 "/profile/$profileName",
74                 ""
75             )
76             log.debug(result.toString())
77             return result.status in 200..299
78         } catch (e: Exception) {
79             log.error("Caught exception trying to get k8s rb profile")
80             throw BluePrintProcessorException("${e.message}")
81         }
82     }
83
84     fun getProfile(definition: String, definitionVersion: String, profileName: String): K8sProfile? {
85         try {
86             val rbDefinitionService = getK8sDefinitionRestClient(
87                 k8sConfiguration,
88                 definition,
89                 definitionVersion
90             )
91             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
92                 GET.name,
93                 "/profile/$profileName",
94                 ""
95             )
96             log.debug(result.toString())
97             return if (result.status in 200..299) {
98                 val parsedObject: K8sProfile? = JacksonUtils.readValue(result.body, K8sProfile::class.java)
99                 parsedObject
100             } else if (result.status == 500 && result.body.contains("Error finding master table"))
101                 null
102             else
103                 throw BluePrintProcessorException(result.body)
104         } catch (e: Exception) {
105             log.error("Caught exception trying to get k8s rb profile")
106             throw BluePrintProcessorException("${e.message}")
107         }
108     }
109
110     fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
111         val profileJsonString: String = objectMapper.writeValueAsString(profile)
112         try {
113             val rbDefinitionService = getK8sDefinitionRestClient(
114                 k8sConfiguration,
115                 definition,
116                 definitionVersion
117             )
118             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
119                 POST.name,
120                 "/profile",
121                 profileJsonString
122             )
123             if (result.status !in 200..299) {
124                 throw Exception(result.body)
125             }
126         } catch (e: Exception) {
127             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
128             throw BluePrintProcessorException("${e.message}")
129         }
130     }
131
132     fun updateProfile(profile: K8sProfile) {
133         val profileJsonString: String = objectMapper.writeValueAsString(profile)
134         try {
135             val rbDefinitionService = getK8sDefinitionRestClient(
136                 k8sConfiguration,
137                 profile.rbName!!,
138                 profile.rbVersion!!
139             )
140             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
141                 PUT.name,
142                 "/profile/${profile.profileName}",
143                 profileJsonString
144             )
145             if (result.status !in 200..299) {
146                 throw Exception(result.body)
147             }
148         } catch (e: Exception) {
149             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
150             throw BluePrintProcessorException("${e.message}")
151         }
152     }
153
154     fun deleteProfile(definition: String, definitionVersion: String, profileName: String) {
155         try {
156             val rbDefinitionService = getK8sDefinitionRestClient(
157                 k8sConfiguration,
158                 definition,
159                 definitionVersion
160             )
161             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
162                 DELETE.name,
163                 "/profile/$profileName",
164                 ""
165             )
166             log.debug(result.toString())
167             if (result.status !in 200..299) {
168                 throw Exception(result.body)
169             }
170         } catch (e: Exception) {
171             log.error("Caught exception during get template")
172             throw BluePrintProcessorException("${e.message}")
173         }
174     }
175
176     fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
177         try {
178             val fileUploadService = getK8sDefinitionRestClient(
179                 k8sConfiguration,
180                 definition,
181                 definitionVersion
182             )
183             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
184                 "/profile/${profile.profileName}/content",
185                 filePath
186             )
187             log.debug(result.toString())
188             if (result.status !in 200..299) {
189                 throw Exception(result.body)
190             }
191             log.debug("Profile uploaded properly")
192         } catch (e: Exception) {
193             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
194             throw BluePrintProcessorException("${e.message}")
195         }
196     }
197
198     fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) {
199         val templateJsonString: String = objectMapper.writeValueAsString(template)
200         try {
201             val rbDefinitionService = getK8sDefinitionRestClient(
202                 k8sConfiguration,
203                 definition,
204                 definitionVersion
205             )
206             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
207                 POST.name,
208                 "/config-template",
209                 templateJsonString
210             )
211             log.debug(result.toString())
212             if (result.status !in 200..299) {
213                 throw Exception(result.body)
214             }
215         } catch (e: Exception) {
216             log.error("Caught exception during create template")
217             throw BluePrintProcessorException("${e.message}")
218         }
219     }
220
221     fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
222         try {
223             val fileUploadService = getK8sDefinitionRestClient(
224                 k8sConfiguration,
225                 definition,
226                 definitionVersion
227             )
228             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
229                 "/config-template/${template.templateName}/content",
230                 filePath
231             )
232             log.debug(result.toString())
233             if (result.status !in 200..299) {
234                 throw Exception(result.body)
235             }
236         } catch (e: Exception) {
237             log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
238             throw BluePrintProcessorException("${e.message}")
239         }
240     }
241
242     fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
243         try {
244             val rbDefinitionService = getK8sDefinitionRestClient(
245                 k8sConfiguration,
246                 definition,
247                 definitionVersion
248             )
249             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
250                 DELETE.name,
251                 "/config-template/$templateName",
252                 ""
253             )
254             log.debug(result.toString())
255             if (result.status !in 200..299) {
256                 throw Exception(result.body)
257             }
258         } catch (e: Exception) {
259             log.error("Caught exception during get template")
260             throw BluePrintProcessorException("${e.message}")
261         }
262     }
263
264     fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
265         try {
266             val rbDefinitionService = getK8sDefinitionRestClient(
267                 k8sConfiguration,
268                 definition,
269                 definitionVersion
270             )
271             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
272             log.debug(result.toString())
273             return objectMapper.readValue(result.body)
274         } catch (e: Exception) {
275             log.error("Caught exception during get template")
276             throw BluePrintProcessorException("${e.message}")
277         }
278     }
279
280     fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
281         try {
282             val interceptedService = getK8sDefinitionRestClient(
283                 k8sConfiguration,
284                 definition,
285                 definitionVersion
286             )
287             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(interceptedService, templateName)
288             log.debug(result.toString())
289             return result.status in 200..299
290         } catch (e: Exception) {
291             log.error("Caught exception during get template")
292             throw BluePrintProcessorException("${e.message}")
293         }
294     }
295
296     private fun getTemplateRequest(rbDefinitionService: BlueprintWebClientService, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
297         return rbDefinitionService.exchangeResource(
298             GET.name,
299             "/config-template/$templateName",
300             ""
301         )
302     }
303 }