From 9c0f6c2b556c9d39766636544827189d75b6af50 Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Mon, 22 Jul 2019 13:09:18 -0400 Subject: Clean restconf duplicate models and Implementation. Change-Id: Id439ac5ded631aac0ee7fc69846ebe9bca650bb2 Issue-ID: CCSDK-1499 Signed-off-by: Brinda Santh --- .../rest/service/BlueprintWebClientService.kt | 87 +++++++++++----------- 1 file changed, 45 insertions(+), 42 deletions(-) (limited to 'ms/blueprintsprocessor/modules/commons/rest-lib/src') 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 5d35186b8..1acd07b7b 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 @@ -23,18 +23,15 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.apache.commons.io.IOUtils import org.apache.http.client.ClientProtocolException -import org.apache.http.client.methods.HttpDelete -import org.apache.http.client.methods.HttpGet -import org.apache.http.client.methods.HttpPatch -import org.apache.http.client.methods.HttpPost -import org.apache.http.client.methods.HttpPut -import org.apache.http.client.methods.HttpUriRequest +import org.apache.http.client.methods.* import org.apache.http.entity.StringEntity import org.apache.http.impl.client.CloseableHttpClient import org.apache.http.impl.client.HttpClients import org.apache.http.message.BasicHeader import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintRetryException +import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintIOUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.springframework.http.HttpMethod import java.io.IOException @@ -49,9 +46,22 @@ interface BlueprintWebClientService { fun httpClient(): CloseableHttpClient { return HttpClients.custom() - .addInterceptorFirst(WebClientUtils.logRequest()) - .addInterceptorLast(WebClientUtils.logResponse()) - .build() + .addInterceptorFirst(WebClientUtils.logRequest()) + .addInterceptorLast(WebClientUtils.logResponse()) + .build() + } + + /** High performance non blocking Retry function, If execution block [block] throws BluePrintRetryException + * exception then this will perform wait and retrigger accoring to times [times] with delay [delay] + */ + suspend fun retry(times: Int = 1, initialDelay: Long = 0, delay: Long = 1000, + block: suspend (Int) -> T): T { + val exceptionBlock = { e: Exception -> + if (e !is BluePrintRetryException) { + throw e + } + } + return BluePrintIOUtils.retry(times, initialDelay, delay, block, exceptionBlock) } fun exchangeResource(methodType: String, path: String, request: String): WebClientResponse { @@ -72,8 +82,7 @@ interface BlueprintWebClientService { HttpMethod.POST -> post(path, request, convertedHeaders, String::class.java) HttpMethod.PUT -> put(path, request, convertedHeaders, String::class.java) HttpMethod.PATCH -> patch(path, request, convertedHeaders, String::class.java) - else -> throw BluePrintProcessorException("Unsupported met" + - "hodType($methodType)") + else -> throw BluePrintProcessorException("Unsupported methodType($methodType)") } } @@ -126,8 +135,8 @@ interface BlueprintWebClientService { @Throws(IOException::class, ClientProtocolException::class) private fun performCallAndExtractTypedWebClientResponse( - httpUriRequest: HttpUriRequest, responseType: Class): - WebClientResponse { + httpUriRequest: HttpUriRequest, responseType: Class): + WebClientResponse { val httpResponse = httpClient().execute(httpUriRequest) val statusCode = httpResponse.statusLine.statusCode httpResponse.entity.content.use { @@ -145,10 +154,9 @@ interface BlueprintWebClientService { } suspend fun getNB(path: String, additionalHeaders: Array?, responseType: Class): - WebClientResponse = - withContext(Dispatchers.IO) { - get(path, additionalHeaders!!, responseType) - } + WebClientResponse = withContext(Dispatchers.IO) { + get(path, additionalHeaders!!, responseType) + } suspend fun postNB(path: String, request: Any): WebClientResponse { return postNB(path, request, null, String::class.java) @@ -159,10 +167,9 @@ interface BlueprintWebClientService { } suspend fun postNB(path: String, request: Any, additionalHeaders: Array?, - responseType: Class): WebClientResponse = - withContext(Dispatchers.IO) { - post(path, request, additionalHeaders!!, responseType) - } + responseType: Class): WebClientResponse = withContext(Dispatchers.IO) { + post(path, request, additionalHeaders!!, responseType) + } suspend fun putNB(path: String, request: Any): WebClientResponse { return putNB(path, request, null, String::class.java) @@ -175,39 +182,36 @@ interface BlueprintWebClientService { suspend fun putNB(path: String, request: Any, additionalHeaders: Array?, - responseType: Class): WebClientResponse = - withContext(Dispatchers.IO) { - put(path, request, additionalHeaders!!, responseType) - } + responseType: Class): WebClientResponse = withContext(Dispatchers.IO) { + put(path, request, additionalHeaders!!, responseType) + } suspend fun deleteNB(path: String): WebClientResponse { return deleteNB(path, null, String::class.java) } suspend fun deleteNB(path: String, additionalHeaders: Array?): - WebClientResponse { + WebClientResponse { return deleteNB(path, additionalHeaders, String::class.java) } suspend fun deleteNB(path: String, additionalHeaders: Array?, responseType: Class): - WebClientResponse = - withContext(Dispatchers.IO) { - delete(path, additionalHeaders!!, responseType) - } + WebClientResponse = withContext(Dispatchers.IO) { + delete(path, additionalHeaders!!, responseType) + } suspend fun patchNB(path: String, request: Any, additionalHeaders: Array?, responseType: Class): - WebClientResponse = - withContext(Dispatchers.IO) { - patch(path, request, additionalHeaders!!, responseType) - } + WebClientResponse = withContext(Dispatchers.IO) { + patch(path, request, additionalHeaders!!, responseType) + } suspend fun exchangeNB(methodType: String, path: String, request: Any): WebClientResponse { return exchangeNB(methodType, path, request, hashMapOf(), - String::class.java) + String::class.java) } suspend fun exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map?): - WebClientResponse { + WebClientResponse { return exchangeNB(methodType, path, request, additionalHeaders, String::class.java) } @@ -224,8 +228,7 @@ interface BlueprintWebClientService { HttpMethod.DELETE -> deleteNB(path, convertedHeaders, responseType) HttpMethod.PUT -> putNB(path, request, convertedHeaders, responseType) HttpMethod.PATCH -> patchNB(path, request, convertedHeaders, responseType) - else -> throw BluePrintProcessorException("Unsupported method" + - "Type($methodType)") + else -> throw BluePrintProcessorException("Unsupported methodType($methodType)") } } @@ -246,7 +249,7 @@ interface BlueprintWebClientService { } private fun basicHeaders(headers: Map?): - Array { + Array { val basicHeaders = mutableListOf() defaultHeaders().forEach { (name, value) -> basicHeaders.add(BasicHeader(name, value)) @@ -260,9 +263,9 @@ interface BlueprintWebClientService { // Non Blocking Rest Implementation suspend fun httpClientNB(): CloseableHttpClient { return HttpClients.custom() - .addInterceptorFirst(WebClientUtils.logRequest()) - .addInterceptorLast(WebClientUtils.logResponse()) - .build() + .addInterceptorFirst(WebClientUtils.logRequest()) + .addInterceptorLast(WebClientUtils.logResponse()) + .build() } //TODO maybe there could be cases where we care about return headers? -- cgit 1.2.3-korg