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.LinkedHashMap;
26 import java.util.List;
28 import java.util.TreeMap;
30 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;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * This class provides the provision of information on TOSCA concepts in the database to callers.
48 * @author Liam Fallon (liam.fallon@est.tech)
50 public class AuthorativeToscaProvider {
51 private static final Logger LOGGER = LoggerFactory.getLogger(AuthorativeToscaProvider.class);
56 * @param dao the DAO to use to access the database
57 * @param name the name of the policy type to get.
58 * @param version the version of the policy type to get.
59 * @return the policy types found
60 * @throws PfModelException on errors getting policy types
62 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
63 throws PfModelException {
65 LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
67 ToscaServiceTemplate serviceTemplate =
68 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
70 LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
71 return serviceTemplate;
77 * @param dao the DAO to use to access the database
78 * @param name the name of the policy type to get, set to null to get all policy types
79 * @param version the version of the policy type to get, set to null to get all versions
80 * @return the policy types found
81 * @throws PfModelException on errors getting policy types
83 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
84 throws PfModelException {
86 LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
88 List<ToscaPolicyType> policyTypeList = new ArrayList<>(
89 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes().values());
91 LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
92 return policyTypeList;
96 * Get filtered policy types.
98 * @param dao the DAO to use to access the database
99 * @param filter the filter for the policy types to get
100 * @return the policy types found
101 * @throws PfModelException on errors getting policy types
103 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
104 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
106 LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
108 ToscaServiceTemplate serviceTemplate =
109 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
111 List<ToscaPolicyType> filteredPolicyTypes = new ArrayList<>(serviceTemplate.getPolicyTypes().values());
112 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
114 serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
116 LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, serviceTemplate);
117 return serviceTemplate;
121 * Get filtered policy types.
123 * @param dao the DAO to use to access the database
124 * @param filter the filter for the policy types to get
125 * @return the policy types found
126 * @throws PfModelException on errors getting policy types
128 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
129 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
131 LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
133 List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
135 LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
136 filteredPolicyTypeList);
137 return filteredPolicyTypeList;
141 * Create policy types.
143 * @param dao the DAO to use to access the database
144 * @param serviceTemplate the service template containing the definition of the policy types to be created
145 * @return the TOSCA service template containing the created policy types
146 * @throws PfModelException on errors creating policy types
148 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
149 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
151 LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
153 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
154 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
156 LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
157 return createdServiceTempalate;
161 * Update policy types.
163 * @param dao the DAO to use to access the database
164 * @param serviceTemplate the service template containing the definition of the policy types to be modified
165 * @return the TOSCA service template containing the modified policy types
166 * @throws PfModelException on errors updating policy types
168 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
169 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
171 LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
173 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
174 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
176 LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
177 return updatedServiceTempalate;
181 * Delete policy type.
183 * @param dao the DAO to use to access the database
184 * @param name the name of the policy type to delete.
185 * @param version the version of the policy type to delete.
186 * @return the TOSCA service template containing the policy type that was deleted
187 * @throws PfModelException on errors deleting policy types
189 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
190 @NonNull final String version) throws PfModelException {
192 LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
194 ToscaServiceTemplate deletedServiceTempalate =
195 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
197 LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
198 deletedServiceTempalate);
199 return deletedServiceTempalate;
205 * @param dao the DAO to use to access the database
206 * @param name the name of the policy to get.
207 * @param version the version of the policy to get.
208 * @return the policies found
209 * @throws PfModelException on errors getting policies
211 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
212 throws PfModelException {
213 LOGGER.debug("->getPolicies: name={}, version={}", name, version);
215 ToscaServiceTemplate gotServiceTempalate =
216 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
218 LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate);
219 return gotServiceTempalate;
225 * @param dao the DAO to use to access the database
226 * @param name the name of the policy to get, null to get all policies
227 * @param version the version of the policy to get, null to get all versions of a policy
228 * @return the policies found
229 * @throws PfModelException on errors getting policies
231 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
232 throws PfModelException {
233 LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
235 List<ToscaPolicy> policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version)
236 .toAuthorative().getToscaTopologyTemplate().getPolicies());
238 LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
243 * Get filtered policies.
245 * @param dao the DAO to use to access the database
246 * @param filter the filter for the policies to get
247 * @return the policies found
248 * @throws PfModelException on errors getting policies
250 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
251 throws PfModelException {
253 LOGGER.debug("->getFilteredPolicies: filter={}", filter);
254 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
256 ToscaServiceTemplate serviceTemplate =
257 new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative();
259 List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
260 filteredPolicies = filter.filter(filteredPolicies);
262 serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMapList(filteredPolicies));
264 LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, serviceTemplate);
265 return serviceTemplate;
269 * Get filtered policies.
271 * @param dao the DAO to use to access the database
272 * @param filter the filter for the policies to get
273 * @return the policies found
274 * @throws PfModelException on errors getting policies
276 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
277 throws PfModelException {
279 LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
280 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
282 List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
284 LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
291 * @param dao the DAO to use to access the database
292 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
293 * @return the TOSCA service template containing the policy types that were created
294 * @throws PfModelException on errors creating policies
296 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
297 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
299 LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
301 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
302 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
304 LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
305 return createdServiceTempalate;
311 * @param dao the DAO to use to access the database
312 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
313 * @return the TOSCA service template containing the policies that were updated
314 * @throws PfModelException on errors updating policies
316 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
317 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
319 LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
321 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
322 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
324 LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
325 return updatedServiceTempalate;
331 * @param dao the DAO to use to access the database
332 * @param name the name of the policy to delete.
333 * @param version the version of the policy to delete.
334 * @return the TOSCA service template containing the policy that weas deleted
335 * @throws PfModelException on errors deleting policies
337 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
338 @NonNull final String version) throws PfModelException {
340 LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
342 ToscaServiceTemplate deletedServiceTempalate =
343 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
345 LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
346 deletedServiceTempalate);
347 return deletedServiceTempalate;
351 * Return the contents of a list of maps as a plain list.
353 * @param listOfMaps the list of maps
354 * @return the plain list
356 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
357 List<T> returnList = new ArrayList<>();
358 for (Map<String, T> conceptMap : listOfMaps) {
359 for (T concept : conceptMap.values()) {
360 returnList.add(concept);
368 * Return the contents of a list of concepts as a list of maps of concepts.
370 * @param conceptList the concept list
371 * @return the list of concept map
373 private <T extends ToscaEntity> List<Map<String, T>> asConceptMapList(List<T> conceptList) {
374 List<Map<String, T>> toscaEntityMapList = new ArrayList<>();
375 for (T concept : conceptList) {
376 Map<String, T> conceptMap = new TreeMap<>();
377 conceptMap.put(concept.getName(), concept);
378 toscaEntityMapList.add(conceptMap);
381 return toscaEntityMapList;
385 * Return the contents of a list of concepts as map of concepts.
387 * @param conceptList the concept list
388 * @return the list of concept map
390 private <T extends ToscaEntity> Map<String, T> asConceptMap(List<T> conceptList) {
391 Map<String, T> conceptMap = new LinkedHashMap<>();
392 for (T concept : conceptList) {
393 conceptMap.put(concept.getName(), concept);