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 / instance / K8sPluginInstanceApi.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  * Modifications Copyright © 2021 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.instance
21
22 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
23 import com.fasterxml.jackson.module.kotlin.readValue
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheck
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheckList
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheckSimple
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.slf4j.LoggerFactory
32 import org.springframework.http.HttpMethod.DELETE
33 import org.springframework.http.HttpMethod.GET
34 import org.springframework.http.HttpMethod.POST
35 import org.springframework.http.HttpMethod.PUT
36
37 class K8sPluginInstanceApi(
38     private val k8sConfiguration: K8sConnectionPluginConfiguration
39 ) {
40     private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
41
42     fun getInstanceList(): List<K8sRbInstance>? {
43         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
44         try {
45             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
46                 GET.name,
47                 "",
48                 ""
49             )
50             log.debug(result.toString())
51             return if (result.status in 200..299) {
52                 val objectMapper = jacksonObjectMapper()
53                 val parsedObject: ArrayList<K8sRbInstance>? = objectMapper.readValue(result.body)
54                 parsedObject
55             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
56                 null
57             else
58                 throw BluePrintProcessorException(result.body)
59         } catch (e: Exception) {
60             log.error("Caught exception trying to get k8s rb instance")
61             throw BluePrintProcessorException("${e.message}")
62         }
63     }
64
65     fun getInstanceById(instanceId: String): K8sRbInstance? {
66         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
67         try {
68             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
69                 GET.name,
70                 "",
71                 ""
72             )
73             log.debug(result.toString())
74             return if (result.status in 200..299) {
75                 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
76                 parsedObject
77             } else if (result.status == 500 && result.body.contains("Error finding master table"))
78                 null
79             else
80                 throw BluePrintProcessorException(result.body)
81         } catch (e: Exception) {
82             log.error("Caught exception trying to get k8s rb instance")
83             throw BluePrintProcessorException("${e.message}")
84         }
85     }
86
87     fun getInstanceByRequestProperties(
88         rbDefinitionName: String,
89         rbDefinitionVersion: String,
90         rbProfileName: String
91     ): K8sRbInstance? {
92         val instances: List<K8sRbInstance>? = this.getInstanceList()
93         instances?.forEach {
94             if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
95                 it.request?.profileName == rbProfileName
96             )
97                 return it
98         }
99         return null
100     }
101
102     fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
103         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
104         try {
105             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
106                 GET.name,
107                 "/status",
108                 ""
109             )
110             log.debug(result.toString())
111             return if (result.status in 200..299) {
112                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
113                     result.body, K8sRbInstanceStatus::class.java
114                 )
115                 parsedObject
116             } else if (result.status == 500 && result.body.contains("Error finding master table"))
117                 null
118             else
119                 throw BluePrintProcessorException(result.body)
120         } catch (e: Exception) {
121             log.error("Caught exception trying to get k8s rb instance")
122             throw BluePrintProcessorException("${e.message}")
123         }
124     }
125
126     fun queryInstanceStatus(
127         instanceId: String,
128         kind: String,
129         apiVersion: String,
130         name: String? = null,
131         labels: Map<String, String>? = null
132     ): K8sRbInstanceStatus? {
133         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
134         try {
135             var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
136             if (name != null)
137                 path = path.plus("&Name=$name")
138             if (labels != null && labels.isNotEmpty()) {
139                 path = path.plus("&Labels=")
140                 for ((name, value) in labels)
141                     path = path.plus("$name%3D$value,")
142                 path = path.trimEnd(',')
143             }
144             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
145                 GET.name,
146                 path,
147                 ""
148             )
149             log.debug(result.toString())
150             return if (result.status in 200..299) {
151                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
152                     result.body, K8sRbInstanceStatus::class.java
153                 )
154                 parsedObject
155             } else if (result.status == 500 && result.body.contains("Error finding master table"))
156                 null
157             else
158                 throw BluePrintProcessorException(result.body)
159         } catch (e: Exception) {
160             log.error("Caught exception trying to get k8s rb instance")
161             throw BluePrintProcessorException("${e.message}")
162         }
163     }
164
165     fun getInstanceHealthCheckList(instanceId: String): K8sRbInstanceHealthCheckList? {
166         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
167         try {
168             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
169                 GET.name,
170                 "/healthcheck",
171                 ""
172             )
173             log.debug(result.toString())
174             return if (result.status in 200..299) {
175                 val parsedObject: K8sRbInstanceHealthCheckList? = JacksonUtils.readValue(
176                     result.body,
177                     K8sRbInstanceHealthCheckList::class.java
178                 )
179                 parsedObject
180             } else if (result.status == 500 && result.body.contains("Error finding master table"))
181                 null
182             else
183                 throw BluePrintProcessorException(result.body)
184         } catch (e: Exception) {
185             log.error("Caught exception trying to get k8s rb instance")
186             throw BluePrintProcessorException("${e.message}")
187         }
188     }
189
190     fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
191         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
192         try {
193             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
194                 GET.name,
195                 "/healthcheck/$healthCheckId",
196                 ""
197             )
198             log.debug(result.toString())
199             return if (result.status in 200..299) {
200                 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
201                     result.body,
202                     K8sRbInstanceHealthCheck::class.java
203                 )
204                 parsedObject
205             } else if (result.status == 500 && result.body.contains("Error finding master table"))
206                 null
207             else
208                 throw BluePrintProcessorException(result.body)
209         } catch (e: Exception) {
210             log.error("Caught exception trying to get k8s rb instance")
211             throw BluePrintProcessorException("${e.message}")
212         }
213     }
214
215     fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheckSimple? {
216         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
217         try {
218             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
219                 POST.name,
220                 "/healthcheck",
221                 ""
222             )
223             log.debug(result.toString())
224             return if (result.status in 200..299) {
225                 val parsedObject: K8sRbInstanceHealthCheckSimple? = JacksonUtils.readValue(
226                     result.body,
227                     K8sRbInstanceHealthCheckSimple::class.java
228                 )
229                 parsedObject
230             } else if (result.status == 500 && result.body.contains("Error finding master table"))
231                 null
232             else
233                 throw BluePrintProcessorException(result.body)
234         } catch (e: Exception) {
235             log.error("Caught exception trying to get k8s rb instance")
236             throw BluePrintProcessorException("${e.message}")
237         }
238     }
239
240     fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
241         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
242         try {
243             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
244                 POST.name,
245                 "/config",
246                 JacksonUtils.getJson(configValues)
247             )
248             log.debug(result.toString())
249             return if (result.status in 200..299) {
250                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
251                     result.body, K8sConfigValueResponse::class.java
252                 )
253                 parsedObject
254             } else
255                 throw BluePrintProcessorException(result.body)
256         } catch (e: Exception) {
257             log.error("Caught exception trying to create config instance")
258             throw BluePrintProcessorException("${e.message}")
259         }
260     }
261
262     fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
263         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
264         try {
265             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
266                 PUT.name,
267                 "/config/$configName",
268                 JacksonUtils.getJson(configValues)
269             )
270             log.debug(result.toString())
271             return if (result.status in 200..299) {
272                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
273                     result.body, K8sConfigValueResponse::class.java
274                 )
275                 parsedObject
276             } else
277                 throw BluePrintProcessorException(result.body)
278         } catch (e: Exception) {
279             log.error("Caught exception trying to edit config instance")
280             throw BluePrintProcessorException("${e.message}")
281         }
282     }
283
284     fun hasConfigurationValues(instanceId: String, configName: String): Boolean {
285         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
286         try {
287             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
288                 GET.name,
289                 "/config/$configName",
290                 ""
291             )
292             log.debug(result.toString())
293             return result.status in 200..299
294         } catch (e: Exception) {
295             log.error("Caught exception trying to get k8s rb instance")
296             throw BluePrintProcessorException("${e.message}")
297         }
298     }
299
300     fun getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? {
301         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
302         try {
303             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
304                 GET.name,
305                 "/config/$configName",
306                 ""
307             )
308             log.debug(result.toString())
309             return if (result.status in 200..299) {
310                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
311                     result.body, K8sConfigValueResponse::class.java
312                 )
313                 parsedObject
314             } else
315                 throw BluePrintProcessorException(result.body)
316         } catch (e: Exception) {
317             log.error("Caught exception trying to get config instance")
318             throw BluePrintProcessorException("${e.message}")
319         }
320     }
321
322     fun deleteConfigurationValues(instanceId: String, configName: String) {
323         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
324         try {
325             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
326                 DELETE.name,
327                 "/config/$configName",
328                 ""
329             )
330             log.debug(result.toString())
331             if (result.status !in 200..299)
332                 throw BluePrintProcessorException(result.body)
333         } catch (e: Exception) {
334             log.error("Caught exception trying to delete config instance")
335             throw BluePrintProcessorException("${e.message}")
336         }
337     }
338
339     fun rollbackConfigurationValues(instanceId: String): K8sConfigValueResponse? {
340         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
341         try {
342             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
343                 POST.name,
344                 "/rollback",
345                 ""
346             )
347             log.debug(result.toString())
348             return if (result.status in 200..299) {
349                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
350                     result.body, K8sConfigValueResponse::class.java
351                 )
352                 parsedObject
353             } else
354                 throw BluePrintProcessorException(result.body)
355         } catch (e: Exception) {
356             log.error("Caught exception trying to get k8s rb instance")
357             throw BluePrintProcessorException("${e.message}")
358         }
359     }
360
361     fun tagConfigurationValues(instanceId: String): K8sConfigValueResponse? {
362         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
363         try {
364             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
365                 POST.name,
366                 "/tagit",
367                 ""
368             )
369             log.debug(result.toString())
370             return if (result.status in 200..299) {
371                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
372                     result.body, K8sConfigValueResponse::class.java
373                 )
374                 parsedObject
375             } else
376                 throw BluePrintProcessorException(result.body)
377         } catch (e: Exception) {
378             log.error("Caught exception trying to get k8s rb instance")
379             throw BluePrintProcessorException("${e.message}")
380         }
381     }
382
383     fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
384         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
385         try {
386             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
387                 DELETE.name,
388                 "/healthcheck/$healthCheckId",
389                 ""
390             )
391             log.debug(result.toString())
392             if (result.status !in 200..299)
393                 throw BluePrintProcessorException(result.body)
394         } catch (e: Exception) {
395             log.error("Caught exception trying to get k8s rb instance")
396             throw BluePrintProcessorException("${e.message}")
397         }
398     }
399 }