Add Service Template Blueprint Validation.
[ccsdk/apps.git] / components / core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / 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.apps.controllerblueprints.core.validation
18
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import com.google.common.base.Preconditions
22 import org.apache.commons.lang3.StringUtils
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintValidationError
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintServiceTemplateValidator
28 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
30
31 open class BluePrintServiceTemplateValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintServiceTemplateValidator {
32
33     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
34
35     var bluePrintContext: BluePrintContext? = null
36     var error: BluePrintValidationError? = null
37
38     override fun validate(bluePrintContext: BluePrintContext, error: BluePrintValidationError, name: String, serviceTemplate: ServiceTemplate) {
39         log.info("Validating Service Template..")
40         try {
41             this.bluePrintContext = bluePrintContext
42             this.error = error
43
44             serviceTemplate.metadata?.let { validateMetadata(serviceTemplate.metadata!!) }
45             serviceTemplate.dataTypes?.let { validateDataTypes(serviceTemplate.dataTypes!!) }
46             serviceTemplate.artifactTypes?.let { validateArtifactTypes(serviceTemplate.artifactTypes!!) }
47             serviceTemplate.nodeTypes?.let { validateNodeTypes(serviceTemplate.nodeTypes!!) }
48             serviceTemplate.topologyTemplate?.let { validateTopologyTemplate(serviceTemplate.topologyTemplate!!) }
49         } catch (e: Exception) {
50             throw BluePrintException(e, "failed to validate blueprint with message ${e.message}")
51         }
52     }
53
54     fun validateMetadata(metaDataMap: MutableMap<String, String>) {
55
56         val templateName = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_NAME]
57         val templateVersion = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_VERSION]
58         val templateTags = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_TAGS]
59         val templateAuthor = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
60
61         Preconditions.checkArgument(StringUtils.isNotBlank(templateName), "failed to get template name metadata")
62         Preconditions.checkArgument(StringUtils.isNotBlank(templateVersion), "failed to get template version metadata")
63         Preconditions.checkArgument(StringUtils.isNotBlank(templateTags), "failed to get template tags metadata")
64         Preconditions.checkArgument(StringUtils.isNotBlank(templateAuthor), "failed to get template author metadata")
65     }
66
67
68     fun validateDataTypes(dataTypes: MutableMap<String, DataType>) {
69         dataTypes.forEach { dataTypeName, dataType ->
70             // Validate Single Data Type
71             bluePrintTypeValidatorService.validateDataType(bluePrintContext!!, error!!, dataTypeName, dataType)
72         }
73     }
74
75     fun validateArtifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
76         artifactTypes.forEach { artifactName, artifactType ->
77             // Validate Single Artifact Type
78             bluePrintTypeValidatorService.validateArtifactType(bluePrintContext!!, error!!, artifactName, artifactType)
79         }
80     }
81
82     fun validateNodeTypes(nodeTypes: MutableMap<String, NodeType>) {
83         nodeTypes.forEach { nodeTypeName, nodeType ->
84             // Validate Single Node Type
85             bluePrintTypeValidatorService.validateNodeType(bluePrintContext!!, error!!, nodeTypeName, nodeType)
86         }
87     }
88
89     fun validateTopologyTemplate(topologyTemplate: TopologyTemplate) {
90         bluePrintTypeValidatorService.validateTopologyTemplate(bluePrintContext!!, error!!, "topologyTemplate", topologyTemplate)
91     }
92 }