2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.provider.impl;
24 import java.util.Base64;
25 import java.util.Date;
26 import java.util.List;
28 import java.util.Properties;
30 import javax.ws.rs.core.Response;
32 import lombok.NonNull;
34 import org.eclipse.persistence.config.PersistenceUnitProperties;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.dao.DaoParameters;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.dao.PfDaoFactory;
40 import org.onap.policy.models.dao.impl.DefaultPfDao;
41 import org.onap.policy.models.pdp.concepts.Pdp;
42 import org.onap.policy.models.pdp.concepts.PdpGroup;
43 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
44 import org.onap.policy.models.pdp.concepts.PdpStatistics;
45 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
46 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
47 import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
48 import org.onap.policy.models.provider.PolicyModelsProvider;
49 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
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.ToscaPolicyIdentifier;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
57 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
58 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
59 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
60 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
61 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
67 * a relational database.
69 * @author Liam Fallon (liam.fallon@est.tech)
71 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
73 private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class);
75 private final PolicyModelsProviderParameters parameters;
77 // Database connection and the DAO for reading and writing Policy Framework concepts
81 * Constructor that takes the parameters.
83 * @param parameters the parameters for the provider
85 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
86 this.parameters = parameters;
90 public void init() throws PfModelException {
91 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
92 parameters.getPersistenceUnit());
95 String errorMessage = "provider is already initialized";
96 LOGGER.warn(errorMessage);
97 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
100 // Parameters for the DAO
101 final DaoParameters daoParameters = new DaoParameters();
102 daoParameters.setPluginClass(DefaultPfDao.class.getName());
103 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
105 // Decode the password using Base64
106 String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
109 Properties jdbcProperties = new Properties();
110 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver());
111 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl());
112 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser());
113 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, decodedPassword);
116 daoParameters.setJdbcProperties(jdbcProperties);
119 pfDao = new PfDaoFactory().createPfDao(daoParameters);
120 pfDao.init(daoParameters);
121 } catch (Exception exc) {
122 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
123 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
126 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
131 public void close() throws PfModelException {
132 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
133 parameters.getPersistenceUnit());
140 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
141 parameters.getPersistenceUnit());
145 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
147 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
151 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
153 return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
157 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
159 return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
163 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
164 throws PfModelException {
166 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
170 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
171 throws PfModelException {
173 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
177 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
178 throws PfModelException {
180 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
184 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
185 throws PfModelException {
188 ToscaPolicyTypeIdentifier policyTypeIdentifier = new ToscaPolicyTypeIdentifier(name, version);
189 assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
191 return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
195 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
197 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
201 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
203 return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
207 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
209 return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
213 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
215 return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
219 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
220 throws PfModelException {
222 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
226 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
227 throws PfModelException {
229 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
233 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
234 throws PfModelException {
237 ToscaPolicyIdentifier policyIdentifier = new ToscaPolicyIdentifier(name, version);
238 assertPolicyNotDeployedInPdpGroup(policyIdentifier);
240 return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
244 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId, final String policyVersion)
245 throws PfModelException {
247 return new LegacyProvider().getOperationalPolicy(pfDao, policyId, policyVersion);
251 public LegacyOperationalPolicy createOperationalPolicy(
252 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
254 return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
258 public LegacyOperationalPolicy updateOperationalPolicy(
259 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
261 return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
265 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId,
266 @NonNull final String policyVersion) throws PfModelException {
269 assertPolicyNotDeployedInPdpGroup(
270 new ToscaPolicyIdentifier(policyId, policyVersion + LegacyProvider.LEGACY_MINOR_PATCH_SUFFIX));
272 return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId, policyVersion);
276 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId,
277 final String policyVersion) throws PfModelException {
279 return new LegacyProvider().getGuardPolicy(pfDao, policyId, policyVersion);
283 public Map<String, LegacyGuardPolicyOutput>
284 createGuardPolicy(@NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
286 return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
290 public Map<String, LegacyGuardPolicyOutput>
291 updateGuardPolicy(@NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
293 return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
297 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId,
298 @NonNull final String policyVersion) throws PfModelException {
301 assertPolicyNotDeployedInPdpGroup(
302 new ToscaPolicyIdentifier(policyId, policyVersion + LegacyProvider.LEGACY_MINOR_PATCH_SUFFIX));
304 return new LegacyProvider().deleteGuardPolicy(pfDao, policyId, policyVersion);
308 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
310 return new PdpProvider().getPdpGroups(pfDao, name);
314 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
316 return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
320 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
322 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
326 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
328 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
332 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
333 throws PfModelException {
335 new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup);
339 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
340 throws PfModelException {
341 new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpSubGroup, pdp);
345 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
347 return new PdpProvider().deletePdpGroup(pfDao, name);
351 public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException {
353 return new PdpStatisticsProvider().getPdpStatistics(pfDao, name, timestamp);
357 public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName,
358 final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp, final String sortOrder,
359 final int getRecordNum) throws PfModelException {
361 return new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, name, pdpGroupName, pdpSubGroup,
362 startTimeStamp, endTimeStamp, sortOrder, getRecordNum);
366 public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
367 throws PfModelException {
369 return new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsList);
373 public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
374 throws PfModelException {
376 return new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsList);
380 public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Date timestamp)
381 throws PfModelException {
383 return new PdpStatisticsProvider().deletePdpStatistics(pfDao, name, timestamp);
387 * Check if the model provider is initialized.
389 private void assertInitialized() {
391 String errorMessage = "policy models provider is not initilaized";
392 LOGGER.warn(errorMessage);
393 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
398 * Assert that the policy type is not supported in any PDP group.
400 * @param policyTypeIdentifier the policy type identifier
401 * @throws PfModelException if the policy type is supported in a PDP group
403 private void assertPolicyTypeNotSupportedInPdpGroup(ToscaPolicyTypeIdentifier policyTypeIdentifier)
404 throws PfModelException {
405 for (PdpGroup pdpGroup : getPdpGroups(null)) {
406 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
407 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
408 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
409 "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
410 + pdpSubGroup.getPdpType());
417 * Assert that the policy is not deployed in a PDP group.
419 * @param policyIdentifier the identifier of the policy
420 * @throws PfModelException thrown if the policy is deployed in a PDP group
422 private void assertPolicyNotDeployedInPdpGroup(final ToscaPolicyIdentifier policyIdentifier)
423 throws PfModelException {
424 for (PdpGroup pdpGroup : getPdpGroups(null)) {
425 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
426 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
427 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
428 "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
429 + pdpSubGroup.getPdpType());