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
34 class K8sPluginInstanceApi(
35 private val k8sConfiguration: K8sConnectionPluginConfiguration
37 private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
39 fun getInstanceList(): List<K8sRbInstance>? {
40 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
42 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
47 log.debug(result.toString())
48 return if (result.status in 200..299) {
49 val objectMapper = jacksonObjectMapper()
50 val parsedObject: ArrayList<K8sRbInstance>? = objectMapper.readValue(result.body)
52 } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
55 throw BlueprintProcessorException(result.body)
56 } catch (e: Exception) {
57 log.error("Caught exception trying to get k8s rb instance")
58 throw BlueprintProcessorException("${e.message}")
62 fun getInstanceById(instanceId: String): K8sRbInstance? {
63 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
65 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
70 log.debug(result.toString())
71 return if (result.status in 200..299) {
72 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
74 } else if (result.status == 500 && result.body.contains("Error finding master table"))
77 throw BlueprintProcessorException(result.body)
78 } catch (e: Exception) {
79 log.error("Caught exception trying to get k8s rb instance")
80 throw BlueprintProcessorException("${e.message}")
84 fun getInstanceByRequestProperties(
85 rbDefinitionName: String,
86 rbDefinitionVersion: String,
89 val instances: List<K8sRbInstance>? = this.getInstanceList()
91 if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
92 it.request?.profileName == rbProfileName
99 fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
100 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
102 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
107 log.debug(result.toString())
108 return if (result.status in 200..299) {
109 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
110 result.body, K8sRbInstanceStatus::class.java
113 } else if (result.status == 500 && result.body.contains("Error finding master table"))
116 throw BlueprintProcessorException(result.body)
117 } catch (e: Exception) {
118 log.error("Caught exception trying to get k8s rb instance")
119 throw BlueprintProcessorException("${e.message}")
123 fun getInstanceHealthCheckList(instanceId: String): List<K8sRbInstanceHealthCheck>? {
124 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
126 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
131 log.debug(result.toString())
132 return if (result.status in 200..299) {
133 val objectMapper = jacksonObjectMapper()
134 val parsedObject: ArrayList<K8sRbInstanceHealthCheck>? = objectMapper.readValue(result.body)
136 } else if (result.status == 500 && result.body.contains("Error finding master table"))
139 throw BlueprintProcessorException(result.body)
140 } catch (e: Exception) {
141 log.error("Caught exception trying to get k8s rb instance")
142 throw BlueprintProcessorException("${e.message}")
146 fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
147 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
149 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
151 "/healthcheck/$healthCheckId",
154 log.debug(result.toString())
155 return if (result.status in 200..299) {
156 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
158 K8sRbInstanceHealthCheck::class.java
161 } else if (result.status == 500 && result.body.contains("Error finding master table"))
164 throw BlueprintProcessorException(result.body)
165 } catch (e: Exception) {
166 log.error("Caught exception trying to get k8s rb instance")
167 throw BlueprintProcessorException("${e.message}")
171 fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheck? {
172 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
174 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
179 log.debug(result.toString())
180 return if (result.status in 200..299) {
181 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
183 K8sRbInstanceHealthCheck::class.java
186 } else if (result.status == 500 && result.body.contains("Error finding master table"))
189 throw BlueprintProcessorException(result.body)
190 } catch (e: Exception) {
191 log.error("Caught exception trying to get k8s rb instance")
192 throw BlueprintProcessorException("${e.message}")
196 fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
197 val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
199 val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
201 "/healthcheck/$healthCheckId",
204 log.debug(result.toString())
205 if (result.status !in 200..299)
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}")