Refactor components core and resource dict modules
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-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.BluePrintError
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.BluePrintRuntimeService
29
30 open class BluePrintServiceTemplateValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintServiceTemplateValidator {
31
32     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
33
34     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
35     lateinit var error: BluePrintError
36
37     var paths: MutableList<String> = arrayListOf()
38
39     override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, serviceTemplate: ServiceTemplate) {
40         log.trace("Validating Service Template..")
41         try {
42             this.bluePrintRuntimeService = bluePrintRuntimeService
43             this.error = bluePrintRuntimeService.getBluePrintError()
44
45             serviceTemplate.metadata?.let { validateMetadata(serviceTemplate.metadata!!) }
46             serviceTemplate.dataTypes?.let { validateDataTypes(serviceTemplate.dataTypes!!) }
47             serviceTemplate.artifactTypes?.let { validateArtifactTypes(serviceTemplate.artifactTypes!!) }
48             serviceTemplate.nodeTypes?.let { validateNodeTypes(serviceTemplate.nodeTypes!!) }
49             serviceTemplate.topologyTemplate?.let { validateTopologyTemplate(serviceTemplate.topologyTemplate!!) }
50         } catch (e: Exception) {
51             log.error("failed in blueprint service template validation", e)
52             error.addError(BluePrintConstants.PATH_SERVICE_TEMPLATE, paths.joinToString(BluePrintConstants.PATH_DIVIDER), e.message!!)
53         }
54     }
55
56     fun validateMetadata(metaDataMap: MutableMap<String, String>) {
57
58         paths.add(BluePrintConstants.PATH_METADATA)
59
60         val templateName = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_NAME]
61         val templateVersion = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_VERSION]
62         val templateTags = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_TAGS]
63         val templateAuthor = metaDataMap[BluePrintConstants.METADATA_TEMPLATE_AUTHOR]
64
65         Preconditions.checkArgument(StringUtils.isNotBlank(templateName), "failed to get template name metadata")
66         Preconditions.checkArgument(StringUtils.isNotBlank(templateVersion), "failed to get template version metadata")
67         Preconditions.checkArgument(StringUtils.isNotBlank(templateTags), "failed to get template tags metadata")
68         Preconditions.checkArgument(StringUtils.isNotBlank(templateAuthor), "failed to get template author metadata")
69
70         paths.removeAt(paths.lastIndex)
71     }
72
73
74     fun validateDataTypes(dataTypes: MutableMap<String, DataType>) {
75
76         paths.add(BluePrintConstants.PATH_DATA_TYPES)
77         dataTypes.forEach { dataTypeName, dataType ->
78             // Validate Single Data Type
79             bluePrintTypeValidatorService.validateDataType(bluePrintRuntimeService, dataTypeName, dataType)
80         }
81         paths.removeAt(paths.lastIndex)
82     }
83
84     fun validateArtifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
85         paths.add(BluePrintConstants.PATH_ARTIFACT_TYPES)
86         artifactTypes.forEach { artifactName, artifactType ->
87             // Validate Single Artifact Type
88             bluePrintTypeValidatorService.validateArtifactType(bluePrintRuntimeService, artifactName, artifactType)
89         }
90         paths.removeAt(paths.lastIndex)
91     }
92
93     fun validateNodeTypes(nodeTypes: MutableMap<String, NodeType>) {
94         paths.add(BluePrintConstants.PATH_NODE_TYPES)
95         nodeTypes.forEach { nodeTypeName, nodeType ->
96             // Validate Single Node Type
97             bluePrintTypeValidatorService.validateNodeType(bluePrintRuntimeService, nodeTypeName, nodeType)
98         }
99         paths.removeAt(paths.lastIndex)
100     }
101
102     fun validateTopologyTemplate(topologyTemplate: TopologyTemplate) {
103         paths.add(BluePrintConstants.PATH_TOPOLOGY_TEMPLATE)
104         bluePrintTypeValidatorService.validateTopologyTemplate(bluePrintRuntimeService, "topologyTemplate", topologyTemplate)
105         paths.removeAt(paths.lastIndex)
106     }
107 }