2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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;
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 ToscaServiceTemplate serviceTemplate =
69 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
71 LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
72 return serviceTemplate;
78 * @param dao the DAO to use to access the database
79 * @param name the name of the policy type to get, set to null to get all policy types
80 * @param version the version of the policy type to get, set to null to get all versions
81 * @return the policy types found
82 * @throws PfModelException on errors getting policy types
84 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
85 throws PfModelException {
87 LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
89 List<ToscaPolicyType> policyTypeList = asConceptList(
90 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes());
92 LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
93 return policyTypeList;
97 * Get filtered policy types.
99 * @param dao the DAO to use to access the database
100 * @param filter the filter for the policy types to get
101 * @return the policy types found
102 * @throws PfModelException on errors getting policy types
104 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
105 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
107 LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
109 ToscaServiceTemplate serviceTemplate =
110 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
112 List<ToscaPolicyType> filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes());
113 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
115 serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
117 LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, serviceTemplate);
118 return serviceTemplate;
122 * Get filtered policy types.
124 * @param dao the DAO to use to access the database
125 * @param filter the filter for the policy types to get
126 * @return the policy types found
127 * @throws PfModelException on errors getting policy types
129 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
130 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
132 LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
134 List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
136 LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
137 filteredPolicyTypeList);
138 return filteredPolicyTypeList;
142 * Create policy types.
144 * @param dao the DAO to use to access the database
145 * @param serviceTemplate the service template containing the definition of the policy types to be created
146 * @return the TOSCA service template containing the created policy types
147 * @throws PfModelException on errors creating policy types
149 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
150 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
152 LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
154 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
155 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
157 LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
158 return createdServiceTempalate;
162 * Update policy types.
164 * @param dao the DAO to use to access the database
165 * @param serviceTemplate the service template containing the definition of the policy types to be modified
166 * @return the TOSCA service template containing the modified policy types
167 * @throws PfModelException on errors updating policy types
169 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
170 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
172 LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
174 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
175 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
177 LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
178 return updatedServiceTempalate;
182 * Delete policy type.
184 * @param dao the DAO to use to access the database
185 * @param name the name of the policy type to delete.
186 * @param version the version of the policy type to delete.
187 * @return the TOSCA service template containing the policy type that was deleted
188 * @throws PfModelException on errors deleting policy types
190 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
191 @NonNull final String version) throws PfModelException {
193 LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
195 ToscaServiceTemplate deletedServiceTempalate =
196 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
198 LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
199 deletedServiceTempalate);
200 return deletedServiceTempalate;
206 * @param dao the DAO to use to access the database
207 * @param name the name of the policy to get.
208 * @param version the version of the policy to get.
209 * @return the policies found
210 * @throws PfModelException on errors getting policies
212 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
213 throws PfModelException {
214 LOGGER.debug("->getPolicies: name={}, version={}", name, version);
216 ToscaServiceTemplate gotServiceTempalate =
217 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
219 LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate);
220 return gotServiceTempalate;
226 * @param dao the DAO to use to access the database
227 * @param name the name of the policy to get, null to get all policies
228 * @param version the version of the policy to get, null to get all versions of a policy
229 * @return the policies found
230 * @throws PfModelException on errors getting policies
232 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
233 throws PfModelException {
234 LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
236 List<ToscaPolicy> policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version)
237 .toAuthorative().getToscaTopologyTemplate().getPolicies());
239 LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
244 * Get filtered policies.
246 * @param dao the DAO to use to access the database
247 * @param filter the filter for the policies to get
248 * @return the policies found
249 * @throws PfModelException on errors getting policies
251 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
252 throws PfModelException {
254 LOGGER.debug("->getFilteredPolicies: filter={}", filter);
255 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
257 ToscaServiceTemplate serviceTemplate =
258 new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative();
260 List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
261 filteredPolicies = filter.filter(filteredPolicies);
263 serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies));
265 LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, serviceTemplate);
266 return serviceTemplate;
270 * Get filtered policies.
272 * @param dao the DAO to use to access the database
273 * @param filter the filter for the policies to get
274 * @return the policies found
275 * @throws PfModelException on errors getting policies
277 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
278 throws PfModelException {
280 LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
281 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
283 List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
285 LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
292 * @param dao the DAO to use to access the database
293 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
294 * @return the TOSCA service template containing the policy types that were created
295 * @throws PfModelException on errors creating policies
297 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
298 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
300 LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
302 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
303 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
305 LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
306 return createdServiceTempalate;
312 * @param dao the DAO to use to access the database
313 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
314 * @return the TOSCA service template containing the policies that were updated
315 * @throws PfModelException on errors updating policies
317 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
318 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
320 LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
322 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
323 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
325 LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
326 return updatedServiceTempalate;
332 * @param dao the DAO to use to access the database
333 * @param name the name of the policy to delete.
334 * @param version the version of the policy to delete.
335 * @return the TOSCA service template containing the policy that weas deleted
336 * @throws PfModelException on errors deleting policies
338 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
339 @NonNull final String version) throws PfModelException {
341 LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
343 ToscaServiceTemplate deletedServiceTempalate =
344 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
346 LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
347 deletedServiceTempalate);
348 return deletedServiceTempalate;
352 * Return the contents of a list of maps as a plain list.
354 * @param listOfMaps the list of maps
355 * @return the plain list
357 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
358 List<T> returnList = new ArrayList<>();
359 for (Map<String, T> conceptMap : listOfMaps) {
360 for (T concept : conceptMap.values()) {
361 returnList.add(concept);
369 * Return the contents of a list of concepts as a list of maps of concepts.
371 * @param conceptList the concept list
372 * @return the list of concept map
374 private <T extends ToscaEntity> List<Map<String, T>> asConceptMap(List<T> conceptList) {
375 List<Map<String, T>> toscaEntityMapList = new ArrayList<>();
376 for (T concept : conceptList) {
377 Map<String, T> conceptMap = new LinkedHashMap<>();
378 conceptMap.put(concept.getName(), concept);
379 toscaEntityMapList.add(conceptMap);
382 return toscaEntityMapList;