2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.provider.impl;
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.util.Base64;
28 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.dao.DaoParameters;
36 import org.onap.policy.models.dao.PfDao;
37 import org.onap.policy.models.dao.PfDaoFactory;
38 import org.onap.policy.models.dao.impl.DefaultPfDao;
39 import org.onap.policy.models.pdp.concepts.PdpGroups;
40 import org.onap.policy.models.pdp.provider.PdpProvider;
41 import org.onap.policy.models.provider.PolicyModelsProvider;
42 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
43 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
45 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
46 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
47 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
54 * a relational database.
56 * @author Liam Fallon (liam.fallon@est.tech)
58 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
59 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
61 private final PolicyModelsProviderParameters parameters;
63 // Database connection and the DAO for reading and writing Policy Framework concepts
64 private Connection connection;
68 * Constructor that takes the parameters.
70 * @param parameters the parameters for the provider
72 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
73 this.parameters = parameters;
77 public void init() throws PfModelException {
78 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
79 parameters.getPersistenceUnit());
81 // Decode the password using Base64
82 String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
84 // Connect to the database, call does not implement AutoCloseable for try-with-resources
86 connection = DriverManager.getConnection(parameters.getDatabaseUrl(), parameters.getDatabaseUser(),
88 } catch (Exception exc) {
89 String errorMessage = "could not connect to database with URL \"" + parameters.getDatabaseUrl() + "\"";
90 LOGGER.warn(errorMessage, exc);
91 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
94 // Parameters for the DAO
95 final DaoParameters daoParameters = new DaoParameters();
96 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
97 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
100 pfDao = new PfDaoFactory().createPfDao(daoParameters);
101 pfDao.init(daoParameters);
102 } catch (Exception exc) {
103 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
104 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
105 LOGGER.warn(errorMessage, exc);
108 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
113 public void close() throws PfModelException {
114 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
115 parameters.getPersistenceUnit());
122 if (connection != null) {
125 } catch (Exception exc) {
127 String errorMessage =
128 "could not close connection to database with URL \"" + parameters.getDatabaseUrl() + "\"";
129 LOGGER.warn(errorMessage, exc);
130 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
136 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
137 parameters.getPersistenceUnit());
141 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException {
143 return new SimpleToscaProvider().getPolicyTypes(pfDao, policyTypeKey);
147 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
148 throws PfModelException {
150 return new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
154 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
155 throws PfModelException {
157 return new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
161 public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException {
163 return new SimpleToscaProvider().deletePolicyTypes(pfDao, policyTypeKey);
167 public ToscaServiceTemplate getPolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
169 return new SimpleToscaProvider().getPolicies(pfDao, policyKey);
173 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
174 throws PfModelException {
176 return new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate);
180 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
181 throws PfModelException {
183 return new SimpleToscaProvider().updatePolicies(pfDao, serviceTemplate);
187 public ToscaServiceTemplate deletePolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
189 return new SimpleToscaProvider().deletePolicies(pfDao, policyKey);
193 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
195 return new LegacyProvider().getOperationalPolicy(pfDao, policyId);
199 public LegacyOperationalPolicy createOperationalPolicy(
200 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
202 return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
206 public LegacyOperationalPolicy updateOperationalPolicy(
207 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
209 return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
213 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
215 return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId);
219 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
221 return new LegacyProvider().getGuardPolicy(pfDao, policyId);
225 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
226 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
228 return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
232 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
233 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
235 return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
239 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
240 throws PfModelException {
242 return new LegacyProvider().deleteGuardPolicy(pfDao, policyId);
246 public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
248 return new PdpProvider().getPdpGroups(pfDao, pdpGroupFilter);
252 public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
254 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
258 public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
260 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
264 public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
266 return new PdpProvider().deletePdpGroups(pfDao, pdpGroupFilter);
270 * Check if the model provider is initialized.
272 private void assertInitilized() {
274 String errorMessage = "policy models provider is not initilaized";
275 LOGGER.warn(errorMessage);
276 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
281 * Hook for unit test mocking of database connection.
283 * @param client the mocked client
285 protected void setConnection(final Connection connection) {
286 this.connection = connection;