761dd25a3dfb7806dbc87faabb4b747d7f3ace24
[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.service.load
19
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
32 import java.io.File
33 import java.nio.file.Files
34
35 @Service
36 class BluePrintCatalogServiceImpl(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
37                                   private val bluePrintValidatorService: BluePrintValidatorService,
38                                   private val blueprintModelContentRepository: BlueprintModelContentRepository,
39                                   private val blueprintModelRepository: BlueprintModelRepository) : BluePrintCatalogService {
40
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) {
48
49             val zipFile = File("${bluePrintLoadConfiguration.blueprintArchivePath}/$fileName")
50             // zip the directory
51             BluePrintArchiveUtils.compress(blueprintFile, zipFile, true)
52
53             // Upload to the Data Base
54             saveToDataBase(blueprintFile, id, zipFile)
55
56             // After Upload to Database delete the zip file
57             zipFile.delete()
58
59         } else {
60             // If the file is ZIP
61             // unzip the CBA file to validate before store in database
62             val targetDir = "${bluePrintLoadConfiguration.blueprintDeployPath}/$id/"
63             val extractedDirectory = BluePrintArchiveUtils.deCompress(blueprintFile, targetDir)
64
65             // Upload to the Data Base
66             saveToDataBase(extractedDirectory, id, blueprintFile)
67
68             // After Upload to Database delete the zip file
69             blueprintFile.delete()
70             extractedDirectory.delete()
71         }
72
73         return id
74     }
75
76     override fun downloadFromDataBase(name: String, version: String, path: String): String {
77         // If path ends with zip, then compress otherwise download as extracted folder
78
79         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
80     }
81
82     override fun downloadFromDataBase(uuid: String, path: String): String {
83         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
84     }
85
86     override fun prepareBluePrint(name: String, version: String): String {
87         val preparedPath = "${bluePrintLoadConfiguration.blueprintDeployPath}/$name/$version"
88         downloadFromDataBase(name, version, preparedPath)
89         return preparedPath
90     }
91
92     private fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean? = false) {
93         // Upload to the Data Base
94         //val id = "save-$uuid"
95         var valid = false
96         val firstItem = BluePrintArchiveUtils.getFirstItemInDirectory(extractedDirectory)
97         val blueprintBaseDirectory = extractedDirectory.absolutePath + "/" + firstItem
98         // Validate Blueprint
99         val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(id, blueprintBaseDirectory)
100
101         // Check Validity of blueprint
102         if (checkValidity!!) {
103             valid = bluePrintValidatorService.validateBluePrints(bluePrintRuntimeService)
104         }
105
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}"
118
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())
125
126             // Set the Blueprint Model into blueprintModelContent
127             blueprintModelContent.blueprintModel = blueprintModel
128
129             // Set the Blueprint Model Content into blueprintModel
130             blueprintModel.blueprintModelContent = blueprintModelContent
131
132             blueprintModelRepository.saveAndFlush(blueprintModel)
133         }
134     }
135 }