K8sPlugin Healthcheck API and API refactoring
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / k8s-connection-plugin / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / k8s / definition / K8sPluginDefinitionApi.kt
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.definition
21
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.profile.K8sProfile
24 import com.fasterxml.jackson.module.kotlin.readValue
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template.K8sTemplate
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
28 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
29 import org.slf4j.LoggerFactory
30 import org.springframework.http.HttpMethod.DELETE
31 import org.springframework.http.HttpMethod.GET
32 import org.springframework.http.HttpMethod.POST
33 import java.nio.file.Path
34
35 class K8sPluginDefinitionApi(
36     private val k8sConfiguration: K8sConnectionPluginConfiguration
37 ) {
38     private val log = LoggerFactory.getLogger(K8sPluginDefinitionApi::class.java)!!
39     private val objectMapper = ObjectMapper()
40
41     fun hasDefinition(definition: String, definitionVersion: String): Boolean {
42         val rbDefinitionService = K8sDefinitionRestClient(
43             k8sConfiguration,
44             definition,
45             definitionVersion
46         )
47         try {
48             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
49                 GET.name,
50                 "",
51                 ""
52             )
53             log.debug(result.toString())
54             return result.status in 200..299
55         } catch (e: Exception) {
56             log.error("Caught exception trying to get k8s rb definition")
57             throw BlueprintProcessorException("${e.message}")
58         }
59     }
60
61     fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
62         val rbDefinitionService = K8sDefinitionRestClient(
63             k8sConfiguration,
64             definition,
65             definitionVersion
66         )
67         try {
68             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
69                 GET.name,
70                 "/profile/$profileName",
71                 ""
72             )
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}")
78         }
79     }
80
81     fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
82         val rbDefinitionService = K8sDefinitionRestClient(
83             k8sConfiguration,
84             definition,
85             definitionVersion
86         )
87         val profileJsonString: String = objectMapper.writeValueAsString(profile)
88         try {
89             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
90                 POST.name,
91                 "/profile",
92                 profileJsonString
93             )
94             if (result.status !in 200..299) {
95                 throw Exception(result.body)
96             }
97         } catch (e: Exception) {
98             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
99             throw BlueprintProcessorException("${e.message}")
100         }
101     }
102
103     fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
104         val fileUploadService = K8sUploadFileRestClientService(
105             k8sConfiguration,
106             definition,
107             definitionVersion
108         )
109         try {
110             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
111                 "/profile/${profile.profileName}/content",
112                 filePath
113             )
114             if (result.status !in 200..299) {
115                 throw Exception(result.body)
116             }
117         } catch (e: Exception) {
118             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
119             throw BlueprintProcessorException("${e.message}")
120         }
121     }
122
123     fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate): Boolean {
124         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
125         val templateJsonString: String = objectMapper.writeValueAsString(template)
126         try {
127             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
128                 POST.name,
129                 "/config-template",
130                 templateJsonString
131             )
132             log.debug(result.toString())
133             return result.status in 200..299
134         } catch (e: Exception) {
135             log.error("Caught exception during create template")
136             throw BlueprintProcessorException("${e.message}")
137         }
138     }
139
140     fun uploadTemplate(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
141         val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
142         try {
143             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
144                 "/config-template/${template.templateName}/content",
145                 filePath
146             )
147             if (result.status !in 200..299) {
148                 throw Exception(result.body)
149             }
150         } catch (e: Exception) {
151             log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
152             throw BlueprintProcessorException("${e.message}")
153         }
154     }
155
156     fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
157         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
158         try {
159             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
160                 DELETE.name,
161                 "/config-template/$templateName",
162                 ""
163             )
164             log.debug(result.toString())
165         } catch (e: Exception) {
166             log.error("Caught exception during get template")
167             throw BlueprintProcessorException("${e.message}")
168         }
169     }
170
171     fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
172         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
173         try {
174             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
175             log.debug(result.toString())
176             return objectMapper.readValue(result.body)
177         } catch (e: Exception) {
178             log.error("Caught exception during get template")
179             throw BlueprintProcessorException("${e.message}")
180         }
181     }
182
183     fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
184         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
185         try {
186             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
187             log.debug(result.toString())
188             return result.status in 200..299
189         } catch (e: Exception) {
190             log.error("Caught exception during get template")
191             throw BlueprintProcessorException("${e.message}")
192         }
193     }
194
195     private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
196         return rbDefinitionService.exchangeResource(
197             GET.name,
198             "/config-template/$templateName",
199             ""
200         )
201     }
202 }