2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.validation
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
36 @Service("default-service-template-validator")
37 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
38 open class BlueprintServiceTemplateValidatorImpl(private val bluePrintTypeValidatorService: BlueprintTypeValidatorService) :
39 BlueprintServiceTemplateValidator {
41 private val log = LoggerFactory.getLogger(BlueprintServiceTemplateValidatorImpl::class.toString())
43 lateinit var bluePrintRuntimeService: BlueprintRuntimeService<*>
44 lateinit var error: BlueprintError
46 var paths: MutableList<String> = arrayListOf()
48 override fun validate(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, serviceTemplate: ServiceTemplate) {
49 log.trace("Validating Service Template..")
51 this.bluePrintRuntimeService = bluePrintRuntimeService
52 this.error = bluePrintRuntimeService.getBlueprintError()
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")
65 fun validateMetadata(metaDataMap: MutableMap<String, String>) {
67 paths.add(BlueprintConstants.PATH_METADATA)
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]
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")
79 paths.removeAt(paths.lastIndex)
82 fun validateDataTypes(dataTypes: MutableMap<String, DataType>) {
84 paths.add(BlueprintConstants.PATH_DATA_TYPES)
85 dataTypes.forEach { dataTypeName, dataType ->
86 // Validate Single Data Type
87 bluePrintTypeValidatorService.validateDataType(bluePrintRuntimeService, dataTypeName, dataType)
89 paths.removeAt(paths.lastIndex)
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)
98 paths.removeAt(paths.lastIndex)
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)
107 paths.removeAt(paths.lastIndex)
110 fun validateTopologyTemplate(topologyTemplate: TopologyTemplate) {
111 paths.add(BlueprintConstants.PATH_TOPOLOGY_TEMPLATE)
112 bluePrintTypeValidatorService.validateTopologyTemplate(bluePrintRuntimeService, "topologyTemplate", topologyTemplate)
113 paths.removeAt(paths.lastIndex)