0e76be49e7052d1254f18b9ae7d8499c7cfad569
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-validation / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / validation / BluePrintServiceTemplateValidatorImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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 com.google.common.base.Preconditions
21 import org.apache.commons.lang3.StringUtils
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintServiceTemplateValidator
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
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
32 @Service("default-service-template-validator")
33 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
34 open class BluePrintServiceTemplateValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintServiceTemplateValidator {
35
36     private val log= LoggerFactory.getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
37
38     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
39     lateinit var error: BluePrintError
40
41     var paths: MutableList<String> = arrayListOf()
42
43     override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, serviceTemplate: ServiceTemplate) {
44         log.trace("Validating Service Template..")
45         try {
46             this.bluePrintRuntimeService = bluePrintRuntimeService
47             this.error = bluePrintRuntimeService.getBluePrintError()
48
49             serviceTemplate.metadata?.let { validateMetadata(serviceTemplate.metadata!!) }
50             serviceTemplate.dataTypes?.let { validateDataTypes(serviceTemplate.dataTypes!!) }
51             serviceTemplate.artifactTypes?.let { validateArtifactTypes(serviceTemplate.artifactTypes!!) }
52             serviceTemplate.nodeTypes?.let { validateNodeTypes(serviceTemplate.nodeTypes!!) }
53             serviceTemplate.topologyTemplate?.let { validateTopologyTemplate(serviceTemplate.topologyTemplate!!) }
54         } catch (e: Exception) {
55             log.error("failed in blueprint service template validation", e)
56             error.addError(BluePrintConstants.PATH_SERVICE_TEMPLATE, paths.joinToString(BluePrintConstants.PATH_DIVIDER), e.message!!)
57         }
58     }
59
60     fun validateMetadata(metaDataMap: MutableMap<String, String>) {
61
62         paths.add(BluePrintConstants.PATH_METADATA)
63
64         val templateName = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_NAME]
65         val templateVersion = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_VERSION]
66         val templateTags = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_TAGS]
67         val templateAuthor = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
68
69         Preconditions.checkArgument(StringUtils.isNotBlank(templateName), "failed to get template name metadata")
70         Preconditions.checkArgument(StringUtils.isNotBlank(templateVersion), "failed to get template version metadata")
71         Preconditions.checkArgument(StringUtils.isNotBlank(templateTags), "failed to get template tags metadata")
72         Preconditions.checkArgument(StringUtils.isNotBlank(templateAuthor), "failed to get template author metadata")
73
74         paths.removeAt(paths.lastIndex)
75     }
76
77
78     fun validateDataTypes(dataTypes: MutableMap<String, DataType>) {
79
80         paths.add(BluePrintConstants.PATH_DATA_TYPES)
81         dataTypes.forEach { dataTypeName, dataType ->
82             // Validate Single Data Type
83             bluePrintTypeValidatorService.validateDataType(bluePrintRuntimeService, dataTypeName, dataType)
84         }
85         paths.removeAt(paths.lastIndex)
86     }
87
88     fun validateArtifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
89         paths.add(BluePrintConstants.PATH_ARTIFACT_TYPES)
90         artifactTypes.forEach { artifactName, artifactType ->
91             // Validate Single Artifact Type
92             bluePrintTypeValidatorService.validateArtifactType(bluePrintRuntimeService, artifactName, artifactType)
93         }
94         paths.removeAt(paths.lastIndex)
95     }
96
97     fun validateNodeTypes(nodeTypes: MutableMap<String, NodeType>) {
98         paths.add(BluePrintConstants.PATH_NODE_TYPES)
99         nodeTypes.forEach { nodeTypeName, nodeType ->
100             // Validate Single Node Type
101             bluePrintTypeValidatorService.validateNodeType(bluePrintRuntimeService, nodeTypeName, nodeType)
102         }
103         paths.removeAt(paths.lastIndex)
104     }
105
106     fun validateTopologyTemplate(topologyTemplate: TopologyTemplate) {
107         paths.add(BluePrintConstants.PATH_TOPOLOGY_TEMPLATE)
108         bluePrintTypeValidatorService.validateTopologyTemplate(bluePrintRuntimeService, "topologyTemplate", topologyTemplate)
109         paths.removeAt(paths.lastIndex)
110     }
111 }