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.blueprintsprocessor.db
 
  20 import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.domain.BlueprintProcessorModel
 
  21 import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.domain.BlueprintProcessorModelContent
 
  22 import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.repository.BlueprintProcessorModelContentRepository
 
  23 import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.repository.BlueprintProcessorModelRepository
 
  24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
 
  25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
 
  26 import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants
 
  27 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration
 
  28 import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode
 
  29 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintValidatorService
 
  30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
 
  31 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
 
  32 import org.onap.ccsdk.apps.controllerblueprints.db.resources.BlueprintCatalogServiceImpl
 
  33 import org.springframework.dao.DataIntegrityViolationException
 
  34 import org.springframework.stereotype.Service
 
  36 import java.nio.file.Files
 
  39 Similar/Duplicate implementation in [org.onap.ccsdk.apps.controllerblueprints.service.load.ControllerBlueprintCatalogServiceImpl]
 
  42 class BlueprintProcessorCatalogServiceImpl(bluePrintLoadConfiguration: BluePrintLoadConfiguration,
 
  43                                            private val bluePrintValidatorService: BluePrintValidatorService,
 
  44                                            private val blueprintModelRepository: BlueprintProcessorModelRepository)
 
  45     : BlueprintCatalogServiceImpl(bluePrintLoadConfiguration) {
 
  47     override fun saveToDataBase(extractedDirectory: File, id: String, archiveFile: File, checkValidity: Boolean?) {
 
  49         val firstItem = BluePrintArchiveUtils.getFirstItemInDirectory(extractedDirectory)
 
  50         val blueprintBaseDirectory = extractedDirectory.absolutePath + "/" + firstItem
 
  52         val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(id, blueprintBaseDirectory)
 
  54         // Check Validity of blueprint
 
  55         if (checkValidity!!) {
 
  56             valid = bluePrintValidatorService.validateBluePrints(bluePrintRuntimeService)
 
  59         if ((valid && checkValidity!!) || (!valid && !checkValidity!!)) {
 
  60             val metaData = bluePrintRuntimeService.bluePrintContext().metadata!!
 
  61             // FIXME("Check Duplicate for Artifact Name and Artifact Version")
 
  62             val blueprintModel = BlueprintProcessorModel()
 
  63             blueprintModel.id = id
 
  64             blueprintModel.artifactType = ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL
 
  65             blueprintModel.published = ApplicationConstants.ACTIVE_N
 
  66             blueprintModel.artifactName = metaData[BluePrintConstants.METADATA_TEMPLATE_NAME]
 
  67             blueprintModel.artifactVersion = metaData[BluePrintConstants.METADATA_TEMPLATE_VERSION]
 
  68             blueprintModel.updatedBy = metaData[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
 
  69             blueprintModel.tags = metaData[BluePrintConstants.METADATA_TEMPLATE_TAGS]
 
  70             blueprintModel.artifactDescription = "Controller Blueprint for ${blueprintModel.artifactName}:${blueprintModel.artifactVersion}"
 
  72             val blueprintModelContent = BlueprintProcessorModelContent()
 
  73             blueprintModelContent.id = id // For quick access both id's are same.always have one to one mapping.
 
  74             blueprintModelContent.contentType = "CBA_ZIP"
 
  75             blueprintModelContent.name = "${blueprintModel.artifactName}:${blueprintModel.artifactVersion}"
 
  76             blueprintModelContent.description = "(${blueprintModel.artifactName}:${blueprintModel.artifactVersion} CBA Zip Content"
 
  77             blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath())
 
  79             // Set the Blueprint Model into blueprintModelContent
 
  80             blueprintModelContent.blueprintModel = blueprintModel
 
  82             // Set the Blueprint Model Content into blueprintModel
 
  83             blueprintModel.blueprintModelContent = blueprintModelContent
 
  86                 blueprintModelRepository.saveAndFlush(blueprintModel)
 
  87             } catch (ex: DataIntegrityViolationException) {
 
  88                 throw BluePrintException(ErrorCode.CONFLICT_ADDING_RESOURCE.value, "The blueprint entry " +
 
  89                         "is already exist in database: ${ex.message}", ex)