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.profile.upload.K8sProfile
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
26 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
27 import org.slf4j.LoggerFactory
28 import org.springframework.http.HttpMethod
29 import java.nio.file.Path
35 val definition: String?,
36 val definitionVersion: String?
39 private val service: K8sUploadFileRestClientService // BasicAuthRestClientService
40 private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
43 var mapOfHeaders = hashMapOf<String, String>()
44 mapOfHeaders.put("Accept", "application/json")
45 mapOfHeaders.put("Content-Type", "application/json")
46 mapOfHeaders.put("cache-control", " no-cache")
47 mapOfHeaders.put("Accept", "application/json")
48 var basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()
49 basicAuthRestClientProperties.username = username
50 basicAuthRestClientProperties.password = password
51 basicAuthRestClientProperties.url = "$baseUrl/v1/rb/definition/$definition/$definitionVersion"
52 basicAuthRestClientProperties.additionalHeaders = mapOfHeaders
54 this.service = K8sUploadFileRestClientService(basicAuthRestClientProperties)
57 fun hasDefinition(): Boolean {
59 val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource(HttpMethod.GET.name, "", "")
60 log.debug(result.toString())
61 return result.status in 200..299
62 } catch (e: Exception) {
63 log.error("Caught exception trying to get k8s rb definition")
64 throw BlueprintProcessorException("${e.message}")
68 fun hasProfile(profileName: String): Boolean {
70 val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource(
72 "/profile/$profileName",
75 log.debug(result.toString())
76 return result.status in 200..299
77 } catch (e: Exception) {
78 log.error("Caught exception trying to get k8s rb profile")
79 throw BlueprintProcessorException("${e.message}")
83 fun createProfile(profile: K8sProfile) {
84 val objectMapper = ObjectMapper()
85 val profileJsonString: String = objectMapper.writeValueAsString(profile)
87 val result: BlueprintWebClientService.WebClientResponse<String> = service.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(profile: K8sProfile, filePath: Path) {
103 val result: BlueprintWebClientService.WebClientResponse<String> = service.uploadBinaryFile(
104 "/profile/${profile.profileName}/content",
107 if (result.status !in 200..299) {
108 throw Exception(result.body)
110 } catch (e: Exception) {
111 log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
112 throw BlueprintProcessorException("${e.message}")