Add vaidation for entity hierarchies
[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.Collection;
24 import java.util.Set;
25 import java.util.function.Function;
26
27 import javax.ws.rs.core.Response;
28
29 import lombok.NonNull;
30
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.onap.policy.models.base.PfConcept;
33 import org.onap.policy.models.base.PfConceptContainer;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.base.PfNameVersion;
37 import org.onap.policy.models.base.PfValidationMessage;
38 import org.onap.policy.models.base.PfValidationResult;
39 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Utility class for TOSCA concepts.
47  *
48  * @author Liam Fallon (liam.fallon@est.tech)
49  */
50 public final class ToscaUtils {
51     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class);
52
53     private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
54
55     /**
56      * Private constructor to prevent subclassing.
57      */
58     private ToscaUtils() {
59         // Private constructor to prevent subclassing
60     }
61
62     /**
63      * Assert that data types have been specified correctly.
64      *
65      * @param serviceTemplate the service template containing data types to be checked
66      */
67     public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
68         assertExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
69     }
70
71     /**
72      * Assert that policy types have been specified correctly.
73      *
74      * @param serviceTemplate the service template containing policy types to be checked
75      */
76     public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
77         assertExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
78     }
79
80     /**
81      * Assert that policies have been specified correctly.
82      *
83      * @param serviceTemplate the service template containing policy types to be checked
84      */
85     public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
86         assertExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
87     }
88
89     /**
90      * Check that data types have been specified correctly.
91      *
92      * @param serviceTemplate the service template containing data types to be checked
93      */
94     public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
95         return doExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
96     }
97
98     /**
99      * Check that policy types have been specified correctly.
100      *
101      * @param serviceTemplate the service template containing policy types to be checked
102      */
103     public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
104         return doExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
105     }
106
107     /**
108      * Check that policies have been specified correctly.
109      *
110      * @param serviceTemplate the service template containing policy types to be checked
111      */
112     public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
113
114         return doExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
115     }
116
117     /**
118      * Assert that something have been specified correctly.
119      *
120      * @param serviceTemplate the service template containing policy types to be checked
121      */
122     public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
123             final Function<JpaToscaServiceTemplate, String> checkerFunction) {
124         String message = checkerFunction.apply(serviceTemplate);
125         if (message != null) {
126             LOGGER.warn(message);
127             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
128         }
129     }
130
131     /**
132      * Check that something have been specified correctly.
133      *
134      * @param serviceTemplate the service template containing policy types to be checked
135      */
136     public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
137             final Function<JpaToscaServiceTemplate, String> checkerFunction) {
138         return checkerFunction.apply(serviceTemplate) == null;
139     }
140
141     /**
142      * Check if data types have been specified correctly.
143      */
144     public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
145         if (serviceTemplate.getDataTypes() == null) {
146             return "no data types specified on service template";
147         }
148
149         if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
150             return "list of data types specified on service template is empty";
151         }
152
153         return null;
154     }
155
156     /**
157      * Check if policy types have been specified correctly.
158      */
159     public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
160         if (serviceTemplate.getPolicyTypes() == null) {
161             return "no policy types specified on service template";
162         }
163
164         if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
165             return "list of policy types specified on service template is empty";
166         }
167
168         return null;
169     }
170
171     /**
172      * Check if policies have been specified correctly.
173      */
174     public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
175         if (serviceTemplate.getTopologyTemplate() == null) {
176             return "topology template not specified on service template";
177         }
178
179         if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
180             return "no policies specified on topology template of service template";
181         }
182
183         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
184             return "list of policies specified on topology template of service template is empty";
185         }
186
187         return null;
188     }
189
190     /**
191      * Find all the ancestors of an entity type.
192      *
193      * @param entityTypes the set of entity types that exist
194      * @param entityType the entity type for which to get the parents
195      * @param result the result of the ancestor search with any warnings or errors
196      * @return
197      */
198     public static Collection<? extends JpaToscaEntityType<?>> getEntityTypeAncestors(
199             @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
200             @NonNull JpaToscaEntityType<?> entityType, @NonNull final PfValidationResult result) {
201
202         PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom();
203         if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) {
204             return CollectionUtils.emptyCollection();
205         }
206
207         @SuppressWarnings("unchecked")
208         Set<JpaToscaEntityType<?>> ancestorEntitySet = (Set<JpaToscaEntityType<?>>) entityTypes
209                 .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
210
211         if (ancestorEntitySet.isEmpty()) {
212             result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class,
213                     ValidationResult.INVALID, "parent " + parentEntityTypeKey.getId() + " of entity not found"));
214         } else {
215             for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) {
216                 ancestorEntitySet.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result));
217             }
218         }
219         return ancestorEntitySet;
220     }
221 }