diff options
Diffstat (limited to 'ms/blueprintsprocessor')
12 files changed, 286 insertions, 170 deletions
diff --git a/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatDefinition.kt b/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatDefinition.kt index d5bf3f4c6..f2036fee9 100644 --- a/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatDefinition.kt +++ b/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatDefinition.kt @@ -25,6 +25,7 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.module.kotlin.convertValue +import org.onap.ccsdk.cds.blueprintsprocessor.uat.utils.RequestType.EXCHANGE_RESOURCE import org.yaml.snakeyaml.DumperOptions import org.yaml.snakeyaml.Yaml import org.yaml.snakeyaml.nodes.Tag @@ -41,11 +42,17 @@ data class ProcessDefinition( data class RequestDefinition( val method: String, @JsonDeserialize(using = PathDeserializer::class) - val path: String, + val path: String = "", val headers: Map<String, String> = emptyMap(), - val body: JsonNode? = null + val body: JsonNode? = null, + val requestType: RequestType = EXCHANGE_RESOURCE ) +enum class RequestType { + EXCHANGE_RESOURCE, + UPLOAD_BINARY_FILE +} + @JsonInclude(JsonInclude.Include.NON_EMPTY) data class ResponseDefinition( val status: Int = 200, diff --git a/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatExecutor.kt b/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatExecutor.kt index 45677fac1..357154cb1 100644 --- a/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatExecutor.kt +++ b/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatExecutor.kt @@ -52,14 +52,18 @@ import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientSer import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.COLOR_MOCKITO import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.markerOf import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.MockInvocationLogger +import org.onap.ccsdk.cds.blueprintsprocessor.uat.utils.RequestType.EXCHANGE_RESOURCE +import org.onap.ccsdk.cds.blueprintsprocessor.uat.utils.RequestType.UPLOAD_BINARY_FILE import org.skyscreamer.jsonassert.JSONAssert import org.skyscreamer.jsonassert.JSONCompareMode import org.slf4j.Logger import org.slf4j.LoggerFactory import org.springframework.core.env.ConfigurableEnvironment +import org.springframework.http.HttpMethod import org.springframework.http.MediaType import org.springframework.stereotype.Component import org.springframework.util.Base64Utils +import java.nio.file.Path import java.util.concurrent.ConcurrentHashMap /** @@ -151,12 +155,19 @@ open class UatExecutor( for ((mockClient, expectations) in expectationsPerClient) { expectations.forEach { expectation -> val request = expectation.request - verify(mockClient, evalVerificationMode(expectation.times)).exchangeResource( - eq(request.method), - eq(request.path), - any(), - argThat(RequiredMapEntriesMatcher(request.headers)) - ) + if (request.requestType == EXCHANGE_RESOURCE) { + verify(mockClient, evalVerificationMode(expectation.times)).exchangeResource( + eq(request.method), + eq(request.path), + any(), + argThat(RequiredMapEntriesMatcher(request.headers)) + ) + } else if (request.requestType == UPLOAD_BINARY_FILE) { + verify(mockClient, evalVerificationMode(expectation.times)).uploadBinaryFile( + eq(request.path), + any() + ) + } } // Don't mind the invocations to the overloaded exchangeResource(String, String, String) verify(mockClient, atLeast(0)).exchangeResource(any(), any(), any()) @@ -198,16 +209,32 @@ open class UatExecutor( restClient.exchangeResource(method, path, request, emptyMap()) } for (expectation in restExpectations) { - var stubbing = whenever( - restClient.exchangeResource( - eq(expectation.request.method), - eq(expectation.request.path), - argThat(JsonMatcher(expectation.request.body.toString())), - any() + if (expectation.request.requestType == EXCHANGE_RESOURCE) { + var stubbing = whenever( + restClient.exchangeResource( + eq(expectation.request.method), + eq(expectation.request.path), + argThat(JsonMatcher(expectation.request.body.toString())), + any() + ) ) - ) - for (response in expectation.responses) { - stubbing = stubbing.thenReturn(WebClientResponse(response.status, response.body.toString())) + for (response in expectation.responses) { + stubbing = stubbing.thenReturn(WebClientResponse(response.status, response.body.toString())) + } + } + } + + for (expectation in restExpectations) { + if (expectation.request.requestType == UPLOAD_BINARY_FILE) { + var stubbing = whenever( + restClient.uploadBinaryFile( + eq(expectation.request.path), + any() + ) + ) + for (response in expectation.responses) { + stubbing = stubbing.thenReturn(WebClientResponse(response.status, response.body.toString())) + } } } return restClient @@ -361,7 +388,7 @@ open class UatExecutor( headers: Map<String, String> ): WebClientResponse<String> { val requestDefinition = - RequestDefinition(methodType, path, headers, toJson(request)) + RequestDefinition(methodType, path, headers, toJson(request), EXCHANGE_RESOURCE) val realAnswer = realService.exchangeResource(methodType, path, request, headers) val responseBody = when { // TODO: confirm if we need to normalize the response here @@ -379,6 +406,30 @@ open class UatExecutor( return realAnswer } + override fun uploadBinaryFile(path: String, filePath: Path): + WebClientResponse<String> { + val method = HttpMethod.POST.name + val headers = DEFAULT_HEADERS + val request = "" + val requestDefinition = + RequestDefinition(method, path, headers, toJson(request), UPLOAD_BINARY_FILE) + val realAnswer = realService.uploadBinaryFile(path, filePath) + val responseBody = when { + // TODO: confirm if we need to normalize the response here + realAnswer.status == HttpStatus.SC_OK -> toJson(realAnswer.body) + else -> null + } + val responseDefinition = + ResponseDefinition(realAnswer.status, responseBody) + expectations.add( + ExpectationDefinition( + requestDefinition, + responseDefinition + ) + ) + return realAnswer + } + override suspend fun <T> retry(times: Int, initialDelay: Long, delay: Long, block: suspend (Int) -> T): T { return super.retry(times, initialDelay, delay, block) } diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt index f0f8e298d..e979a21c1 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt @@ -23,6 +23,7 @@ import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService abstract class K8sAbstractRestClientService( @@ -65,4 +66,17 @@ abstract class K8sAbstractRestClientService( } abstract fun apiUrl(): String + + companion object { + fun getInterceptedWebclientService( + service: K8sAbstractRestClientService, + clientName: String + ): BlueprintWebClientService { + val restLibPropertyService: BluePrintRestLibPropertyService = + BluePrintDependencyService.instance(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY) + return restLibPropertyService.interceptExternalBlueprintWebClientService( + service, clientName + ) + } + } } diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sDefinitionRestClient.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sDefinitionRestClient.kt index 224c6a884..051c025f4 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sDefinitionRestClient.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sDefinitionRestClient.kt @@ -21,13 +21,31 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sAbstractRestClientService import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService open class K8sDefinitionRestClient( k8sConfiguration: K8sConnectionPluginConfiguration, private val definition: String, - private val definitionVersion: String -) : K8sAbstractRestClientService(k8sConfiguration, "k8s-plugin-definition") { + private val definitionVersion: String, + private val clientName: String = CLIENT_NAME +) : K8sAbstractRestClientService(k8sConfiguration, CLIENT_NAME) { + companion object { + public const val CLIENT_NAME = "k8s-plugin-definition" + + fun getK8sDefinitionRestClient( + k8sConfiguration: K8sConnectionPluginConfiguration, + definition: String, + definitionVersion: String + ): BlueprintWebClientService { + val rbDefinitionService = K8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) + return getInterceptedWebclientService(rbDefinitionService, CLIENT_NAME) + } + } override fun apiUrl(): String { return "$baseUrl/v1/rb/definition/$definition/$definitionVersion" } diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sPluginDefinitionApi.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sPluginDefinitionApi.kt index ed0b164ef..86c019aed 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sPluginDefinitionApi.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sPluginDefinitionApi.kt @@ -22,6 +22,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition import com.fasterxml.jackson.databind.ObjectMapper 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.definition.K8sDefinitionRestClient.Companion.getK8sDefinitionRestClient 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 @@ -41,12 +42,12 @@ class K8sPluginDefinitionApi( private val objectMapper = ObjectMapper() fun hasDefinition(definition: String, definitionVersion: String): Boolean { - val rbDefinitionService = K8sDefinitionRestClient( - k8sConfiguration, - definition, - definitionVersion - ) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( GET.name, "", @@ -61,12 +62,12 @@ class K8sPluginDefinitionApi( } fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean { - val rbDefinitionService = K8sDefinitionRestClient( - k8sConfiguration, - definition, - definitionVersion - ) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( GET.name, "/profile/$profileName", @@ -81,12 +82,12 @@ class K8sPluginDefinitionApi( } fun getProfile(definition: String, definitionVersion: String, profileName: String): K8sProfile? { - val rbDefinitionService = K8sDefinitionRestClient( - k8sConfiguration, - definition, - definitionVersion - ) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( GET.name, "/profile/$profileName", @@ -107,13 +108,13 @@ class K8sPluginDefinitionApi( } fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) { - val rbDefinitionService = K8sDefinitionRestClient( - k8sConfiguration, - definition, - definitionVersion - ) val profileJsonString: String = objectMapper.writeValueAsString(profile) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( POST.name, "/profile", @@ -129,13 +130,13 @@ class K8sPluginDefinitionApi( } fun updateProfile(profile: K8sProfile) { - val rbDefinitionService = K8sDefinitionRestClient( - k8sConfiguration, - profile.rbName!!, - profile.rbVersion!! - ) val profileJsonString: String = objectMapper.writeValueAsString(profile) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + profile.rbName!!, + profile.rbVersion!! + ) val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( PUT.name, "/profile/${profile.profileName}", @@ -151,8 +152,12 @@ class K8sPluginDefinitionApi( } fun deleteProfile(definition: String, definitionVersion: String, profileName: String) { - val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( DELETE.name, "/profile/$profileName", @@ -169,12 +174,12 @@ class K8sPluginDefinitionApi( } fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) { - val fileUploadService = K8sUploadFileRestClientService( - k8sConfiguration, - definition, - definitionVersion - ) try { + val fileUploadService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile( "/profile/${profile.profileName}/content", filePath @@ -191,9 +196,13 @@ class K8sPluginDefinitionApi( } fun createTemplate(definition: String, definitionVersion: String, template: K8sTemplate) { - val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion) val templateJsonString: String = objectMapper.writeValueAsString(template) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( POST.name, "/config-template", @@ -210,8 +219,12 @@ class K8sPluginDefinitionApi( } fun uploadConfigTemplateContent(definition: String, definitionVersion: String, template: K8sTemplate, filePath: Path) { - val fileUploadService = K8sUploadFileRestClientService(k8sConfiguration, definition, definitionVersion) try { + val fileUploadService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile( "/config-template/${template.templateName}/content", filePath @@ -227,8 +240,12 @@ class K8sPluginDefinitionApi( } fun deleteTemplate(definition: String, definitionVersion: String, templateName: String) { - val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( DELETE.name, "/config-template/$templateName", @@ -245,8 +262,12 @@ class K8sPluginDefinitionApi( } fun getTemplate(definition: String, definitionVersion: String, templateName: String): K8sTemplate { - val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion) try { + val rbDefinitionService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName) log.debug(result.toString()) return objectMapper.readValue(result.body) @@ -257,9 +278,13 @@ class K8sPluginDefinitionApi( } fun hasTemplate(definition: String, definitionVersion: String, templateName: String): Boolean { - val rbDefinitionService = K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion) try { - val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(rbDefinitionService, templateName) + val interceptedService = getK8sDefinitionRestClient( + k8sConfiguration, + definition, + definitionVersion + ) + val result: BlueprintWebClientService.WebClientResponse<String> = getTemplateRequest(interceptedService, templateName) log.debug(result.toString()) return result.status in 200..299 } catch (e: Exception) { @@ -268,7 +293,7 @@ class K8sPluginDefinitionApi( } } - private fun getTemplateRequest(rbDefinitionService: K8sDefinitionRestClient, templateName: String): BlueprintWebClientService.WebClientResponse<String> { + private fun getTemplateRequest(rbDefinitionService: BlueprintWebClientService, templateName: String): BlueprintWebClientService.WebClientResponse<String> { return rbDefinitionService.exchangeResource( GET.name, "/config-template/$templateName", diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sUploadFileRestClientService.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sUploadFileRestClientService.kt deleted file mode 100644 index e44c4eb04..000000000 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/K8sUploadFileRestClientService.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2019 IBM. - * Modifications Copyright © 2020 Orange. - * Modifications Copyright © 2020 Deutsche Telekom AG. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition - -import org.apache.commons.io.IOUtils -import org.apache.http.client.ClientProtocolException -import org.apache.http.client.entity.EntityBuilder -import org.apache.http.client.methods.HttpPost -import org.apache.http.client.methods.HttpUriRequest -import org.apache.http.message.BasicHeader -import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration -import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService -import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.RestLoggerService -import java.io.IOException -import java.nio.charset.Charset -import java.nio.file.Files -import java.nio.file.Path - -class K8sUploadFileRestClientService( - k8sConfiguration: K8sConnectionPluginConfiguration, - definition: String, - definitionVersion: String -) : K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion) { - - @Throws(IOException::class, ClientProtocolException::class) - private fun performHttpCall(httpUriRequest: HttpUriRequest): BlueprintWebClientService.WebClientResponse<String> { - val httpResponse = httpClient().execute(httpUriRequest) - val statusCode = httpResponse.statusLine.statusCode - httpResponse.entity.content.use { - val body = IOUtils.toString(it, Charset.defaultCharset()) - return BlueprintWebClientService.WebClientResponse(statusCode, body) - } - } - - fun uploadBinaryFile(path: String, filePath: Path): BlueprintWebClientService.WebClientResponse<String> { - val convertedHeaders: Array<BasicHeader> = 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) - } -} diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sPluginInstanceApi.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sPluginInstanceApi.kt index a99179599..b58a7eaf4 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sPluginInstanceApi.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sPluginInstanceApi.kt @@ -22,6 +22,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance 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.K8sRbInstanceRestClient.Companion.getK8sRbInstanceRestClient import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheck import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheckList import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheckSimple @@ -40,7 +41,7 @@ class K8sPluginInstanceApi( private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!! fun getInstanceList(): List<K8sRbInstance>? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -63,7 +64,7 @@ class K8sPluginInstanceApi( } fun getInstanceById(instanceId: String): K8sRbInstance? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -85,7 +86,7 @@ class K8sPluginInstanceApi( } fun getFullInstanceById(instanceId: String): K8sRbInstanceFull? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -122,7 +123,7 @@ class K8sPluginInstanceApi( } fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -152,7 +153,7 @@ class K8sPluginInstanceApi( name: String? = null, labels: Map<String, String>? = null ): K8sRbInstanceStatus? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind" if (name != null) @@ -185,7 +186,7 @@ class K8sPluginInstanceApi( } fun getInstanceHealthCheckList(instanceId: String): K8sRbInstanceHealthCheckList? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -210,7 +211,7 @@ class K8sPluginInstanceApi( } fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -235,7 +236,7 @@ class K8sPluginInstanceApi( } fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheckSimple? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( POST.name, @@ -260,7 +261,7 @@ class K8sPluginInstanceApi( } fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( POST.name, @@ -282,7 +283,7 @@ class K8sPluginInstanceApi( } fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( PUT.name, @@ -304,7 +305,7 @@ class K8sPluginInstanceApi( } fun editConfigurationValuesByDelete(instanceId: String, configName: String): K8sConfigValueResponse? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( POST.name, @@ -326,7 +327,7 @@ class K8sPluginInstanceApi( } fun hasConfigurationValues(instanceId: String, configName: String): Boolean { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -342,7 +343,7 @@ class K8sPluginInstanceApi( } fun hasConfigurationValuesVersion(instanceId: String, configName: String, version: String): Boolean { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -358,7 +359,7 @@ class K8sPluginInstanceApi( } fun getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -380,7 +381,7 @@ class K8sPluginInstanceApi( } fun getConfigurationValuesVersion(instanceId: String, configName: String, version: String): K8sConfigValueResponse? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -402,7 +403,7 @@ class K8sPluginInstanceApi( } fun getConfigurationValuesVersionByTag(instanceId: String, configName: String, tag: String): K8sConfigValueResponse? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -424,7 +425,7 @@ class K8sPluginInstanceApi( } fun getConfigurationValuesList(instanceId: String): List<K8sConfigValueResponse>? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -447,7 +448,7 @@ class K8sPluginInstanceApi( } fun getConfigurationValuesVersionList(instanceId: String, configName: String): List<K8sConfigValueResponse>? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -470,7 +471,7 @@ class K8sPluginInstanceApi( } fun getConfigurationValuesTagList(instanceId: String, configName: String): List<K8sConfigValueTag>? { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( GET.name, @@ -493,7 +494,7 @@ class K8sPluginInstanceApi( } fun deleteConfigurationValues(instanceId: String, configName: String, deleteConfigOnly: Boolean) { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { var path: String = "/config/$configName" if (deleteConfigOnly) @@ -513,7 +514,7 @@ class K8sPluginInstanceApi( } fun rollbackConfigurationValues(instanceId: String, configName: String, configVersion: String?, configTag: String?) { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val configValues = hashMapOf<String, String>() if (configVersion != null) @@ -535,7 +536,7 @@ class K8sPluginInstanceApi( } fun tagConfigurationValues(instanceId: String, configName: String, tagName: String) { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val configValues = hashMapOf<String, String>() configValues["tag-name"] = tagName @@ -554,7 +555,7 @@ class K8sPluginInstanceApi( } fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) { - val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId) + val rbInstanceService = getK8sRbInstanceRestClient(k8sConfiguration, instanceId) try { val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource( DELETE.name, diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sRbInstanceRestClient.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sRbInstanceRestClient.kt index 9a28ed854..6e43d31b1 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sRbInstanceRestClient.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/instance/K8sRbInstanceRestClient.kt @@ -21,12 +21,28 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sAbstractRestClientService import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService open class K8sRbInstanceRestClient( k8sConfiguration: K8sConnectionPluginConfiguration, private val instanceId: String = "" ) : K8sAbstractRestClientService(k8sConfiguration, "k8s-plugin-instance") { + companion object { + public const val CLIENT_NAME = "k8s-plugin-instance" + + fun getK8sRbInstanceRestClient( + k8sConfiguration: K8sConnectionPluginConfiguration, + instanceId: String = "" + ): BlueprintWebClientService { + val rbInstanceService = K8sRbInstanceRestClient( + k8sConfiguration, + instanceId + ) + return getInterceptedWebclientService(rbInstanceService, CLIENT_NAME) + } + } + override fun apiUrl(): String { return if (instanceId != "") "$baseUrl/v1/instance/$instanceId" diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sPluginQueryApi.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sPluginQueryApi.kt index 36ebe1133..227ed6730 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sPluginQueryApi.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sPluginQueryApi.kt @@ -1,6 +1,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.query import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.query.K8sQueryRestClient.Companion.getK8sQueryRestClient 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 @@ -20,7 +21,7 @@ public class K8sPluginQueryApi( namespace: String? = null, labels: Map<String, String>? = null ): K8sResourceStatus? { - val rbQueryService = K8sQueryRestClient(k8sConfiguration) + val rbQueryService = getK8sQueryRestClient(k8sConfiguration) try { var path: String = "?CloudRegion=$cloudRegion&ApiVersion=$apiVersion&Kind=$kind" if (name != null) diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sQueryRestClient.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sQueryRestClient.kt index b28c56d2d..9f97f9a3a 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sQueryRestClient.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sQueryRestClient.kt @@ -20,10 +20,24 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.query import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sAbstractRestClientService import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService open class K8sQueryRestClient( k8sConfiguration: K8sConnectionPluginConfiguration -) : K8sAbstractRestClientService(k8sConfiguration, "k8s-plugin-query") { +) : K8sAbstractRestClientService(k8sConfiguration, CLIENT_NAME) { + + companion object { + public const val CLIENT_NAME = "k8s-plugin-query" + + fun getK8sQueryRestClient( + k8sConfiguration: K8sConnectionPluginConfiguration + ): BlueprintWebClientService { + val rbQueryService = K8sQueryRestClient( + k8sConfiguration + ) + return getInterceptedWebclientService(rbQueryService, CLIENT_NAME) + } + } override fun apiUrl(): String { return "$baseUrl/v1/query" 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<out E : RestClientProperties> : BlueprintWebClientService { @@ -78,7 +82,7 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu .build() } - override fun exchangeResource(methodType: String, path: String, request: String): BlueprintWebClientService.WebClientResponse<String> { + override fun exchangeResource(methodType: String, path: String, request: String): WebClientResponse<String> { return this.exchangeResource(methodType, path, request, defaultHeaders()) } @@ -87,7 +91,7 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu path: String, request: String, headers: Map<String, String> - ): BlueprintWebClientService.WebClientResponse<String> { + ): WebClientResponse<String> { /** * 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<out E : RestClientProperties> : Blu } } + @Throws(IOException::class, ClientProtocolException::class) + protected fun performHttpCall(httpUriRequest: HttpUriRequest): WebClientResponse<String> { + 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<String> { + val convertedHeaders: Array<BasicHeader> = 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<String, String>): Array<BasicHeader> { return headers.map { BasicHeader(it.key, it.value) }.toTypedArray() } - open fun <T> delete(path: String, headers: Array<BasicHeader>, responseType: Class<T>): BlueprintWebClientService.WebClientResponse<T> { + open fun <T> delete(path: String, headers: Array<BasicHeader>, responseType: Class<T>): WebClientResponse<T> { val httpDelete = HttpDelete(host(path)) RestLoggerService.httpInvoking(headers) httpDelete.setHeaders(headers) return performCallAndExtractTypedWebClientResponse(httpDelete, responseType) } - open fun <T> get(path: String, headers: Array<BasicHeader>, responseType: Class<T>): BlueprintWebClientService.WebClientResponse<T> { + open fun <T> get(path: String, headers: Array<BasicHeader>, responseType: Class<T>): WebClientResponse<T> { val httpGet = HttpGet(host(path)) RestLoggerService.httpInvoking(headers) httpGet.setHeaders(headers) return performCallAndExtractTypedWebClientResponse(httpGet, responseType) } - open fun <T> post(path: String, request: Any, headers: Array<BasicHeader>, responseType: Class<T>): BlueprintWebClientService.WebClientResponse<T> { + open fun <T> post(path: String, request: Any, headers: Array<BasicHeader>, responseType: Class<T>): WebClientResponse<T> { val httpPost = HttpPost(host(path)) val entity = StringEntity(strRequest(request)) httpPost.entity = entity @@ -134,7 +158,7 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu return performCallAndExtractTypedWebClientResponse(httpPost, responseType) } - open fun <T> put(path: String, request: Any, headers: Array<BasicHeader>, responseType: Class<T>): BlueprintWebClientService.WebClientResponse<T> { + open fun <T> put(path: String, request: Any, headers: Array<BasicHeader>, responseType: Class<T>): WebClientResponse<T> { val httpPut = HttpPut(host(path)) val entity = StringEntity(strRequest(request)) httpPut.entity = entity @@ -143,7 +167,7 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu return performCallAndExtractTypedWebClientResponse(httpPut, responseType) } - open fun <T> patch(path: String, request: Any, headers: Array<BasicHeader>, responseType: Class<T>): BlueprintWebClientService.WebClientResponse<T> { + open fun <T> patch(path: String, request: Any, headers: Array<BasicHeader>, responseType: Class<T>): WebClientResponse<T> { val httpPatch = HttpPatch(host(path)) val entity = StringEntity(strRequest(request)) httpPatch.entity = entity @@ -164,19 +188,19 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu httpUriRequest: HttpUriRequest, responseType: Class<T> ): - BlueprintWebClientService.WebClientResponse<T> { + WebClientResponse<T> { 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<out E : RestClientProperties> : Blu status != HttpStatus.SC_RESET_CONTENT } - open suspend fun getNB(path: String): BlueprintWebClientService.WebClientResponse<String> { + open suspend fun getNB(path: String): WebClientResponse<String> { return getNB(path, null, String::class.java) } - open suspend fun getNB(path: String, additionalHeaders: Array<BasicHeader>?): BlueprintWebClientService.WebClientResponse<String> { + open suspend fun getNB(path: String, additionalHeaders: Array<BasicHeader>?): WebClientResponse<String> { return getNB(path, additionalHeaders, String::class.java) } open suspend fun <T> getNB(path: String, additionalHeaders: Array<BasicHeader>?, responseType: Class<T>): - BlueprintWebClientService.WebClientResponse<T> = withContext(Dispatchers.IO) { + WebClientResponse<T> = withContext(Dispatchers.IO) { get(path, additionalHeaders!!, responseType) } - open suspend fun postNB(path: String, request: Any): BlueprintWebClientService.WebClientResponse<String> { + open suspend fun postNB(path: String, request: Any): WebClientResponse<String> { return postNB(path, request, null, String::class.java) } - open suspend fun postNB(path: String, request: Any, additionalHeaders: Array<BasicHeader>?): BlueprintWebClientService.WebClientResponse<String> { + open suspend fun postNB(path: String, request: Any, additionalHeaders: Array<BasicHeader>?): WebClientResponse<String> { return postNB(path, request, additionalHeaders, String::class.java) } @@ -213,11 +237,11 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu request: Any, additionalHeaders: Array<BasicHeader>?, responseType: Class<T> - ): BlueprintWebClientService.WebClientResponse<T> = withContext(Dispatchers.IO) { + ): WebClientResponse<T> = withContext(Dispatchers.IO) { post(path, request, additionalHeaders!!, responseType) } - open suspend fun putNB(path: String, request: Any): BlueprintWebClientService.WebClientResponse<String> { + open suspend fun putNB(path: String, request: Any): WebClientResponse<String> { return putNB(path, request, null, String::class.java) } @@ -225,7 +249,7 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu path: String, request: Any, additionalHeaders: Array<BasicHeader>? - ): BlueprintWebClientService.WebClientResponse<String> { + ): WebClientResponse<String> { return putNB(path, request, additionalHeaders, String::class.java) } @@ -234,30 +258,30 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu request: Any, additionalHeaders: Array<BasicHeader>?, responseType: Class<T> - ): BlueprintWebClientService.WebClientResponse<T> = withContext(Dispatchers.IO) { + ): WebClientResponse<T> = withContext(Dispatchers.IO) { put(path, request, additionalHeaders!!, responseType) } - open suspend fun <T> deleteNB(path: String): BlueprintWebClientService.WebClientResponse<String> { + open suspend fun <T> deleteNB(path: String): WebClientResponse<String> { return deleteNB(path, null, String::class.java) } open suspend fun <T> deleteNB(path: String, additionalHeaders: Array<BasicHeader>?): - BlueprintWebClientService.WebClientResponse<String> { + WebClientResponse<String> { return deleteNB(path, additionalHeaders, String::class.java) } open suspend fun <T> deleteNB(path: String, additionalHeaders: Array<BasicHeader>?, responseType: Class<T>): - BlueprintWebClientService.WebClientResponse<T> = withContext(Dispatchers.IO) { + WebClientResponse<T> = withContext(Dispatchers.IO) { delete(path, additionalHeaders!!, responseType) } open suspend fun <T> patchNB(path: String, request: Any, additionalHeaders: Array<BasicHeader>?, responseType: Class<T>): - BlueprintWebClientService.WebClientResponse<T> = withContext(Dispatchers.IO) { + WebClientResponse<T> = withContext(Dispatchers.IO) { patch(path, request, additionalHeaders!!, responseType) } - override suspend fun exchangeNB(methodType: String, path: String, request: Any): BlueprintWebClientService.WebClientResponse<String> { + override suspend fun exchangeNB(methodType: String, path: String, request: Any): WebClientResponse<String> { return exchangeNB( methodType, path, request, hashMapOf(), String::class.java @@ -265,7 +289,7 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu } override suspend fun exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map<String, String>?): - BlueprintWebClientService.WebClientResponse<String> { + WebClientResponse<String> { return exchangeNB(methodType, path, request, additionalHeaders, String::class.java) } @@ -275,7 +299,7 @@ abstract class BaseBlueprintWebClientService<out E : RestClientProperties> : Blu request: Any, additionalHeaders: Map<String, String>?, responseType: Class<T> - ): BlueprintWebClientService.WebClientResponse<T> { + ): WebClientResponse<T> { // 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<String, String> @@ -42,6 +43,11 @@ interface BlueprintWebClientService { request: String ): WebClientResponse<String> + fun uploadBinaryFile( + path: String, + filePath: Path + ): WebClientResponse<String> + suspend fun exchangeNB(methodType: String, path: String, request: Any): WebClientResponse<String> suspend fun exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map<String, String>?): |