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.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
37 class K8sPluginDefinitionApi(
38 private val k8sConfiguration: K8sConnectionPluginConfiguration
40 private val log = LoggerFactory.getLogger(K8sPluginDefinitionApi::class.java)!!
41 private val objectMapper = ObjectMapper()
43 fun hasDefinition(definition: String, definitionVersion: String): Boolean {
44 val rbDefinitionService = K8sDefinitionRestClient(
50 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
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}")
63 fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
64 val rbDefinitionService = K8sDefinitionRestClient(
70 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
72 "/profile/$profileName",
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}")
83 fun getProfile(definition: String, definitionVersion: String, profileName: String): K8sProfile? {
84 val rbDefinitionService = K8sDefinitionRestClient(
90 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
92 "/profile/$profileName",
95 log.debug(result.toString())
96 return if (result.status in 200..299) {
97 val parsedObject: K8sProfile? = JacksonUtils.readValue(result.body, K8sProfile::class.java)
99 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
109 fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
110 val rbDefinitionService = K8sDefinitionRestClient(
115 val profileJsonString: String = objectMapper.writeValueAsString(profile)
117 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
122 if (result.status !in 200..299) {
123 throw Exception(result.body)
125 } catch (e: Exception) {
126 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
127 throw BluePrintProcessorException("${e.message}")
131 fun updateProfile(profile: K8sProfile) {
132 val rbDefinitionService = K8sDefinitionRestClient(
137 val profileJsonString: String = objectMapper.writeValueAsString(profile)
139 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
141 "/profile/${profile.profileName}",
144 if (result.status !in 200..299) {
145 throw Exception(result.body)
147 } catch (e: Exception) {
148 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
149 throw BluePrintProcessorException("${e.message}")
153 fun deleteProfile(definition: String, definitionVersion: String, profileName: String) {
154 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
156 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
158 "/profile/$profileName",
161 log.debug(result.toString())
162 if (result.status !in 200..299) {
163 throw Exception(result.body)
165 } catch (e: Exception) {
166 log.error("Caught exception during get template")
167 throw BluePrintProcessorException("${e.message}")
171 fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
172 val fileUploadService = K8sUploadFileRestClientService(
178 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
179 "/profile/${profile.profileName}/content",
182 log.debug(result.toString())
183 if (result.status !in 200..299) {
184 throw Exception(result.body)
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}")
193 fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) {
194 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
195 val templateJsonString: String = objectMapper.writeValueAsString(template)
197 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
202 log.debug(result.toString())
203 if (result.status !in 200..299) {
204 throw Exception(result.body)
206 } catch (e: Exception) {
207 log.error("Caught exception during create template")
208 throw BluePrintProcessorException("${e.message}")
212 fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
213 val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
215 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
216 "/config-template/${template.templateName}/content",
219 log.debug(result.toString())
220 if (result.status !in 200..299) {
221 throw Exception(result.body)
223 } catch (e: Exception) {
224 log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
225 throw BluePrintProcessorException("${e.message}")
229 fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
230 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
232 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
234 "/config-template/$templateName",
237 log.debug(result.toString())
238 if (result.status !in 200..299) {
239 throw Exception(result.body)
241 } catch (e: Exception) {
242 log.error("Caught exception during get template")
243 throw BluePrintProcessorException("${e.message}")
247 fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
248 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
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}")
259 fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
260 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
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}")
271 private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
272 return rbDefinitionService.exchangeResource(
274 "/config-template/$templateName",