2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.authorative.provider;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.TreeMap;
31 import javax.ws.rs.core.Response.Status;
33 import lombok.NonNull;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
46 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * This class provides the provision of information on TOSCA concepts in the database to callers.
53 * @author Liam Fallon (liam.fallon@est.tech)
55 public class AuthorativeToscaProvider {
56 private static final Logger LOGGER = LoggerFactory.getLogger(AuthorativeToscaProvider.class);
61 * @param dao the DAO to use to access the database
62 * @param name the name of the policy type to get.
63 * @param version the version of the policy type to get.
64 * @return the policy types found
65 * @throws PfModelException on errors getting policy types
67 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
68 throws PfModelException {
70 LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
72 JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version);
74 ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative();
76 LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
77 return serviceTemplate;
83 * @param dao the DAO to use to access the database
84 * @param name the name of the policy type to get, set to null to get all policy types
85 * @param version the version of the policy type to get, set to null to get all versions
86 * @return the policy types found
87 * @throws PfModelException on errors getting policy types
89 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
90 throws PfModelException {
92 LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
94 List<ToscaPolicyType> policyTypeList;
97 policyTypeList = new ArrayList<>(new SimpleToscaProvider().getPolicyTypes(dao, name, version)
98 .toAuthorative().getPolicyTypes().values());
99 } catch (PfModelRuntimeException pfme) {
100 return handlePfModelRuntimeException(pfme);
103 LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
104 return policyTypeList;
108 * Get filtered policy types.
110 * @param dao the DAO to use to access the database
111 * @param filter the filter for the policy types to get
112 * @return the policy types found
113 * @throws PfModelException on errors getting policy types
115 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
116 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
118 LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
120 ToscaServiceTemplate serviceTemplate =
121 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
123 List<ToscaPolicyType> filteredPolicyTypes = new ArrayList<>(serviceTemplate.getPolicyTypes().values());
124 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
126 serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
128 LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, serviceTemplate);
129 return serviceTemplate;
133 * Get filtered policy types.
135 * @param dao the DAO to use to access the database
136 * @param filter the filter for the policy types to get
137 * @return the policy types found
138 * @throws PfModelException on errors getting policy types
140 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
141 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
143 LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
145 List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
147 LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
148 filteredPolicyTypeList);
150 return filteredPolicyTypeList;
154 * Create policy types.
156 * @param dao the DAO to use to access the database
157 * @param serviceTemplate the service template containing the definition of the policy types to be created
158 * @return the TOSCA service template containing the created policy types
159 * @throws PfModelException on errors creating policy types
161 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
162 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
164 LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
166 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
167 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
169 LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
170 return createdServiceTempalate;
174 * Update policy types.
176 * @param dao the DAO to use to access the database
177 * @param serviceTemplate the service template containing the definition of the policy types to be modified
178 * @return the TOSCA service template containing the modified policy types
179 * @throws PfModelException on errors updating policy types
181 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
182 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
184 LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
186 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
187 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
189 LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
190 return updatedServiceTempalate;
194 * Delete policy type.
196 * @param dao the DAO to use to access the database
197 * @param name the name of the policy type to delete.
198 * @param version the version of the policy type to delete.
199 * @return the TOSCA service template containing the policy type that was deleted
200 * @throws PfModelException on errors deleting policy types
202 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
203 @NonNull final String version) throws PfModelException {
205 LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
207 ToscaServiceTemplate deletedServiceTempalate =
208 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
210 LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
211 deletedServiceTempalate);
212 return deletedServiceTempalate;
218 * @param dao the DAO to use to access the database
219 * @param name the name of the policy to get.
220 * @param version the version of the policy to get.
221 * @return the policies found
222 * @throws PfModelException on errors getting policies
224 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
225 throws PfModelException {
226 LOGGER.debug("->getPolicies: name={}, version={}", name, version);
228 ToscaServiceTemplate gotServiceTempalate =
229 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
231 LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate);
232 return gotServiceTempalate;
238 * @param dao the DAO to use to access the database
239 * @param name the name of the policy to get, null to get all policies
240 * @param version the version of the policy to get, null to get all versions of a policy
241 * @return the policies found
242 * @throws PfModelException on errors getting policies
244 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
245 throws PfModelException {
246 LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
248 List<ToscaPolicy> policyList;
251 policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
252 .getToscaTopologyTemplate().getPolicies());
253 } catch (PfModelRuntimeException pfme) {
254 return handlePfModelRuntimeException(pfme);
257 LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
262 * Get filtered policies.
264 * @param dao the DAO to use to access the database
265 * @param filter the filter for the policies to get
266 * @return the policies found
267 * @throws PfModelException on errors getting policies
269 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
270 throws PfModelException {
272 LOGGER.debug("->getFilteredPolicies: filter={}", filter);
273 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
275 ToscaServiceTemplate serviceTemplate =
276 new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative();
278 List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
279 filteredPolicies = filter.filter(filteredPolicies);
281 serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMapList(filteredPolicies));
283 LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, serviceTemplate);
284 return serviceTemplate;
288 * Get filtered policies.
290 * @param dao the DAO to use to access the database
291 * @param filter the filter for the policies to get
292 * @return the policies found
293 * @throws PfModelException on errors getting policies
295 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
296 throws PfModelException {
298 LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
299 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
301 List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
303 LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
310 * @param dao the DAO to use to access the database
311 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
312 * @return the TOSCA service template containing the policy types that were created
313 * @throws PfModelException on errors creating policies
315 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
316 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
318 LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
320 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
321 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
323 LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
324 return createdServiceTempalate;
330 * @param dao the DAO to use to access the database
331 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
332 * @return the TOSCA service template containing the policies that were updated
333 * @throws PfModelException on errors updating policies
335 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
336 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
338 LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
340 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
341 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
343 LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
344 return updatedServiceTempalate;
350 * @param dao the DAO to use to access the database
351 * @param name the name of the policy to delete.
352 * @param version the version of the policy to delete.
353 * @return the TOSCA service template containing the policy that weas deleted
354 * @throws PfModelException on errors deleting policies
356 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
357 @NonNull final String version) throws PfModelException {
359 LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
361 ToscaServiceTemplate deletedServiceTempalate =
362 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
364 LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
365 deletedServiceTempalate);
366 return deletedServiceTempalate;
370 * Return the contents of a list of maps as a plain list.
372 * @param listOfMaps the list of maps
373 * @return the plain list
375 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
376 List<T> returnList = new ArrayList<>();
377 for (Map<String, T> conceptMap : listOfMaps) {
378 for (T concept : conceptMap.values()) {
379 returnList.add(concept);
387 * Return the contents of a list of concepts as a list of maps of concepts.
389 * @param conceptList the concept list
390 * @return the list of concept map
392 private <T extends ToscaEntity> List<Map<String, T>> asConceptMapList(List<T> conceptList) {
393 List<Map<String, T>> toscaEntityMapList = new ArrayList<>();
394 for (T concept : conceptList) {
395 Map<String, T> conceptMap = new TreeMap<>();
396 conceptMap.put(concept.getName(), concept);
397 toscaEntityMapList.add(conceptMap);
400 return toscaEntityMapList;
404 * Return the contents of a list of concepts as map of concepts.
406 * @param conceptList the concept list
407 * @return the list of concept map
409 private <T extends ToscaEntity> Map<String, T> asConceptMap(List<T> conceptList) {
410 Map<String, T> conceptMap = new LinkedHashMap<>();
411 for (T concept : conceptList) {
412 conceptMap.put(concept.getName(), concept);
419 * Handle a PfModelRuntimeException on a list call.
421 * @param pfme the model exception
422 * @return an empty list on 404
424 private <T extends ToscaEntity> List<T> handlePfModelRuntimeException(final PfModelRuntimeException pfme) {
425 if (Status.NOT_FOUND.equals(pfme.getErrorResponse().getResponseCode())) {
426 return Collections.emptyList();