2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 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.Date;
26 import java.util.List;
27 import java.util.Properties;
28 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
30 import org.eclipse.persistence.config.PersistenceUnitProperties;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.dao.DaoParameters;
34 import org.onap.policy.models.dao.PfDao;
35 import org.onap.policy.models.dao.PfDaoFactory;
36 import org.onap.policy.models.dao.impl.DefaultPfDao;
37 import org.onap.policy.models.pdp.concepts.Pdp;
38 import org.onap.policy.models.pdp.concepts.PdpGroup;
39 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
40 import org.onap.policy.models.pdp.concepts.PdpStatistics;
41 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
42 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
43 import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
44 import org.onap.policy.models.provider.PolicyModelsProvider;
45 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter;
53 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
59 * a relational database.
61 * @author Liam Fallon (liam.fallon@est.tech)
63 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
65 private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class);
67 private final PolicyModelsProviderParameters parameters;
69 // Database connection and the DAO for reading and writing Policy Framework concepts
73 * Constructor that takes the parameters.
75 * @param parameters the parameters for the provider
77 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
78 this.parameters = parameters;
82 public void init() throws PfModelException {
83 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
84 parameters.getPersistenceUnit());
87 String errorMessage = "provider is already initialized";
88 LOGGER.warn(errorMessage);
89 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
92 // Parameters for the DAO
93 final DaoParameters daoParameters = new DaoParameters();
94 daoParameters.setPluginClass(DefaultPfDao.class.getName());
95 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
98 Properties jdbcProperties = new Properties();
99 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver());
100 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl());
101 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser());
102 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
105 daoParameters.setJdbcProperties(jdbcProperties);
108 pfDao = new PfDaoFactory().createPfDao(daoParameters);
109 pfDao.init(daoParameters);
110 } catch (Exception exc) {
111 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
112 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
115 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
120 public void close() throws PfModelException {
121 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
122 parameters.getPersistenceUnit());
129 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
130 parameters.getPersistenceUnit());
134 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
135 throws PfModelException {
137 return new AuthorativeToscaProvider().getServiceTemplateList(pfDao, name, version);
142 public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull ToscaServiceTemplateFilter filter)
143 throws PfModelException {
145 return new AuthorativeToscaProvider().getFilteredServiceTemplateList(pfDao, filter);
149 public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
150 throws PfModelException {
152 return new AuthorativeToscaProvider().createServiceTemplate(pfDao, serviceTemplate);
156 public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
157 throws PfModelException {
159 return new AuthorativeToscaProvider().updateServiceTemplate(pfDao, serviceTemplate);
163 public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
164 throws PfModelException {
167 return new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, name, version);
171 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
173 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
177 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
179 return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
183 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
185 return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
189 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
190 throws PfModelException {
192 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
196 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
197 throws PfModelException {
199 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
203 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
204 throws PfModelException {
206 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
210 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
211 throws PfModelException {
214 ToscaConceptIdentifier policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
215 assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
217 return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
221 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
223 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
227 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
229 return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
233 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
235 return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
239 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
241 return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
245 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
246 throws PfModelException {
248 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
252 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
253 throws PfModelException {
255 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
259 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
260 throws PfModelException {
263 ToscaConceptIdentifier policyIdentifier = new ToscaConceptIdentifier(name, version);
264 assertPolicyNotDeployedInPdpGroup(policyIdentifier);
266 return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
270 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
272 return new PdpProvider().getPdpGroups(pfDao, name);
276 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
278 return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
282 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
284 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
288 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
290 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
294 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
295 throws PfModelException {
297 new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup);
301 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
302 throws PfModelException {
303 new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpSubGroup, pdp);
307 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
309 return new PdpProvider().deletePdpGroup(pfDao, name);
313 public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException {
315 return new PdpStatisticsProvider().getPdpStatistics(pfDao, name, timestamp);
319 public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName,
320 final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp, final String sortOrder,
321 final int getRecordNum) throws PfModelException {
323 return new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, name, pdpGroupName, pdpSubGroup,
324 startTimeStamp, endTimeStamp, sortOrder, getRecordNum);
328 public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
329 throws PfModelException {
331 return new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsList);
335 public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
336 throws PfModelException {
338 return new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsList);
342 public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Date timestamp)
343 throws PfModelException {
345 return new PdpStatisticsProvider().deletePdpStatistics(pfDao, name, timestamp);
349 * Check if the model provider is initialized.
351 private void assertInitialized() {
353 String errorMessage = "policy models provider is not initilaized";
354 LOGGER.warn(errorMessage);
355 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
360 * Assert that the policy type is not supported in any PDP group.
362 * @param policyTypeIdentifier the policy type identifier
363 * @throws PfModelException if the policy type is supported in a PDP group
365 private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
366 throws PfModelException {
367 for (PdpGroup pdpGroup : getPdpGroups(null)) {
368 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
369 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
370 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
371 "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
372 + pdpSubGroup.getPdpType());
379 * Assert that the policy is not deployed in a PDP group.
381 * @param policyIdentifier the identifier of the policy
382 * @throws PfModelException thrown if the policy is deployed in a PDP group
384 private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
385 throws PfModelException {
386 for (PdpGroup pdpGroup : getPdpGroups(null)) {
387 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
388 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
389 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
390 "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
391 + pdpSubGroup.getPdpType());