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.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.cds.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintServiceTemplateValidator
27 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
28 import org.onap.ccsdk.cds.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
33 @Service("default-service-template-validator")
34 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
35 open class BluePrintServiceTemplateValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintServiceTemplateValidator {
37 private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
39 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
40 lateinit var error: BluePrintError
42 var paths: MutableList<String> = arrayListOf()
44 override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, serviceTemplate: ServiceTemplate) {
45 log.trace("Validating Service Template..")
47 this.bluePrintRuntimeService = bluePrintRuntimeService
48 this.error = bluePrintRuntimeService.getBluePrintError()
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!!)
61 fun validateMetadata(metaDataMap: MutableMap<String, String>) {
63 paths.add(BluePrintConstants.PATH_METADATA)
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]
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")
75 paths.removeAt(paths.lastIndex)
79 fun validateDataTypes(dataTypes: MutableMap<String, DataType>) {
81 paths.add(BluePrintConstants.PATH_DATA_TYPES)
82 dataTypes.forEach { dataTypeName, dataType ->
83 // Validate Single Data Type
84 bluePrintTypeValidatorService.validateDataType(bluePrintRuntimeService, dataTypeName, dataType)
86 paths.removeAt(paths.lastIndex)
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)
95 paths.removeAt(paths.lastIndex)
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)
104 paths.removeAt(paths.lastIndex)
107 fun validateTopologyTemplate(topologyTemplate: TopologyTemplate) {
108 paths.add(BluePrintConstants.PATH_TOPOLOGY_TEMPLATE)
109 bluePrintTypeValidatorService.validateTopologyTemplate(bluePrintRuntimeService, "topologyTemplate", topologyTemplate)
110 paths.removeAt(paths.lastIndex)