6b367c4db574c864014b5fa59ec4feeca69826e5
[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.BluePrintException
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
27 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
28 import org.onap.ccsdk.apps.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
29 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel
30 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelContent
31 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository
32 import org.springframework.dao.DataIntegrityViolationException
33 import org.springframework.stereotype.Service
34 import java.io.File
35 import java.nio.file.Files
36
37 /**
38 Similar implementation in [org.onap.ccsdk.apps.blueprintsprocessor.db.BlueprintProcessorCatalogServiceImpl]
39  */
40 @Service
41 class ControllerBlueprintCatalogServiceImpl(bluePrintLoadConfiguration: BluePrintLoadConfiguration,
42                                             private val bluePrintValidatorService: BluePrintValidatorService,
43                                             private val blueprintModelRepository: ControllerBlueprintModelRepository)
44     : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration) {
45
46     override fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean?) {
47         var valid = false
48         val firstItem = BluePrintArchiveUtils.getFirstItemInDirectory(extractedDirectory)
49         val blueprintBaseDirectory = extractedDirectory.absolutePath + "/" + firstItem
50         // Validate Blueprint
51         val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(id, blueprintBaseDirectory)
52
53         // Check Validity of blueprint
54         if (checkValidity!!) {
55             valid = bluePrintValidatorService.validateBluePrints(bluePrintRuntimeService)
56         }
57
58         if ((valid && checkValidity!!) || (!valid && !checkValidity!!)) {
59             val metaData = bluePrintRuntimeService.bluePrintContext().metadata!!
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             try {
84                 blueprintModelRepository.saveAndFlush(blueprintModel)
85             } catch (ex: DataIntegrityViolationException) {
86                 throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
87                         "is already exist in database: ${ex.message}", ex)
88             }
89         }
90     }
91 }