2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
4 * Modifications Copyright © 2019 IBM.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.controllerblueprints.db.resources
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidatorService
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
29 import java.nio.file.Path
31 import javax.persistence.MappedSuperclass
34 abstract class BlueprintCatalogServiceImpl(private val blueprintValidator: BluePrintValidatorService)
35 : BluePrintCatalogService {
37 override fun saveToDatabase(blueprintFile: File, validate: Boolean): String {
38 val extractedDirectory: File
39 val archivedDirectory: File
40 val toDeleteDirectory: File
41 val blueprintId = UUID.randomUUID().toString()
43 if (blueprintFile.isDirectory) {
44 extractedDirectory = blueprintFile
45 archivedDirectory = File("$blueprintFile.zip")
46 toDeleteDirectory = archivedDirectory
48 if (!BluePrintArchiveUtils.compress(blueprintFile, archivedDirectory, true)) {
49 throw BluePrintException("Fail to compress blueprint")
52 val targetDir = "${blueprintFile.parent}/${BluePrintFileUtils.stripFileExtension(blueprintFile.name)}"
54 extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir)
55 archivedDirectory = blueprintFile
56 toDeleteDirectory = extractedDirectory
59 var valid = BluePrintConstants.FLAG_N
61 blueprintValidator.validateBluePrints(extractedDirectory.path)
62 valid = BluePrintConstants.FLAG_Y
65 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(blueprintId, extractedDirectory.path)
66 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
67 metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
68 metadata[BluePrintConstants.PROPERTY_BLUEPRINT_VALID] = valid
70 save(metadata, archivedDirectory)
72 toDeleteDirectory.deleteRecursively()
77 override fun getFromDatabase(name: String, version: String, extract: Boolean): Path = get(name, version, extract)
78 ?: throw BluePrintException("Could not find blueprint $name:$version from database")
80 override fun deleteFromDatabase(name: String, version: String) = delete(name, version)
82 abstract fun save(metadata: MutableMap<String, String>, archiveFile: File)
83 abstract fun get(name: String, version: String, extract: Boolean): Path?
84 abstract fun delete(name: String, version: String)