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
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
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
-
open class K8sProfileUpload : AbstractScriptComponentFunction() {
private val log = LoggerFactory.getLogger(K8sProfileUpload::class.java)!!
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()
val prefixList: ArrayList<String> = 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")
+ val vfModuleModelInvariantUuid: String = getResolvedParameter(payloadObject, "vf-module-model-invariant-uuid")
+ val vfModuleModelUuid: String = getResolvedParameter(payloadObject, "vf-module-model-version")
val k8sRbProfileName: String = getResolvedParameter(payloadObject, "k8s-rb-profile-name")
val k8sRbProfileNamespace: String = getResolvedParameter(payloadObject, "k8s-rb-profile-namespace")
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 {
- profileFilePath = prepareProfileFile(k8sRbProfileName)
+ val profileFilePath: Path = prepareProfileFile(k8sRbProfileName)
var profile = K8sProfile()
profile.profileName = k8sRbProfileName
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")
+
+ //Here we can add extra files inside the archive
+ 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
}
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)
override fun hashCode(): Int {
return javaClass.hashCode()
}
-}
\ No newline at end of file
+}