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.slf4j.LoggerFactory
30 import org.springframework.http.HttpMethod.DELETE
31 import org.springframework.http.HttpMethod.GET
32 import org.springframework.http.HttpMethod.POST
33 import java.nio.file.Path
35 class K8sPluginDefinitionApi(
36 private val k8sConfiguration: K8sConnectionPluginConfiguration
38 private val log = LoggerFactory.getLogger(K8sPluginDefinitionApi::class.java)!!
39 private val objectMapper = ObjectMapper()
41 fun hasDefinition(definition: String, definitionVersion: String): Boolean {
42 val rbDefinitionService = K8sDefinitionRestClient(
48 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
53 log.debug(result.toString())
54 return result.status in 200..299
55 } catch (e: Exception) {
56 log.error("Caught exception trying to get k8s rb definition")
57 throw BlueprintProcessorException("${e.message}")
61 fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
62 val rbDefinitionService = K8sDefinitionRestClient(
68 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
70 "/profile/$profileName",
73 log.debug(result.toString())
74 return result.status in 200..299
75 } catch (e: Exception) {
76 log.error("Caught exception trying to get k8s rb profile")
77 throw BlueprintProcessorException("${e.message}")
81 fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
82 val rbDefinitionService = K8sDefinitionRestClient(
87 val profileJsonString: String = objectMapper.writeValueAsString(profile)
89 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
94 if (result.status !in 200..299) {
95 throw Exception(result.body)
97 } catch (e: Exception) {
98 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
99 throw BlueprintProcessorException("${e.message}")
103 fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
104 val fileUploadService = K8sUploadFileRestClientService(
110 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
111 "/profile/${profile.profileName}/content",
114 if (result.status !in 200..299) {
115 throw Exception(result.body)
117 } catch (e: Exception) {
118 log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
119 throw BlueprintProcessorException("${e.message}")
123 fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate): Boolean {
124 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
125 val templateJsonString: String = objectMapper.writeValueAsString(template)
127 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
132 log.debug(result.toString())
133 return result.status in 200..299
134 } catch (e: Exception) {
135 log.error("Caught exception during create template")
136 throw BlueprintProcessorException("${e.message}")
140 fun uploadTemplate(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
141 val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
143 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
144 "/config-template/${template.templateName}/content",
147 if (result.status !in 200..299) {
148 throw Exception(result.body)
150 } catch (e: Exception) {
151 log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
152 throw BlueprintProcessorException("${e.message}")
156 fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
157 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
159 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
161 "/config-template/$templateName",
164 log.debug(result.toString())
165 } catch (e: Exception) {
166 log.error("Caught exception during get template")
167 throw BlueprintProcessorException("${e.message}")
171 fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
172 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
174 val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
175 log.debug(result.toString())
176 return objectMapper.readValue(result.body)
177 } catch (e: Exception) {
178 log.error("Caught exception during get template")
179 throw BlueprintProcessorException("${e.message}")
183 fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
184 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
186 val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
187 log.debug(result.toString())
188 return result.status in 200..299
189 } catch (e: Exception) {
190 log.error("Caught exception during get template")
191 throw BlueprintProcessorException("${e.message}")
195 private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
196 return rbDefinitionService.exchangeResource(
198 "/config-template/$templateName",