K8sPlugin Healthcheck API and API refactoring 93/117993/1
authorLukasz Rajewski <lukasz.rajewski@orange.com>
Wed, 17 Feb 2021 19:33:21 +0000 (20:33 +0100)
committerLukasz Rajewski <lukasz.rajewski@orange.com>
Wed, 17 Feb 2021 19:34:07 +0000 (20:34 +0100)
K8sPlugin Healthcheck API and API refactoring

Issue-ID: CCSDK-3146
Signed-off-by: Lukasz Rajewski <lukasz.rajewski@orange.com>
Change-Id: Ic3040c888bf78b6d3c91f282200e6f32a3f69816

ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sPluginDefinitionApi.kt [moved from ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sPluginApi.kt with 96% similarity]
ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/profile/K8sProfileUploadComponent.kt
ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/template/K8sConfigTemplateComponent.kt
ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sPluginInstanceApi.kt
ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/healthcheck/K8sRbInstanceHealthCheck.kt [new file with mode: 0644]

  * limitations under the License.
  */
 
-package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s
+package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition
 
 import com.fasterxml.jackson.databind.ObjectMapper
-import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.K8sDefinitionRestClient
-import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.K8sUploadFileRestClientService
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.profile.K8sProfile
 import com.fasterxml.jackson.module.kotlin.readValue
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template.K8sTemplate
 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
@@ -33,10 +32,10 @@ import org.springframework.http.HttpMethod.GET
 import org.springframework.http.HttpMethod.POST
 import java.nio.file.Path
 
-class K8sPluginApi(
+class K8sPluginDefinitionApi(
     private val k8sConfiguration: K8sConnectionPluginConfiguration
 ) {
-    private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
+    private val log = LoggerFactory.getLogger(K8sPluginDefinitionApi::class.java)!!
     private val objectMapper = ObjectMapper()
 
     fun hasDefinition(definition: String, definitionVersion: String): Boolean {
index 47fe6d9..db549f3 100644 (file)
@@ -26,7 +26,7 @@ import org.apache.commons.io.FileUtils
 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService
 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
-import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sPluginApi
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.K8sPluginDefinitionApi
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionService
@@ -125,7 +125,7 @@ open class K8sProfileUploadComponent(
             val k8sProfileUploadConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService)
 
             // Creating API connector
-            var api = K8sPluginApi(k8sProfileUploadConfiguration)
+            var api = K8sPluginDefinitionApi(k8sProfileUploadConfiguration)
 
             if ((profileName == null) || (definitionName == null) || (definitionVersion == null)) {
                 log.warn("Prefix $prefix does not have required data for us to continue.")
index 1156e1b..4e20dcb 100644 (file)
@@ -26,7 +26,7 @@ import org.apache.commons.io.FileUtils
 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService
 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
-import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sPluginApi
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.K8sPluginDefinitionApi
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionService
 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
@@ -122,7 +122,7 @@ open class K8sConfigTemplateComponent(
             val k8sConnectionPluginConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService)
 
             // Creating API connector
-            val api = K8sPluginApi(k8sConnectionPluginConfiguration)
+            val api = K8sPluginDefinitionApi(k8sConnectionPluginConfiguration)
             if ((templateName == null) || (definitionName == null) || (definitionVersion == null)) {
                 log.warn("Prefix $prefix does not have required data for us to continue.")
             } else if (!api.hasDefinition(definitionName, definitionVersion)) {
index 3faaac0..120c380 100644 (file)
 
 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(
@@ -42,7 +46,8 @@ class K8sPluginInstanceApi(
             )
             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
@@ -64,7 +69,6 @@ class K8sPluginInstanceApi(
             )
             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"))
@@ -82,25 +86,31 @@ class K8sPluginInstanceApi(
         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)
@@ -110,18 +120,42 @@ class K8sPluginInstanceApi(
         }
     }
 
-    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"))
@@ -133,4 +167,46 @@ class K8sPluginInstanceApi(
             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}")
+        }
+    }
 }
diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/healthcheck/K8sRbInstanceHealthCheck.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/healthcheck/K8sRbInstanceHealthCheck.kt
new file mode 100644 (file)
index 0000000..b8e7e83
--- /dev/null
@@ -0,0 +1,67 @@
+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()
+    }
+}