2 * Copyright © 2018 IBM.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.validation
19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
22 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionValidator
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
24 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
26 import org.slf4j.LoggerFactory
27 import org.springframework.beans.factory.config.ConfigurableBeanFactory
28 import org.springframework.context.annotation.Scope
29 import org.springframework.stereotype.Service
32 @Service("default-artifact-definition-validator")
33 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
34 open class BluePrintArtifactDefinitionValidatorImpl(
35 private val bluePrintTypeValidatorService: BluePrintTypeValidatorService
36 ) : BluePrintArtifactDefinitionValidator {
38 private val log = LoggerFactory.getLogger(BluePrintArtifactDefinitionValidatorImpl::class.toString())
40 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
41 lateinit var bluePrintContext: BluePrintContext
42 var paths: MutableList<String> = arrayListOf()
44 override fun validate(
45 bluePrintRuntimeService: BluePrintRuntimeService<*>,
47 artifactDefinition: ArtifactDefinition
50 this.bluePrintRuntimeService = bluePrintRuntimeService
51 this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
54 val type: String = artifactDefinition.type
55 log.trace("Validation ArtifactDefinition of type {$type}")
56 // Check Artifact Type
57 checkValidArtifactType(name, type)
59 val file: String = artifactDefinition.file
61 val completePath = bluePrintContext.rootPath.plus(File.separator).plus(file)
63 check(File(completePath).exists()) {
64 throw BluePrintException("couldn't find file ($completePath)")
67 // Perform Extension Validation
68 validateExtension("$type-artifact-definition-validator", name, artifactDefinition)
70 paths.removeAt(paths.lastIndex)
73 open fun checkValidArtifactType(artifactDefinitionName: String, artifactTypeName: String) {
75 val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
76 ?: throw BluePrintException("failed to get artifactType($artifactTypeName) for ArtifactDefinition($artifactDefinitionName)")
78 checkValidArtifactTypeDerivedFrom(artifactTypeName, artifactType.derivedFrom)
81 @Throws(BluePrintException::class)
82 open fun checkValidArtifactTypeDerivedFrom(artifactTypeName: String, derivedFrom: String) {
83 check(BluePrintTypes.validArtifactTypeDerivedFroms.contains(derivedFrom)) {
84 throw BluePrintException("failed to get artifactType($artifactTypeName)'s derivedFrom($derivedFrom) definition")
88 private fun validateExtension(referencePrefix: String, name: String, artifactDefinition: ArtifactDefinition) {
90 val customValidators = bluePrintTypeValidatorService
91 .bluePrintValidators(referencePrefix, BluePrintArtifactDefinitionValidator::class.java)
93 customValidators?.let {
94 it.forEach { validator ->
95 validator.validate(bluePrintRuntimeService, name, artifactDefinition)