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