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.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.slf4j.LoggerFactory
30 import org.springframework.http.HttpMethod.DELETE
31 import org.springframework.http.HttpMethod.GET
32 import org.springframework.http.HttpMethod.POST
33 import org.springframework.http.HttpMethod.PUT
35 class K8sPluginInstanceApi(
36 private val k8sConfiguration: K8sConnectionPluginConfiguration
38 private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
40 fun getInstanceList(): List<K8sRbInstance>? {
41 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
43 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
48 log.debug(result.toString())
49 return if (result.status in 200..299) {
50 val objectMapper = jacksonObjectMapper()
51 val parsedObject: ArrayList<K8sRbInstance>? = objectMapper.readValue(result.body)
53 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
56 throw BlueprintProcessorException(result.body)
57 } catch (e: Exception) {
58 log.error("Caught exception trying to get k8s rb instance")
59 throw BlueprintProcessorException("${e.message}")
63 fun getInstanceById(instanceId: String): K8sRbInstance? {
64 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
66 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
71 log.debug(result.toString())
72 return if (result.status in 200..299) {
73 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
75 } else if (result.status == 500 && result.body.contains("Error finding master table"))
78 throw BlueprintProcessorException(result.body)
79 } catch (e: Exception) {
80 log.error("Caught exception trying to get k8s rb instance")
81 throw BlueprintProcessorException("${e.message}")
85 fun getInstanceByRequestProperties(
86 rbDefinitionName: String,
87 rbDefinitionVersion: String,
90 val instances: List<K8sRbInstance>? = this.getInstanceList()
92 if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
93 it.request?.profileName == rbProfileName
100 fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
101 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
103 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
108 log.debug(result.toString())
109 return if (result.status in 200..299) {
110 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
111 result.body, K8sRbInstanceStatus::class.java
114 } else if (result.status == 500 && result.body.contains("Error finding master table"))
117 throw BlueprintProcessorException(result.body)
118 } catch (e: Exception) {
119 log.error("Caught exception trying to get k8s rb instance")
120 throw BlueprintProcessorException("${e.message}")
124 fun queryInstanceStatus(
128 name: String? = null,
129 labels: Map<String, String>? = null
130 ): K8sRbInstanceStatus? {
131 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
133 var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
135 path = path.plus("&Name=$name")
136 if (labels != null && labels.isNotEmpty()) {
137 path = path.plus("&Labels=")
138 for ((name, value) in labels)
139 path = path.plus("$name%3D$value,")
140 path = path.trimEnd(',')
142 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
147 log.debug(result.toString())
148 return if (result.status in 200..299) {
149 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
150 result.body, K8sRbInstanceStatus::class.java
153 } else if (result.status == 500 && result.body.contains("Error finding master table"))
156 throw BlueprintProcessorException(result.body)
157 } catch (e: Exception) {
158 log.error("Caught exception trying to get k8s rb instance")
159 throw BlueprintProcessorException("${e.message}")
163 fun getInstanceHealthCheckList(instanceId: String): List<K8sRbInstanceHealthCheck>? {
164 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
166 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
171 log.debug(result.toString())
172 return if (result.status in 200..299) {
173 val objectMapper = jacksonObjectMapper()
174 val parsedObject: ArrayList<K8sRbInstanceHealthCheck>? = objectMapper.readValue(result.body)
176 } else if (result.status == 500 && result.body.contains("Error finding master table"))
179 throw BlueprintProcessorException(result.body)
180 } catch (e: Exception) {
181 log.error("Caught exception trying to get k8s rb instance")
182 throw BlueprintProcessorException("${e.message}")
186 fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
187 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
189 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
191 "/healthcheck/$healthCheckId",
194 log.debug(result.toString())
195 return if (result.status in 200..299) {
196 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
198 K8sRbInstanceHealthCheck::class.java
201 } else if (result.status == 500 && result.body.contains("Error finding master table"))
204 throw BlueprintProcessorException(result.body)
205 } catch (e: Exception) {
206 log.error("Caught exception trying to get k8s rb instance")
207 throw BlueprintProcessorException("${e.message}")
211 fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheck? {
212 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
214 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
219 log.debug(result.toString())
220 return if (result.status in 200..299) {
221 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
223 K8sRbInstanceHealthCheck::class.java
226 } else if (result.status == 500 && result.body.contains("Error finding master table"))
229 throw BlueprintProcessorException(result.body)
230 } catch (e: Exception) {
231 log.error("Caught exception trying to get k8s rb instance")
232 throw BlueprintProcessorException("${e.message}")
236 fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
237 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
239 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
242 JacksonUtils.getJson(configValues)
244 log.debug(result.toString())
245 return if (result.status in 200..299) {
246 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
247 result.body, K8sConfigValueResponse::class.java
251 throw BlueprintProcessorException(result.body)
252 } catch (e: Exception) {
253 log.error("Caught exception trying to create config instance")
254 throw BlueprintProcessorException("${e.message}")
258 fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
259 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
261 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
263 "/config/$configName",
264 JacksonUtils.getJson(configValues)
266 log.debug(result.toString())
267 return if (result.status in 200..299) {
268 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
269 result.body, K8sConfigValueResponse::class.java
273 throw BlueprintProcessorException(result.body)
274 } catch (e: Exception) {
275 log.error("Caught exception trying to edit config instance")
276 throw BlueprintProcessorException("${e.message}")
280 fun hasConfigurationValues(instanceId: String, configName: String): Boolean {
281 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
283 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
285 "/config/$configName",
288 log.debug(result.toString())
289 return result.status in 200..299
290 } catch (e: Exception) {
291 log.error("Caught exception trying to get k8s rb instance")
292 throw BlueprintProcessorException("${e.message}")
296 fun getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? {
297 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
299 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
301 "/config/$configName",
304 log.debug(result.toString())
305 return if (result.status in 200..299) {
306 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
307 result.body, K8sConfigValueResponse::class.java
311 throw BlueprintProcessorException(result.body)
312 } catch (e: Exception) {
313 log.error("Caught exception trying to get config instance")
314 throw BlueprintProcessorException("${e.message}")
318 fun deleteConfigurationValues(instanceId: String, configName: String) {
319 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
321 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
323 "/config/$configName",
326 log.debug(result.toString())
327 if (result.status !in 200..299)
328 throw BlueprintProcessorException(result.body)
329 } catch (e: Exception) {
330 log.error("Caught exception trying to delete config instance")
331 throw BlueprintProcessorException("${e.message}")
335 fun rollbackConfigurationValues(instanceId: String): K8sConfigValueResponse? {
336 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
338 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
343 log.debug(result.toString())
344 return if (result.status in 200..299) {
345 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
346 result.body, K8sConfigValueResponse::class.java
350 throw BlueprintProcessorException(result.body)
351 } catch (e: Exception) {
352 log.error("Caught exception trying to get k8s rb instance")
353 throw BlueprintProcessorException("${e.message}")
357 fun tagConfigurationValues(instanceId: String): K8sConfigValueResponse? {
358 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
360 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
365 log.debug(result.toString())
366 return if (result.status in 200..299) {
367 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
368 result.body, K8sConfigValueResponse::class.java
372 throw BlueprintProcessorException(result.body)
373 } catch (e: Exception) {
374 log.error("Caught exception trying to get k8s rb instance")
375 throw BlueprintProcessorException("${e.message}")
379 fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
380 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
382 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
384 "/healthcheck/$healthCheckId",
387 log.debug(result.toString())
388 if (result.status !in 200..299)
389 throw BlueprintProcessorException(result.body)
390 } catch (e: Exception) {
391 log.error("Caught exception trying to get k8s rb instance")
392 throw BlueprintProcessorException("${e.message}")