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;
45 * This class provides the provision of information on TOSCA concepts in the database to callers.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class AuthorativeToscaProvider {
53 * @param dao the DAO to use to access the database
54 * @param name the name of the policy type to get.
55 * @param version the version of the policy type to get.
56 * @return the policy types found
57 * @throws PfModelException on errors getting policy types
59 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
60 throws PfModelException {
62 return new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
68 * @param dao the DAO to use to access the database
69 * @param name the name of the policy type to get, set to null to get all policy types
70 * @param version the version of the policy type to get, set to null to get all versions
71 * @return the policy types found
72 * @throws PfModelException on errors getting policy types
74 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
75 throws PfModelException {
77 return (asConceptList(
78 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes()));
82 * Get filtered policy types.
84 * @param dao the DAO to use to access the database
85 * @param filter the filter for the policy types to get
86 * @return the policy types found
87 * @throws PfModelException on errors getting policy types
89 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
90 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
92 ToscaServiceTemplate serviceTemplate =
93 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
95 List<ToscaPolicyType> filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes());
96 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
98 serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
100 return serviceTemplate;
104 * Get filtered policy types.
106 * @param dao the DAO to use to access the database
107 * @param filter the filter for the policy types to get
108 * @return the policy types found
109 * @throws PfModelException on errors getting policy types
111 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
112 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
114 return filter.filter(getPolicyTypeList(dao, null, null));
118 * Create policy types.
120 * @param dao the DAO to use to access the database
121 * @param serviceTemplate the service template containing the definition of the policy types to be created
122 * @return the TOSCA service template containing the created policy types
123 * @throws PfModelException on errors creating policy types
125 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
126 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
128 return new SimpleToscaProvider().createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
133 * Update policy types.
135 * @param dao the DAO to use to access the database
136 * @param serviceTemplate the service template containing the definition of the policy types to be modified
137 * @return the TOSCA service template containing the modified policy types
138 * @throws PfModelException on errors updating policy types
140 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
141 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
143 return new SimpleToscaProvider().updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
148 * Delete policy type.
150 * @param dao the DAO to use to access the database
151 * @param name the name of the policy type to delete.
152 * @param version the version of the policy type to delete.
153 * @return the TOSCA service template containing the policy type that was deleted
154 * @throws PfModelException on errors deleting policy types
156 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
157 @NonNull final String version) throws PfModelException {
159 return new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
165 * @param dao the DAO to use to access the database
166 * @param name the name of the policy to get.
167 * @param version the version of the policy to get.
168 * @return the policies found
169 * @throws PfModelException on errors getting policies
171 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
172 throws PfModelException {
174 return new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
180 * @param dao the DAO to use to access the database
181 * @param name the name of the policy to get, null to get all policies
182 * @param version the version of the policy to get, null to get all versions of a policy
183 * @return the policies found
184 * @throws PfModelException on errors getting policies
186 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
187 throws PfModelException {
189 return asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
190 .getToscaTopologyTemplate().getPolicies());
194 * Get filtered policies.
196 * @param dao the DAO to use to access the database
197 * @param filter the filter for the policies to get
198 * @return the policies found
199 * @throws PfModelException on errors getting policies
201 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
202 throws PfModelException {
204 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
206 ToscaServiceTemplate serviceTemplate =
207 new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative();
209 List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
210 filteredPolicies = filter.filter(filteredPolicies);
212 serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies));
214 return serviceTemplate;
218 * Get filtered policies.
220 * @param dao the DAO to use to access the database
221 * @param filter the filter for the policies to get
222 * @return the policies found
223 * @throws PfModelException on errors getting policies
225 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
226 throws PfModelException {
228 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
229 return filter.filter(getPolicyList(dao, filter.getName(), version));
235 * @param dao the DAO to use to access the database
236 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
237 * @return the TOSCA service template containing the policy types that were created
238 * @throws PfModelException on errors creating policies
240 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
241 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
243 return new SimpleToscaProvider().createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
250 * @param dao the DAO to use to access the database
251 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
252 * @return the TOSCA service template containing the policies that were updated
253 * @throws PfModelException on errors updating policies
255 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
256 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
258 return new SimpleToscaProvider().updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
265 * @param dao the DAO to use to access the database
266 * @param name the name of the policy to delete.
267 * @param version the version of the policy to delete.
268 * @return the TOSCA service template containing the policy that weas deleted
269 * @throws PfModelException on errors deleting policies
271 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
272 @NonNull final String version) throws PfModelException {
274 return new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
278 * Return the contents of a list of maps as a plain list.
280 * @param listOfMaps the list of maps
281 * @return the plain list
283 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
284 List<T> returnList = new ArrayList<>();
285 for (Map<String, T> conceptMap : listOfMaps) {
286 for (T concept : conceptMap.values()) {
287 returnList.add(concept);
295 * Return the contents of a list of concepts as a list of maps of concepts.
297 * @param comceptList the concept list
298 * @return the concept map
300 private <T extends ToscaEntity> List<Map<String, T>> asConceptMap(List<T> conceptList) {
301 Map<String, T> conceptMap = new LinkedHashMap<>();
302 for (T concept : conceptList) {
303 conceptMap.put(concept.getName(), concept);
306 return Collections.singletonList(conceptMap);