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