From 97c07491d6dfb1fca6e4aeebaf7318324c1d3eb4 Mon Sep 17 00:00:00 2001 From: Frank Kimmlingen Date: Tue, 21 Mar 2023 16:18:16 +0100 Subject: K8sPlugin: support UAT functionality K8sAbstractRestClientService and all derived classes K8sQueryRestClient K8sRbInstanceRestClient K8sDefinitionRestClient K8sUploadFileRestClientService does not support the spy / verify functionality of UatExecutor / UatServices Issue-ID: CCSDK-3872 Signed-off-by: Frank Kimmlingen Change-Id: Iee30f48e9d86efd07a2ab6dde0d5743e4657934f --- .../rest/service/BaseBlueprintWebClientService.kt | 76 ++++++++++++++-------- .../rest/service/BlueprintWebClientService.kt | 6 ++ 2 files changed, 56 insertions(+), 26 deletions(-) (limited to 'ms/blueprintsprocessor/modules') diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BaseBlueprintWebClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BaseBlueprintWebClientService.kt index ccc2f92a1..1505374be 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BaseBlueprintWebClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BaseBlueprintWebClientService.kt @@ -28,6 +28,7 @@ import org.apache.http.HttpResponse import org.apache.http.HttpStatus import org.apache.http.client.ClientProtocolException import org.apache.http.client.config.RequestConfig +import org.apache.http.client.entity.EntityBuilder import org.apache.http.client.methods.HttpDelete import org.apache.http.client.methods.HttpGet import org.apache.http.client.methods.HttpPatch @@ -40,6 +41,7 @@ import org.apache.http.impl.client.HttpClients import org.apache.http.message.BasicHeader import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService.WebClientResponse import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils @@ -49,6 +51,8 @@ import java.io.IOException import java.io.InputStream import java.net.URI import java.nio.charset.Charset +import java.nio.file.Files +import java.nio.file.Path abstract class BaseBlueprintWebClientService : BlueprintWebClientService { @@ -78,7 +82,7 @@ abstract class BaseBlueprintWebClientService : Blu .build() } - override fun exchangeResource(methodType: String, path: String, request: String): BlueprintWebClientService.WebClientResponse { + override fun exchangeResource(methodType: String, path: String, request: String): WebClientResponse { return this.exchangeResource(methodType, path, request, defaultHeaders()) } @@ -87,7 +91,7 @@ abstract class BaseBlueprintWebClientService : Blu path: String, request: String, headers: Map - ): BlueprintWebClientService.WebClientResponse { + ): WebClientResponse { /** * TODO: Basic headers in the implementations of this client do not get added * in blocking version, whereas in NB version defaultHeaders get added. @@ -106,26 +110,46 @@ abstract class BaseBlueprintWebClientService : Blu } } + @Throws(IOException::class, ClientProtocolException::class) + protected fun performHttpCall(httpUriRequest: HttpUriRequest): WebClientResponse { + val httpResponse = httpClient().execute(httpUriRequest) + val statusCode = httpResponse.statusLine.statusCode + httpResponse.entity.content.use { + val body = IOUtils.toString(it, Charset.defaultCharset()) + return WebClientResponse(statusCode, body) + } + } + + open override fun uploadBinaryFile(path: String, filePath: Path): WebClientResponse { + val convertedHeaders: Array = convertToBasicHeaders(defaultHeaders()) + val httpPost = HttpPost(host(path)) + val entity = EntityBuilder.create().setBinary(Files.readAllBytes(filePath)).build() + httpPost.setEntity(entity) + RestLoggerService.httpInvoking(convertedHeaders) + httpPost.setHeaders(convertedHeaders) + return performHttpCall(httpPost) + } + // TODO: convert to multi-map override fun convertToBasicHeaders(headers: Map): Array { return headers.map { BasicHeader(it.key, it.value) }.toTypedArray() } - open fun delete(path: String, headers: Array, responseType: Class): BlueprintWebClientService.WebClientResponse { + open fun delete(path: String, headers: Array, responseType: Class): WebClientResponse { val httpDelete = HttpDelete(host(path)) RestLoggerService.httpInvoking(headers) httpDelete.setHeaders(headers) return performCallAndExtractTypedWebClientResponse(httpDelete, responseType) } - open fun get(path: String, headers: Array, responseType: Class): BlueprintWebClientService.WebClientResponse { + open fun get(path: String, headers: Array, responseType: Class): WebClientResponse { val httpGet = HttpGet(host(path)) RestLoggerService.httpInvoking(headers) httpGet.setHeaders(headers) return performCallAndExtractTypedWebClientResponse(httpGet, responseType) } - open fun post(path: String, request: Any, headers: Array, responseType: Class): BlueprintWebClientService.WebClientResponse { + open fun post(path: String, request: Any, headers: Array, responseType: Class): WebClientResponse { val httpPost = HttpPost(host(path)) val entity = StringEntity(strRequest(request)) httpPost.entity = entity @@ -134,7 +158,7 @@ abstract class BaseBlueprintWebClientService : Blu return performCallAndExtractTypedWebClientResponse(httpPost, responseType) } - open fun put(path: String, request: Any, headers: Array, responseType: Class): BlueprintWebClientService.WebClientResponse { + open fun put(path: String, request: Any, headers: Array, responseType: Class): WebClientResponse { val httpPut = HttpPut(host(path)) val entity = StringEntity(strRequest(request)) httpPut.entity = entity @@ -143,7 +167,7 @@ abstract class BaseBlueprintWebClientService : Blu return performCallAndExtractTypedWebClientResponse(httpPut, responseType) } - open fun patch(path: String, request: Any, headers: Array, responseType: Class): BlueprintWebClientService.WebClientResponse { + open fun patch(path: String, request: Any, headers: Array, responseType: Class): WebClientResponse { val httpPatch = HttpPatch(host(path)) val entity = StringEntity(strRequest(request)) httpPatch.entity = entity @@ -164,19 +188,19 @@ abstract class BaseBlueprintWebClientService : Blu httpUriRequest: HttpUriRequest, responseType: Class ): - BlueprintWebClientService.WebClientResponse { + WebClientResponse { val httpResponse = httpClient().execute(httpUriRequest) val statusCode = httpResponse.statusLine.statusCode val entity: HttpEntity? = httpResponse.entity if (canResponseHaveBody(httpResponse)) { entity!!.content.use { val body = getResponse(it, responseType) - return BlueprintWebClientService.WebClientResponse(statusCode, body) + return WebClientResponse(statusCode, body) } } else { val constructor = responseType.getConstructor() val body = constructor.newInstance() - return BlueprintWebClientService.WebClientResponse(statusCode, body) + return WebClientResponse(statusCode, body) } } fun canResponseHaveBody(response: HttpResponse): Boolean { @@ -187,24 +211,24 @@ abstract class BaseBlueprintWebClientService : Blu status != HttpStatus.SC_RESET_CONTENT } - open suspend fun getNB(path: String): BlueprintWebClientService.WebClientResponse { + open suspend fun getNB(path: String): WebClientResponse { return getNB(path, null, String::class.java) } - open suspend fun getNB(path: String, additionalHeaders: Array?): BlueprintWebClientService.WebClientResponse { + open suspend fun getNB(path: String, additionalHeaders: Array?): WebClientResponse { return getNB(path, additionalHeaders, String::class.java) } open suspend fun getNB(path: String, additionalHeaders: Array?, responseType: Class): - BlueprintWebClientService.WebClientResponse = withContext(Dispatchers.IO) { + WebClientResponse = withContext(Dispatchers.IO) { get(path, additionalHeaders!!, responseType) } - open suspend fun postNB(path: String, request: Any): BlueprintWebClientService.WebClientResponse { + open suspend fun postNB(path: String, request: Any): WebClientResponse { return postNB(path, request, null, String::class.java) } - open suspend fun postNB(path: String, request: Any, additionalHeaders: Array?): BlueprintWebClientService.WebClientResponse { + open suspend fun postNB(path: String, request: Any, additionalHeaders: Array?): WebClientResponse { return postNB(path, request, additionalHeaders, String::class.java) } @@ -213,11 +237,11 @@ abstract class BaseBlueprintWebClientService : Blu request: Any, additionalHeaders: Array?, responseType: Class - ): BlueprintWebClientService.WebClientResponse = withContext(Dispatchers.IO) { + ): WebClientResponse = withContext(Dispatchers.IO) { post(path, request, additionalHeaders!!, responseType) } - open suspend fun putNB(path: String, request: Any): BlueprintWebClientService.WebClientResponse { + open suspend fun putNB(path: String, request: Any): WebClientResponse { return putNB(path, request, null, String::class.java) } @@ -225,7 +249,7 @@ abstract class BaseBlueprintWebClientService : Blu path: String, request: Any, additionalHeaders: Array? - ): BlueprintWebClientService.WebClientResponse { + ): WebClientResponse { return putNB(path, request, additionalHeaders, String::class.java) } @@ -234,30 +258,30 @@ abstract class BaseBlueprintWebClientService : Blu request: Any, additionalHeaders: Array?, responseType: Class - ): BlueprintWebClientService.WebClientResponse = withContext(Dispatchers.IO) { + ): WebClientResponse = withContext(Dispatchers.IO) { put(path, request, additionalHeaders!!, responseType) } - open suspend fun deleteNB(path: String): BlueprintWebClientService.WebClientResponse { + open suspend fun deleteNB(path: String): WebClientResponse { return deleteNB(path, null, String::class.java) } open suspend fun deleteNB(path: String, additionalHeaders: Array?): - BlueprintWebClientService.WebClientResponse { + WebClientResponse { return deleteNB(path, additionalHeaders, String::class.java) } open suspend fun deleteNB(path: String, additionalHeaders: Array?, responseType: Class): - BlueprintWebClientService.WebClientResponse = withContext(Dispatchers.IO) { + WebClientResponse = withContext(Dispatchers.IO) { delete(path, additionalHeaders!!, responseType) } open suspend fun patchNB(path: String, request: Any, additionalHeaders: Array?, responseType: Class): - BlueprintWebClientService.WebClientResponse = withContext(Dispatchers.IO) { + WebClientResponse = withContext(Dispatchers.IO) { patch(path, request, additionalHeaders!!, responseType) } - override suspend fun exchangeNB(methodType: String, path: String, request: Any): BlueprintWebClientService.WebClientResponse { + override suspend fun exchangeNB(methodType: String, path: String, request: Any): WebClientResponse { return exchangeNB( methodType, path, request, hashMapOf(), String::class.java @@ -265,7 +289,7 @@ abstract class BaseBlueprintWebClientService : Blu } override suspend fun exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map?): - BlueprintWebClientService.WebClientResponse { + WebClientResponse { return exchangeNB(methodType, path, request, additionalHeaders, String::class.java) } @@ -275,7 +299,7 @@ abstract class BaseBlueprintWebClientService : Blu request: Any, additionalHeaders: Map?, responseType: Class - ): BlueprintWebClientService.WebClientResponse { + ): WebClientResponse { // TODO: possible inconsistency // NOTE: this basic headers function is different from non-blocking diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt index ed52e6212..76d6d4646 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt @@ -22,6 +22,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.rest.service import org.apache.http.message.BasicHeader import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintRetryException import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintIOUtils +import java.nio.file.Path interface BlueprintWebClientService { fun defaultHeaders(): Map @@ -42,6 +43,11 @@ interface BlueprintWebClientService { request: String ): WebClientResponse + fun uploadBinaryFile( + path: String, + filePath: Path + ): WebClientResponse + suspend fun exchangeNB(methodType: String, path: String, request: Any): WebClientResponse suspend fun exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map?): -- cgit 1.2.3-korg