2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.models.acm.document.base;
23 import java.util.Collection;
24 import java.util.List;
27 import lombok.AccessLevel;
28 import lombok.NoArgsConstructor;
29 import lombok.NonNull;
30 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaEntity;
31 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
32 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaTopologyTemplate;
33 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
34 import org.onap.policy.common.parameters.BeanValidationResult;
35 import org.onap.policy.common.parameters.ValidationStatus;
36 import org.onap.policy.models.base.Validated;
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public final class ToscaServiceTemplateValidation {
41 private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
42 private static final String AC_NODE_TYPE_NOT_PRESENT =
43 "NodeTemplate with type " + AcmUtils.AUTOMATION_COMPOSITION_NODE_TYPE + " must exist!";
46 * validate a serviceTemplate.
48 * @param result the result
49 * @param serviceTemplate the serviceTemplate to validate
51 public static void validate(final BeanValidationResult result, DocToscaServiceTemplate serviceTemplate) {
53 var references = DocUtil.getToscaReferences(serviceTemplate);
55 validEntityTypeAncestors(serviceTemplate.getDataTypes(), references.get(DocUtil.REF_DATA_TYPES), result);
56 validEntityTypeAncestors(serviceTemplate.getCapabilityTypes(), references.get(DocUtil.REF_CAPABILITY_TYPES),
58 validEntityTypeAncestors(serviceTemplate.getNodeTypes(), references.get(DocUtil.REF_NODE_TYPES), result);
59 validEntityTypeAncestors(serviceTemplate.getRelationshipTypes(), references.get(DocUtil.REF_RELATIONSHIP_TYPES),
61 validEntityTypeAncestors(serviceTemplate.getPolicyTypes(), references.get(DocUtil.REF_POLICY_TYPES), result);
63 if (serviceTemplate.getNodeTypes() != null) {
64 for (var nodeType : serviceTemplate.getNodeTypes().values()) {
65 validEntityTypeAncestors(nodeType.getRequirements(), references.get(DocUtil.REF_REQUIREMENTS), result);
69 validateToscaTopologyTemplate(result, serviceTemplate.getToscaTopologyTemplate());
71 if (serviceTemplate.getToscaTopologyTemplate() != null) {
72 validEntityTypeAncestors(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates(),
73 references.get(DocUtil.REF_NODE_TEMPLATES), result);
74 validEntityTypeAncestors(serviceTemplate.getToscaTopologyTemplate().getPolicies(),
75 references.get(DocUtil.REF_POLICIES), result);
77 if (serviceTemplate.getToscaTopologyTemplate().getNodeTemplates() != null) {
78 for (var nodeTemplate : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().values()) {
79 validEntityTypeAncestors(nodeTemplate.getCapabilities(), references.get(DocUtil.REF_CAPABILITIES),
81 validEntityTypeAncestors(nodeTemplate.getRequirements(), references.get(DocUtil.REF_REQUIREMENTS),
87 validateReferencedDataTypes(result, serviceTemplate, references);
89 validatePolicyTypesInPolicies(result, serviceTemplate, references);
94 * Validate ToscaTopologyTemplate.
98 * @param topologyTemplate the ToscaServiceTemplate
100 public static void validateToscaTopologyTemplate(BeanValidationResult result,
101 DocToscaTopologyTemplate topologyTemplate) {
102 if (topologyTemplate != null && topologyTemplate.getNodeTemplates() != null) {
103 var nodeTemplates = topologyTemplate.getNodeTemplates();
104 var acNumber = nodeTemplates.values().stream().filter(
105 nodeTemplate -> AcmUtils.AUTOMATION_COMPOSITION_NODE_TYPE.equals(nodeTemplate.getType()))
108 result.addResult("TopologyTemplate", nodeTemplates, ValidationStatus.INVALID, AC_NODE_TYPE_NOT_PRESENT);
111 result.addResult("TopologyTemplate", nodeTemplates, ValidationStatus.INVALID, "NodeTemplate with type "
112 + AcmUtils.AUTOMATION_COMPOSITION_NODE_TYPE + " not allowed to be more than one!");
115 result.addResult("TopologyTemplate", topologyTemplate, ValidationStatus.INVALID, AC_NODE_TYPE_NOT_PRESENT);
120 * Validate that all data types referenced in policy types exist.
122 * @param result where the results are added
124 private static void validateReferencedDataTypes(final BeanValidationResult result,
125 DocToscaServiceTemplate serviceTemplate, Map<String, Set<String>> references) {
126 if (serviceTemplate.getDataTypes() != null) {
127 for (var dataType : serviceTemplate.getDataTypes().values()) {
128 validateReferencedDataTypesExists(result, dataType.getReferencedDataTypes(), references);
132 if (serviceTemplate.getPolicyTypes() != null) {
133 for (var policyType : serviceTemplate.getPolicyTypes().values()) {
134 validateReferencedDataTypesExists(result, policyType.getReferencedDataTypes(), references);
137 if (serviceTemplate.getNodeTypes() != null) {
138 for (var nodeType : serviceTemplate.getNodeTypes().values()) {
139 validateReferencedDataTypesExists(result, nodeType.getReferencedDataTypes(), references);
145 * Validate that the referenced data types exist for a collection of data type keys.
147 * @param dataTypeKeyCollection the data type key collection
148 * @param result where the results are added
150 private static void validateReferencedDataTypesExists(final BeanValidationResult result,
151 final Collection<DocConceptKey> dataTypeKeyCollection, Map<String, Set<String>> references) {
152 for (DocConceptKey dataTypeKey : dataTypeKeyCollection) {
153 if (!isTypePresent(dataTypeKey, references.get(DocUtil.REF_DATA_TYPES))) {
154 result.addResult("data type", dataTypeKey.getId(), ValidationStatus.INVALID, Validated.NOT_FOUND);
160 * Validate that all policy types referenced in policies exist.
162 * @param result where the results are added
164 private static void validatePolicyTypesInPolicies(final BeanValidationResult result,
165 DocToscaServiceTemplate serviceTemplate, Map<String, Set<String>> references) {
166 if (serviceTemplate.getToscaTopologyTemplate() == null) {
170 if (serviceTemplate.getToscaTopologyTemplate().getPolicies() != null) {
171 for (var policy : serviceTemplate.getToscaTopologyTemplate().getPolicies().values()) {
172 var key = policy.getTypeDocConceptKey();
173 if (!isTypePresent(key, references.get(DocUtil.REF_POLICY_TYPES))) {
174 result.addResult("policy type", key, ValidationStatus.INVALID, Validated.NOT_FOUND);
178 if (serviceTemplate.getToscaTopologyTemplate().getNodeTemplates() != null) {
179 for (var nodeTemplate : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().values()) {
180 var key = nodeTemplate.getTypeDocConceptKey();
181 if (!isTypePresent(key, references.get(DocUtil.REF_NODE_TYPES))) {
182 result.addResult("node Template", key, ValidationStatus.INVALID, Validated.NOT_FOUND);
188 private static boolean isTypePresent(String key, Set<String> reference) {
189 if (reference == null || reference.isEmpty()) {
192 return reference.contains(key);
195 private static boolean isTypePresent(DocConceptKey key, Set<String> reference) {
196 if (reference == null || reference.isEmpty()) {
199 return reference.contains(key.getId());
202 private static String extractDerivedFrom(DocToscaEntity<?> entityType, final BeanValidationResult result) {
203 if (entityType.getDerivedFrom() == null) {
206 var parentEntityTypeKey = entityType.getDerivedFrom();
208 if (parentEntityTypeKey.endsWith(ROOT_KEY_NAME_SUFFIX)) {
211 if (entityType.getName().equals(parentEntityTypeKey)) {
212 result.addResult("entity type", entityType.getDocConceptKey().getId(), ValidationStatus.INVALID,
213 "ancestor of itself");
216 return parentEntityTypeKey;
220 * validate all the ancestors of an entity type.
222 * @param entityTypes the set of entity types that exist
223 * @param result the result of the ancestor search with any warnings or errors
225 private static void validEntityTypeAncestors(Map<String, ? extends DocToscaEntity<?>> entityTypes,
226 Set<String> reference, @NonNull final BeanValidationResult result) {
227 if (entityTypes != null) {
228 for (var entityType : entityTypes.values()) {
229 var parentEntityTypeKey = extractDerivedFrom(entityType, result);
230 if (parentEntityTypeKey == null) {
233 if (!isTypePresent(parentEntityTypeKey, reference)) {
234 result.addResult("parent", parentEntityTypeKey, ValidationStatus.INVALID, Validated.NOT_FOUND);
241 * validate all the ancestors of an entity type.
243 * @param entityTypesList the set of entity types that exist
244 * @param result the result of the ancestor search with any warnings or errors
246 private static <T extends DocToscaEntity<?>> void validEntityTypeAncestors(List<Map<String, T>> entityTypesList,
247 Set<String> reference, @NonNull final BeanValidationResult result) {
248 if (entityTypesList != null) {
249 for (var entityTypes : entityTypesList) {
250 for (var entityType : entityTypes.values()) {
251 var parentEntityTypeKey = extractDerivedFrom(entityType, result);
252 if (parentEntityTypeKey == null) {
255 if (!isTypePresent(parentEntityTypeKey, reference)) {
256 result.addResult("parent", parentEntityTypeKey, ValidationStatus.INVALID, Validated.NOT_FOUND);