X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=heat%2FvFW_CNF_CDS%2Ftemplates%2Fcba%2FScripts%2Fkotlin%2FKotlinK8sProfileUpload.kt;h=d47b476a56a7d6814ef9d234491af99235a8bf7d;hb=0806ad4aec588a090f4588bf64f68d1804d51113;hp=5231fb5c8c63cffc6d5ef272da46c06fd9e7ee01;hpb=08c029aa4bc814599cf75b5db692ae3266b830b3;p=demo.git diff --git a/heat/vFW_CNF_CDS/templates/cba/Scripts/kotlin/KotlinK8sProfileUpload.kt b/heat/vFW_CNF_CDS/templates/cba/Scripts/kotlin/KotlinK8sProfileUpload.kt index 5231fb5c..d47b476a 100644 --- a/heat/vFW_CNF_CDS/templates/cba/Scripts/kotlin/KotlinK8sProfileUpload.kt +++ b/heat/vFW_CNF_CDS/templates/cba/Scripts/kotlin/KotlinK8sProfileUpload.kt @@ -19,7 +19,6 @@ package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts import com.fasterxml.jackson.databind.node.ObjectNode import com.fasterxml.jackson.databind.ObjectMapper import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService @@ -27,6 +26,8 @@ import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientSer import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.RestLoggerService import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils +import org.onap.ccsdk.cds.controllerblueprints.core.utils.ArchiveType import org.apache.commons.io.IOUtils import org.apache.commons.io.FilenameUtils import org.apache.http.client.entity.EntityBuilder @@ -41,6 +42,7 @@ import org.springframework.web.client.RestTemplate import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonProperty import java.util.ArrayList +import java.util.LinkedHashMap import java.io.IOException import java.io.File import java.nio.file.Files @@ -51,11 +53,7 @@ import org.springframework.http.MediaType import java.nio.charset.Charset import java.util.Base64 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException - -import java.io.BufferedInputStream -import java.io.FileInputStream -import java.io.FileOutputStream -import java.util.zip.GZIPInputStream +import org.yaml.snakeyaml.Yaml open class K8sProfileUpload : AbstractScriptComponentFunction() { @@ -67,8 +65,6 @@ open class K8sProfileUpload : AbstractScriptComponentFunction() { override suspend fun processNB(executionRequest: ExecutionServiceInput) { log.info("executing K8s Profile Upload script") - val resolution_key = getDynamicProperties("resolution-key").asText() - log.info("resolution_key: $resolution_key") val baseK8sApiUrl = getDynamicProperties("api-access").get("url").asText() val k8sApiUsername = getDynamicProperties("api-access").get("username").asText() @@ -77,13 +73,15 @@ open class K8sProfileUpload : AbstractScriptComponentFunction() { val prefixList: ArrayList = getTemplatePrefixList(executionRequest) for (prefix in prefixList) { - if (prefix.toLowerCase().equals("vnf")) + if (prefix.toLowerCase().equals("vnf")) { + log.info("For vnf-level resource-assignment, profile upload is not performed") continue + } - val payload = storedContentFromResolvedArtifactNB(resolution_key, prefix) - log.info("Uploading K8S profile for template prefix $prefix") + val assignmentParams = getDynamicProperties("assignment-params") + val payloadObject = JacksonUtils.jsonNode(assignmentParams.get(prefix).asText()) as ObjectNode - val payloadObject = JacksonUtils.jsonNode(payload) as ObjectNode + log.info("Uploading K8S profile for template prefix $prefix") val vfModuleModelInvariantUuid: String = getResolvedParameter(payloadObject, "vf-module-model-invariant-uuid") val vfModuleModelUuid: String = getResolvedParameter(payloadObject, "vf-module-model-version") @@ -98,12 +96,15 @@ open class K8sProfileUpload : AbstractScriptComponentFunction() { log.info("k8s-rb-profile-name: $k8sRbProfileName") if (k8sRbProfileName.equals("")) { - log.info("Profile Name Not Defined - skipping upload") + throw BluePrintProcessorException("K8s rb profile name is empty! Either define profile name to use or choose default") + } + if (k8sRbProfileName.equals("default") and api.hasProfile(k8sRbProfileName)) { + log.info("Using default profile - skipping upload") } else { if (api.hasProfile(k8sRbProfileName)) { log.info("Profile Already Existing - skipping upload") } else { - val profileFilePath = prepareProfileFile(k8sRbProfileName) + val profileFilePath: Path = prepareProfileFile(k8sRbProfileName, prefix.equals("vpkg")) var profile = K8sProfile() profile.profileName = k8sRbProfileName @@ -119,7 +120,7 @@ open class K8sProfileUpload : AbstractScriptComponentFunction() { } } - fun prepareProfileFile(k8sRbProfileName: String): Path { + fun prepareProfileFile(k8sRbProfileName: String, profileModificationAllowed: Boolean): Path { val bluePrintContext = bluePrintRuntimeService.bluePrintContext() val bluePrintBasePath: String = bluePrintContext.rootPath var profileFilePath: Path = Paths.get(bluePrintBasePath.plus(File.separator).plus("Templates").plus(File.separator).plus("k8s-profiles").plus(File.separator).plus("${k8sRbProfileName}.tar.gz")) @@ -130,6 +131,58 @@ open class K8sProfileUpload : AbstractScriptComponentFunction() { if (!profileFile.exists()) throw BluePrintProcessorException("K8s Profile template file ${profileFilePath} does not exists") + val tempMainPath: File = createTempDir("k8s-profile-", "") + val tempProfilePath: File = createTempDir("${k8sRbProfileName}-", "", tempMainPath) + log.info("Decompressing profile to ${tempProfilePath.toString()}") + + val decompressedProfile: File = BluePrintArchiveUtils.deCompress(profileFilePath.toFile(), + "${tempProfilePath.toString()}", ArchiveType.TarGz) + + log.info("${profileFilePath.toString()} decompression completed") + + if (profileModificationAllowed) { + //Here we can add extra files inside the archive + val profileModificationDecisionData = getDynamicProperties("profile-modification-decision-data") + log.info("Profile modification decision data: ${profileModificationDecisionData}") + if (profileModificationDecisionData != null && profileModificationDecisionData.asText().toInt() > 0) { + log.info("Modification of profile content") + + val profileArtifacts = getDynamicProperties("profile-artifacts") + val sshServiceFileContent = profileArtifacts.get("ssh-service").asText() + val sshServiceFileName = "ssh-service.yaml" + val serviceFilePath = tempProfilePath.toString().plus(File.separator).plus(sshServiceFileName) + File(serviceFilePath).bufferedWriter().use { out -> out.write(sshServiceFileContent) } + val manifestFileName = tempProfilePath.toString().plus(File.separator).plus("manifest.yaml") + var finalManifest = "" + File(manifestFileName).bufferedReader().use { inr -> + val manifestYaml = Yaml() + val manifestObject: Map = manifestYaml.load(inr) + val typeObject: MutableMap = manifestObject.get("type") as MutableMap + if (!typeObject.containsKey("configresource")) + typeObject.put("configresource", ArrayList>()) + val configFiles: MutableList> = typeObject.get("configresource") as MutableList> + val sshConfigFile = LinkedHashMap() + sshConfigFile.put("filepath", sshServiceFileName) + sshConfigFile.put("chartpath", "vpkg/templates/${sshServiceFileName}") + configFiles.add(sshConfigFile) + finalManifest = manifestYaml.dump(manifestObject) + } + File(manifestFileName).bufferedWriter().use { out -> out.write(finalManifest) } + log.info("Modified K8s profile manifest file") + log.info(finalManifest) + log.info("Modification of profile completed") + } + } + + profileFilePath = Paths.get(tempMainPath.toString().plus(File.separator).plus("${k8sRbProfileName}.tar.gz")) + + if (!BluePrintArchiveUtils.compress(decompressedProfile, profileFilePath.toFile(), + ArchiveType.TarGz)) { + throw BluePrintProcessorException("Profile compression has failed") + } + + log.info("${profileFilePath.toString()} compression completed") + return profileFilePath } @@ -167,7 +220,7 @@ open class K8sProfileUpload : AbstractScriptComponentFunction() { var basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties() basicAuthRestClientProperties.username = username basicAuthRestClientProperties.password = password - basicAuthRestClientProperties.url = "$baseUrl/api/multicloud-k8s/v1/v1/rb/definition/${definition}/${definitionVersion}" + basicAuthRestClientProperties.url = "$baseUrl/v1/rb/definition/${definition}/${definitionVersion}" basicAuthRestClientProperties.additionalHeaders = mapOfHeaders this.service = UploadFileRestClientService(basicAuthRestClientProperties)