182c22ce9ddf9ee9f9092d49382299b1a34dd0a2
[ccsdk/cds.git] /
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.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
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) : BluePrintArtifactDefinitionValidator {
36
37     private val log= LoggerFactory.getLogger(BluePrintArtifactDefinitionValidatorImpl::class.toString())
38
39     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
40     lateinit var bluePrintContext: BluePrintContext
41     var paths: MutableList<String> = arrayListOf()
42
43     override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String,
44                           artifactDefinition: ArtifactDefinition) {
45
46         this.bluePrintRuntimeService = bluePrintRuntimeService
47         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
48
49         paths.add(name)
50         val type: String = artifactDefinition.type
51         log.trace("Validation ArtifactDefinition of type {$type}")
52         // Check Artifact Type
53         checkValidArtifactType(name, type)
54
55         val file: String = artifactDefinition.file
56
57         val completePath = bluePrintContext.rootPath.plus(File.separator).plus(file)
58
59         check(File(completePath).exists()) {
60             throw BluePrintException("couldn't find file ($completePath)")
61         }
62
63         // Perform Extension Validation
64         validateExtension("$type-artifact-definition-validator", name, artifactDefinition)
65
66         paths.removeAt(paths.lastIndex)
67     }
68
69     open fun checkValidArtifactType(artifactDefinitionName: String, artifactTypeName: String) {
70
71         val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
72                 ?: throw BluePrintException("failed to get artifactType($artifactTypeName) for ArtifactDefinition($artifactDefinitionName)")
73
74         checkValidArtifactTypeDerivedFrom(artifactTypeName, artifactType.derivedFrom)
75     }
76
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")
81         }
82     }
83
84     private fun validateExtension(referencePrefix: String, name: String, artifactDefinition: ArtifactDefinition) {
85
86         val customValidators = bluePrintTypeValidatorService
87                 .bluePrintValidators(referencePrefix, BluePrintArtifactDefinitionValidator::class.java)
88
89         customValidators?.let {
90             it.forEach { validator ->
91                 validator.validate(bluePrintRuntimeService, name, artifactDefinition)
92             }
93
94         }
95     }
96 }