K8sPlugin integration changes for Istanbul APIs
[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.functions.k8s.instance.K8sRbInstanceFull
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         val rbDefinitionService = K8sDefinitionRestClient(
46             k8sConfiguration,
47             definition,
48             definitionVersion
49         )
50         try {
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         val rbDefinitionService = K8sDefinitionRestClient(
66             k8sConfiguration,
67             definition,
68             definitionVersion
69         )
70         try {
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         val rbDefinitionService = K8sDefinitionRestClient(
86             k8sConfiguration,
87             definition,
88             definitionVersion
89         )
90         try {
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 rbDefinitionService = K8sDefinitionRestClient(
112             k8sConfiguration,
113             definition,
114             definitionVersion
115         )
116         val profileJsonString: String = objectMapper.writeValueAsString(profile)
117         try {
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 rbDefinitionService = K8sDefinitionRestClient(
134             k8sConfiguration,
135             profile.rbName!!,
136             profile.rbVersion!!
137         )
138         val profileJsonString: String = objectMapper.writeValueAsString(profile)
139         try {
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         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
156         try {
157             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
158                 DELETE.name,
159                 "/profile/${profileName}",
160                 ""
161             )
162             log.debug(result.toString())
163             if (result.status !in 200..299) {
164                 throw Exception(result.body)
165             }
166         } catch (e: Exception) {
167             log.error("Caught exception during get template")
168             throw BluePrintProcessorException("${e.message}")
169         }
170     }
171
172     fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
173         val fileUploadService = K8sUploadFileRestClientService(
174             k8sConfiguration,
175             definition,
176             definitionVersion
177         )
178         try {
179             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
180                 "/profile/${profile.profileName}/content",
181                 filePath
182             )
183             log.debug(result.toString())
184             if (result.status !in 200..299) {
185                 throw Exception(result.body)
186             }
187             log.debug("Profile uploaded properly")
188         } catch (e: Exception) {
189             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
190             throw BluePrintProcessorException("${e.message}")
191         }
192     }
193
194     fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) {
195         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
196         val templateJsonString: String = objectMapper.writeValueAsString(template)
197         try {
198             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
199                 POST.name,
200                 "/config-template",
201                 templateJsonString
202             )
203             log.debug(result.toString())
204             if (result.status !in 200..299) {
205                 throw Exception(result.body)
206             }
207         } catch (e: Exception) {
208             log.error("Caught exception during create template")
209             throw BluePrintProcessorException("${e.message}")
210         }
211     }
212
213     fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
214         val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
215         try {
216             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
217                 "/config-template/${template.templateName}/content",
218                 filePath
219             )
220             log.debug(result.toString())
221             if (result.status !in 200..299) {
222                 throw Exception(result.body)
223             }
224         } catch (e: Exception) {
225             log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
226             throw BluePrintProcessorException("${e.message}")
227         }
228     }
229
230     fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
231         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
232         try {
233             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
234                 DELETE.name,
235                 "/config-template/$templateName",
236                 ""
237             )
238             log.debug(result.toString())
239             if (result.status !in 200..299) {
240                 throw Exception(result.body)
241             }
242         } catch (e: Exception) {
243             log.error("Caught exception during get template")
244             throw BluePrintProcessorException("${e.message}")
245         }
246     }
247
248     fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
249         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
250         try {
251             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
252             log.debug(result.toString())
253             return objectMapper.readValue(result.body)
254         } catch (e: Exception) {
255             log.error("Caught exception during get template")
256             throw BluePrintProcessorException("${e.message}")
257         }
258     }
259
260     fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
261         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
262         try {
263             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
264             log.debug(result.toString())
265             return result.status in 200..299
266         } catch (e: Exception) {
267             log.error("Caught exception during get template")
268             throw BluePrintProcessorException("${e.message}")
269         }
270     }
271
272     private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
273         return rbDefinitionService.exchangeResource(
274             GET.name,
275             "/config-template/$templateName",
276             ""
277         )
278     }
279 }