diff options
Diffstat (limited to 'ms/blueprintsprocessor/functions')
7 files changed, 170 insertions, 100 deletions
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 new file mode 100644 index 000000000..69f647387 --- /dev/null +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt @@ -0,0 +1,91 @@ +/* + * 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 + +import org.apache.http.message.BasicHeader +import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService +import org.springframework.http.HttpHeaders.ACCEPT +import org.springframework.http.HttpHeaders.AUTHORIZATION +import org.springframework.http.HttpHeaders.CONTENT_TYPE +import org.springframework.http.MediaType.APPLICATION_JSON_VALUE +import java.nio.charset.Charset +import java.util.Base64 + +abstract class K8sAbstractRestClientService(val username: String, val password: String) : BlueprintWebClientService { + + private val restClientProperties: BasicAuthRestClientProperties = getBasicAuthRestClientProperties() + + private fun getBasicAuthRestClientProperties(): BasicAuthRestClientProperties { + val basicAuthRestClientProperties = BasicAuthRestClientProperties() + basicAuthRestClientProperties.username = username + basicAuthRestClientProperties.password = password + basicAuthRestClientProperties.url = apiUrl() + basicAuthRestClientProperties.additionalHeaders = getHeaders() + return basicAuthRestClientProperties + } + + private fun getHeaders(): HashMap<String, String> { + val mapOfHeaders = hashMapOf<String, String>() + mapOfHeaders["Accept"] = "application/json" + mapOfHeaders["Content-Type"] = "application/json" + mapOfHeaders["cache-control"] = " no-cache" + mapOfHeaders["Accept"] = "application/json" + return mapOfHeaders + } + + private fun setBasicAuth(username: String, password: String): String { + val credentialsString = "$username:$password" + return Base64.getEncoder().encodeToString(credentialsString.toByteArray(Charset.defaultCharset())) + } + + override fun defaultHeaders(): Map<String, String> { + val encodedCredentials = setBasicAuth( + restClientProperties.username, + restClientProperties.password + ) + return mapOf( + CONTENT_TYPE to APPLICATION_JSON_VALUE, + ACCEPT to APPLICATION_JSON_VALUE, + AUTHORIZATION to "Basic $encodedCredentials" + ) + } + + override fun host(uri: String): String { + return restClientProperties.url + uri + } + + override fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> { + val customHeaders: MutableMap<String, String> = headers.toMutableMap() + // inject additionalHeaders + customHeaders.putAll(verifyAdditionalHeaders(restClientProperties)) + + if (!headers.containsKey(AUTHORIZATION)) { + val encodedCredentials = setBasicAuth( + restClientProperties.username, + restClientProperties.password + ) + customHeaders[AUTHORIZATION] = "Basic $encodedCredentials" + } + return super.convertToBasicHeaders(customHeaders) + } + + abstract fun apiUrl(): String +} diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/profile/upload/K8sProfileUploadConfiguration.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sConnectionPluginConfiguration.kt index 76e8af910..2419e5a13 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/profile/upload/K8sProfileUploadConfiguration.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sConnectionPluginConfiguration.kt @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.profile.upload +package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintCoreConfiguration import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService @@ -32,18 +32,18 @@ import org.springframework.context.annotation.Import BlueprintCoreConfiguration::class ) @EnableConfigurationProperties -open class K8sProfileUploadConfiguration(private var bluePrintPropertiesService: BlueprintPropertiesService) { +open class K8sConnectionPluginConfiguration(private var bluePrintPropertiesService: BlueprintPropertiesService) { @Bean("k8s-plugin-properties") - open fun getProperties(): K8sProfileUploadProperties { + open fun getProperties(): K8sConnectionPluginProperties { return bluePrintPropertiesService.propertyBeanType( - K8sProfileUploadConstants.PREFIX_K8S_PLUGIN, - K8sProfileUploadProperties::class.java + K8sConnectionPluginConstants.PREFIX_K8S_PLUGIN, + K8sConnectionPluginProperties::class.java ) } } -class K8sProfileUploadConstants { +class K8sConnectionPluginConstants { companion object { const val PREFIX_K8S_PLUGIN: String = "blueprintprocessor.k8s.plugin" diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/profile/upload/K8sProfileUploadProperties.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sConnectionPluginProperties.kt index ae277e360..003b03dfa 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/profile/upload/K8sProfileUploadProperties.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sConnectionPluginProperties.kt @@ -15,11 +15,11 @@ * limitations under the License. */ -package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.profile.upload +package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s -open class K8sProfileUploadProperties { +open class K8sConnectionPluginProperties { - var type: String = K8sProfileUploadConstants.PREFIX_K8S_PLUGIN + var type: String = K8sConnectionPluginConstants.PREFIX_K8S_PLUGIN lateinit var url: String lateinit var username: String lateinit var password: String diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sDefinitionRestClient.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sDefinitionRestClient.kt new file mode 100644 index 000000000..0daa276ec --- /dev/null +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sDefinitionRestClient.kt @@ -0,0 +1,33 @@ +/* + * 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 + +open class K8sDefinitionRestClient( + username: String, + password: String, + private val baseUrl: String, + private val definition: String, + private val definitionVersion: String +) : K8sAbstractRestClientService(username, password) { + + 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/K8sPluginApi.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sPluginApi.kt index eed1be599..2c58645c8 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sPluginApi.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sPluginApi.kt @@ -21,42 +21,29 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s import com.fasterxml.jackson.databind.ObjectMapper import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.profile.upload.K8sProfile -import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException import org.slf4j.LoggerFactory -import org.springframework.http.HttpMethod +import org.springframework.http.HttpMethod.GET +import org.springframework.http.HttpMethod.POST import java.nio.file.Path class K8sPluginApi( - val username: String, - val password: String, - val baseUrl: String, - val definition: String?, - val definitionVersion: String? + private val username: String, + private val password: String, + private val baseUrl: String ) { - - private val service: K8sUploadFileRestClientService // BasicAuthRestClientService private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!! + private val objectMapper = ObjectMapper() - init { - var mapOfHeaders = hashMapOf<String, String>() - mapOfHeaders.put("Accept", "application/json") - mapOfHeaders.put("Content-Type", "application/json") - mapOfHeaders.put("cache-control", " no-cache") - mapOfHeaders.put("Accept", "application/json") - var basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties() - basicAuthRestClientProperties.username = username - basicAuthRestClientProperties.password = password - basicAuthRestClientProperties.url = "$baseUrl/v1/rb/definition/$definition/$definitionVersion" - basicAuthRestClientProperties.additionalHeaders = mapOfHeaders - - this.service = K8sUploadFileRestClientService(basicAuthRestClientProperties) - } - - fun hasDefinition(): Boolean { + fun hasDefinition(definition: String, definitionVersion: String): Boolean { + val rbDefinitionService = K8sDefinitionRestClient(username, password, baseUrl, definition, definitionVersion) try { - val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource(HttpMethod.GET.name, "", "") + val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( + GET.name, + "", + "" + ) log.debug(result.toString()) return result.status in 200..299 } catch (e: Exception) { @@ -65,10 +52,11 @@ class K8sPluginApi( } } - fun hasProfile(profileName: String): Boolean { + fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean { + val rbDefinitionService = K8sDefinitionRestClient(username, password, baseUrl, definition, definitionVersion) try { - val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource( - HttpMethod.GET.name, + val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( + GET.name, "/profile/$profileName", "" ) @@ -80,12 +68,12 @@ class K8sPluginApi( } } - fun createProfile(profile: K8sProfile) { - val objectMapper = ObjectMapper() + fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) { + val rbDefinitionService = K8sDefinitionRestClient(username, password, baseUrl, definition, definitionVersion) val profileJsonString: String = objectMapper.writeValueAsString(profile) try { - val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource( - HttpMethod.POST.name, + val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource( + POST.name, "/profile", profileJsonString ) @@ -98,9 +86,10 @@ class K8sPluginApi( } } - fun uploadProfileContent(profile: K8sProfile, filePath: Path) { + fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) { + val fileUploadService = K8sUploadFileRestClientService(username, password, baseUrl, definition, definitionVersion) try { - val result: BlueprintWebClientService.WebClientResponse<String> = service.uploadBinaryFile( + val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile( "/profile/${profile.profileName}/content", filePath ) diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sUploadFileRestClientService.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sUploadFileRestClientService.kt index ce3e164cf..ac545cfe5 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sUploadFileRestClientService.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sUploadFileRestClientService.kt @@ -25,62 +25,20 @@ 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.rest.BasicAuthRestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.RestLoggerService -import org.springframework.http.HttpHeaders -import org.springframework.http.MediaType import java.io.IOException import java.nio.charset.Charset import java.nio.file.Files import java.nio.file.Path -import java.util.Base64 class K8sUploadFileRestClientService( - private val restClientProperties: - BasicAuthRestClientProperties -) : BlueprintWebClientService { - - override fun defaultHeaders(): Map<String, String> { - - val encodedCredentials = setBasicAuth( - restClientProperties.username, - restClientProperties.password - ) - return mapOf( - HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE, - HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE, - HttpHeaders.AUTHORIZATION to "Basic $encodedCredentials" - ) - } - - override fun host(uri: String): String { - return restClientProperties.url + uri - } - - override fun convertToBasicHeaders(headers: Map<String, String>): - Array<BasicHeader> { - val customHeaders: MutableMap<String, String> = headers.toMutableMap() - // inject additionalHeaders - customHeaders.putAll(verifyAdditionalHeaders(restClientProperties)) - - if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) { - val encodedCredentials = setBasicAuth( - restClientProperties.username, - restClientProperties.password - ) - customHeaders[HttpHeaders.AUTHORIZATION] = - "Basic $encodedCredentials" - } - return super.convertToBasicHeaders(customHeaders) - } - - private fun setBasicAuth(username: String, password: String): String { - val credentialsString = "$username:$password" - return Base64.getEncoder().encodeToString( - credentialsString.toByteArray(Charset.defaultCharset()) - ) - } + username: String, + password: String, + baseUrl: String, + definition: String, + definitionVersion: String +) : K8sDefinitionRestClient(username, password, baseUrl, definition, definitionVersion) { @Throws(IOException::class, ClientProtocolException::class) private fun performHttpCall(httpUriRequest: HttpUriRequest): BlueprintWebClientService.WebClientResponse<String> { diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/profile/upload/K8sProfileUploadComponent.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/profile/upload/K8sProfileUploadComponent.kt index 1f17b2c6e..fb42fa7c5 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/profile/upload/K8sProfileUploadComponent.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/profile/upload/K8sProfileUploadComponent.kt @@ -25,6 +25,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode 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.controllerblueprints.resource.dict.ResourceAssignment import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants @@ -121,24 +122,22 @@ open class K8sProfileUploadComponent( val definitionName: String? = prefixInputParamsMap[INPUT_K8S_DEFINITION_NAME]?.returnNullIfMissing()?.asText() val definitionVersion: String? = prefixInputParamsMap[INPUT_K8S_DEFINITION_VERSION]?.returnNullIfMissing()?.asText() - val k8sProfileUploadConfiguration = K8sProfileUploadConfiguration(bluePrintPropertiesService) + val k8sProfileUploadConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService) // Creating API connector var api = K8sPluginApi( k8sProfileUploadConfiguration.getProperties().username, k8sProfileUploadConfiguration.getProperties().password, - k8sProfileUploadConfiguration.getProperties().url, - definitionName, - definitionVersion + k8sProfileUploadConfiguration.getProperties().url ) if ((profileName == null) || (definitionName == null) || (definitionVersion == null)) { log.warn("Prefix $prefix does not have required data for us to continue.") - } else if (!api.hasDefinition()) { + } else if (!api.hasDefinition(definitionName, definitionVersion)) { log.warn("K8s RB Definition ($definitionName/$definitionVersion) not found ") } else if (profileName == "") { log.warn("K8s rb profile name is empty! Either define profile name to use or choose default") - } else if (api.hasProfile(profileName)) { + } else if (api.hasProfile(definitionName, definitionVersion, profileName)) { log.info("Profile Already Existing - skipping upload") } else { log.info("Uploading K8s Profile..") @@ -164,8 +163,8 @@ open class K8sProfileUploadComponent( profile.rbVersion = definitionVersion profile.namespace = profileNamespace val profileFilePath: Path = prepareProfileFile(profileName, profileSource, artifact.file) - api.createProfile(profile) - api.uploadProfileContent(profile, profileFilePath) + api.createProfile(definitionName, definitionVersion, profile) + api.uploadProfileContent(definitionName, definitionVersion, profile, profileFilePath) log.info("K8s Profile Upload Completed") outputPrefixStatuses.put(prefix, OUTPUT_UPLOADED) |