2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
4 * Modifications Copyright © 2021 Orange.
5 * Modifications Copyright © 2020 Deutsche Telekom AG.
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 getInstanceByRequestProperties(
88 rbDefinitionName: String,
89 rbDefinitionVersion: String,
92 val instances: List<K8sRbInstance>? = this.getInstanceList()
94 if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
95 it.request?.profileName == rbProfileName
102 fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
103 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
105 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
110 log.debug(result.toString())
111 return if (result.status in 200..299) {
112 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
113 result.body, K8sRbInstanceStatus::class.java
116 } else if (result.status == 500 && result.body.contains("Error finding master table"))
119 throw BluePrintProcessorException(result.body)
120 } catch (e: Exception) {
121 log.error("Caught exception trying to get k8s rb instance")
122 throw BluePrintProcessorException("${e.message}")
126 fun queryInstanceStatus(
130 name: String? = null,
131 labels: Map<String, String>? = null
132 ): K8sRbInstanceStatus? {
133 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
135 var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
137 path = path.plus("&Name=$name")
138 if (labels != null && labels.isNotEmpty()) {
139 path = path.plus("&Labels=")
140 for ((name, value) in labels)
141 path = path.plus("$name%3D$value,")
142 path = path.trimEnd(',')
144 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
149 log.debug(result.toString())
150 return if (result.status in 200..299) {
151 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
152 result.body, K8sRbInstanceStatus::class.java
155 } else if (result.status == 500 && result.body.contains("Error finding master table"))
158 throw BluePrintProcessorException(result.body)
159 } catch (e: Exception) {
160 log.error("Caught exception trying to get k8s rb instance")
161 throw BluePrintProcessorException("${e.message}")
165 fun getInstanceHealthCheckList(instanceId: String): K8sRbInstanceHealthCheckList? {
166 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
168 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
173 log.debug(result.toString())
174 return if (result.status in 200..299) {
175 val parsedObject: K8sRbInstanceHealthCheckList? = JacksonUtils.readValue(
177 K8sRbInstanceHealthCheckList::class.java
180 } else if (result.status == 500 && result.body.contains("Error finding master table"))
183 throw BluePrintProcessorException(result.body)
184 } catch (e: Exception) {
185 log.error("Caught exception trying to get k8s rb instance")
186 throw BluePrintProcessorException("${e.message}")
190 fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
191 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
193 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
195 "/healthcheck/$healthCheckId",
198 log.debug(result.toString())
199 return if (result.status in 200..299) {
200 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
202 K8sRbInstanceHealthCheck::class.java
205 } else if (result.status == 500 && result.body.contains("Error finding master table"))
208 throw BluePrintProcessorException(result.body)
209 } catch (e: Exception) {
210 log.error("Caught exception trying to get k8s rb instance")
211 throw BluePrintProcessorException("${e.message}")
215 fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheckSimple? {
216 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
218 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
223 log.debug(result.toString())
224 return if (result.status in 200..299) {
225 val parsedObject: K8sRbInstanceHealthCheckSimple? = JacksonUtils.readValue(
227 K8sRbInstanceHealthCheckSimple::class.java
230 } else if (result.status == 500 && result.body.contains("Error finding master table"))
233 throw BluePrintProcessorException(result.body)
234 } catch (e: Exception) {
235 log.error("Caught exception trying to get k8s rb instance")
236 throw BluePrintProcessorException("${e.message}")
240 fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
241 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
243 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
246 JacksonUtils.getJson(configValues)
248 log.debug(result.toString())
249 return if (result.status in 200..299) {
250 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
251 result.body, K8sConfigValueResponse::class.java
255 throw BluePrintProcessorException(result.body)
256 } catch (e: Exception) {
257 log.error("Caught exception trying to create config instance")
258 throw BluePrintProcessorException("${e.message}")
262 fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
263 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
265 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
267 "/config/$configName",
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 edit config instance")
280 throw BluePrintProcessorException("${e.message}")
284 fun hasConfigurationValues(instanceId: String, configName: String): Boolean {
285 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
287 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
289 "/config/$configName",
292 log.debug(result.toString())
293 return result.status in 200..299
294 } catch (e: Exception) {
295 log.error("Caught exception trying to get k8s rb instance")
296 throw BluePrintProcessorException("${e.message}")
300 fun getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? {
301 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
303 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
305 "/config/$configName",
308 log.debug(result.toString())
309 return if (result.status in 200..299) {
310 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
311 result.body, K8sConfigValueResponse::class.java
315 throw BluePrintProcessorException(result.body)
316 } catch (e: Exception) {
317 log.error("Caught exception trying to get config instance")
318 throw BluePrintProcessorException("${e.message}")
322 fun deleteConfigurationValues(instanceId: String, configName: String) {
323 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
325 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
327 "/config/$configName",
330 log.debug(result.toString())
331 if (result.status !in 200..299)
332 throw BluePrintProcessorException(result.body)
333 } catch (e: Exception) {
334 log.error("Caught exception trying to delete config instance")
335 throw BluePrintProcessorException("${e.message}")
339 fun rollbackConfigurationValues(instanceId: String): K8sConfigValueResponse? {
340 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
342 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
347 log.debug(result.toString())
348 return if (result.status in 200..299) {
349 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
350 result.body, K8sConfigValueResponse::class.java
354 throw BluePrintProcessorException(result.body)
355 } catch (e: Exception) {
356 log.error("Caught exception trying to get k8s rb instance")
357 throw BluePrintProcessorException("${e.message}")
361 fun tagConfigurationValues(instanceId: String): K8sConfigValueResponse? {
362 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
364 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
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 k8s rb instance")
379 throw BluePrintProcessorException("${e.message}")
383 fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
384 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
386 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
388 "/healthcheck/$healthCheckId",
391 log.debug(result.toString())
392 if (result.status !in 200..299)
393 throw BluePrintProcessorException(result.body)
394 } catch (e: Exception) {
395 log.error("Caught exception trying to get k8s rb instance")
396 throw BluePrintProcessorException("${e.message}")