ac81f8fa69cc60bd2a35d30461b6b36e19ab50cf
[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.common.ApplicationConstants
22 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration
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.BluePrintMetadataUtils
26 import org.onap.ccsdk.apps.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
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.ControllerBlueprintModelContentRepository
30 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository
31 import org.springframework.stereotype.Service
32 import java.io.File
33 import java.nio.file.Files
34
35 /**
36 Similar implementation in [org.onap.ccsdk.apps.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl]
37  */
38 @Service
39 class ControllerBlueprintCatalogServiceImpl(bluePrintLoadConfiguration: BluePrintLoadConfiguration,
40                                             private val bluePrintValidatorService: BluePrintValidatorService,
41                                             private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository,
42                                             private val blueprintModelRepository: ControllerBlueprintModelRepository)
43     : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration) {
44
45     override fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean?) {
46         var valid = false
47         val firstItem = BluePrintArchiveUtils.getFirstItemInDirectory(extractedDirectory)
48         val blueprintBaseDirectory = extractedDirectory.absolutePath + "/" + firstItem
49         // Validate Blueprint
50         val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(id, blueprintBaseDirectory)
51
52         // Check Validity of blueprint
53         if (checkValidity!!) {
54             valid = bluePrintValidatorService.validateBluePrints(bluePrintRuntimeService)
55         }
56
57         if ((valid && checkValidity!!) || (!valid && !checkValidity!!)) {
58             val metaData = bluePrintRuntimeService.bluePrintContext().metadata!!
59             // FIXME("Check Duplicate for Artifact Name and Artifact Version")
60             val blueprintModel = BlueprintModel()
61             blueprintModel.id = id
62             blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
63             blueprintModel.published = ApplicationConstants.ACTIVE_N
64             blueprintModel.artifactName = metaData[BluePrintConstants.METADATA_TEMPLATE_NAME]
65             blueprintModel.artifactVersion = metaData[BluePrintConstants.METADATA_TEMPLATE_VERSION]
66             blueprintModel.updatedBy = metaData[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
67             blueprintModel.tags = metaData[BluePrintConstants.METADATA_TEMPLATE_TAGS]
68             blueprintModel.artifactDescription = "Controller Blueprint for ${blueprintModel.artifactName}:${blueprintModel.artifactVersion}"
69
70             val blueprintModelContent = BlueprintModelContent()
71             blueprintModelContent.id = id // For quick access both id's are same.always have one to one mapping.
72             blueprintModelContent.contentType = "CBA_ZIP"
73             blueprintModelContent.name = "${blueprintModel.artifactName}:${blueprintModel.artifactVersion}"
74             blueprintModelContent.description = "(${blueprintModel.artifactName}:${blueprintModel.artifactVersion} CBA Zip Content"
75             blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
76
77             // Set the Blueprint Model into blueprintModelContent
78             blueprintModelContent.blueprintModel = blueprintModel
79
80             // Set the Blueprint Model Content into blueprintModel
81             blueprintModel.blueprintModelContent = blueprintModelContent
82
83             blueprintModelRepository.saveAndFlush(blueprintModel)
84         }
85     }
86 }