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.LinkedHashSet;
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.simple.concepts.JpaToscaEntityType;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Utility class for TOSCA concepts.
50 * @author Liam Fallon (liam.fallon@est.tech)
52 public final class ToscaUtils {
53 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class);
55 private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
57 private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = new LinkedHashSet<>();
61 PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
62 PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("integer", PfKey.NULL_KEY_VERSION));
63 PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("float", PfKey.NULL_KEY_VERSION));
64 PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("boolean", PfKey.NULL_KEY_VERSION));
65 PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION));
66 PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("null", PfKey.NULL_KEY_VERSION));
67 PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("list", PfKey.NULL_KEY_VERSION));
68 PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("map", PfKey.NULL_KEY_VERSION));
73 * Private constructor to prevent subclassing.
75 private ToscaUtils() {
76 // Private constructor to prevent subclassing
80 * Assert that data types have been specified correctly.
82 * @param serviceTemplate the service template containing data types to be checked
84 public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
85 assertExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
89 * Assert that policy types have been specified correctly.
91 * @param serviceTemplate the service template containing policy types to be checked
93 public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
94 assertExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
98 * Assert that policies have been specified correctly.
100 * @param serviceTemplate the service template containing policy types to be checked
102 public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
103 assertExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
107 * Check that data types have been specified correctly.
109 * @param serviceTemplate the service template containing data types to be checked
111 public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
112 return doExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
116 * Check that policy types have been specified correctly.
118 * @param serviceTemplate the service template containing policy types to be checked
120 public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
121 return doExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
125 * Check that policies have been specified correctly.
127 * @param serviceTemplate the service template containing policy types to be checked
129 public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
131 return doExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
135 * Assert that something have been specified correctly.
137 * @param serviceTemplate the service template containing policy types to be checked
139 public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
140 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
141 String message = checkerFunction.apply(serviceTemplate);
142 if (message != null) {
143 LOGGER.warn(message);
144 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
149 * Check that something have been specified correctly.
151 * @param serviceTemplate the service template containing policy types to be checked
153 public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
154 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
155 return checkerFunction.apply(serviceTemplate) == null;
159 * Check if data types have been specified correctly.
161 public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
162 if (serviceTemplate.getDataTypes() == null) {
163 return "no data types specified on service template";
166 if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
167 return "list of data types specified on service template is empty";
174 * Check if policy types have been specified correctly.
176 public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
177 if (serviceTemplate.getPolicyTypes() == null) {
178 return "no policy types specified on service template";
181 if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
182 return "list of policy types specified on service template is empty";
189 * Check if policies have been specified correctly.
191 public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
192 if (serviceTemplate.getTopologyTemplate() == null) {
193 return "topology template not specified on service template";
196 if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
197 return "no policies specified on topology template of service template";
200 if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
201 return "list of policies specified on topology template of service template is empty";
208 * Find all the ancestors of an entity type.
210 * @param entityTypes the set of entity types that exist
211 * @param entityType the entity type for which to get the parents
212 * @param result the result of the ancestor search with any warnings or errors
215 public static Collection<? extends JpaToscaEntityType<?>> getEntityTypeAncestors(
216 @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
217 @NonNull JpaToscaEntityType<?> entityType, @NonNull final PfValidationResult result) {
219 PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom();
220 if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) {
221 return CollectionUtils.emptyCollection();
224 @SuppressWarnings("unchecked")
225 Set<JpaToscaEntityType<?>> ancestorEntitySet = (Set<JpaToscaEntityType<?>>) entityTypes
226 .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
228 if (ancestorEntitySet.isEmpty()) {
229 result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class,
230 ValidationResult.INVALID, "parent " + parentEntityTypeKey.getId() + " of entity not found"));
232 for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) {
233 ancestorEntitySet.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result));
236 return ancestorEntitySet;
240 * Get the predefined policy types.
242 * @return the predefined policy types
244 public static Collection<?> getPredefinedDataTypes() {
245 return PREDEFINED_TOSCA_DATA_TYPES;