2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.apps.controllerblueprints.service.load
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
22 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService
23 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
26 import org.onap.ccsdk.apps.controllerblueprints.service.common.ApplicationConstants
27 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel
28 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelContent
29 import org.onap.ccsdk.apps.controllerblueprints.service.repository.BlueprintModelContentRepository
30 import org.onap.ccsdk.apps.controllerblueprints.service.repository.BlueprintModelRepository
31 import org.springframework.stereotype.Service
33 import java.nio.file.Files
36 class BluePrintCatalogServiceImpl(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
37 private val bluePrintValidatorService: BluePrintValidatorService,
38 private val blueprintModelContentRepository: BlueprintModelContentRepository,
39 private val blueprintModelRepository: BlueprintModelRepository) : BluePrintCatalogService {
41 override fun uploadToDataBase(file: String, validate: Boolean): String {
42 // The file name provided here is unique as we transform to UUID before storing
43 val blueprintFile = File(file)
44 val fileName = blueprintFile.name
45 val id = BluePrintFileUtils.stripFileExtension(fileName)
46 // If the file is directory
47 if (blueprintFile.isDirectory) {
49 val zipFile = File("${bluePrintLoadConfiguration.blueprintArchivePath}/$fileName")
51 BluePrintArchiveUtils.compress(blueprintFile, zipFile, true)
53 // Upload to the Data Base
54 saveToDataBase(blueprintFile, id, zipFile)
56 // After Upload to Database delete the zip file
61 // unzip the CBA file to validate before store in database
62 val targetDir = "${bluePrintLoadConfiguration.blueprintDeployPath}/$id/"
63 val extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir)
65 // Upload to the Data Base
66 saveToDataBase(extractedDirectory, id, blueprintFile)
68 // After Upload to Database delete the zip file
69 blueprintFile.delete()
70 extractedDirectory.delete()
76 override fun downloadFromDataBase(name: String, version: String, path: String): String {
77 // If path ends with zip, then compress otherwise download as extracted folder
79 TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
82 override fun downloadFromDataBase(uuid: String, path: String): String {
83 TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
86 override fun prepareBluePrint(name: String, version: String): String {
87 val preparedPath = "${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version"
88 downloadFromDataBase(name, version, preparedPath)
92 private fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean? = false) {
93 // Upload to the Data Base
94 //val id = "save-$uuid"
96 val firstItem = BluePrintArchiveUtils.getFirstItemInDirectory(extractedDirectory)
97 val blueprintBaseDirectory = extractedDirectory.absolutePath + "/" + firstItem
99 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(id, blueprintBaseDirectory)
101 // Check Validity of blueprint
102 if (checkValidity!!) {
103 valid = bluePrintValidatorService.validateBluePrints(bluePrintRuntimeService)
106 if ((valid && checkValidity!!) || (!valid && !checkValidity!!)) {
107 val metaData = bluePrintRuntimeService.bluePrintContext().metadata!!
108 // FIXME("Check Duplicate for Artifact Name and Artifact Version")
109 val blueprintModel = BlueprintModel()
110 blueprintModel.id = id
111 blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
112 blueprintModel.published = ApplicationConstants.ACTIVE_N
113 blueprintModel.artifactName = metaData[BluePrintConstants.METADATA_TEMPLATE_NAME]
114 blueprintModel.artifactVersion = metaData[BluePrintConstants.METADATA_TEMPLATE_VERSION]
115 blueprintModel.updatedBy = metaData[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
116 blueprintModel.tags = metaData[BluePrintConstants.METADATA_TEMPLATE_TAGS]
117 blueprintModel.artifactDescription = "Controller Blueprint for ${blueprintModel.artifactName}:${blueprintModel.artifactVersion}"
119 val blueprintModelContent = BlueprintModelContent()
120 blueprintModelContent.id = id // For quick access both id's are same.always have one to one mapping.
121 blueprintModelContent.contentType = "CBA_ZIP"
122 blueprintModelContent.name = "${blueprintModel.artifactName}:${blueprintModel.artifactVersion}"
123 blueprintModelContent.description = "(${blueprintModel.artifactName}:${blueprintModel.artifactVersion} CBA Zip Content"
124 blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
126 // Set the Blueprint Model into blueprintModelContent
127 blueprintModelContent.blueprintModel = blueprintModel
129 // Set the Blueprint Model Content into blueprintModel
130 blueprintModel.blueprintModelContent = blueprintModelContent
132 blueprintModelRepository.saveAndFlush(blueprintModel)