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.apps.controllerblueprints.core.validation
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintValidationError
24 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyNThrow
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
26 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintNodeTypeValidator
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
31 open class BluePrintNodeTypeValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintNodeTypeValidator {
33 private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
35 var bluePrintContext: BluePrintContext? = null
36 var error: BluePrintValidationError? = null
37 var paths: MutableList<String> = arrayListOf()
39 override fun validate(bluePrintContext: BluePrintContext, error: BluePrintValidationError, nodeTypeName: String, nodeType: NodeType) {
41 this.bluePrintContext = bluePrintContext
43 paths.add(nodeTypeName)
45 val derivedFrom: String = nodeType.derivedFrom
47 checkValidNodeTypesDerivedFrom(nodeTypeName, derivedFrom)
49 if (!BluePrintTypes.rootNodeTypes().contains(derivedFrom)) {
50 bluePrintContext.serviceTemplate.nodeTypes?.get(derivedFrom)
51 ?: throw BluePrintException("Failed to get derivedFrom NodeType($derivedFrom)'s for NodeType($nodeTypeName)")
54 nodeType.properties?.let { bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintContext, error, nodeType.properties!!) }
55 nodeType.capabilities?.let { validateCapabilityDefinitions(nodeTypeName, nodeType) }
56 nodeType.requirements?.let { validateRequirementDefinitions(nodeTypeName, nodeType) }
57 nodeType.interfaces?.let { validateInterfaceDefinitions(nodeType.interfaces!!) }
59 paths.removeAt(paths.lastIndex)
62 fun checkValidNodeTypesDerivedFrom(nodeTypeName: String, derivedFrom: String) {
63 check(BluePrintTypes.validNodeTypeDerivedFroms.contains(derivedFrom)) {
64 throw BluePrintException("Failed to get node type ($nodeTypeName)'s derivedFrom($derivedFrom) definition ")
68 open fun validateCapabilityDefinitions(nodeTypeName: String, nodeType: NodeType) {
69 val capabilities = nodeType.capabilities
70 paths.add("capabilities")
71 capabilities?.forEach { capabilityName, capabilityDefinition ->
72 paths.add(capabilityName)
74 validateCapabilityDefinition(nodeTypeName, nodeType, capabilityName, capabilityDefinition)
76 paths.removeAt(paths.lastIndex)
78 paths.removeAt(paths.lastIndex)
81 open fun validateCapabilityDefinition(nodeTypeName: String, nodeType: NodeType, capabilityName: String,
82 capabilityDefinition: CapabilityDefinition) {
83 val capabilityType = capabilityDefinition.type
84 check(BluePrintTypes.validCapabilityTypes.contains(capabilityType)) {
85 throw BluePrintException("failed to get CapabilityType($capabilityType) for NodeType($nodeTypeName)")
89 open fun validateRequirementDefinitions(nodeName: String, nodeType: NodeType) {
90 paths.add("requirements")
91 val requirements = nodeType.requirements
93 requirements?.forEach { requirementDefinitionName, requirementDefinition ->
94 paths.add(requirementDefinitionName)
95 validateRequirementDefinition(nodeName, nodeType, requirementDefinitionName, requirementDefinition)
96 paths.removeAt(paths.lastIndex)
98 paths.removeAt(paths.lastIndex)
101 open fun validateRequirementDefinition(nodeTypeName: String, nodeType: NodeType, requirementDefinitionName: String,
102 requirementDefinition: RequirementDefinition) {
104 log.info("validating NodeType({}) RequirementDefinition ({}) ", nodeTypeName, requirementDefinitionName)
105 val requirementNodeTypeName = requirementDefinition.node!!
106 val capabilityName = requirementDefinition.capability
107 val relationship = requirementDefinition.relationship!!
109 check(BluePrintTypes.validRelationShipDerivedFroms.contains(relationship)) {
110 throw BluePrintException("failed to get relationship($relationship) for NodeType($nodeTypeName)'s requirement($requirementDefinitionName)")
113 val relationShipNodeType = bluePrintContext!!.serviceTemplate.nodeTypes?.get(requirementNodeTypeName)
114 ?: throw BluePrintException("failed to get requirement NodeType($requirementNodeTypeName)'s for requirement($requirementDefinitionName) ")
116 relationShipNodeType.capabilities?.get(capabilityName)
117 ?: throw BluePrintException("failed to get requirement NodeType($requirementNodeTypeName)'s " +
118 "capability($nodeTypeName) for NodeType ($capabilityName)'s requirement($requirementDefinitionName) ")
122 open fun validateInterfaceDefinitions(interfaces: MutableMap<String, InterfaceDefinition>) {
123 paths.add("interfaces")
124 interfaces.forEach { interfaceName, interfaceDefinition ->
125 paths.add(interfaceName)
126 interfaceDefinition.operations?.let { validateOperationDefinitions(interfaceDefinition.operations!!) }
127 paths.removeAt(paths.lastIndex)
129 paths.removeAt(paths.lastIndex)
132 open fun validateOperationDefinitions(operations: MutableMap<String, OperationDefinition>) {
133 paths.add("operations")
134 operations.forEach { opertaionName, operationDefinition ->
135 paths.add(opertaionName)
136 operationDefinition.implementation?.let { validateImplementation(operationDefinition.implementation!!) }
138 operationDefinition.inputs?.let {
139 bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintContext!!, error!!, operationDefinition.inputs!!)
142 operationDefinition.outputs?.let {
143 bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintContext!!, error!!, operationDefinition.outputs!!)
145 paths.removeAt(paths.lastIndex)
147 paths.removeAt(paths.lastIndex)
150 open fun validateImplementation(implementation: Implementation) {
151 checkNotEmptyNThrow(implementation.primary)