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 org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
28 import org.slf4j.LoggerFactory
29 import org.springframework.http.HttpMethod.GET
30 import org.springframework.http.HttpMethod.POST
31 import java.nio.file.Path
34 private val k8sConfiguration: K8sConnectionPluginConfiguration
36 private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
37 private val objectMapper = ObjectMapper()
39 fun hasDefinition(definition: String, definitionVersion: String): Boolean {
40 val rbDefinitionService = K8sDefinitionRestClient(
46 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
51 log.debug(result.toString())
52 return result.status in 200..299
53 } catch (e: Exception) {
54 log.error("Caught exception trying to get k8s rb definition")
55 throw BlueprintProcessorException("${e.message}")
59 fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
60 val rbDefinitionService = K8sDefinitionRestClient(
66 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
68 "/profile/$profileName",
71 log.debug(result.toString())
72 return result.status in 200..299
73 } catch (e: Exception) {
74 log.error("Caught exception trying to get k8s rb profile")
75 throw BlueprintProcessorException("${e.message}")
79 fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
80 val rbDefinitionService = K8sDefinitionRestClient(
85 val profileJsonString: String = objectMapper.writeValueAsString(profile)
87 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
92 if (result.status !in 200..299) {
93 throw Exception(result.body)
95 } catch (e: Exception) {
96 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
97 throw BlueprintProcessorException("${e.message}")
101 fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
102 val fileUploadService = K8sUploadFileRestClientService(
108 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
109 "/profile/${profile.profileName}/content",
112 if (result.status !in 200..299) {
113 throw Exception(result.body)
115 } catch (e: Exception) {
116 log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
117 throw BlueprintProcessorException("${e.message}")