Fixing Blueprint Typo's and docs
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-validation / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / validation / BlueprintArtifactDefinitionValidatorImpl.kt
1 /*
2  *  Copyright © 2018 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.validation
18
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
30 import java.io.File
31
32 @Service("default-artifact-definition-validator")
33 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
34 open class BlueprintArtifactDefinitionValidatorImpl(
35     private val bluePrintTypeValidatorService: BlueprintTypeValidatorService
36 ) : BlueprintArtifactDefinitionValidator {
37
38     private val log = LoggerFactory.getLogger(BlueprintArtifactDefinitionValidatorImpl::class.toString())
39
40     lateinit var bluePrintRuntimeService: BlueprintRuntimeService<*>
41     lateinit var bluePrintContext: BlueprintContext
42     var paths: MutableList<String> = arrayListOf()
43
44     override fun validate(
45         bluePrintRuntimeService: BlueprintRuntimeService<*>,
46         name: String,
47         artifactDefinition: ArtifactDefinition
48     ) {
49
50         this.bluePrintRuntimeService = bluePrintRuntimeService
51         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
52
53         paths.add(name)
54         val type: String = artifactDefinition.type
55         log.trace("Validation ArtifactDefinition of type {$type}")
56         // Check Artifact Type
57         checkValidArtifactType(name, type)
58
59         val file: String = artifactDefinition.file
60
61         val completePath = bluePrintContext.rootPath.plus(File.separator).plus(file)
62
63         check(File(completePath).exists()) {
64             throw BlueprintException("couldn't find file ($completePath)")
65         }
66
67         // Perform Extension Validation
68         validateExtension("$type-artifact-definition-validator", name, artifactDefinition)
69
70         paths.removeAt(paths.lastIndex)
71     }
72
73     open fun checkValidArtifactType(artifactDefinitionName: String, artifactTypeName: String) {
74
75         val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
76             ?: throw BlueprintException("failed to get artifactType($artifactTypeName) for ArtifactDefinition($artifactDefinitionName)")
77
78         checkValidArtifactTypeDerivedFrom(artifactTypeName, artifactType.derivedFrom)
79     }
80
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")
85         }
86     }
87
88     private fun validateExtension(referencePrefix: String, name: String, artifactDefinition: ArtifactDefinition) {
89
90         val customValidators = bluePrintTypeValidatorService
91             .bluePrintValidators(referencePrefix, BlueprintArtifactDefinitionValidator::class.java)
92
93         customValidators?.let {
94             it.forEach { validator ->
95                 validator.validate(bluePrintRuntimeService, name, artifactDefinition)
96             }
97         }
98     }
99 }