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