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 log.debug(result.toString())
115 if (result.status !in 200..299) {
116 throw Exception(result.body)
118 log.debug("Profile uploaded properly")
119 } catch (e: Exception) {
120 log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
121 throw BluePrintProcessorException("${e.message}")
125 fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) {
126 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
127 val templateJsonString: String = objectMapper.writeValueAsString(template)
129 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
134 log.debug(result.toString())
135 if (result.status !in 200..299) {
136 throw Exception(result.body)
138 } catch (e: Exception) {
139 log.error("Caught exception during create template")
140 throw BluePrintProcessorException("${e.message}")
144 fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
145 val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
147 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
148 "/config-template/${template.templateName}/content",
151 log.debug(result.toString())
152 if (result.status !in 200..299) {
153 throw Exception(result.body)
155 } catch (e: Exception) {
156 log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
157 throw BluePrintProcessorException("${e.message}")
161 fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
162 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
164 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
166 "/config-template/$templateName",
169 log.debug(result.toString())
170 if (result.status !in 200..299) {
171 throw Exception(result.body)
173 } catch (e: Exception) {
174 log.error("Caught exception during get template")
175 throw BluePrintProcessorException("${e.message}")
179 fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
180 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
182 val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
183 log.debug(result.toString())
184 return objectMapper.readValue(result.body)
185 } catch (e: Exception) {
186 log.error("Caught exception during get template")
187 throw BluePrintProcessorException("${e.message}")
191 fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
192 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
194 val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
195 log.debug(result.toString())
196 return result.status in 200..299
197 } catch (e: Exception) {
198 log.error("Caught exception during get template")
199 throw BluePrintProcessorException("${e.message}")
203 private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
204 return rbDefinitionService.exchangeResource(
206 "/config-template/$templateName",