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(instanceId: String, kind: String, apiVersion: String, name: String?, labels: String?): K8sRbInstanceStatus? {
125 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
127 var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
129 path = path.plus("&Name=$name")
131 path = path.plus("&Labels=$labels")
132 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
137 log.debug(result.toString())
138 return if (result.status in 200..299) {
139 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
140 result.body, K8sRbInstanceStatus::class.java
143 } else if (result.status == 500 && result.body.contains("Error finding master table"))
146 throw BlueprintProcessorException(result.body)
147 } catch (e: Exception) {
148 log.error("Caught exception trying to get k8s rb instance")
149 throw BlueprintProcessorException("${e.message}")
153 fun getInstanceHealthCheckList(instanceId: String): List<K8sRbInstanceHealthCheck>? {
154 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
156 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
161 log.debug(result.toString())
162 return if (result.status in 200..299) {
163 val objectMapper = jacksonObjectMapper()
164 val parsedObject: ArrayList<K8sRbInstanceHealthCheck>? = objectMapper.readValue(result.body)
166 } else if (result.status == 500 && result.body.contains("Error finding master table"))
169 throw BlueprintProcessorException(result.body)
170 } catch (e: Exception) {
171 log.error("Caught exception trying to get k8s rb instance")
172 throw BlueprintProcessorException("${e.message}")
176 fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
177 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
179 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
181 "/healthcheck/$healthCheckId",
184 log.debug(result.toString())
185 return if (result.status in 200..299) {
186 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
188 K8sRbInstanceHealthCheck::class.java
191 } else if (result.status == 500 && result.body.contains("Error finding master table"))
194 throw BlueprintProcessorException(result.body)
195 } catch (e: Exception) {
196 log.error("Caught exception trying to get k8s rb instance")
197 throw BlueprintProcessorException("${e.message}")
201 fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheck? {
202 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
204 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
209 log.debug(result.toString())
210 return if (result.status in 200..299) {
211 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
213 K8sRbInstanceHealthCheck::class.java
216 } else if (result.status == 500 && result.body.contains("Error finding master table"))
219 throw BlueprintProcessorException(result.body)
220 } catch (e: Exception) {
221 log.error("Caught exception trying to get k8s rb instance")
222 throw BlueprintProcessorException("${e.message}")
226 fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
227 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
229 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
232 JacksonUtils.getJson(configValues)
234 log.debug(result.toString())
235 return if (result.status in 200..299) {
236 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
237 result.body, K8sConfigValueResponse::class.java
241 throw BlueprintProcessorException(result.body)
242 } catch (e: Exception) {
243 log.error("Caught exception trying to get k8s rb instance")
244 throw BlueprintProcessorException("${e.message}")
248 fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
249 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
251 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
253 "/config/$configName",
254 JacksonUtils.getJson(configValues)
256 log.debug(result.toString())
257 return if (result.status in 200..299) {
258 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
259 result.body, K8sConfigValueResponse::class.java
263 throw BlueprintProcessorException(result.body)
264 } catch (e: Exception) {
265 log.error("Caught exception trying to get k8s rb instance")
266 throw BlueprintProcessorException("${e.message}")
270 fun hasConfigurationValues(instanceId: String, configName: String): Boolean {
271 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
273 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
275 "/config/$configName",
278 log.debug(result.toString())
279 return result.status in 200..299
280 } catch (e: Exception) {
281 log.error("Caught exception trying to get k8s rb instance")
282 throw BlueprintProcessorException("${e.message}")
286 fun rollbackConfigurationValues(instanceId: String): K8sConfigValueResponse? {
287 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
289 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
294 log.debug(result.toString())
295 return if (result.status in 200..299) {
296 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
297 result.body, K8sConfigValueResponse::class.java
301 throw BlueprintProcessorException(result.body)
302 } catch (e: Exception) {
303 log.error("Caught exception trying to get k8s rb instance")
304 throw BlueprintProcessorException("${e.message}")
308 fun createConfigurationValues(instanceId: String): K8sConfigValueResponse? {
309 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
311 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
316 log.debug(result.toString())
317 return if (result.status in 200..299) {
318 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
319 result.body, K8sConfigValueResponse::class.java
323 throw BlueprintProcessorException(result.body)
324 } catch (e: Exception) {
325 log.error("Caught exception trying to get k8s rb instance")
326 throw BlueprintProcessorException("${e.message}")
330 fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
331 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
333 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
335 "/healthcheck/$healthCheckId",
338 log.debug(result.toString())
339 if (result.status !in 200..299)
340 throw BlueprintProcessorException(result.body)
341 } catch (e: Exception) {
342 log.error("Caught exception trying to get k8s rb instance")
343 throw BlueprintProcessorException("${e.message}")