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