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.provider.impl;
24 import java.util.Base64;
25 import java.util.List;
27 import java.util.Properties;
29 import javax.ws.rs.core.Response;
31 import lombok.NonNull;
33 import org.eclipse.persistence.config.PersistenceUnitProperties;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.dao.DaoParameters;
37 import org.onap.policy.models.dao.PfDao;
38 import org.onap.policy.models.dao.PfDaoFactory;
39 import org.onap.policy.models.dao.impl.DefaultPfDao;
40 import org.onap.policy.models.pdp.concepts.Pdp;
41 import org.onap.policy.models.pdp.concepts.PdpGroup;
42 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
43 import org.onap.policy.models.pdp.concepts.PdpStatistics;
44 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
45 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
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.ToscaPolicy;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
53 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
54 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
55 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
56 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
57 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
63 * a relational database.
65 * @author Liam Fallon (liam.fallon@est.tech)
67 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
69 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
71 private final PolicyModelsProviderParameters parameters;
73 // Database connection and the DAO for reading and writing Policy Framework concepts
77 * Constructor that takes the parameters.
79 * @param parameters the parameters for the provider
81 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
82 this.parameters = parameters;
86 public void init() throws PfModelException {
87 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
88 parameters.getPersistenceUnit());
91 String errorMessage = "provider is already initialized";
92 LOGGER.warn(errorMessage);
93 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
96 // Parameters for the DAO
97 final DaoParameters daoParameters = new DaoParameters();
98 daoParameters.setPluginClass(DefaultPfDao.class.getName());
99 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
101 // Decode the password using Base64
102 String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
105 Properties jdbcProperties = new Properties();
106 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver());
107 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl());
108 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser());
109 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, decodedPassword);
112 daoParameters.setJdbcProperties(jdbcProperties);
114 pfDao = new PfDaoFactory().createPfDao(daoParameters);
116 pfDao = new PfDaoFactory().createPfDao(daoParameters);
117 pfDao.init(daoParameters);
118 } catch (Exception exc) {
119 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
120 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
121 LOGGER.warn(errorMessage, exc);
124 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
129 public void close() throws PfModelException {
130 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
131 parameters.getPersistenceUnit());
138 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
139 parameters.getPersistenceUnit());
143 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
145 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
149 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
151 return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
155 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
157 return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
161 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
162 throws PfModelException {
164 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
168 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
169 throws PfModelException {
171 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
175 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
176 throws PfModelException {
178 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
182 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
183 throws PfModelException {
185 return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
189 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
191 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
195 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
197 return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
201 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
203 return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
207 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
209 return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
214 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
215 throws PfModelException {
217 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
221 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
222 throws PfModelException {
224 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
228 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
229 throws PfModelException {
231 return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
235 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId, final String policyVersion)
236 throws PfModelException {
238 return new LegacyProvider().getOperationalPolicy(pfDao, policyId, policyVersion);
242 public LegacyOperationalPolicy createOperationalPolicy(
243 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
245 return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
249 public LegacyOperationalPolicy updateOperationalPolicy(
250 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
252 return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
256 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId,
257 @NonNull final String policyVersion) throws PfModelException {
259 return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId, policyVersion);
263 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId,
264 final String policyVersion) throws PfModelException {
266 return new LegacyProvider().getGuardPolicy(pfDao, policyId, policyVersion);
270 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
271 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
273 return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
277 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
278 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
280 return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
284 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId,
285 @NonNull final String policyVersion) throws PfModelException {
287 return new LegacyProvider().deleteGuardPolicy(pfDao, policyId, policyVersion);
291 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
293 return new PdpProvider().getPdpGroups(pfDao, name);
297 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
299 return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
303 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
305 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
309 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
311 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
315 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
316 throws PfModelException {
318 new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup);
322 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
323 throws PfModelException {
324 new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpSubGroup, pdp);
328 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
330 return new PdpProvider().deletePdpGroup(pfDao, name);
334 public List<PdpStatistics> getPdpStatistics(final String name) throws PfModelException {
336 return new PdpProvider().getPdpStatistics(pfDao, name);
340 public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpType,
341 @NonNull final String pdpInstanceId, @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
343 new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpType, pdpInstanceId, pdpStatistics);
347 * Check if the model provider is initialized.
349 private void assertInitilized() {
351 String errorMessage = "policy models provider is not initilaized";
352 LOGGER.warn(errorMessage);
353 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);