2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2023 Nordix Foundation.
4 * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.utils;
24 import jakarta.ws.rs.core.Response;
25 import java.util.Collection;
26 import java.util.HashSet;
28 import java.util.function.Function;
29 import lombok.AccessLevel;
30 import lombok.NoArgsConstructor;
31 import lombok.NonNull;
32 import org.apache.commons.collections4.CollectionUtils;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.common.parameters.ValidationStatus;
35 import org.onap.policy.models.base.PfConcept;
36 import org.onap.policy.models.base.PfConceptContainer;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.base.PfNameVersion;
41 import org.onap.policy.models.base.Validated;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
44 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
47 * Utility class for TOSCA concepts.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 @NoArgsConstructor(access = AccessLevel.PRIVATE)
52 public final class ToscaUtils {
53 private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
56 private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = Set.of(
57 new PfConceptKey("string", PfKey.NULL_KEY_VERSION),
58 new PfConceptKey("integer", PfKey.NULL_KEY_VERSION),
59 new PfConceptKey("float", PfKey.NULL_KEY_VERSION),
60 new PfConceptKey("boolean", PfKey.NULL_KEY_VERSION),
61 new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION),
62 new PfConceptKey("null", PfKey.NULL_KEY_VERSION),
63 new PfConceptKey("list", PfKey.NULL_KEY_VERSION),
64 new PfConceptKey("map", PfKey.NULL_KEY_VERSION),
65 new PfConceptKey("object", PfKey.NULL_KEY_VERSION),
66 new PfConceptKey("scalar-unit.size", PfKey.NULL_KEY_VERSION),
67 new PfConceptKey("scalar-unit.time", PfKey.NULL_KEY_VERSION),
68 new PfConceptKey("scalar-unit.frequency", PfKey.NULL_KEY_VERSION),
69 new PfConceptKey("tosca.datatypes.TimeInterval", PfKey.NULL_KEY_VERSION)
74 * Get the predefined policy types.
76 * @return the predefined policy types
78 public static Collection<PfConceptKey> getPredefinedDataTypes() {
79 return PREDEFINED_TOSCA_DATA_TYPES;
83 * Assert that data types have been specified correctly.
85 * @param serviceTemplate the service template containing data types to be checked
87 public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
88 assertExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
92 * Assert that policy types have been specified correctly.
94 * @param serviceTemplate the service template containing policy types to be checked
96 public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
97 assertExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
101 * Assert that policies have been specified correctly.
103 * @param serviceTemplate the service template containing policy types to be checked
105 public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
106 assertExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
110 * Assert that node templates have been specified correctly.
112 * @param serviceTemplate the service template containing node templates to be checked
114 public static void assertNodeTemplatesExist(final JpaToscaServiceTemplate serviceTemplate) {
115 assertExist(serviceTemplate, ToscaUtils::checkNodeTemplateExist);
119 * Check that data types have been specified correctly.
121 * @param serviceTemplate the service template containing data types to be checked
123 public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
124 return doExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
128 * Check that policy types have been specified correctly.
130 * @param serviceTemplate the service template containing policy types to be checked
132 public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
133 return doExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
137 * Check that policies have been specified correctly.
139 * @param serviceTemplate the service template containing policy types to be checked
141 public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
143 return doExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
147 * Check that tosca node templates have been specified correctly.
149 * @param serviceTemplate the service template containing node templates to be checked
151 public static boolean doNodeTemplatesExist(final JpaToscaServiceTemplate serviceTemplate) {
153 return doExist(serviceTemplate, ToscaUtils::checkNodeTemplateExist);
157 * Assert that something have been specified correctly.
159 * @param serviceTemplate the service template containing policy types to be checked
161 public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
162 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
163 String message = checkerFunction.apply(serviceTemplate);
164 if (message != null) {
165 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, message);
170 * Check that something have been specified correctly.
172 * @param serviceTemplate the service template containing policy types to be checked
174 public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
175 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
176 return checkerFunction.apply(serviceTemplate) == null;
180 * Check if data types have been specified correctly.
182 public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
183 if (serviceTemplate.getDataTypes() == null) {
184 return "no data types specified on service template";
187 if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
188 return "list of data types specified on service template is empty";
195 * Check if policy types have been specified correctly.
197 public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
198 if (serviceTemplate.getPolicyTypes() == null) {
199 return "no policy types specified on service template";
202 if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
203 return "list of policy types specified on service template is empty";
210 * Check if policies have been specified correctly.
212 public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
213 if (serviceTemplate.getTopologyTemplate() == null) {
214 return "topology template not specified on service template";
217 if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
218 return "no policies specified on topology template of service template";
221 if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
222 return "list of policies specified on topology template of service template is empty";
229 * Check if node templates have been specified correctly.
231 public static String checkNodeTemplateExist(final JpaToscaServiceTemplate serviceTemplate) {
232 if (serviceTemplate.getTopologyTemplate().getNodeTemplates() == null) {
233 return "node templates not present on the service template";
236 if (serviceTemplate.getTopologyTemplate().getNodeTemplates().getConceptMap().isEmpty()) {
237 return "no parameters present on the node templates";
243 * getLatestPolicyTypeVersion Find all the ancestors of an entity type.
245 * @param entityTypes the set of entity types that exist
246 * @param entityType the entity type for which to get the parents
247 * @param result the result of the ancestor search with any warnings or errors
248 * @return the entity set containing the ancestors of the incoming entity
250 public static Collection<JpaToscaEntityType<ToscaEntity>> getEntityTypeAncestors(
251 @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
252 @NonNull JpaToscaEntityType<?> entityType, @NonNull final BeanValidationResult result) {
254 PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom();
255 if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) {
256 return CollectionUtils.emptyCollection();
259 if (entityType.getKey().equals(parentEntityTypeKey)) {
260 result.addResult("entity type", entityType.getKey().getId(),
261 ValidationStatus.INVALID, "ancestor of itself");
262 throw new PfModelRuntimeException(Response.Status.CONFLICT, result.getResult());
265 @SuppressWarnings("unchecked")
266 Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySet = (Set<JpaToscaEntityType<ToscaEntity>>) entityTypes
267 .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
268 Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySetToReturn = new HashSet<>(ancestorEntitySet);
269 if (ancestorEntitySet.isEmpty()) {
270 result.addResult("parent", parentEntityTypeKey.getId(), ValidationStatus.INVALID, Validated.NOT_FOUND);
272 for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) {
273 ancestorEntitySetToReturn.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result));
276 return ancestorEntitySetToReturn;
280 * Get the entity tree from a concept container for a given entity key.
282 * @param entityTypes the concept container containing entity types
283 * @param entityName the name of the entity
284 * @param entityVersion the version of the entity
286 public static void getEntityTree(
287 @NonNull final PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
288 final String entityName, final String entityVersion) {
290 var result = new BeanValidationResult("entity", entityName);
292 @SuppressWarnings("unchecked")
293 Set<JpaToscaEntityType<?>> filteredEntitySet =
294 (Set<JpaToscaEntityType<?>>) entityTypes.getAllNamesAndVersions(entityName, entityVersion);
295 Set<JpaToscaEntityType<?>> filteredEntitySetToReturn = new HashSet<>(filteredEntitySet);
296 for (JpaToscaEntityType<?> filteredEntityType : filteredEntitySet) {
297 filteredEntitySetToReturn
298 .addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result));
301 if (!result.isValid()) {
302 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.getResult());
305 entityTypes.getConceptMap().entrySet()
306 .removeIf(entityEntry -> !filteredEntitySetToReturn.contains(entityEntry.getValue()));