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
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.K8sDefinitionRestClient
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.K8sUploadFileRestClientService
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.profile.K8sProfile
26 import com.fasterxml.jackson.module.kotlin.readValue
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.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 java.nio.file.Path
37 private val k8sConfiguration: K8sConnectionPluginConfiguration
39 private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
40 private val objectMapper = ObjectMapper()
42 fun hasDefinition(definition: String, definitionVersion: String): Boolean {
43 val rbDefinitionService = K8sDefinitionRestClient(
49 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
54 log.debug(result.toString())
55 return result.status in 200..299
56 } catch (e: Exception) {
57 log.error("Caught exception trying to get k8s rb definition")
58 throw BlueprintProcessorException("${e.message}")
62 fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
63 val rbDefinitionService = K8sDefinitionRestClient(
69 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
71 "/profile/$profileName",
74 log.debug(result.toString())
75 return result.status in 200..299
76 } catch (e: Exception) {
77 log.error("Caught exception trying to get k8s rb profile")
78 throw BlueprintProcessorException("${e.message}")
82 fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
83 val rbDefinitionService = K8sDefinitionRestClient(
88 val profileJsonString: String = objectMapper.writeValueAsString(profile)
90 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
95 if (result.status !in 200..299) {
96 throw Exception(result.body)
98 } catch (e: Exception) {
99 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
100 throw BlueprintProcessorException("${e.message}")
104 fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
105 val fileUploadService = K8sUploadFileRestClientService(
111 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
112 "/profile/${profile.profileName}/content",
115 if (result.status !in 200..299) {
116 throw Exception(result.body)
118 } catch (e: Exception) {
119 log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
120 throw BlueprintProcessorException("${e.message}")
124 fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate): Boolean {
125 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
126 val templateJsonString: String = objectMapper.writeValueAsString(template)
128 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
133 log.debug(result.toString())
134 return result.status in 200..299
135 } catch (e: Exception) {
136 log.error("Caught exception during create template")
137 throw BlueprintProcessorException("${e.message}")
141 fun uploadTemplate(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
142 val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
144 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
145 "/config-template/${template.templateName}/content",
148 if (result.status !in 200..299) {
149 throw Exception(result.body)
151 } catch (e: Exception) {
152 log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
153 throw BlueprintProcessorException("${e.message}")
157 fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
158 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
160 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
162 "/config-template/$templateName",
165 log.debug(result.toString())
166 } catch (e: Exception) {
167 log.error("Caught exception during get template")
168 throw BlueprintProcessorException("${e.message}")
172 fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
173 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
175 val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
176 log.debug(result.toString())
177 return objectMapper.readValue(result.body)
178 } catch (e: Exception) {
179 log.error("Caught exception during get template")
180 throw BlueprintProcessorException("${e.message}")
184 fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
185 val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
187 val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
188 log.debug(result.toString())
189 return result.status in 200..299
190 } catch (e: Exception) {
191 log.error("Caught exception during get template")
192 throw BlueprintProcessorException("${e.message}")
196 private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
197 return rbDefinitionService.exchangeResource(
199 "/config-template/$templateName",