Store and return data types for policies
[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 javax.ws.rs.core.Response;
24
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;
29
30 /**
31  * Utility class for TOSCA concepts.
32  *
33  * @author Liam Fallon (liam.fallon@est.tech)
34  */
35 public final class ToscaUtils {
36     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class);
37
38     /**
39      * Private constructor to prevent subclassing.
40      */
41     private ToscaUtils() {
42         // Private constructor to prevent subclassing
43     }
44
45     /**
46      * Assert that data types have been specified correctly.
47      *
48      * @param serviceTemplate the service template containing data types to be checked
49      */
50     public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
51         String message = checkDataTypesExist(serviceTemplate);
52         if (message != null) {
53             LOGGER.warn(message);
54             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
55         }
56     }
57
58     /**
59      * Assert that policy types have been specified correctly.
60      *
61      * @param serviceTemplate the service template containing policy types to be checked
62      */
63     public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
64         String message = checkPolicyTypesExist(serviceTemplate);
65         if (message != null) {
66             LOGGER.warn(message);
67             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
68         }
69     }
70
71     /**
72      * Assert that policies have been specified correctly.
73      *
74      * @param serviceTemplate the service template containing policy types to be checked
75      */
76     public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
77         String message = checkPoliciesExist(serviceTemplate);
78         if (message != null) {
79             LOGGER.warn(message);
80             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
81         }
82     }
83
84     /**
85      * Check that data types have been specified correctly.
86      *
87      * @param serviceTemplate the service template containing data types to be checked
88      */
89     public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
90         return checkDataTypesExist(serviceTemplate) == null;
91     }
92
93     /**
94      * Check that policy types have been specified correctly.
95      *
96      * @param serviceTemplate the service template containing policy types to be checked
97      */
98     public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
99         return checkPolicyTypesExist(serviceTemplate) == null;
100     }
101
102     /**
103      * Check that policies have been specified correctly.
104      *
105      * @param serviceTemplate the service template containing policy types to be checked
106      */
107     public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
108         return checkPoliciesExist(serviceTemplate) == null;
109     }
110
111     /**
112      * Check if data types have been specified correctly.
113      *
114      * @param serviceTemplate the service template containing data types to be checked
115      */
116     public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
117         if (serviceTemplate.getDataTypes() == null) {
118             return "no data types specified on service template";
119         }
120
121         if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
122             return "list of data types specified on service template is empty";
123         }
124
125         return null;
126     }
127
128     /**
129      * Check if policy types have been specified correctly.
130      *
131      * @param serviceTemplate the service template containing policy types to be checked
132      */
133     public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
134         if (serviceTemplate.getPolicyTypes() == null) {
135             return "no policy types specified on service template";
136         }
137
138         if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
139             return "list of policy types specified on service template is empty";
140         }
141
142         return null;
143     }
144
145     /**
146      * Check if policies have been specified correctly.
147      *
148      * @param serviceTemplate the service template containing policy types to be checked
149      */
150     public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
151         if (serviceTemplate.getTopologyTemplate() == null) {
152             return "topology template not specified on service template";
153         }
154
155         if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
156             return "no policies specified on topology template of service template";
157         }
158
159         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
160             return "list of policies specified on topology template of service template is empty";
161         }
162
163         return null;
164     }
165 }