Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / 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 com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionValidator
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
27 import org.onap.ccsdk.cds.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
31 import java.io.File
32
33 @Service("default-artifact-definition-validator")
34 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
35 open class BluePrintArtifactDefinitionValidatorImpl(
36         private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintArtifactDefinitionValidator {
37
38     private val log: EELFLogger = EELFManager.getInstance().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(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String,
45                           artifactDefinition: ArtifactDefinition) {
46
47         this.bluePrintRuntimeService = bluePrintRuntimeService
48         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
49
50         paths.add(name)
51         val type: String = artifactDefinition.type
52         log.trace("Validation ArtifactDefinition of type {$type}")
53         // Check Artifact Type
54         checkValidArtifactType(name, type)
55
56         val file: String = artifactDefinition.file
57
58         val completePath = bluePrintContext.rootPath.plus(File.separator).plus(file)
59
60         check(File(completePath).exists()) {
61             throw BluePrintException("couldn't find file ($completePath)")
62         }
63
64         // Perform Extension Validation
65         validateExtension("$type-artifact-definition-validator", name, artifactDefinition)
66
67         paths.removeAt(paths.lastIndex)
68     }
69
70     open fun checkValidArtifactType(artifactDefinitionName: String, artifactTypeName: String) {
71
72         val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
73                 ?: throw BluePrintException("failed to get artifactType($artifactTypeName) for ArtifactDefinition($artifactDefinitionName)")
74
75         checkValidArtifactTypeDerivedFrom(artifactTypeName, artifactType.derivedFrom)
76     }
77
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")
82         }
83     }
84
85     private fun validateExtension(referencePrefix: String, name: String, artifactDefinition: ArtifactDefinition) {
86
87         val customValidators = bluePrintTypeValidatorService
88                 .bluePrintValidators(referencePrefix, BluePrintArtifactDefinitionValidator::class.java)
89
90         customValidators?.let {
91             it.forEach { validator ->
92                 validator.validate(bluePrintRuntimeService, name, artifactDefinition)
93             }
94
95         }
96     }
97 }