2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.models.tosca.utils;
23 import java.util.Collection;
24 import java.util.HashSet;
26 import java.util.function.Function;
27 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.onap.policy.models.base.PfConcept;
31 import org.onap.policy.models.base.PfConceptContainer;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfKey;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.base.PfNameVersion;
36 import org.onap.policy.models.base.PfValidationMessage;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
44 * Utility class for TOSCA concepts.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public final class ToscaUtils {
49 private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
52 private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = Set.of(
53 new PfConceptKey("string", PfKey.NULL_KEY_VERSION),
54 new PfConceptKey("integer", PfKey.NULL_KEY_VERSION),
55 new PfConceptKey("float", PfKey.NULL_KEY_VERSION),
56 new PfConceptKey("boolean", PfKey.NULL_KEY_VERSION),
57 new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION),
58 new PfConceptKey("null", PfKey.NULL_KEY_VERSION),
59 new PfConceptKey("list", PfKey.NULL_KEY_VERSION),
60 new PfConceptKey("map", PfKey.NULL_KEY_VERSION),
61 new PfConceptKey("object", PfKey.NULL_KEY_VERSION),
62 new PfConceptKey("scalar-unit.size", PfKey.NULL_KEY_VERSION),
63 new PfConceptKey("scalar-unit.time", PfKey.NULL_KEY_VERSION),
64 new PfConceptKey("scalar-unit.frequency", PfKey.NULL_KEY_VERSION),
65 new PfConceptKey("tosca.datatypes.TimeInterval", PfKey.NULL_KEY_VERSION)
70 * Private constructor to prevent subclassing.
72 private ToscaUtils() {
73 // Private constructor to prevent subclassing
77 * Get the predefined policy types.
79 * @return the predefined policy types
81 public static Collection<PfConceptKey> getPredefinedDataTypes() {
82 return PREDEFINED_TOSCA_DATA_TYPES;
86 * Assert that data types have been specified correctly.
88 * @param serviceTemplate the service template containing data types to be checked
90 public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
91 assertExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
95 * Assert that policy types have been specified correctly.
97 * @param serviceTemplate the service template containing policy types to be checked
99 public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
100 assertExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
104 * Assert that policies have been specified correctly.
106 * @param serviceTemplate the service template containing policy types to be checked
108 public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
109 assertExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
113 * Check that data types have been specified correctly.
115 * @param serviceTemplate the service template containing data types to be checked
117 public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
118 return doExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
122 * Check that policy types have been specified correctly.
124 * @param serviceTemplate the service template containing policy types to be checked
126 public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
127 return doExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
131 * Check that policies have been specified correctly.
133 * @param serviceTemplate the service template containing policy types to be checked
135 public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
137 return doExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
141 * Assert that something have been specified correctly.
143 * @param serviceTemplate the service template containing policy types to be checked
145 public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
146 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
147 String message = checkerFunction.apply(serviceTemplate);
148 if (message != null) {
149 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, message);
154 * Check that something have been specified correctly.
156 * @param serviceTemplate the service template containing policy types to be checked
158 public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
159 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
160 return checkerFunction.apply(serviceTemplate) == null;
164 * Check if data types have been specified correctly.
166 public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
167 if (serviceTemplate.getDataTypes() == null) {
168 return "no data types specified on service template";
171 if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
172 return "list of data types specified on service template is empty";
179 * Check if policy types have been specified correctly.
181 public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
182 if (serviceTemplate.getPolicyTypes() == null) {
183 return "no policy types specified on service template";
186 if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
187 return "list of policy types specified on service template is empty";
194 * Check if policies have been specified correctly.
196 public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
197 if (serviceTemplate.getTopologyTemplate() == null) {
198 return "topology template not specified on service template";
201 if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
202 return "no policies specified on topology template of service template";
205 if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
206 return "list of policies specified on topology template of service template is empty";
213 * getLatestPolicyTypeVersion Find all the ancestors of an entity type.
215 * @param entityTypes the set of entity types that exist
216 * @param entityType the entity type for which to get the parents
217 * @param result the result of the ancestor search with any warnings or errors
218 * @return the entity set containing the ancestors of the incoming entity
220 public static Collection<JpaToscaEntityType<ToscaEntity>> getEntityTypeAncestors(
221 @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
222 @NonNull JpaToscaEntityType<?> entityType, @NonNull final PfValidationResult result) {
224 PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom();
225 if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) {
226 return CollectionUtils.emptyCollection();
229 if (entityType.getKey().equals(parentEntityTypeKey)) {
230 result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class,
231 ValidationResult.INVALID, "entity cannot be an ancestor of itself"));
232 throw new PfModelRuntimeException(Response.Status.CONFLICT, result.toString());
235 @SuppressWarnings("unchecked")
236 Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySet = (Set<JpaToscaEntityType<ToscaEntity>>) entityTypes
237 .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
238 Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySetToReturn = new HashSet<>(ancestorEntitySet);
239 if (ancestorEntitySet.isEmpty()) {
240 result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class,
241 ValidationResult.INVALID, "parent " + parentEntityTypeKey.getId() + " of entity not found"));
243 for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) {
244 ancestorEntitySetToReturn.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result));
247 return ancestorEntitySetToReturn;
251 * Get the entity tree from a concept container for a given entity key.
253 * @param entityTypes the concept container containing entity types
254 * @param entityName the name of the entity
255 * @param entityVersion the version of the entity
257 public static void getEntityTree(
258 @NonNull final PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
259 final String entityName, final String entityVersion) {
261 PfValidationResult result = new PfValidationResult();
263 @SuppressWarnings("unchecked")
264 Set<JpaToscaEntityType<?>> filteredEntitySet =
265 (Set<JpaToscaEntityType<?>>) entityTypes.getAllNamesAndVersions(entityName, entityVersion);
266 Set<JpaToscaEntityType<?>> filteredEntitySetToReturn = new HashSet<>(filteredEntitySet);
267 for (JpaToscaEntityType<?> filteredEntityType : filteredEntitySet) {
268 filteredEntitySetToReturn
269 .addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result));
272 if (!result.isValid()) {
273 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.toString());
276 entityTypes.getConceptMap().entrySet()
277 .removeIf(entityEntry -> !filteredEntitySetToReturn.contains(entityEntry.getValue()));