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;
28 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
32 import org.apache.commons.collections4.CollectionUtils;
33 import org.onap.policy.models.base.PfConcept;
34 import org.onap.policy.models.base.PfConceptContainer;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfKey;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.base.PfNameVersion;
39 import org.onap.policy.models.base.PfValidationMessage;
40 import org.onap.policy.models.base.PfValidationResult;
41 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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 public final class ToscaUtils {
52 private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
55 private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = Set.of(
56 new PfConceptKey("string", PfKey.NULL_KEY_VERSION),
57 new PfConceptKey("integer", PfKey.NULL_KEY_VERSION),
58 new PfConceptKey("float", PfKey.NULL_KEY_VERSION),
59 new PfConceptKey("boolean", PfKey.NULL_KEY_VERSION),
60 new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION),
61 new PfConceptKey("null", PfKey.NULL_KEY_VERSION),
62 new PfConceptKey("list", PfKey.NULL_KEY_VERSION),
63 new PfConceptKey("map", 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 PfValidationResult 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.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class,
233 ValidationResult.INVALID, "entity cannot be an ancestor of itself"));
234 throw new PfModelRuntimeException(Response.Status.CONFLICT, result.toString());
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.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class,
243 ValidationResult.INVALID, "parent " + parentEntityTypeKey.getId() + " of entity not found"));
245 for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) {
246 ancestorEntitySetToReturn.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result));
249 return ancestorEntitySetToReturn;
253 * Get the entity tree from a concept container for a given entity key.
255 * @param entityTypes the concept container containing entity types
256 * @param entityName the name of the entity
257 * @param entityVersion the version of the entity
259 public static void getEntityTree(
260 @NonNull final PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
261 final String entityName, final String entityVersion) {
263 PfValidationResult result = new PfValidationResult();
265 @SuppressWarnings("unchecked")
266 Set<JpaToscaEntityType<?>> filteredEntitySet =
267 (Set<JpaToscaEntityType<?>>) entityTypes.getAllNamesAndVersions(entityName, entityVersion);
268 Set<JpaToscaEntityType<?>> filteredEntitySetToReturn = new HashSet<>(filteredEntitySet);
269 for (JpaToscaEntityType<?> filteredEntityType : filteredEntitySet) {
270 filteredEntitySetToReturn
271 .addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result));
274 if (!result.isValid()) {
275 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.toString());
278 entityTypes.getConceptMap().entrySet()
279 .removeIf(entityEntry -> !filteredEntitySetToReturn.contains(entityEntry.getValue()));