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.apps.controllerblueprints.validation
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactDefinition
24 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionValidator
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
28 import org.springframework.beans.factory.config.ConfigurableBeanFactory
29 import org.springframework.context.annotation.Scope
30 import org.springframework.stereotype.Service
33 @Service("default-artifact-definition-validator")
34 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
35 open class BluePrintArtifactDefinitionValidatorImpl(
36 private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintArtifactDefinitionValidator {
38 private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintArtifactDefinitionValidatorImpl::class.toString())
40 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
41 lateinit var bluePrintContext: BluePrintContext
42 var paths: MutableList<String> = arrayListOf()
44 override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String,
45 artifactDefinition: ArtifactDefinition) {
47 this.bluePrintRuntimeService = bluePrintRuntimeService
48 this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
51 val type: String = artifactDefinition.type
52 log.trace("Validation ArtifactDefinition of type {$type}")
53 // Check Artifact Type
54 checkValidArtifactType(name, type)
56 val file: String = artifactDefinition.file
58 val completePath = bluePrintContext.rootPath.plus(File.separator).plus(file)
60 check(File(completePath).exists()) {
61 throw BluePrintException("couldn't find file ($completePath)")
64 // Perform Extension Validation
65 validateExtension("$type-artifact-definition-validator", name, artifactDefinition)
67 paths.removeAt(paths.lastIndex)
70 open fun checkValidArtifactType(artifactDefinitionName: String, artifactTypeName: String) {
72 val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
73 ?: throw BluePrintException("failed to get artifactType($artifactTypeName) for ArtifactDefinition($artifactDefinitionName)")
75 checkValidArtifactTypeDerivedFrom(artifactTypeName, artifactType.derivedFrom)
78 @Throws(BluePrintException::class)
79 open fun checkValidArtifactTypeDerivedFrom(artifactTypeName: String, derivedFrom: String) {
80 check(BluePrintTypes.validArtifactTypeDerivedFroms.contains(derivedFrom)) {
81 throw BluePrintException("failed to get artifactType($artifactTypeName)'s derivedFrom($derivedFrom) definition")
85 private fun validateExtension(referencePrefix: String, name: String, artifactDefinition: ArtifactDefinition) {
87 val customValidators = bluePrintTypeValidatorService
88 .bluePrintValidators(referencePrefix, BluePrintArtifactDefinitionValidator::class.java)
90 customValidators?.let {
91 it.forEach { validator ->
92 validator.validate(bluePrintRuntimeService, name, artifactDefinition)