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.db.resources
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
23 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
28 import java.nio.file.Path
30 import javax.persistence.MappedSuperclass
33 abstract class BlueprintCatalogServiceImpl(private val blueprintValidator: BluePrintValidatorService)
34 : BluePrintCatalogService {
36 override fun saveToDatabase(blueprintFile: File, validate: Boolean): String {
37 val extractedDirectory: File
38 val archivedDirectory: File
39 val toDeleteDirectory: File
40 val blueprintId = UUID.randomUUID().toString()
42 if (blueprintFile.isDirectory) {
43 extractedDirectory = blueprintFile
44 archivedDirectory = File(":$blueprintFile.zip")
45 toDeleteDirectory = archivedDirectory
47 if (!BluePrintArchiveUtils.compress(blueprintFile, archivedDirectory, true)) {
48 throw BluePrintException("Fail to compress blueprint")
51 val targetDir = "${blueprintFile.parent}/${BluePrintFileUtils.stripFileExtension(blueprintFile.name)}"
53 extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir)
54 archivedDirectory = blueprintFile
55 toDeleteDirectory = extractedDirectory
59 blueprintValidator.validateBluePrints(extractedDirectory.path)
62 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(blueprintId, extractedDirectory.path)
63 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
64 metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
66 save(metadata, archivedDirectory)
68 toDeleteDirectory.deleteRecursively()
73 override fun getFromDatabase(name: String, version: String, extract: Boolean): Path = get(name, version, extract)
74 ?: throw BluePrintException("Could not find blueprint $name:$version from database")
76 override fun deleteFromDatabase(name: String, version: String) = delete(name, version)
78 abstract fun save(metadata: MutableMap<String, String>, archiveFile: File)
79 abstract fun get(name: String, version: String, extract: Boolean): Path?
80 abstract fun delete(name: String, version: String)