2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.authorative.provider;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
29 import lombok.NonNull;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.dao.PfDao;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
41 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
44 * This class provides the provision of information on TOSCA concepts in the database to callers.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class AuthorativeToscaProvider {
52 * @param dao the DAO to use to access the database
53 * @param name the name of the policy type to get.
54 * @param version the version of the policy type to get.
55 * @return the policy types found
56 * @throws PfModelException on errors getting policy types
58 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
59 throws PfModelException {
61 return new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
67 * @param dao the DAO to use to access the database
68 * @param name the name of the policy type to get, set to null to get all policy types
69 * @param version the version of the policy type to get, set to null to get all versions
70 * @return the policy types found
71 * @throws PfModelException on errors getting policy types
73 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
74 throws PfModelException {
76 return (asConceptList(
77 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes()));
81 * Get filtered policy types.
83 * @param dao the DAO to use to access the database
84 * @param filter the filter for the policy types to get
85 * @return the policy types found
86 * @throws PfModelException on errors getting policy types
88 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
89 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
91 ToscaServiceTemplate serviceTemplate =
92 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
94 List<ToscaPolicyType> filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes());
95 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
97 serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
99 return serviceTemplate;
103 * Get filtered policy types.
105 * @param dao the DAO to use to access the database
106 * @param filter the filter for the policy types to get
107 * @return the policy types found
108 * @throws PfModelException on errors getting policy types
110 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
111 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
113 return filter.filter(getPolicyTypeList(dao, null, null));
117 * Create policy types.
119 * @param dao the DAO to use to access the database
120 * @param serviceTemplate the service template containing the definition of the policy types to be created
121 * @return the TOSCA service template containing the created policy types
122 * @throws PfModelException on errors creating policy types
124 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
125 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
127 return new SimpleToscaProvider().createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
132 * Update policy types.
134 * @param dao the DAO to use to access the database
135 * @param serviceTemplate the service template containing the definition of the policy types to be modified
136 * @return the TOSCA service template containing the modified policy types
137 * @throws PfModelException on errors updating policy types
139 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
140 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
142 return new SimpleToscaProvider().updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
147 * Delete policy type.
149 * @param dao the DAO to use to access the database
150 * @param name the name of the policy type to delete.
151 * @param version the version of the policy type to delete.
152 * @return the TOSCA service template containing the policy type that was deleted
153 * @throws PfModelException on errors deleting policy types
155 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
156 @NonNull final String version) throws PfModelException {
158 return new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
164 * @param dao the DAO to use to access the database
165 * @param name the name of the policy to get.
166 * @param version the version of the policy to get.
167 * @return the policies found
168 * @throws PfModelException on errors getting policies
170 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
171 throws PfModelException {
173 return new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
179 * @param dao the DAO to use to access the database
180 * @param name the name of the policy to get, null to get all policies
181 * @param version the version of the policy to get, null to get all versions of a policy
182 * @return the policies found
183 * @throws PfModelException on errors getting policies
185 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
186 throws PfModelException {
188 return asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
189 .getToscaTopologyTemplate().getPolicies());
193 * Get filtered policies.
195 * @param dao the DAO to use to access the database
196 * @param filter the filter for the policies to get
197 * @return the policies found
198 * @throws PfModelException on errors getting policies
200 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
201 throws PfModelException {
203 ToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicies(dao, null, null).toAuthorative();
205 List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
206 filteredPolicies = filter.filter(filteredPolicies);
208 serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies));
210 return serviceTemplate;
214 * Get filtered policies.
216 * @param dao the DAO to use to access the database
217 * @param filter the filter for the policies to get
218 * @return the policies found
219 * @throws PfModelException on errors getting policies
221 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
222 throws PfModelException {
224 return filter.filter(getPolicyList(dao, null, null));
230 * @param dao the DAO to use to access the database
231 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
232 * @return the TOSCA service template containing the policy types that were created
233 * @throws PfModelException on errors creating policies
235 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
236 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
238 return new SimpleToscaProvider().createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
245 * @param dao the DAO to use to access the database
246 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
247 * @return the TOSCA service template containing the policies that were updated
248 * @throws PfModelException on errors updating policies
250 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
251 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
253 return new SimpleToscaProvider().updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
260 * @param dao the DAO to use to access the database
261 * @param name the name of the policy to delete.
262 * @param version the version of the policy to delete.
263 * @return the TOSCA service template containing the policy that weas deleted
264 * @throws PfModelException on errors deleting policies
266 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
267 @NonNull final String version) throws PfModelException {
269 return new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
273 * Return the contents of a list of maps as a plain list.
275 * @param listOfMaps the list of maps
276 * @return the plain list
278 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
279 if (listOfMaps == null) {
280 return Collections.emptyList();
283 List<T> returnList = new ArrayList<>();
284 for (Map<String, T> conceptMap : listOfMaps) {
285 for (T concept : conceptMap.values()) {
286 returnList.add(concept);
294 * Return the contents of a list of concepts as a list of maps of concepts.
296 * @param comceptList the concept list
297 * @return the concept map
299 private <T extends ToscaEntity> List<Map<String, T>> asConceptMap(List<T> conceptList) {
300 Map<String, T> conceptMap = new LinkedHashMap<>();
301 for (T concept : conceptList) {
302 conceptMap.put(concept.getName(), concept);
305 return Collections.singletonList(conceptMap);