Merge "Added new policy examples to support integration of new version validation...
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / utils / ToscaUtils.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.utils;
22
23 import java.util.function.Function;
24
25 import javax.ws.rs.core.Response;
26
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;
31
32 /**
33  * Utility class for TOSCA concepts.
34  *
35  * @author Liam Fallon (liam.fallon@est.tech)
36  */
37 public final class ToscaUtils {
38     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class);
39
40     /**
41      * Private constructor to prevent subclassing.
42      */
43     private ToscaUtils() {
44         // Private constructor to prevent subclassing
45     }
46
47     /**
48      * Assert that data types have been specified correctly.
49      *
50      * @param serviceTemplate the service template containing data types to be checked
51      */
52     public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
53         assertExist(serviceTemplate, checkDataTypesExist());
54     }
55
56     /**
57      * Assert that policy types have been specified correctly.
58      *
59      * @param serviceTemplate the service template containing policy types to be checked
60      */
61     public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
62         assertExist(serviceTemplate, checkPolicyTypesExist());
63     }
64
65     /**
66      * Assert that policies have been specified correctly.
67      *
68      * @param serviceTemplate the service template containing policy types to be checked
69      */
70     public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
71         assertExist(serviceTemplate, checkPoliciesExist());
72     }
73
74     /**
75      * Check that data types have been specified correctly.
76      *
77      * @param serviceTemplate the service template containing data types to be checked
78      */
79     public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
80         return doExist(serviceTemplate, checkDataTypesExist());
81     }
82
83     /**
84      * Check that policy types have been specified correctly.
85      *
86      * @param serviceTemplate the service template containing policy types to be checked
87      */
88     public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
89         return doExist(serviceTemplate, checkPolicyTypesExist());
90     }
91
92     /**
93      * Check that policies have been specified correctly.
94      *
95      * @param serviceTemplate the service template containing policy types to be checked
96      */
97     public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
98         return doExist(serviceTemplate, checkPoliciesExist());
99     }
100
101     /**
102      * Assert that something have been specified correctly.
103      *
104      * @param serviceTemplate the service template containing policy types to be checked
105      */
106     public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
107             final Function<JpaToscaServiceTemplate, String> checkerFunction) {
108         String message = checkerFunction.apply(serviceTemplate);
109         if (message != null) {
110             LOGGER.warn(message);
111             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
112         }
113     }
114
115     /**
116      * Check that something have been specified correctly.
117      *
118      * @param serviceTemplate the service template containing policy types to be checked
119      */
120     public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
121             final Function<JpaToscaServiceTemplate, String> checkerFunction) {
122         return checkerFunction.apply(serviceTemplate) == null;
123     }
124
125     /**
126      * Check if data types have been specified correctly.
127      */
128     public static Function<JpaToscaServiceTemplate, String> checkDataTypesExist() {
129         return serviceTemplate -> {
130             if (serviceTemplate.getDataTypes() == null) {
131                 return "no data types specified on service template";
132             }
133
134             if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
135                 return "list of data types specified on service template is empty";
136             }
137
138             return null;
139         };
140     }
141
142     /**
143      * Check if policy types have been specified correctly.
144      */
145     public static Function<JpaToscaServiceTemplate, String> checkPolicyTypesExist() {
146         return serviceTemplate -> {
147             if (serviceTemplate.getPolicyTypes() == null) {
148                 return "no policy types specified on service template";
149             }
150
151             if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
152                 return "list of policy types specified on service template is empty";
153             }
154
155             return null;
156         };
157     }
158
159     /**
160      * Check if policies have been specified correctly.
161      */
162     public static Function<JpaToscaServiceTemplate, String> checkPoliciesExist() {
163         return serviceTemplate -> {
164             if (serviceTemplate.getTopologyTemplate() == null) {
165                 return "topology template not specified on service template";
166             }
167
168             if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
169                 return "no policies specified on topology template of service template";
170             }
171
172             if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
173                 return "list of policies specified on topology template of service template is empty";
174             }
175
176             return null;
177         };
178     }
179 }