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.profile.upload
22 import org.springframework.http.HttpMethod
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
26 import java.nio.file.Path
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.slf4j.LoggerFactory
34 val definition: String?,
35 val definitionVersion: String?
37 private val service: K8sUploadFileRestClientService // BasicAuthRestClientService
38 private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
41 var mapOfHeaders = hashMapOf<String, String>()
42 mapOfHeaders.put("Accept", "application/json")
43 mapOfHeaders.put("Content-Type", "application/json")
44 mapOfHeaders.put("cache-control", " no-cache")
45 mapOfHeaders.put("Accept", "application/json")
46 var basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()
47 basicAuthRestClientProperties.username = username
48 basicAuthRestClientProperties.password = password
49 basicAuthRestClientProperties.url = "$baseUrl/v1/rb/definition/$definition/$definitionVersion"
50 basicAuthRestClientProperties.additionalHeaders = mapOfHeaders
52 this.service = K8sUploadFileRestClientService(basicAuthRestClientProperties)
55 fun hasDefinition(): Boolean {
57 val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource(HttpMethod.GET.name, "", "")
58 log.debug(result.toString())
59 return result.status in 200..299
60 } catch (e: Exception) {
61 log.error("Caught exception trying to get k8s rb definition")
62 throw BluePrintProcessorException("${e.message}")
66 fun hasProfile(profileName: String): Boolean {
68 val result: BlueprintWebClientService.WebClientResponse<String> = service.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(profile: K8sProfile) {
82 val objectMapper = ObjectMapper()
83 val profileJsonString: String = objectMapper.writeValueAsString(profile)
85 val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource(
90 if (result.status !in 200..299) {
91 throw Exception(result.body)
93 } catch (e: Exception) {
94 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
95 throw BluePrintProcessorException("${e.message}")
99 fun uploadProfileContent(profile: K8sProfile, filePath: Path) {
101 val result: BlueprintWebClientService.WebClientResponse<String> = service.uploadBinaryFile(
102 "/profile/${profile.profileName}/content",
105 if (result.status !in 200..299) {
106 throw Exception(result.body)
108 } catch (e: Exception) {
109 log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
110 throw BluePrintProcessorException("${e.message}")