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.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
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 {
45 val rbDefinitionService = K8sDefinitionRestClient(
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 {
65 val rbDefinitionService = K8sDefinitionRestClient(
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? {
85 val rbDefinitionService = K8sDefinitionRestClient(
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 rbDefinitionService = K8sDefinitionRestClient(
116 val profileJsonString: String = objectMapper.writeValueAsString(profile)
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 rbDefinitionService = K8sDefinitionRestClient(
138 val profileJsonString: String = objectMapper.writeValueAsString(profile)
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) {
155 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
157 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
159 "/profile/${profileName}",
162 log.debug(result.toString())
163 if (result.status !in 200..299) {
164 throw Exception(result.body)
166 } catch (e: Exception) {
167 log.error("Caught exception during get template")
168 throw BluePrintProcessorException("${e.message}")
172 fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
173 val fileUploadService = K8sUploadFileRestClientService(
179 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
180 "/profile/${profile.profileName}/content",
183 log.debug(result.toString())
184 if (result.status !in 200..299) {
185 throw Exception(result.body)
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}")
194 fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) {
195 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
196 val templateJsonString: String = objectMapper.writeValueAsString(template)
198 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
203 log.debug(result.toString())
204 if (result.status !in 200..299) {
205 throw Exception(result.body)
207 } catch (e: Exception) {
208 log.error("Caught exception during create template")
209 throw BluePrintProcessorException("${e.message}")
213 fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
214 val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
216 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
217 "/config-template/${template.templateName}/content",
220 log.debug(result.toString())
221 if (result.status !in 200..299) {
222 throw Exception(result.body)
224 } catch (e: Exception) {
225 log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
226 throw BluePrintProcessorException("${e.message}")
230 fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
231 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
233 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
235 "/config-template/$templateName",
238 log.debug(result.toString())
239 if (result.status !in 200..299) {
240 throw Exception(result.body)
242 } catch (e: Exception) {
243 log.error("Caught exception during get template")
244 throw BluePrintProcessorException("${e.message}")
248 fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
249 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
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}")
260 fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
261 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
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}")
272 private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
273 return rbDefinitionService.exchangeResource(
275 "/config-template/$templateName",