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.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance
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
37 class K8sPluginInstanceApi(
38 private val k8sConfiguration: K8sConnectionPluginConfiguration
40 private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
42 fun getInstanceList(): List<K8sRbInstance>? {
43 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
45 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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)
55 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
65 fun getInstanceById(instanceId: String): K8sRbInstance? {
66 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
68 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
73 log.debug(result.toString())
74 return if (result.status in 200..299) {
75 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
77 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
87 fun getFullInstanceById(instanceId: String): K8sRbInstanceFull? {
88 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
90 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
95 log.debug(result.toString())
96 return if (result.status in 200..299) {
97 val parsedObject: K8sRbInstanceFull? = JacksonUtils.readValue(result.body, K8sRbInstanceFull::class.java)
99 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
109 fun getInstanceByRequestProperties(
110 rbDefinitionName: String,
111 rbDefinitionVersion: String,
112 rbProfileName: String
114 val instances: List<K8sRbInstance>? = this.getInstanceList()
116 if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
117 it.request?.profileName == rbProfileName
124 fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
125 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
127 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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
138 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
148 fun queryInstanceStatus(
152 name: String? = null,
153 labels: Map<String, String>? = null
154 ): K8sRbInstanceStatus? {
155 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
157 var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
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(',')
166 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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
177 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
187 fun getInstanceHealthCheckList(instanceId: String): K8sRbInstanceHealthCheckList? {
188 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
190 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
195 log.debug(result.toString())
196 return if (result.status in 200..299) {
197 val parsedObject: K8sRbInstanceHealthCheckList? = JacksonUtils.readValue(
199 K8sRbInstanceHealthCheckList::class.java
202 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
212 fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
213 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
215 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
217 "/healthcheck/$healthCheckId",
220 log.debug(result.toString())
221 return if (result.status in 200..299) {
222 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
224 K8sRbInstanceHealthCheck::class.java
227 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
237 fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheckSimple? {
238 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
240 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
245 log.debug(result.toString())
246 return if (result.status in 200..299) {
247 val parsedObject: K8sRbInstanceHealthCheckSimple? = JacksonUtils.readValue(
249 K8sRbInstanceHealthCheckSimple::class.java
252 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
262 fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
263 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
265 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
268 JacksonUtils.getJson(configValues)
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
277 throw BluePrintProcessorException(result.body)
278 } catch (e: Exception) {
279 log.error("Caught exception trying to create config instance")
280 throw BluePrintProcessorException("${e.message}")
284 fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
285 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
287 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
289 "/config/$configName",
290 JacksonUtils.getJson(configValues)
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
299 throw BluePrintProcessorException(result.body)
300 } catch (e: Exception) {
301 log.error("Caught exception trying to edit config instance")
302 throw BluePrintProcessorException("${e.message}")
306 fun editConfigurationValuesByDelete(instanceId: String, configName: String): K8sConfigValueResponse? {
307 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
309 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
311 "/config/$configName/delete",
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
321 throw BluePrintProcessorException(result.body)
322 } catch (e: Exception) {
323 log.error("Caught exception trying to delete config instance")
324 throw BluePrintProcessorException("${e.message}")
328 fun hasConfigurationValues(instanceId: String, configName: String): Boolean {
329 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
331 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
333 "/config/$configName",
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}")
344 fun hasConfigurationValuesVersion(instanceId: String, configName: String, version: String): Boolean {
345 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
347 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
349 "/config/$configName/version/$version",
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}")
360 fun getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? {
361 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
363 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
365 "/config/$configName",
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
375 throw BluePrintProcessorException(result.body)
376 } catch (e: Exception) {
377 log.error("Caught exception trying to get config instance")
378 throw BluePrintProcessorException("${e.message}")
382 fun getConfigurationValuesVersion(instanceId: String, configName: String, version: String): K8sConfigValueResponse? {
383 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
385 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
387 "/config/$configName/version/$version",
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
397 throw BluePrintProcessorException(result.body)
398 } catch (e: Exception) {
399 log.error("Caught exception trying to get config instance")
400 throw BluePrintProcessorException("${e.message}")
404 fun getConfigurationValuesVersionByTag(instanceId: String, configName: String, tag: String): K8sConfigValueResponse? {
405 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
407 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
409 "/config/$configName/tag/$tag",
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
419 throw BluePrintProcessorException(result.body)
420 } catch (e: Exception) {
421 log.error("Caught exception trying to get config instance")
422 throw BluePrintProcessorException("${e.message}")
426 fun getConfigurationValuesList(instanceId: String): List<K8sConfigValueResponse>? {
427 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
429 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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)
439 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
449 fun getConfigurationValuesVersionList(instanceId: String, configName: String): List<K8sConfigValueResponse>? {
450 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
452 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
454 "/config/$configName/version",
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)
462 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
472 fun getConfigurationValuesTagList(instanceId: String, configName: String): List<K8sConfigValueTag>? {
473 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
475 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
477 "/config/$configName/tag",
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)
485 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
495 fun deleteConfigurationValues(instanceId: String, configName: String, deleteConfigOnly: Boolean) {
496 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
498 var path: String = "/config/$configName"
499 if (deleteConfigOnly)
500 path = path.plus("?deleteConfigOnly=true")
501 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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}")
515 fun rollbackConfigurationValues(instanceId: String, configName: String, configVersion: String?, configTag: String?) {
516 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
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(
525 "/config/$configName/rollback",
526 JacksonUtils.getJson(configValues)
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}")
537 fun tagConfigurationValues(instanceId: String, configName: String, tagName: String) {
538 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
540 val configValues = hashMapOf<String, String>()
541 configValues["tag-name"] = tagName
542 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
544 "/config/$configName/tagit",
545 JacksonUtils.getJson(configValues)
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}")
556 fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
557 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
559 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
561 "/healthcheck/$healthCheckId",
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}")