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.function.Function;
25 import javax.ws.rs.core.Response;
27 import org.onap.policy.models.base.PfModelRuntimeException;
28 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Utility class for TOSCA concepts.
35 * @author Liam Fallon (liam.fallon@est.tech)
37 public final class ToscaUtils {
38 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class);
41 * Private constructor to prevent subclassing.
43 private ToscaUtils() {
44 // Private constructor to prevent subclassing
48 * Assert that data types have been specified correctly.
50 * @param serviceTemplate the service template containing data types to be checked
52 public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
53 assertExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
57 * Assert that policy types have been specified correctly.
59 * @param serviceTemplate the service template containing policy types to be checked
61 public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
62 assertExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
66 * Assert that policies have been specified correctly.
68 * @param serviceTemplate the service template containing policy types to be checked
70 public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
71 assertExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
75 * Check that data types have been specified correctly.
77 * @param serviceTemplate the service template containing data types to be checked
79 public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
80 return doExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
84 * Check that policy types have been specified correctly.
86 * @param serviceTemplate the service template containing policy types to be checked
88 public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
89 return doExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
93 * Check that policies have been specified correctly.
95 * @param serviceTemplate the service template containing policy types to be checked
97 public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
99 return doExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
103 * Assert that something have been specified correctly.
105 * @param serviceTemplate the service template containing policy types to be checked
107 public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
108 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
109 String message = checkerFunction.apply(serviceTemplate);
110 if (message != null) {
111 LOGGER.warn(message);
112 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
117 * Check that something have been specified correctly.
119 * @param serviceTemplate the service template containing policy types to be checked
121 public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
122 final Function<JpaToscaServiceTemplate, String> checkerFunction) {
123 return checkerFunction.apply(serviceTemplate) == null;
127 * Check if data types have been specified correctly.
129 public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
130 if (serviceTemplate.getDataTypes() == null) {
131 return "no data types specified on service template";
134 if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
135 return "list of data types specified on service template is empty";
142 * Check if policy types have been specified correctly.
144 public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
145 if (serviceTemplate.getPolicyTypes() == null) {
146 return "no policy types specified on service template";
149 if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
150 return "list of policy types specified on service template is empty";
157 * Check if policies have been specified correctly.
159 public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
160 if (serviceTemplate.getTopologyTemplate() == null) {
161 return "topology template not specified on service template";
164 if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
165 return "no policies specified on topology template of service template";
168 if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
169 return "list of policies specified on topology template of service template is empty";