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.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition
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
38 class K8sPluginDefinitionApi(
39 private val k8sConfiguration: K8sConnectionPluginConfiguration
41 private val log = LoggerFactory.getLogger(K8sPluginDefinitionApi::class.java)!!
42 private val objectMapper = ObjectMapper()
44 fun hasDefinition(definition: String, definitionVersion: String): Boolean {
46 val rbDefinitionService = getK8sDefinitionRestClient(
51 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
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}")
64 fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
66 val rbDefinitionService = getK8sDefinitionRestClient(
71 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
73 "/profile/$profileName",
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}")
84 fun getProfile(definition: String, definitionVersion: String, profileName: String): K8sProfile? {
86 val rbDefinitionService = getK8sDefinitionRestClient(
91 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
93 "/profile/$profileName",
96 log.debug(result.toString())
97 return if (result.status in 200..299) {
98 val parsedObject: K8sProfile? = JacksonUtils.readValue(result.body, K8sProfile::class.java)
100 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
110 fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
111 val profileJsonString: String = objectMapper.writeValueAsString(profile)
113 val rbDefinitionService = getK8sDefinitionRestClient(
118 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
123 if (result.status !in 200..299) {
124 throw Exception(result.body)
126 } catch (e: Exception) {
127 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
128 throw BluePrintProcessorException("${e.message}")
132 fun updateProfile(profile: K8sProfile) {
133 val profileJsonString: String = objectMapper.writeValueAsString(profile)
135 val rbDefinitionService = getK8sDefinitionRestClient(
140 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
142 "/profile/${profile.profileName}",
145 if (result.status !in 200..299) {
146 throw Exception(result.body)
148 } catch (e: Exception) {
149 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
150 throw BluePrintProcessorException("${e.message}")
154 fun deleteProfile(definition: String, definitionVersion: String, profileName: String) {
156 val rbDefinitionService = getK8sDefinitionRestClient(
161 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
163 "/profile/$profileName",
166 log.debug(result.toString())
167 if (result.status !in 200..299) {
168 throw Exception(result.body)
170 } catch (e: Exception) {
171 log.error("Caught exception during get template")
172 throw BluePrintProcessorException("${e.message}")
176 fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
178 val fileUploadService = getK8sDefinitionRestClient(
183 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
184 "/profile/${profile.profileName}/content",
187 log.debug(result.toString())
188 if (result.status !in 200..299) {
189 throw Exception(result.body)
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}")
198 fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) {
199 val templateJsonString: String = objectMapper.writeValueAsString(template)
201 val rbDefinitionService = getK8sDefinitionRestClient(
206 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
211 log.debug(result.toString())
212 if (result.status !in 200..299) {
213 throw Exception(result.body)
215 } catch (e: Exception) {
216 log.error("Caught exception during create template")
217 throw BluePrintProcessorException("${e.message}")
221 fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
223 val fileUploadService = getK8sDefinitionRestClient(
228 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
229 "/config-template/${template.templateName}/content",
232 log.debug(result.toString())
233 if (result.status !in 200..299) {
234 throw Exception(result.body)
236 } catch (e: Exception) {
237 log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
238 throw BluePrintProcessorException("${e.message}")
242 fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
244 val rbDefinitionService = getK8sDefinitionRestClient(
249 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
251 "/config-template/$templateName",
254 log.debug(result.toString())
255 if (result.status !in 200..299) {
256 throw Exception(result.body)
258 } catch (e: Exception) {
259 log.error("Caught exception during get template")
260 throw BluePrintProcessorException("${e.message}")
264 fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
266 val rbDefinitionService = getK8sDefinitionRestClient(
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}")
280 fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
282 val interceptedService = getK8sDefinitionRestClient(
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}")
296 private fun getTemplateRequest(rbDefinitionService: BlueprintWebClientService, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
297 return rbDefinitionService.exchangeResource(
299 "/config-template/$templateName",