b58a7eaf4c1a27b275ba30feef33827d2df50018
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  * Modifications Copyright © 2022 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.K8sRbInstanceRestClient.Companion.getK8sRbInstanceRestClient
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheck
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheckList
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheckSimple
29 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.slf4j.LoggerFactory
33 import org.springframework.http.HttpMethod.DELETE
34 import org.springframework.http.HttpMethod.GET
35 import org.springframework.http.HttpMethod.POST
36 import org.springframework.http.HttpMethod.PUT
37
38 class K8sPluginInstanceApi(
39     private val k8sConfiguration: K8sConnectionPluginConfiguration
40 ) {
41     private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
42
43     fun getInstanceList(): List<K8sRbInstance>? {
44         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration)
45         try {
46             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
47                 GET.name,
48                 "",
49                 ""
50             )
51             log.debug(result.toString())
52             return if (result.status in 200..299) {
53                 val objectMapper = jacksonObjectMapper()
54                 val parsedObject: ArrayList<K8sRbInstance>? = objectMapper.readValue(result.body)
55                 parsedObject
56             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
57                 null
58             else
59                 throw BluePrintProcessorException(result.body)
60         } catch (e: Exception) {
61             log.error("Caught exception trying to get k8s rb instance")
62             throw BluePrintProcessorException("${e.message}")
63         }
64     }
65
66     fun getInstanceById(instanceId: String): K8sRbInstance? {
67         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
68         try {
69             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
70                 GET.name,
71                 "",
72                 ""
73             )
74             log.debug(result.toString())
75             return if (result.status in 200..299) {
76                 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
77                 parsedObject
78             } else if (result.status == 500 && result.body.contains("Error finding master table"))
79                 null
80             else
81                 throw BluePrintProcessorException(result.body)
82         } catch (e: Exception) {
83             log.error("Caught exception trying to get k8s rb instance")
84             throw BluePrintProcessorException("${e.message}")
85         }
86     }
87
88     fun getFullInstanceById(instanceId: String): K8sRbInstanceFull? {
89         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
90         try {
91             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
92                 GET.name,
93                 "?full=true",
94                 ""
95             )
96             log.debug(result.toString())
97             return if (result.status in 200..299) {
98                 val parsedObject: K8sRbInstanceFull? = JacksonUtils.readValue(result.body, K8sRbInstanceFull::class.java)
99                 parsedObject
100             } else if (result.status == 500 && result.body.contains("Error finding master table"))
101                 null
102             else
103                 throw BluePrintProcessorException(result.body)
104         } catch (e: Exception) {
105             log.error("Caught exception trying to get k8s rb instance")
106             throw BluePrintProcessorException("${e.message}")
107         }
108     }
109
110     fun getInstanceByRequestProperties(
111         rbDefinitionName: String,
112         rbDefinitionVersion: String,
113         rbProfileName: String
114     ): K8sRbInstance? {
115         val instances: List<K8sRbInstance>? = this.getInstanceList()
116         instances?.forEach {
117             if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
118                 it.request?.profileName == rbProfileName
119             )
120                 return it
121         }
122         return null
123     }
124
125     fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
126         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
127         try {
128             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
129                 GET.name,
130                 "/status",
131                 ""
132             )
133             log.debug(result.toString())
134             return if (result.status in 200..299) {
135                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
136                     result.body, K8sRbInstanceStatus::class.java
137                 )
138                 parsedObject
139             } else if (result.status == 500 && result.body.contains("Error finding master table"))
140                 null
141             else
142                 throw BluePrintProcessorException(result.body)
143         } catch (e: Exception) {
144             log.error("Caught exception trying to get k8s rb instance")
145             throw BluePrintProcessorException("${e.message}")
146         }
147     }
148
149     fun queryInstanceStatus(
150         instanceId: String,
151         kind: String,
152         apiVersion: String,
153         name: String? = null,
154         labels: Map<String, String>? = null
155     ): K8sRbInstanceStatus? {
156         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
157         try {
158             var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
159             if (name != null)
160                 path = path.plus("&Name=$name")
161             if (labels != null && labels.isNotEmpty()) {
162                 path = path.plus("&Labels=")
163                 for ((name, value) in labels)
164                     path = path.plus("$name%3D$value,")
165                 path = path.trimEnd(',')
166             }
167             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
168                 GET.name,
169                 path,
170                 ""
171             )
172             log.debug(result.toString())
173             return if (result.status in 200..299) {
174                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
175                     result.body, K8sRbInstanceStatus::class.java
176                 )
177                 parsedObject
178             } else if (result.status == 500 && result.body.contains("Error finding master table"))
179                 null
180             else
181                 throw BluePrintProcessorException(result.body)
182         } catch (e: Exception) {
183             log.error("Caught exception trying to get k8s rb instance")
184             throw BluePrintProcessorException("${e.message}")
185         }
186     }
187
188     fun getInstanceHealthCheckList(instanceId: String): K8sRbInstanceHealthCheckList? {
189         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
190         try {
191             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
192                 GET.name,
193                 "/healthcheck",
194                 ""
195             )
196             log.debug(result.toString())
197             return if (result.status in 200..299) {
198                 val parsedObject: K8sRbInstanceHealthCheckList? = JacksonUtils.readValue(
199                     result.body,
200                     K8sRbInstanceHealthCheckList::class.java
201                 )
202                 parsedObject
203             } else if (result.status == 500 && result.body.contains("Error finding master table"))
204                 null
205             else
206                 throw BluePrintProcessorException(result.body)
207         } catch (e: Exception) {
208             log.error("Caught exception trying to get k8s rb instance")
209             throw BluePrintProcessorException("${e.message}")
210         }
211     }
212
213     fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
214         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
215         try {
216             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
217                 GET.name,
218                 "/healthcheck/$healthCheckId",
219                 ""
220             )
221             log.debug(result.toString())
222             return if (result.status in 200..299) {
223                 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
224                     result.body,
225                     K8sRbInstanceHealthCheck::class.java
226                 )
227                 parsedObject
228             } else if (result.status == 500 && result.body.contains("Error finding master table"))
229                 null
230             else
231                 throw BluePrintProcessorException(result.body)
232         } catch (e: Exception) {
233             log.error("Caught exception trying to get k8s rb instance")
234             throw BluePrintProcessorException("${e.message}")
235         }
236     }
237
238     fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheckSimple? {
239         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
240         try {
241             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
242                 POST.name,
243                 "/healthcheck",
244                 ""
245             )
246             log.debug(result.toString())
247             return if (result.status in 200..299) {
248                 val parsedObject: K8sRbInstanceHealthCheckSimple? = JacksonUtils.readValue(
249                     result.body,
250                     K8sRbInstanceHealthCheckSimple::class.java
251                 )
252                 parsedObject
253             } else if (result.status == 500 && result.body.contains("Error finding master table"))
254                 null
255             else
256                 throw BluePrintProcessorException(result.body)
257         } catch (e: Exception) {
258             log.error("Caught exception trying to get k8s rb instance")
259             throw BluePrintProcessorException("${e.message}")
260         }
261     }
262
263     fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
264         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
265         try {
266             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
267                 POST.name,
268                 "/config",
269                 JacksonUtils.getJson(configValues)
270             )
271             log.debug(result.toString())
272             return if (result.status in 200..299) {
273                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
274                     result.body, K8sConfigValueResponse::class.java
275                 )
276                 parsedObject
277             } else
278                 throw BluePrintProcessorException(result.body)
279         } catch (e: Exception) {
280             log.error("Caught exception trying to create config instance")
281             throw BluePrintProcessorException("${e.message}")
282         }
283     }
284
285     fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
286         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
287         try {
288             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
289                 PUT.name,
290                 "/config/$configName",
291                 JacksonUtils.getJson(configValues)
292             )
293             log.debug(result.toString())
294             return if (result.status in 200..299) {
295                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
296                     result.body, K8sConfigValueResponse::class.java
297                 )
298                 parsedObject
299             } else
300                 throw BluePrintProcessorException(result.body)
301         } catch (e: Exception) {
302             log.error("Caught exception trying to edit config instance")
303             throw BluePrintProcessorException("${e.message}")
304         }
305     }
306
307     fun editConfigurationValuesByDelete(instanceId: String, configName: String): K8sConfigValueResponse? {
308         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
309         try {
310             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
311                 POST.name,
312                 "/config/$configName/delete",
313                 ""
314             )
315             log.debug(result.toString())
316             return if (result.status in 200..299) {
317                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
318                     result.body, K8sConfigValueResponse::class.java
319                 )
320                 parsedObject
321             } else
322                 throw BluePrintProcessorException(result.body)
323         } catch (e: Exception) {
324             log.error("Caught exception trying to delete config instance")
325             throw BluePrintProcessorException("${e.message}")
326         }
327     }
328
329     fun hasConfigurationValues(instanceId: String, configName: String): Boolean {
330         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
331         try {
332             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
333                 GET.name,
334                 "/config/$configName",
335                 ""
336             )
337             log.debug(result.toString())
338             return result.status in 200..299
339         } catch (e: Exception) {
340             log.error("Caught exception trying to get k8s rb instance")
341             throw BluePrintProcessorException("${e.message}")
342         }
343     }
344
345     fun hasConfigurationValuesVersion(instanceId: String, configName: String, version: String): Boolean {
346         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
347         try {
348             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
349                 GET.name,
350                 "/config/$configName/version/$version",
351                 ""
352             )
353             log.debug(result.toString())
354             return result.status in 200..299
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 getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? {
362         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
363         try {
364             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
365                 GET.name,
366                 "/config/$configName",
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 config instance")
379             throw BluePrintProcessorException("${e.message}")
380         }
381     }
382
383     fun getConfigurationValuesVersion(instanceId: String, configName: String, version: String): K8sConfigValueResponse? {
384         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
385         try {
386             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
387                 GET.name,
388                 "/config/$configName/version/$version",
389                 ""
390             )
391             log.debug(result.toString())
392             return if (result.status in 200..299) {
393                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
394                     result.body, K8sConfigValueResponse::class.java
395                 )
396                 parsedObject
397             } else
398                 throw BluePrintProcessorException(result.body)
399         } catch (e: Exception) {
400             log.error("Caught exception trying to get config instance")
401             throw BluePrintProcessorException("${e.message}")
402         }
403     }
404
405     fun getConfigurationValuesVersionByTag(instanceId: String, configName: String, tag: String): K8sConfigValueResponse? {
406         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
407         try {
408             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
409                 GET.name,
410                 "/config/$configName/tag/$tag",
411                 ""
412             )
413             log.debug(result.toString())
414             return if (result.status in 200..299) {
415                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
416                     result.body, K8sConfigValueResponse::class.java
417                 )
418                 parsedObject
419             } else
420                 throw BluePrintProcessorException(result.body)
421         } catch (e: Exception) {
422             log.error("Caught exception trying to get config instance")
423             throw BluePrintProcessorException("${e.message}")
424         }
425     }
426
427     fun getConfigurationValuesList(instanceId: String): List<K8sConfigValueResponse>? {
428         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
429         try {
430             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
431                 GET.name,
432                 "/config",
433                 ""
434             )
435             log.debug(result.toString())
436             return if (result.status in 200..299) {
437                 val objectMapper = jacksonObjectMapper()
438                 val parsedObject: ArrayList<K8sConfigValueResponse>? = objectMapper.readValue(result.body)
439                 parsedObject
440             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
441                 null
442             else
443                 throw BluePrintProcessorException(result.body)
444         } catch (e: Exception) {
445             log.error("Caught exception trying to get k8s config instance list")
446             throw BluePrintProcessorException("${e.message}")
447         }
448     }
449
450     fun getConfigurationValuesVersionList(instanceId: String, configName: String): List<K8sConfigValueResponse>? {
451         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
452         try {
453             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
454                 GET.name,
455                 "/config/$configName/version",
456                 ""
457             )
458             log.debug(result.toString())
459             return if (result.status in 200..299) {
460                 val objectMapper = jacksonObjectMapper()
461                 val parsedObject: ArrayList<K8sConfigValueResponse>? = objectMapper.readValue(result.body)
462                 parsedObject
463             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
464                 null
465             else
466                 throw BluePrintProcessorException(result.body)
467         } catch (e: Exception) {
468             log.error("Caught exception trying to get k8s config instance version list")
469             throw BluePrintProcessorException("${e.message}")
470         }
471     }
472
473     fun getConfigurationValuesTagList(instanceId: String, configName: String): List<K8sConfigValueTag>? {
474         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
475         try {
476             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
477                 GET.name,
478                 "/config/$configName/tag",
479                 ""
480             )
481             log.debug(result.toString())
482             return if (result.status in 200..299) {
483                 val objectMapper = jacksonObjectMapper()
484                 val parsedObject: ArrayList<K8sConfigValueTag>? = objectMapper.readValue(result.body)
485                 parsedObject
486             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
487                 null
488             else
489                 throw BluePrintProcessorException(result.body)
490         } catch (e: Exception) {
491             log.error("Caught exception trying to get k8s config instance tag list")
492             throw BluePrintProcessorException("${e.message}")
493         }
494     }
495
496     fun deleteConfigurationValues(instanceId: String, configName: String, deleteConfigOnly: Boolean) {
497         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
498         try {
499             var path: String = "/config/$configName"
500             if (deleteConfigOnly)
501                 path = path.plus("?deleteConfigOnly=true")
502             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
503                 DELETE.name,
504                 path,
505                 ""
506             )
507             log.debug(result.toString())
508             if (result.status !in 200..299)
509                 throw BluePrintProcessorException(result.body)
510         } catch (e: Exception) {
511             log.error("Caught exception trying to delete config instance")
512             throw BluePrintProcessorException("${e.message}")
513         }
514     }
515
516     fun rollbackConfigurationValues(instanceId: String, configName: String, configVersion: String?, configTag: String?) {
517         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
518         try {
519             val configValues = hashMapOf<String, String>()
520             if (configVersion != null)
521                 configValues["config-version"] = configVersion
522             if (configTag != null)
523                 configValues["config-tag"] = configTag
524             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
525                 POST.name,
526                 "/config/$configName/rollback",
527                 JacksonUtils.getJson(configValues)
528             )
529             log.debug(result.toString())
530             if (result.status !in 200..299)
531                 throw BluePrintProcessorException(result.body)
532         } catch (e: Exception) {
533             log.error("Caught exception trying to rollback config instance")
534             throw BluePrintProcessorException("${e.message}")
535         }
536     }
537
538     fun tagConfigurationValues(instanceId: String, configName: String, tagName: String) {
539         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
540         try {
541             val configValues = hashMapOf<String, String>()
542             configValues["tag-name"] = tagName
543             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
544                 POST.name,
545                 "/config/$configName/tagit",
546                 JacksonUtils.getJson(configValues)
547             )
548             log.debug(result.toString())
549             if (result.status !in 200..299)
550                 throw BluePrintProcessorException(result.body)
551         } catch (e: Exception) {
552             log.error("Caught exception trying to tag config instance")
553             throw BluePrintProcessorException("${e.message}")
554         }
555     }
556
557     fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
558         val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
559         try {
560             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
561                 DELETE.name,
562                 "/healthcheck/$healthCheckId",
563                 ""
564             )
565             log.debug(result.toString())
566             if (result.status !in 200..299)
567                 throw BluePrintProcessorException(result.body)
568         } catch (e: Exception) {
569             log.error("Caught exception trying to get k8s rb instance")
570             throw BluePrintProcessorException("${e.message}")
571         }
572     }
573 }