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.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
38 class K8sPluginInstanceApi(
39 private val k8sConfiguration: K8sConnectionPluginConfiguration
41 private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
43 fun getInstanceList(): List<K8sRbInstance>? {
44 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration)
46 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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)
56 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
66 fun getInstanceById(instanceId: String): K8sRbInstance? {
67 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
69 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
74 log.debug(result.toString())
75 return if (result.status in 200..299) {
76 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
78 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
88 fun getFullInstanceById(instanceId: String): K8sRbInstanceFull? {
89 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
91 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
96 log.debug(result.toString())
97 return if (result.status in 200..299) {
98 val parsedObject: K8sRbInstanceFull? = JacksonUtils.readValue(result.body, K8sRbInstanceFull::class.java)
100 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
110 fun getInstanceByRequestProperties(
111 rbDefinitionName: String,
112 rbDefinitionVersion: String,
113 rbProfileName: String
115 val instances: List<K8sRbInstance>? = this.getInstanceList()
117 if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
118 it.request?.profileName == rbProfileName
125 fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
126 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
128 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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
139 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
149 fun queryInstanceStatus(
153 name: String? = null,
154 labels: Map<String, String>? = null
155 ): K8sRbInstanceStatus? {
156 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
158 var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
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(',')
167 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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
178 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
188 fun getInstanceHealthCheckList(instanceId: String): K8sRbInstanceHealthCheckList? {
189 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
191 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
196 log.debug(result.toString())
197 return if (result.status in 200..299) {
198 val parsedObject: K8sRbInstanceHealthCheckList? = JacksonUtils.readValue(
200 K8sRbInstanceHealthCheckList::class.java
203 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
213 fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
214 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
216 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
218 "/healthcheck/$healthCheckId",
221 log.debug(result.toString())
222 return if (result.status in 200..299) {
223 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
225 K8sRbInstanceHealthCheck::class.java
228 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
238 fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheckSimple? {
239 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
241 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
246 log.debug(result.toString())
247 return if (result.status in 200..299) {
248 val parsedObject: K8sRbInstanceHealthCheckSimple? = JacksonUtils.readValue(
250 K8sRbInstanceHealthCheckSimple::class.java
253 } else if (result.status == 500 && result.body.contains("Error finding master table"))
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}")
263 fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
264 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
266 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
269 JacksonUtils.getJson(configValues)
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
278 throw BluePrintProcessorException(result.body)
279 } catch (e: Exception) {
280 log.error("Caught exception trying to create config instance")
281 throw BluePrintProcessorException("${e.message}")
285 fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
286 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
288 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
290 "/config/$configName",
291 JacksonUtils.getJson(configValues)
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
300 throw BluePrintProcessorException(result.body)
301 } catch (e: Exception) {
302 log.error("Caught exception trying to edit config instance")
303 throw BluePrintProcessorException("${e.message}")
307 fun editConfigurationValuesByDelete(instanceId: String, configName: String): K8sConfigValueResponse? {
308 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
310 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
312 "/config/$configName/delete",
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
322 throw BluePrintProcessorException(result.body)
323 } catch (e: Exception) {
324 log.error("Caught exception trying to delete config instance")
325 throw BluePrintProcessorException("${e.message}")
329 fun hasConfigurationValues(instanceId: String, configName: String): Boolean {
330 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
332 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
334 "/config/$configName",
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}")
345 fun hasConfigurationValuesVersion(instanceId: String, configName: String, version: String): Boolean {
346 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
348 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
350 "/config/$configName/version/$version",
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}")
361 fun getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? {
362 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
364 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
366 "/config/$configName",
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
376 throw BluePrintProcessorException(result.body)
377 } catch (e: Exception) {
378 log.error("Caught exception trying to get config instance")
379 throw BluePrintProcessorException("${e.message}")
383 fun getConfigurationValuesVersion(instanceId: String, configName: String, version: String): K8sConfigValueResponse? {
384 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
386 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
388 "/config/$configName/version/$version",
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
398 throw BluePrintProcessorException(result.body)
399 } catch (e: Exception) {
400 log.error("Caught exception trying to get config instance")
401 throw BluePrintProcessorException("${e.message}")
405 fun getConfigurationValuesVersionByTag(instanceId: String, configName: String, tag: String): K8sConfigValueResponse? {
406 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
408 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
410 "/config/$configName/tag/$tag",
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
420 throw BluePrintProcessorException(result.body)
421 } catch (e: Exception) {
422 log.error("Caught exception trying to get config instance")
423 throw BluePrintProcessorException("${e.message}")
427 fun getConfigurationValuesList(instanceId: String): List<K8sConfigValueResponse>? {
428 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
430 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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)
440 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
450 fun getConfigurationValuesVersionList(instanceId: String, configName: String): List<K8sConfigValueResponse>? {
451 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
453 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
455 "/config/$configName/version",
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)
463 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
473 fun getConfigurationValuesTagList(instanceId: String, configName: String): List<K8sConfigValueTag>? {
474 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
476 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
478 "/config/$configName/tag",
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)
486 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
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}")
496 fun deleteConfigurationValues(instanceId: String, configName: String, deleteConfigOnly: Boolean) {
497 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
499 var path: String = "/config/$configName"
500 if (deleteConfigOnly)
501 path = path.plus("?deleteConfigOnly=true")
502 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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}")
516 fun rollbackConfigurationValues(instanceId: String, configName: String, configVersion: String?, configTag: String?) {
517 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
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(
526 "/config/$configName/rollback",
527 JacksonUtils.getJson(configValues)
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}")
538 fun tagConfigurationValues(instanceId: String, configName: String, tagName: String) {
539 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
541 val configValues = hashMapOf<String, String>()
542 configValues["tag-name"] = tagName
543 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
545 "/config/$configName/tagit",
546 JacksonUtils.getJson(configValues)
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}")
557 fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
558 val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId)
560 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
562 "/healthcheck/$healthCheckId",
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}")