2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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 java.util.Collection;
25 import java.util.HashSet;
27 import java.util.function.Function;
28 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.onap.policy.common.parameters.BeanValidationResult;
32 import org.onap.policy.common.parameters.ObjectValidationResult;
33 import org.onap.policy.common.parameters.ValidationStatus;
34 import org.onap.policy.models.base.PfConcept;
35 import org.onap.policy.models.base.PfConceptContainer;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.onap.policy.models.base.PfNameVersion;
40 import org.onap.policy.models.base.Validated;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
46 * Utility class for TOSCA concepts.
48 * @author Liam Fallon (liam.fallon@est.tech)
50 public final class ToscaUtils {
51 private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
54 private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = Set.of(
55 new PfConceptKey("string", PfKey.NULL_KEY_VERSION),
56 new PfConceptKey("integer", PfKey.NULL_KEY_VERSION),
57 new PfConceptKey("float", PfKey.NULL_KEY_VERSION),
58 new PfConceptKey("boolean", PfKey.NULL_KEY_VERSION),
59 new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION),
60 new PfConceptKey("null", PfKey.NULL_KEY_VERSION),
61 new PfConceptKey("list", PfKey.NULL_KEY_VERSION),
62 new PfConceptKey("map", PfKey.NULL_KEY_VERSION),
63 new PfConceptKey("object", PfKey.NULL_KEY_VERSION),
64 new PfConceptKey("scalar-unit.size", PfKey.NULL_KEY_VERSION),
65 new PfConceptKey("scalar-unit.time", PfKey.NULL_KEY_VERSION),
66 new PfConceptKey("scalar-unit.frequency", PfKey.NULL_KEY_VERSION),
67 new PfConceptKey("tosca.datatypes.TimeInterval", PfKey.NULL_KEY_VERSION)
72 * Private constructor to prevent subclassing.
74 private ToscaUtils() {
75 // Private constructor to prevent subclassing
79 * Get the predefined policy types.
81 * @return the predefined policy types
83 public static Collection<PfConceptKey> getPredefinedDataTypes() {
84 return PREDEFINED_TOSCA_DATA_TYPES;
88 * Assert that data types have been specified correctly.
90 * @param serviceTemplate the service template containing data types to be checked
92 public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
93 assertExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
97 * Assert that policy types have been specified correctly.
99 * @param serviceTemplate the service template containing policy types to be checked
101 public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
102 assertExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
106 * Assert that policies have been specified correctly.
108 * @param serviceTemplate the service template containing policy types to be checked
110 public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
111 assertExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
115 * Check that data types have been specified correctly.
117 * @param serviceTemplate the service template containing data types to be checked
119 public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
120 return doExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
124 * Check that policy types have been specified correctly.
126 * @param serviceTemplate the service template containing policy types to be checked
128 public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
129 return doExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
133 * Check that policies have been specified correctly.
135 * @param serviceTemplate the service template containing policy types to be checked
137 public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
139 return doExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
143 * Assert that something have been specified correctly.
145 * @param serviceTemplate the service template containing policy types to be checked
147 public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
148 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
149 String message = checkerFunction.apply(serviceTemplate);
150 if (message != null) {
151 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, message);
156 * Check that something have been specified correctly.
158 * @param serviceTemplate the service template containing policy types to be checked
160 public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
161 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
162 return checkerFunction.apply(serviceTemplate) == null;
166 * Check if data types have been specified correctly.
168 public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
169 if (serviceTemplate.getDataTypes() == null) {
170 return "no data types specified on service template";
173 if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
174 return "list of data types specified on service template is empty";
181 * Check if policy types have been specified correctly.
183 public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
184 if (serviceTemplate.getPolicyTypes() == null) {
185 return "no policy types specified on service template";
188 if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
189 return "list of policy types specified on service template is empty";
196 * Check if policies have been specified correctly.
198 public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
199 if (serviceTemplate.getTopologyTemplate() == null) {
200 return "topology template not specified on service template";
203 if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
204 return "no policies specified on topology template of service template";
207 if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
208 return "list of policies specified on topology template of service template is empty";
215 * getLatestPolicyTypeVersion Find all the ancestors of an entity type.
217 * @param entityTypes the set of entity types that exist
218 * @param entityType the entity type for which to get the parents
219 * @param result the result of the ancestor search with any warnings or errors
220 * @return the entity set containing the ancestors of the incoming entity
222 public static Collection<JpaToscaEntityType<ToscaEntity>> getEntityTypeAncestors(
223 @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
224 @NonNull JpaToscaEntityType<?> entityType, @NonNull final BeanValidationResult result) {
226 PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom();
227 if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) {
228 return CollectionUtils.emptyCollection();
231 if (entityType.getKey().equals(parentEntityTypeKey)) {
232 result.addResult("entity type", entityType.getKey().getId(),
233 ValidationStatus.INVALID, "ancestor of itself");
234 throw new PfModelRuntimeException(Response.Status.CONFLICT, result.getResult());
237 @SuppressWarnings("unchecked")
238 Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySet = (Set<JpaToscaEntityType<ToscaEntity>>) entityTypes
239 .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
240 Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySetToReturn = new HashSet<>(ancestorEntitySet);
241 if (ancestorEntitySet.isEmpty()) {
242 result.addResult("parent", parentEntityTypeKey.getId(), ValidationStatus.INVALID, Validated.NOT_FOUND);
244 for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) {
245 ancestorEntitySetToReturn.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result));
248 return ancestorEntitySetToReturn;
252 * Get the entity tree from a concept container for a given entity key.
254 * @param entityTypes the concept container containing entity types
255 * @param entityName the name of the entity
256 * @param entityVersion the version of the entity
258 public static void getEntityTree(
259 @NonNull final PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
260 final String entityName, final String entityVersion) {
262 BeanValidationResult result = new BeanValidationResult("entity", entityName);
264 @SuppressWarnings("unchecked")
265 Set<JpaToscaEntityType<?>> filteredEntitySet =
266 (Set<JpaToscaEntityType<?>>) entityTypes.getAllNamesAndVersions(entityName, entityVersion);
267 Set<JpaToscaEntityType<?>> filteredEntitySetToReturn = new HashSet<>(filteredEntitySet);
268 for (JpaToscaEntityType<?> filteredEntityType : filteredEntitySet) {
269 filteredEntitySetToReturn
270 .addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result));
273 if (!result.isValid()) {
274 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.getResult());
277 entityTypes.getConceptMap().entrySet()
278 .removeIf(entityEntry -> !filteredEntitySetToReturn.contains(entityEntry.getValue()));