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