Revert "Renaming Files having BluePrint to have Blueprint"
[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             log.debug(result.toString())
115             if (result.status !in 200..299) {
116                 throw Exception(result.body)
117             }
118             log.debug("Profile uploaded properly")
119         } catch (e: Exception) {
120             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
121             throw BluePrintProcessorException("${e.message}")
122         }
123     }
124
125     fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) {
126         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
127         val templateJsonString: String = objectMapper.writeValueAsString(template)
128         try {
129             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
130                 POST.name,
131                 "/config-template",
132                 templateJsonString
133             )
134             log.debug(result.toString())
135             if (result.status !in 200..299) {
136                 throw Exception(result.body)
137             }
138         } catch (e: Exception) {
139             log.error("Caught exception during create template")
140             throw BluePrintProcessorException("${e.message}")
141         }
142     }
143
144     fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) {
145         val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion)
146         try {
147             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
148                 "/config-template/${template.templateName}/content",
149                 filePath
150             )
151             log.debug(result.toString())
152             if (result.status !in 200..299) {
153                 throw Exception(result.body)
154             }
155         } catch (e: Exception) {
156             log.error("Caught exception trying to upload k8s rb template ${template.templateName}")
157             throw BluePrintProcessorException("${e.message}")
158         }
159     }
160
161     fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) {
162         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
163         try {
164             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
165                 DELETE.name,
166                 "/config-template/$templateName",
167                 ""
168             )
169             log.debug(result.toString())
170             if (result.status !in 200..299) {
171                 throw Exception(result.body)
172             }
173         } catch (e: Exception) {
174             log.error("Caught exception during get template")
175             throw BluePrintProcessorException("${e.message}")
176         }
177     }
178
179     fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate {
180         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
181         try {
182             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
183             log.debug(result.toString())
184             return objectMapper.readValue(result.body)
185         } catch (e: Exception) {
186             log.error("Caught exception during get template")
187             throw BluePrintProcessorException("${e.message}")
188         }
189     }
190
191     fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean {
192         val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion)
193         try {
194             val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName)
195             log.debug(result.toString())
196             return result.status in 200..299
197         } catch (e: Exception) {
198             log.error("Caught exception during get template")
199             throw BluePrintProcessorException("${e.message}")
200         }
201     }
202
203     private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> {
204         return rbDefinitionService.exchangeResource(
205             GET.name,
206             "/config-template/$templateName",
207             ""
208         )
209     }
210 }