a36c8e31c8c095b9294947908b2498e2f2de2484
[ccsdk/cds.git] /
1 /*
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.
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s
21
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
35
36 class K8sPluginApi(
37     private val k8sConfiguration: K8sConnectionPluginConfiguration
38 ) {
39     private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
40     private val objectMapper = ObjectMapper()
41
42     fun hasDefinition(definition: String, definitionVersion: String): Boolean {
43         val rbDefinitionService = K8sDefinitionRestClient(
44             k8sConfiguration,
45             definition,
46             definitionVersion
47         )
48         try {
49             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
50                 GET.name,
51                 "",
52                 ""
53             )
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}")
59         }
60     }
61
62     fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
63         val rbDefinitionService = K8sDefinitionRestClient(
64             k8sConfiguration,
65             definition,
66             definitionVersion
67         )
68         try {
69             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
70                 GET.name,
71                 "/profile/$profileName",
72                 ""
73             )
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}")
79         }
80     }
81
82     fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
83         val rbDefinitionService = K8sDefinitionRestClient(
84             k8sConfiguration,
85             definition,
86             definitionVersion
87         )
88         val profileJsonString: String = objectMapper.writeValueAsString(profile)
89         try {
90             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
91                 POST.name,
92                 "/profile",
93                 profileJsonString
94             )
95             if (result.status !in 200..299) {
96                 throw Exception(result.body)
97             }
98         } catch (e: Exception) {
99             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
100             throw BlueprintProcessorException("${e.message}")
101         }
102     }
103
104     fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
105         val fileUploadService = K8sUploadFileRestClientService(
106             k8sConfiguration,
107             definition,
108             definitionVersion
109         )
110         try {
111             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
112                 "/profile/${profile.profileName}/content",
113                 filePath
114             )
115             if (result.status !in 200..299) {
116                 throw Exception(result.body)
117             }
118         } catch (e: Exception) {
119             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
120             throw BlueprintProcessorException("${e.message}")
121         }
122     }
123
124     fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate): Boolean {
125         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
126         val templateJsonString: String = objectMapper.writeValueAsString(template)
127         try {
128             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
129                 POST.name,
130                 "/config-template",
131                 templateJsonString
132             )
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}")
138         }
139     }
140
141     fun uploadTemplate(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
142         val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
143         try {
144             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
145                 "/config-template/${template.templateName}/content",
146                 filePath
147             )
148             if (result.status !in 200..299) {
149                 throw Exception(result.body)
150             }
151         } catch (e: Exception) {
152             log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
153             throw BlueprintProcessorException("${e.message}")
154         }
155     }
156
157     fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
158         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
159         try {
160             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
161                 DELETE.name,
162                 "/config-template/$templateName",
163                 ""
164             )
165             log.debug(result.toString())
166         } catch (e: Exception) {
167             log.error("Caught exception during get template")
168             throw BlueprintProcessorException("${e.message}")
169         }
170     }
171
172     fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
173         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
174         try {
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}")
181         }
182     }
183
184     fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
185         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
186         try {
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}")
193         }
194     }
195
196     private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
197         return rbDefinitionService.exchangeResource(
198             GET.name,
199             "/config-template/$templateName",
200             ""
201         )
202     }
203 }