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.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.PdpGroups;
39 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
40 import org.onap.policy.models.provider.PolicyModelsProvider;
41 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
45 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
46 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
47 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
53 * a relational database.
55 * @author Liam Fallon (liam.fallon@est.tech)
57 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
58 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
60 private final PolicyModelsProviderParameters parameters;
62 // Database connection and the DAO for reading and writing Policy Framework concepts
63 private Connection connection;
67 * Constructor that takes the parameters.
69 * @param parameters the parameters for the provider
71 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
72 this.parameters = parameters;
76 public void init() throws PfModelException {
77 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
78 parameters.getPersistenceUnit());
80 // Decode the password using Base64
81 String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
83 // Connect to the database, call does not implement AutoCloseable for try-with-resources
85 connection = DriverManager.getConnection(parameters.getDatabaseUrl(), parameters.getDatabaseUser(),
87 } catch (Exception exc) {
88 String errorMessage = "could not connect to database with URL \"" + parameters.getDatabaseUrl() + "\"";
89 LOGGER.warn(errorMessage, exc);
90 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
93 // Parameters for the DAO
94 final DaoParameters daoParameters = new DaoParameters();
95 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
96 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
99 pfDao = new PfDaoFactory().createPfDao(daoParameters);
100 pfDao.init(daoParameters);
101 } catch (Exception exc) {
102 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
103 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
104 LOGGER.warn(errorMessage, exc);
107 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
112 public void close() throws PfModelException {
113 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
114 parameters.getPersistenceUnit());
121 if (connection != null) {
124 } catch (Exception exc) {
126 String errorMessage =
127 "could not close connection to database with URL \"" + parameters.getDatabaseUrl() + "\"";
128 LOGGER.warn(errorMessage, exc);
129 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
135 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
136 parameters.getPersistenceUnit());
140 public ToscaServiceTemplate getPolicyTypes(@NonNull final String name, @NonNull final String version)
141 throws PfModelException {
143 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
147 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
148 throws PfModelException {
150 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
154 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
155 throws PfModelException {
157 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
161 public ToscaServiceTemplate deletePolicyTypes(@NonNull final String name, @NonNull final String version)
162 throws PfModelException {
164 return new AuthorativeToscaProvider().deletePolicyTypes(pfDao, name, version);
168 public ToscaServiceTemplate getPolicies(@NonNull final String name, @NonNull final String version)
169 throws PfModelException {
171 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
175 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
176 throws PfModelException {
178 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
182 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
183 throws PfModelException {
185 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
189 public ToscaServiceTemplate deletePolicies(@NonNull final String name, @NonNull final String version)
190 throws PfModelException {
192 return new AuthorativeToscaProvider().deletePolicies(pfDao, name, version);
196 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
198 return new LegacyProvider().getOperationalPolicy(pfDao, policyId);
202 public LegacyOperationalPolicy createOperationalPolicy(
203 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
205 return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
209 public LegacyOperationalPolicy updateOperationalPolicy(
210 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
212 return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
216 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
218 return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId);
222 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
224 return new LegacyProvider().getGuardPolicy(pfDao, policyId);
228 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
229 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
231 return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
235 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
236 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
238 return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
242 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
243 throws PfModelException {
245 return new LegacyProvider().deleteGuardPolicy(pfDao, policyId);
249 public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
251 return new PdpProvider().getPdpGroups(pfDao, pdpGroupFilter);
255 public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
257 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
261 public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
263 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
267 public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
269 return new PdpProvider().deletePdpGroups(pfDao, pdpGroupFilter);
273 * Check if the model provider is initialized.
275 private void assertInitilized() {
277 String errorMessage = "policy models provider is not initilaized";
278 LOGGER.warn(errorMessage);
279 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
284 * Hook for unit test mocking of database connection.
286 * @param client the mocked client
288 protected void setConnection(final Connection connection) {
289 this.connection = connection;