be0d41d9b992b24ab05bcc4584449eb0eab37071
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / utils / ToscaUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2022 Nordix Foundation.
4  *  Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.utils;
23
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.Set;
27 import java.util.function.Function;
28 import javax.ws.rs.core.Response;
29 import lombok.AccessLevel;
30 import lombok.NoArgsConstructor;
31 import lombok.NonNull;
32 import org.apache.commons.collections4.CollectionUtils;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.common.parameters.ValidationStatus;
35 import org.onap.policy.models.base.PfConcept;
36 import org.onap.policy.models.base.PfConceptContainer;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.base.PfNameVersion;
41 import org.onap.policy.models.base.Validated;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
44 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
45
46 /**
47  * Utility class for TOSCA concepts.
48  *
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 @NoArgsConstructor(access = AccessLevel.PRIVATE)
52 public final class ToscaUtils {
53     private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
54
55     // @formatter:off
56     private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = Set.of(
57             new PfConceptKey("string",                       PfKey.NULL_KEY_VERSION),
58             new PfConceptKey("integer",                      PfKey.NULL_KEY_VERSION),
59             new PfConceptKey("float",                        PfKey.NULL_KEY_VERSION),
60             new PfConceptKey("boolean",                      PfKey.NULL_KEY_VERSION),
61             new PfConceptKey("timestamp",                    PfKey.NULL_KEY_VERSION),
62             new PfConceptKey("null",                         PfKey.NULL_KEY_VERSION),
63             new PfConceptKey("list",                         PfKey.NULL_KEY_VERSION),
64             new PfConceptKey("map",                          PfKey.NULL_KEY_VERSION),
65             new PfConceptKey("object",                       PfKey.NULL_KEY_VERSION),
66             new PfConceptKey("scalar-unit.size",             PfKey.NULL_KEY_VERSION),
67             new PfConceptKey("scalar-unit.time",             PfKey.NULL_KEY_VERSION),
68             new PfConceptKey("scalar-unit.frequency",        PfKey.NULL_KEY_VERSION),
69             new PfConceptKey("tosca.datatypes.TimeInterval", PfKey.NULL_KEY_VERSION)
70         );
71     // @formatter:on
72
73     /**
74      * Get the predefined policy types.
75      *
76      * @return the predefined policy types
77      */
78     public static Collection<PfConceptKey> getPredefinedDataTypes() {
79         return PREDEFINED_TOSCA_DATA_TYPES;
80     }
81
82     /**
83      * Assert that data types have been specified correctly.
84      *
85      * @param serviceTemplate the service template containing data types to be checked
86      */
87     public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
88         assertExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
89     }
90
91     /**
92      * Assert that policy types have been specified correctly.
93      *
94      * @param serviceTemplate the service template containing policy types to be checked
95      */
96     public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
97         assertExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
98     }
99
100     /**
101      * Assert that policies have been specified correctly.
102      *
103      * @param serviceTemplate the service template containing policy types to be checked
104      */
105     public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
106         assertExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
107     }
108
109     /**
110      * Assert that node templates have been specified correctly.
111      *
112      * @param serviceTemplate the service template containing node templates to be checked
113      */
114     public static void assertNodeTemplatesExist(final JpaToscaServiceTemplate serviceTemplate) {
115         assertExist(serviceTemplate, ToscaUtils::checkNodeTemplateExist);
116     }
117
118     /**
119      * Check that data types have been specified correctly.
120      *
121      * @param serviceTemplate the service template containing data types to be checked
122      */
123     public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
124         return doExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
125     }
126
127     /**
128      * Check that policy types have been specified correctly.
129      *
130      * @param serviceTemplate the service template containing policy types to be checked
131      */
132     public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
133         return doExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
134     }
135
136     /**
137      * Check that policies have been specified correctly.
138      *
139      * @param serviceTemplate the service template containing policy types to be checked
140      */
141     public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
142
143         return doExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
144     }
145
146     /**
147      * Check that tosca node templates have been specified correctly.
148      *
149      * @param serviceTemplate the service template containing node templates to be checked
150      */
151     public static boolean doNodeTemplatesExist(final JpaToscaServiceTemplate serviceTemplate) {
152
153         return doExist(serviceTemplate, ToscaUtils::checkNodeTemplateExist);
154     }
155
156     /**
157      * Assert that something have been specified correctly.
158      *
159      * @param serviceTemplate the service template containing policy types to be checked
160      */
161     public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
162             final Function<JpaToscaServiceTemplate, String> checkerFunction) {
163         String message = checkerFunction.apply(serviceTemplate);
164         if (message != null) {
165             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, message);
166         }
167     }
168
169     /**
170      * Check that something have been specified correctly.
171      *
172      * @param serviceTemplate the service template containing policy types to be checked
173      */
174     public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
175             final Function<JpaToscaServiceTemplate, String> checkerFunction) {
176         return checkerFunction.apply(serviceTemplate) == null;
177     }
178
179     /**
180      * Check if data types have been specified correctly.
181      */
182     public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
183         if (serviceTemplate.getDataTypes() == null) {
184             return "no data types specified on service template";
185         }
186
187         if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
188             return "list of data types specified on service template is empty";
189         }
190
191         return null;
192     }
193
194     /**
195      * Check if policy types have been specified correctly.
196      */
197     public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
198         if (serviceTemplate.getPolicyTypes() == null) {
199             return "no policy types specified on service template";
200         }
201
202         if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
203             return "list of policy types specified on service template is empty";
204         }
205
206         return null;
207     }
208
209     /**
210      * Check if policies have been specified correctly.
211      */
212     public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
213         if (serviceTemplate.getTopologyTemplate() == null) {
214             return "topology template not specified on service template";
215         }
216
217         if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
218             return "no policies specified on topology template of service template";
219         }
220
221         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
222             return "list of policies specified on topology template of service template is empty";
223         }
224
225         return null;
226     }
227
228     /**
229      * Check if node templates have been specified correctly.
230      */
231     public static String checkNodeTemplateExist(final JpaToscaServiceTemplate serviceTemplate) {
232         if (serviceTemplate.getTopologyTemplate().getNodeTemplates() == null) {
233             return "node templates not present on the service template";
234         }
235
236         if (serviceTemplate.getTopologyTemplate().getNodeTemplates().getConceptMap().isEmpty()) {
237             return "no parameters present on the node templates";
238         }
239         return null;
240     }
241
242     /**
243      * getLatestPolicyTypeVersion Find all the ancestors of an entity type.
244      *
245      * @param entityTypes the set of entity types that exist
246      * @param entityType the entity type for which to get the parents
247      * @param result the result of the ancestor search with any warnings or errors
248      * @return the entity set containing the ancestors of the incoming entity
249      */
250     public static Collection<JpaToscaEntityType<ToscaEntity>> getEntityTypeAncestors(
251             @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
252             @NonNull JpaToscaEntityType<?> entityType, @NonNull final BeanValidationResult result) {
253
254         PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom();
255         if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) {
256             return CollectionUtils.emptyCollection();
257         }
258
259         if (entityType.getKey().equals(parentEntityTypeKey)) {
260             result.addResult("entity type", entityType.getKey().getId(),
261                             ValidationStatus.INVALID, "ancestor of itself");
262             throw new PfModelRuntimeException(Response.Status.CONFLICT, result.getResult());
263         }
264
265         @SuppressWarnings("unchecked")
266         Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySet = (Set<JpaToscaEntityType<ToscaEntity>>) entityTypes
267                 .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
268         Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySetToReturn = new HashSet<>(ancestorEntitySet);
269         if (ancestorEntitySet.isEmpty()) {
270             result.addResult("parent", parentEntityTypeKey.getId(), ValidationStatus.INVALID, Validated.NOT_FOUND);
271         } else {
272             for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) {
273                 ancestorEntitySetToReturn.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result));
274             }
275         }
276         return ancestorEntitySetToReturn;
277     }
278
279     /**
280      * Get the entity tree from a concept container for a given entity key.
281      *
282      * @param entityTypes the concept container containing entity types
283      * @param entityName the name of the entity
284      * @param entityVersion the version of the entity
285      */
286     public static void getEntityTree(
287             @NonNull final PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
288             final String entityName, final String entityVersion) {
289
290         var result = new BeanValidationResult("entity", entityName);
291
292         @SuppressWarnings("unchecked")
293         Set<JpaToscaEntityType<?>> filteredEntitySet =
294                 (Set<JpaToscaEntityType<?>>) entityTypes.getAllNamesAndVersions(entityName, entityVersion);
295         Set<JpaToscaEntityType<?>> filteredEntitySetToReturn = new HashSet<>(filteredEntitySet);
296         for (JpaToscaEntityType<?> filteredEntityType : filteredEntitySet) {
297             filteredEntitySetToReturn
298                     .addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result));
299         }
300
301         if (!result.isValid()) {
302             throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.getResult());
303         }
304
305         entityTypes.getConceptMap().entrySet()
306                 .removeIf(entityEntry -> !filteredEntitySetToReturn.contains(entityEntry.getValue()));
307     }
308 }