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 javax.ws.rs.core.Response;
25 import org.onap.policy.models.base.PfModelRuntimeException;
26 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Utility class for TOSCA concepts.
33 * @author Liam Fallon (liam.fallon@est.tech)
35 public final class ToscaUtils {
36 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class);
39 * Private constructor to prevent subclassing.
41 private ToscaUtils() {
42 // Private constructor to prevent subclassing
46 * Assert that data types have been specified correctly.
48 * @param serviceTemplate the service template containing data types to be checked
50 public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
51 assertExist(serviceTemplate, checkDataTypesExist());
55 * Assert that policy types have been specified correctly.
57 * @param serviceTemplate the service template containing policy types to be checked
59 public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
60 assertExist(serviceTemplate, checkPolicyTypesExist());
64 * Assert that policies have been specified correctly.
66 * @param serviceTemplate the service template containing policy types to be checked
68 public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
69 assertExist(serviceTemplate, checkPoliciesExist());
73 * Check that data types have been specified correctly.
75 * @param serviceTemplate the service template containing data types to be checked
77 public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
78 return doExist(serviceTemplate, checkDataTypesExist());
82 * Check that policy types have been specified correctly.
84 * @param serviceTemplate the service template containing policy types to be checked
86 public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
87 return doExist(serviceTemplate, checkPolicyTypesExist());
91 * Check that policies have been specified correctly.
93 * @param serviceTemplate the service template containing policy types to be checked
95 public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
96 return doExist(serviceTemplate, checkPoliciesExist());
100 * Assert that something have been specified correctly.
102 * @param serviceTemplate the service template containing policy types to be checked
104 public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
105 final ToscaChecker<JpaToscaServiceTemplate> checkerFunction) {
106 String message = checkerFunction.check(serviceTemplate);
107 if (message != null) {
108 LOGGER.warn(message);
109 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
114 * Check that something have been specified correctly.
116 * @param serviceTemplate the service template containing policy types to be checked
118 public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
119 final ToscaChecker<JpaToscaServiceTemplate> checkerFunction) {
120 return checkerFunction.check(serviceTemplate) == null;
124 * Check if data types have been specified correctly.
126 public static ToscaChecker<JpaToscaServiceTemplate> checkDataTypesExist() {
127 return serviceTemplate -> {
128 if (serviceTemplate.getDataTypes() == null) {
129 return "no data types specified on service template";
132 if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
133 return "list of data types specified on service template is empty";
141 * Check if policy types have been specified correctly.
143 public static ToscaChecker<JpaToscaServiceTemplate> checkPolicyTypesExist() {
144 return 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";
158 * Check if policies have been specified correctly.
160 public static ToscaChecker<JpaToscaServiceTemplate> checkPoliciesExist() {
161 return serviceTemplate -> {
162 if (serviceTemplate.getTopologyTemplate() == null) {
163 return "topology template not specified on service template";
166 if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
167 return "no policies specified on topology template of service template";
170 if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
171 return "list of policies specified on topology template of service template is empty";
179 interface ToscaChecker<T> {
180 String check(final T serviceTemplate);