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.slf4j.LoggerFactory
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionValidator
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
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) : BluePrintArtifactDefinitionValidator {
37 private val log= LoggerFactory.getLogger(BluePrintArtifactDefinitionValidatorImpl::class.toString())
39 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
40 lateinit var bluePrintContext: BluePrintContext
41 var paths: MutableList<String> = arrayListOf()
43 override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String,
44 artifactDefinition: ArtifactDefinition) {
46 this.bluePrintRuntimeService = bluePrintRuntimeService
47 this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
50 val type: String = artifactDefinition.type
51 log.trace("Validation ArtifactDefinition of type {$type}")
52 // Check Artifact Type
53 checkValidArtifactType(name, type)
55 val file: String = artifactDefinition.file
57 val completePath = bluePrintContext.rootPath.plus(File.separator).plus(file)
59 check(File(completePath).exists()) {
60 throw BluePrintException("couldn't find file ($completePath)")
63 // Perform Extension Validation
64 validateExtension("$type-artifact-definition-validator", name, artifactDefinition)
66 paths.removeAt(paths.lastIndex)
69 open fun checkValidArtifactType(artifactDefinitionName: String, artifactTypeName: String) {
71 val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
72 ?: throw BluePrintException("failed to get artifactType($artifactTypeName) for ArtifactDefinition($artifactDefinitionName)")
74 checkValidArtifactTypeDerivedFrom(artifactTypeName, artifactType.derivedFrom)
77 @Throws(BluePrintException::class)
78 open fun checkValidArtifactTypeDerivedFrom(artifactTypeName: String, derivedFrom: String) {
79 check(BluePrintTypes.validArtifactTypeDerivedFroms.contains(derivedFrom)) {
80 throw BluePrintException("failed to get artifactType($artifactTypeName)'s derivedFrom($derivedFrom) definition")
84 private fun validateExtension(referencePrefix: String, name: String, artifactDefinition: ArtifactDefinition) {
86 val customValidators = bluePrintTypeValidatorService
87 .bluePrintValidators(referencePrefix, BluePrintArtifactDefinitionValidator::class.java)
89 customValidators?.let {
90 it.forEach { validator ->
91 validator.validate(bluePrintRuntimeService, name, artifactDefinition)