package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance
-import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
+import com.fasterxml.jackson.module.kotlin.readValue
import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheck
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.slf4j.LoggerFactory
+import org.springframework.http.HttpMethod.DELETE
import org.springframework.http.HttpMethod.GET
+import org.springframework.http.HttpMethod.POST
class K8sPluginInstanceApi(
private val k8sConfiguration: K8sConnectionPluginConfiguration
) {
private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
- fun getAllInstances(): List<K8sRbInstance>? {
+ fun getInstanceList(): List<K8sRbInstance>? {
val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
try {
val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
)
log.debug(result.toString())
return if (result.status in 200..299) {
- val parsedObject: List<K8sRbInstance> = JacksonUtils.readValue(result.body)
+ val objectMapper = jacksonObjectMapper()
+ val parsedObject: ArrayList<K8sRbInstance>? = objectMapper.readValue(result.body)
parsedObject
} else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
null
)
log.debug(result.toString())
return if (result.status in 200..299) {
- val instance: JsonNode = JacksonUtils.jsonNode(result.body)
val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
parsedObject
} else if (result.status == 500 && result.body.contains("Error finding master table"))
rbDefinitionVersion: String,
rbProfileName: String
): K8sRbInstance? {
- val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
+ val instances: List<K8sRbInstance>? = this.getInstanceList()
+ instances?.forEach {
+ if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
+ it.request?.profileName == rbProfileName
+ )
+ return it
+ }
+ return null
+ }
+
+ fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
+ val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
try {
val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
GET.name,
- "",
+ "/status",
""
)
log.debug(result.toString())
return if (result.status in 200..299) {
- val parsedObject: List<K8sRbInstance> = JacksonUtils.readValue(result.body)
- var instance: K8sRbInstance? = null
- parsedObject.forEach {
- if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
- it.request?.profileName == rbProfileName
- )
- instance = it
- }
- instance
- } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
+ val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
+ result.body, K8sRbInstanceStatus::class.java
+ )
+ parsedObject
+ } else if (result.status == 500 && result.body.contains("Error finding master table"))
null
else
throw BlueprintProcessorException(result.body)
}
}
- fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
+ fun getInstanceHealthCheckList(instanceId: String): List<K8sRbInstanceHealthCheck>? {
val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
try {
val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
GET.name,
- "/status",
+ "/healthcheck",
""
)
log.debug(result.toString())
return if (result.status in 200..299) {
- val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
- result.body, K8sRbInstanceStatus::class.java
+ val objectMapper = jacksonObjectMapper()
+ val parsedObject: ArrayList<K8sRbInstanceHealthCheck>? = objectMapper.readValue(result.body)
+ parsedObject
+ } else if (result.status == 500 && result.body.contains("Error finding master table"))
+ null
+ else
+ throw BlueprintProcessorException(result.body)
+ } catch (e: Exception) {
+ log.error("Caught exception trying to get k8s rb instance")
+ throw BlueprintProcessorException("${e.message}")
+ }
+ }
+
+ fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
+ val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
+ try {
+ val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
+ GET.name,
+ "/healthcheck/$healthCheckId",
+ ""
+ )
+ log.debug(result.toString())
+ return if (result.status in 200..299) {
+ val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
+ result.body,
+ K8sRbInstanceHealthCheck::class.java
)
parsedObject
} else if (result.status == 500 && result.body.contains("Error finding master table"))
throw BlueprintProcessorException("${e.message}")
}
}
+
+ fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheck? {
+ val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
+ try {
+ val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
+ POST.name,
+ "/healthcheck",
+ ""
+ )
+ log.debug(result.toString())
+ return if (result.status in 200..299) {
+ val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
+ result.body,
+ K8sRbInstanceHealthCheck::class.java
+ )
+ parsedObject
+ } else if (result.status == 500 && result.body.contains("Error finding master table"))
+ null
+ else
+ throw BlueprintProcessorException(result.body)
+ } catch (e: Exception) {
+ log.error("Caught exception trying to get k8s rb instance")
+ throw BlueprintProcessorException("${e.message}")
+ }
+ }
+
+ fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
+ val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
+ try {
+ val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
+ DELETE.name,
+ "/healthcheck/$healthCheckId",
+ ""
+ )
+ log.debug(result.toString())
+ if (result.status !in 200..299)
+ throw BlueprintProcessorException(result.body)
+ } catch (e: Exception) {
+ log.error("Caught exception trying to get k8s rb instance")
+ throw BlueprintProcessorException("${e.message}")
+ }
+ }
}
--- /dev/null
+package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck
+
+import com.fasterxml.jackson.annotation.JsonProperty
+
+class K8sRbInstanceHealthCheck {
+
+ @get:JsonProperty("Id")
+ var id: String? = null
+
+ @get:JsonProperty("StartedAt")
+ var startedAt: String? = null
+
+ @get:JsonProperty("CompletedAt")
+ var completedAt: String? = null
+
+ @get:JsonProperty("Status")
+ var status: String? = null
+
+ @get:JsonProperty("Tests")
+ var tests: List<K8sHealthCheckTest>? = null
+
+ override fun toString(): String {
+ return "$id:$status"
+ }
+
+ override fun equals(other: Any?): Boolean {
+ if (this === other) return true
+ if (javaClass != other?.javaClass) return false
+ return true
+ }
+
+ override fun hashCode(): Int {
+ return javaClass.hashCode()
+ }
+}
+
+class K8sHealthCheckTest {
+
+ @get:JsonProperty("Name")
+ var name: String? = null
+
+ @get:JsonProperty("StartedAt")
+ var startedAt: String? = null
+
+ @get:JsonProperty("CompletedAt")
+ var completedAt: String? = null
+
+ @get:JsonProperty("Status")
+ var status: String? = null
+
+ @get:JsonProperty("Info")
+ var info: String? = null
+
+ override fun toString(): String {
+ return "$name:$status"
+ }
+
+ override fun equals(other: Any?): Boolean {
+ if (this === other) return true
+ if (javaClass != other?.javaClass) return false
+ return true
+ }
+
+ override fun hashCode(): Int {
+ return javaClass.hashCode()
+ }
+}