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.List;
28 import lombok.NonNull;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.dao.PfDao;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
39 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
42 * This class provides the provision of information on TOSCA concepts in the database to callers.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class AuthorativeToscaProvider {
50 * @param dao the DAO to use to access the database
51 * @param name the name of the policy type to get.
52 * @param version the version of the policy type to get.
53 * @return the policy types found
54 * @throws PfModelException on errors getting policy types
56 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
57 throws PfModelException {
59 return new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
65 * @param dao the DAO to use to access the database
66 * @param name the name of the policy type to get, set to null to get all policy types
67 * @param version the version of the policy type to get, set to null to get all versions
68 * @return the policy types found
69 * @throws PfModelException on errors getting policy types
71 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
72 throws PfModelException {
74 return (asConceptList(
75 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes()));
79 * Get filtered policy types.
81 * @param dao the DAO to use to access the database
82 * @param filter the filter for the policy types to get
83 * @return the policy types found
84 * @throws PfModelException on errors getting policy types
86 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
87 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
88 return new SimpleToscaProvider().getFilteredPolicyTypes(dao, filter).toAuthorative();
92 * Get filtered policy types.
94 * @param dao the DAO to use to access the database
95 * @param filter the filter for the policy types to get
96 * @return the policy types found
97 * @throws PfModelException on errors getting policy types
99 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
100 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
102 return (asConceptList(
103 new SimpleToscaProvider().getFilteredPolicyTypes(dao, filter).toAuthorative().getPolicyTypes()));
107 * Create policy types.
109 * @param dao the DAO to use to access the database
110 * @param serviceTemplate the service template containing the definition of the policy types to be created
111 * @return the TOSCA service template containing the created policy types
112 * @throws PfModelException on errors creating policy types
114 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
115 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
117 return new SimpleToscaProvider().createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
122 * Update policy types.
124 * @param dao the DAO to use to access the database
125 * @param serviceTemplate the service template containing the definition of the policy types to be modified
126 * @return the TOSCA service template containing the modified policy types
127 * @throws PfModelException on errors updating policy types
129 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
130 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
132 return new SimpleToscaProvider().updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
137 * Delete policy type.
139 * @param dao the DAO to use to access the database
140 * @param name the name of the policy type to delete.
141 * @param version the version of the policy type to delete.
142 * @return the TOSCA service template containing the policy type that was deleted
143 * @throws PfModelException on errors deleting policy types
145 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
146 @NonNull final String version) throws PfModelException {
148 return new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
154 * @param dao the DAO to use to access the database
155 * @param name the name of the policy to get.
156 * @param version the version of the policy to get.
157 * @return the policies found
158 * @throws PfModelException on errors getting policies
160 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
161 throws PfModelException {
163 return new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
169 * @param dao the DAO to use to access the database
170 * @param name the name of the policy to get, null to get all policies
171 * @param version the version of the policy to get, null to get all versions of a policy
172 * @return the policies found
173 * @throws PfModelException on errors getting policies
175 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
176 throws PfModelException {
178 return asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
179 .getToscaTopologyTemplate().getPolicies());
183 * Get filtered policies.
185 * @param dao the DAO to use to access the database
186 * @param filter the filter for the policies to get
187 * @return the policies found
188 * @throws PfModelException on errors getting policies
190 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
191 throws PfModelException {
193 return new SimpleToscaProvider().getFilteredPolicies(dao, filter).toAuthorative();
197 * Get filtered policies.
199 * @param dao the DAO to use to access the database
200 * @param filter the filter for the policies to get
201 * @return the policies found
202 * @throws PfModelException on errors getting policies
204 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
205 throws PfModelException {
207 return asConceptList(new SimpleToscaProvider().getFilteredPolicies(dao, filter).toAuthorative()
208 .getToscaTopologyTemplate().getPolicies());
214 * @param dao the DAO to use to access the database
215 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
216 * @return the TOSCA service template containing the policy types that were created
217 * @throws PfModelException on errors creating policies
219 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
220 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
222 return new SimpleToscaProvider().createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
229 * @param dao the DAO to use to access the database
230 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
231 * @return the TOSCA service template containing the policies that were updated
232 * @throws PfModelException on errors updating policies
234 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
235 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
237 return new SimpleToscaProvider().updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
244 * @param dao the DAO to use to access the database
245 * @param name the name of the policy to delete.
246 * @param version the version of the policy to delete.
247 * @return the TOSCA service template containing the policy that weas deleted
248 * @throws PfModelException on errors deleting policies
250 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
251 @NonNull final String version) throws PfModelException {
253 return new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
257 * Return the contents of a list of maps as a plain list.
259 * @param listOfMaps the list of maps
260 * @return the plain list
262 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
263 if (listOfMaps == null) {
264 return Collections.emptyList();
267 List<T> returnList = new ArrayList<>();
268 for (Map<String, T> conceptMap : listOfMaps) {
269 for (T concept : conceptMap.values()) {
270 returnList.add(concept);