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.LinkedHashMap;
26 import java.util.List;
28 import java.util.TreeMap;
30 import lombok.NonNull;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.dao.PfDao;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * This class provides the provision of information on TOSCA concepts in the database to callers.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class AuthorativeToscaProvider {
52 private static final Logger LOGGER = LoggerFactory.getLogger(AuthorativeToscaProvider.class);
57 * @param dao the DAO to use to access the database
58 * @param name the name of the policy type to get.
59 * @param version the version of the policy type to get.
60 * @return the policy types found
61 * @throws PfModelException on errors getting policy types
63 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
64 throws PfModelException {
66 LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
68 JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version);
70 ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative();
72 LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
73 return serviceTemplate;
79 * @param dao the DAO to use to access the database
80 * @param name the name of the policy type to get, set to null to get all policy types
81 * @param version the version of the policy type to get, set to null to get all versions
82 * @return the policy types found
83 * @throws PfModelException on errors getting policy types
85 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
86 throws PfModelException {
88 LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
90 List<ToscaPolicyType> policyTypeList = new ArrayList<>(
91 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes().values());
93 LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
94 return policyTypeList;
98 * Get filtered policy types.
100 * @param dao the DAO to use to access the database
101 * @param filter the filter for the policy types to get
102 * @return the policy types found
103 * @throws PfModelException on errors getting policy types
105 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
106 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
108 LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
110 ToscaServiceTemplate serviceTemplate =
111 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
113 List<ToscaPolicyType> filteredPolicyTypes = new ArrayList<>(serviceTemplate.getPolicyTypes().values());
114 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
116 serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
118 LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, serviceTemplate);
119 return serviceTemplate;
123 * Get filtered policy types.
125 * @param dao the DAO to use to access the database
126 * @param filter the filter for the policy types to get
127 * @return the policy types found
128 * @throws PfModelException on errors getting policy types
130 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
131 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
133 LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
135 List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
137 LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
138 filteredPolicyTypeList);
139 return filteredPolicyTypeList;
143 * Create policy types.
145 * @param dao the DAO to use to access the database
146 * @param serviceTemplate the service template containing the definition of the policy types to be created
147 * @return the TOSCA service template containing the created policy types
148 * @throws PfModelException on errors creating policy types
150 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
151 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
153 LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
155 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
156 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
158 LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
159 return createdServiceTempalate;
163 * Update policy types.
165 * @param dao the DAO to use to access the database
166 * @param serviceTemplate the service template containing the definition of the policy types to be modified
167 * @return the TOSCA service template containing the modified policy types
168 * @throws PfModelException on errors updating policy types
170 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
171 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
173 LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
175 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
176 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
178 LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
179 return updatedServiceTempalate;
183 * Delete policy type.
185 * @param dao the DAO to use to access the database
186 * @param name the name of the policy type to delete.
187 * @param version the version of the policy type to delete.
188 * @return the TOSCA service template containing the policy type that was deleted
189 * @throws PfModelException on errors deleting policy types
191 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
192 @NonNull final String version) throws PfModelException {
194 LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
196 ToscaServiceTemplate deletedServiceTempalate =
197 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
199 LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
200 deletedServiceTempalate);
201 return deletedServiceTempalate;
207 * @param dao the DAO to use to access the database
208 * @param name the name of the policy to get.
209 * @param version the version of the policy to get.
210 * @return the policies found
211 * @throws PfModelException on errors getting policies
213 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
214 throws PfModelException {
215 LOGGER.debug("->getPolicies: name={}, version={}", name, version);
217 ToscaServiceTemplate gotServiceTempalate =
218 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
220 LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate);
221 return gotServiceTempalate;
227 * @param dao the DAO to use to access the database
228 * @param name the name of the policy to get, null to get all policies
229 * @param version the version of the policy to get, null to get all versions of a policy
230 * @return the policies found
231 * @throws PfModelException on errors getting policies
233 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
234 throws PfModelException {
235 LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
237 List<ToscaPolicy> policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version)
238 .toAuthorative().getToscaTopologyTemplate().getPolicies());
240 LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
245 * Get filtered policies.
247 * @param dao the DAO to use to access the database
248 * @param filter the filter for the policies to get
249 * @return the policies found
250 * @throws PfModelException on errors getting policies
252 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
253 throws PfModelException {
255 LOGGER.debug("->getFilteredPolicies: filter={}", filter);
256 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
258 ToscaServiceTemplate serviceTemplate =
259 new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative();
261 List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
262 filteredPolicies = filter.filter(filteredPolicies);
264 serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMapList(filteredPolicies));
266 LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, serviceTemplate);
267 return serviceTemplate;
271 * Get filtered policies.
273 * @param dao the DAO to use to access the database
274 * @param filter the filter for the policies to get
275 * @return the policies found
276 * @throws PfModelException on errors getting policies
278 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
279 throws PfModelException {
281 LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
282 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
284 List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
286 LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
293 * @param dao the DAO to use to access the database
294 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
295 * @return the TOSCA service template containing the policy types that were created
296 * @throws PfModelException on errors creating policies
298 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
299 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
301 LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
303 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
304 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
306 LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
307 return createdServiceTempalate;
313 * @param dao the DAO to use to access the database
314 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
315 * @return the TOSCA service template containing the policies that were updated
316 * @throws PfModelException on errors updating policies
318 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
319 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
321 LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
323 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
324 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
326 LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
327 return updatedServiceTempalate;
333 * @param dao the DAO to use to access the database
334 * @param name the name of the policy to delete.
335 * @param version the version of the policy to delete.
336 * @return the TOSCA service template containing the policy that weas deleted
337 * @throws PfModelException on errors deleting policies
339 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
340 @NonNull final String version) throws PfModelException {
342 LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
344 ToscaServiceTemplate deletedServiceTempalate =
345 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
347 LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
348 deletedServiceTempalate);
349 return deletedServiceTempalate;
353 * Return the contents of a list of maps as a plain list.
355 * @param listOfMaps the list of maps
356 * @return the plain list
358 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
359 List<T> returnList = new ArrayList<>();
360 for (Map<String, T> conceptMap : listOfMaps) {
361 for (T concept : conceptMap.values()) {
362 returnList.add(concept);
370 * Return the contents of a list of concepts as a list of maps of concepts.
372 * @param conceptList the concept list
373 * @return the list of concept map
375 private <T extends ToscaEntity> List<Map<String, T>> asConceptMapList(List<T> conceptList) {
376 List<Map<String, T>> toscaEntityMapList = new ArrayList<>();
377 for (T concept : conceptList) {
378 Map<String, T> conceptMap = new TreeMap<>();
379 conceptMap.put(concept.getName(), concept);
380 toscaEntityMapList.add(conceptMap);
383 return toscaEntityMapList;
387 * Return the contents of a list of concepts as map of concepts.
389 * @param conceptList the concept list
390 * @return the list of concept map
392 private <T extends ToscaEntity> Map<String, T> asConceptMap(List<T> conceptList) {
393 Map<String, T> conceptMap = new LinkedHashMap<>();
394 for (T concept : conceptList) {
395 conceptMap.put(concept.getName(), concept);