2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.provider.impl;
25 import java.util.Collection;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.Properties;
29 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
31 import org.eclipse.persistence.config.PersistenceUnitProperties;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.onap.policy.models.dao.DaoParameters;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.dao.PfDaoFactory;
37 import org.onap.policy.models.dao.impl.DefaultPfDao;
38 import org.onap.policy.models.pdp.concepts.Pdp;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
41 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
42 import org.onap.policy.models.pdp.concepts.PdpStatistics;
43 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
44 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
45 import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
46 import org.onap.policy.models.provider.PolicyModelsProvider;
47 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter;
56 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
62 * a relational database.
64 * @author Liam Fallon (liam.fallon@est.tech)
66 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
68 private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class);
70 private final PolicyModelsProviderParameters parameters;
72 // Database connection and the DAO for reading and writing Policy Framework concepts
76 * Constructor that takes the parameters.
78 * @param parameters the parameters for the provider
80 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
81 this.parameters = parameters;
85 public void init() throws PfModelException {
86 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
87 parameters.getPersistenceUnit());
90 String errorMessage = "provider is already initialized";
91 LOGGER.warn(errorMessage);
92 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
95 // Parameters for the DAO
96 final DaoParameters daoParameters = new DaoParameters();
97 daoParameters.setPluginClass(DefaultPfDao.class.getName());
98 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
101 Properties jdbcProperties = new Properties();
102 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver());
103 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl());
104 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser());
105 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
108 daoParameters.setJdbcProperties(jdbcProperties);
111 pfDao = new PfDaoFactory().createPfDao(daoParameters);
112 pfDao.init(daoParameters);
113 } catch (Exception exc) {
114 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
115 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
118 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
123 public void close() throws PfModelException {
124 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
125 parameters.getPersistenceUnit());
132 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
133 parameters.getPersistenceUnit());
137 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
138 throws PfModelException {
140 return new AuthorativeToscaProvider().getServiceTemplateList(pfDao, name, version);
145 public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull ToscaServiceTemplateFilter filter)
146 throws PfModelException {
148 return new AuthorativeToscaProvider().getFilteredServiceTemplateList(pfDao, filter);
152 public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
153 throws PfModelException {
155 return new AuthorativeToscaProvider().createServiceTemplate(pfDao, serviceTemplate);
159 public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
160 throws PfModelException {
162 return new AuthorativeToscaProvider().updateServiceTemplate(pfDao, serviceTemplate);
166 public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
167 throws PfModelException {
170 return new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, name, version);
174 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
176 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
180 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
182 return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
186 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
188 return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
192 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
193 throws PfModelException {
195 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
199 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
200 throws PfModelException {
202 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
206 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
207 throws PfModelException {
209 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
213 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
214 throws PfModelException {
217 ToscaConceptIdentifier policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
218 assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
220 return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
224 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
226 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
230 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
232 return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
236 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
238 return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
242 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
244 return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
248 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
249 throws PfModelException {
251 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
255 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
256 throws PfModelException {
258 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
262 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
263 throws PfModelException {
266 ToscaConceptIdentifier policyIdentifier = new ToscaConceptIdentifier(name, version);
267 assertPolicyNotDeployedInPdpGroup(policyIdentifier);
269 return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
273 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
275 return new PdpProvider().getPdpGroups(pfDao, name);
279 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
281 return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
285 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
287 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
291 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
293 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
297 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
298 throws PfModelException {
300 new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup);
304 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
305 throws PfModelException {
306 new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpSubGroup, pdp);
310 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
312 return new PdpProvider().deletePdpGroup(pfDao, name);
316 public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException {
318 return new PdpStatisticsProvider().getPdpStatistics(pfDao, name, timestamp);
322 public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName,
323 final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp, final String sortOrder,
324 final int getRecordNum) throws PfModelException {
326 return new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, name, pdpGroupName, pdpSubGroup,
327 startTimeStamp, endTimeStamp, sortOrder, getRecordNum);
331 public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
332 throws PfModelException {
334 return new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsList);
338 public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
339 throws PfModelException {
341 return new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsList);
345 public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Date timestamp)
346 throws PfModelException {
348 return new PdpStatisticsProvider().deletePdpStatistics(pfDao, name, timestamp);
352 public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
354 return new PdpProvider().getAllPolicyStatus(pfDao);
358 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
359 throws PfModelException {
361 return new PdpProvider().getAllPolicyStatus(pfDao, policy);
365 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName)
366 throws PfModelException {
368 return new PdpProvider().getGroupPolicyStatus(pfDao, groupName);
372 public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs,
373 Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
375 new PdpProvider().cudPolicyStatus(pfDao, createObjs, updateObjs, deleteObjs);
379 * Check if the model provider is initialized.
381 private void assertInitialized() {
383 String errorMessage = "policy models provider is not initilaized";
384 LOGGER.warn(errorMessage);
385 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
390 * Assert that the policy type is not supported in any PDP group.
392 * @param policyTypeIdentifier the policy type identifier
393 * @throws PfModelException if the policy type is supported in a PDP group
395 private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
396 throws PfModelException {
397 for (PdpGroup pdpGroup : getPdpGroups(null)) {
398 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
399 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
400 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
401 "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
402 + pdpSubGroup.getPdpType());
409 * Assert that the policy is not deployed in a PDP group.
411 * @param policyIdentifier the identifier of the policy
412 * @throws PfModelException thrown if the policy is deployed in a PDP group
414 private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
415 throws PfModelException {
416 for (PdpGroup pdpGroup : getPdpGroups(null)) {
417 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
418 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
419 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
420 "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
421 + pdpSubGroup.getPdpType());