66c504dea77dbc44213a259ecf1a34a060feee3f
[ccsdk/cds.git] /
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.BluePrintValidationError
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
26 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintServiceTemplateValidator
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
29
30 open class BluePrintServiceTemplateValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintServiceTemplateValidator {
31
32     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
33
34     var bluePrintContext: BluePrintContext? = null
35     var error: BluePrintValidationError? = null
36     var paths: MutableList<String> = arrayListOf()
37
38     override fun validate(bluePrintContext: BluePrintContext, error: BluePrintValidationError, name: String, serviceTemplate: ServiceTemplate) {
39         log.trace("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             error.addError(BluePrintConstants.PATH_SERVICE_TEMPLATE, paths.joinToString(BluePrintConstants.PATH_DIVIDER), e.message!!)
51         }
52     }
53
54     fun validateMetadata(metaDataMap: MutableMap<String, String>) {
55
56         paths.add(BluePrintConstants.PATH_METADATA)
57
58         val templateName = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_NAME]
59         val templateVersion = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_VERSION]
60         val templateTags = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_TAGS]
61         val templateAuthor = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
62
63         Preconditions.checkArgument(StringUtils.isNotBlank(templateName), "failed to get template name metadata")
64         Preconditions.checkArgument(StringUtils.isNotBlank(templateVersion), "failed to get template version metadata")
65         Preconditions.checkArgument(StringUtils.isNotBlank(templateTags), "failed to get template tags metadata")
66         Preconditions.checkArgument(StringUtils.isNotBlank(templateAuthor), "failed to get template author metadata")
67
68         paths.removeAt(paths.lastIndex)
69     }
70
71
72     fun validateDataTypes(dataTypes: MutableMap<String, DataType>) {
73
74         paths.add(BluePrintConstants.PATH_DATA_TYPES)
75         dataTypes.forEach { dataTypeName, dataType ->
76             // Validate Single Data Type
77             bluePrintTypeValidatorService.validateDataType(bluePrintContext!!, error!!, dataTypeName, dataType)
78         }
79         paths.removeAt(paths.lastIndex)
80     }
81
82     fun validateArtifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
83         paths.add(BluePrintConstants.PATH_ARTIFACT_TYPES)
84         artifactTypes.forEach { artifactName, artifactType ->
85             // Validate Single Artifact Type
86             bluePrintTypeValidatorService.validateArtifactType(bluePrintContext!!, error!!, artifactName, artifactType)
87         }
88         paths.removeAt(paths.lastIndex)
89     }
90
91     fun validateNodeTypes(nodeTypes: MutableMap<String, NodeType>) {
92         paths.add(BluePrintConstants.PATH_NODE_TYPES)
93         nodeTypes.forEach { nodeTypeName, nodeType ->
94             // Validate Single Node Type
95             bluePrintTypeValidatorService.validateNodeType(bluePrintContext!!, error!!, nodeTypeName, nodeType)
96         }
97         paths.removeAt(paths.lastIndex)
98     }
99
100     fun validateTopologyTemplate(topologyTemplate: TopologyTemplate) {
101         paths.add(BluePrintConstants.PATH_TOPOLOGY_TEMPLATE)
102         bluePrintTypeValidatorService.validateTopologyTemplate(bluePrintContext!!, error!!, "topologyTemplate", topologyTemplate)
103         paths.removeAt(paths.lastIndex)
104     }
105 }