881e3bc40e2de54b0b9c9238d04923c55d10b304
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.apps.controllerblueprints.db.resources
19
20 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration
21 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
22 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
23 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
24 import java.io.File
25 import javax.persistence.MappedSuperclass
26
27 @MappedSuperclass
28 abstract class BlueprintCatalogServiceImpl(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration) : BluePrintCatalogService {
29
30     override fun uploadToDataBase(file: String, validate: Boolean): String {
31         // The file name provided here is unique as we transform to UUID before storing
32         val blueprintFile = File(file)
33         val fileName = blueprintFile.name
34         val id = BluePrintFileUtils.stripFileExtension(fileName)
35         // If the file is directory
36         if (blueprintFile.isDirectory) {
37
38             val zipFile = File("${bluePrintLoadConfiguration.blueprintArchivePath}/$fileName")
39             // zip the directory
40             BluePrintArchiveUtils.compress(blueprintFile, zipFile, true)
41
42             // Upload to the Data Base
43             saveToDataBase(blueprintFile, id, zipFile)
44
45             // After Upload to Database delete the zip file
46             zipFile.delete()
47
48         } else {
49             // If the file is ZIP
50             // unzip the CBA file to validate before store in database
51             val targetDir = "${bluePrintLoadConfiguration.blueprintDeployPath}/$id/"
52             val extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir)
53
54             // Upload to the Data Base
55             saveToDataBase(extractedDirectory, id, blueprintFile)
56
57             // After Upload to Database delete the zip file
58             blueprintFile.delete()
59             extractedDirectory.delete()
60         }
61
62         return id
63     }
64
65     override fun downloadFromDataBase(name: String, version: String, path: String): String {
66         // If path ends with zip, then compress otherwise download as extracted folder
67
68         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
69     }
70
71     override fun downloadFromDataBase(uuid: String, path: String): String {
72         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
73     }
74
75     override fun prepareBluePrint(name: String, version: String): String {
76         val preparedPath = "${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version"
77         downloadFromDataBase(name, version, preparedPath)
78         return preparedPath
79     }
80
81     abstract fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean? = false)
82 }