Merge "Fix VF Module Create in guard"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / utils / ToscaUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.ToscaServiceTemplate;
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     }
43
44     /**
45      * Check if policy types have been specified is initialized.
46      */
47     public static void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) {
48         if (serviceTemplate.getPolicyTypes() == null) {
49             String errorMessage = "no policy types specified on service template";
50             LOGGER.warn(errorMessage);
51             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
52         }
53
54         if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
55             String errorMessage = "list of policy types specified on service template is empty";
56             LOGGER.warn(errorMessage);
57             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
58         }
59     }
60
61     /**
62      * Check if policy types have been specified is initialized.
63      */
64     public static void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) {
65         if (serviceTemplate.getTopologyTemplate() == null) {
66             String errorMessage = "topology template not specified on service template";
67             LOGGER.warn(errorMessage);
68             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
69         }
70
71         if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
72             String errorMessage = "no policies specified on topology template of service template";
73             LOGGER.warn(errorMessage);
74             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
75         }
76
77         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
78             String errorMessage = "list of policies specified on topology template of service template is empty";
79             LOGGER.warn(errorMessage);
80             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
81         }
82     }
83
84
85 }