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;
26 import java.util.List;
29 import javax.ws.rs.core.Response;
31 import lombok.NonNull;
33 import org.apache.commons.lang3.tuple.Pair;
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.PdpGroup;
41 import org.onap.policy.models.pdp.concepts.PdpGroups;
42 import org.onap.policy.models.pdp.concepts.PdpStatistics;
43 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
44 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
45 import org.onap.policy.models.provider.PolicyModelsProvider;
46 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
51 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
52 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
53 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
54 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
60 * a relational database.
62 * @author Liam Fallon (liam.fallon@est.tech)
64 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
65 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
67 private final PolicyModelsProviderParameters parameters;
69 // Database connection and the DAO for reading and writing Policy Framework concepts
70 private Connection connection;
74 * Constructor that takes the parameters.
76 * @param parameters the parameters for the provider
78 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
79 this.parameters = parameters;
83 public void init() throws PfModelException {
84 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
85 parameters.getPersistenceUnit());
87 // Decode the password using Base64
88 String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
90 // Connect to the database, call does not implement AutoCloseable for try-with-resources
92 connection = DriverManager.getConnection(parameters.getDatabaseUrl(), parameters.getDatabaseUser(),
94 } catch (Exception exc) {
95 String errorMessage = "could not connect to database with URL \"" + parameters.getDatabaseUrl() + "\"";
96 LOGGER.warn(errorMessage, exc);
97 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
100 // Parameters for the DAO
101 final DaoParameters daoParameters = new DaoParameters();
102 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
103 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
106 pfDao = new PfDaoFactory().createPfDao(daoParameters);
107 pfDao.init(daoParameters);
108 } catch (Exception exc) {
109 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
110 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
111 LOGGER.warn(errorMessage, exc);
114 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
119 public void close() throws PfModelException {
120 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
121 parameters.getPersistenceUnit());
128 if (connection != null) {
131 } catch (Exception exc) {
133 String errorMessage =
134 "could not close connection to database with URL \"" + parameters.getDatabaseUrl() + "\"";
135 LOGGER.warn(errorMessage, exc);
136 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
142 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
143 parameters.getPersistenceUnit());
147 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
149 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
153 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
155 return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
159 public ToscaServiceTemplate getLatestPolicyTypes(final String name) throws PfModelException {
161 return new AuthorativeToscaProvider().getLatestPolicyTypes(pfDao, name);
165 public List<ToscaPolicyType> getLatestPolicyTypeList(final String name) throws PfModelException {
167 return new AuthorativeToscaProvider().getLatestPolicyTypeList(pfDao, name);
171 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
172 throws PfModelException {
174 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
178 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
179 throws PfModelException {
181 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
185 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
186 throws PfModelException {
188 return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
192 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
194 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
198 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
200 return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
204 public List<ToscaPolicy> getPolicyList4PolicyType(@NonNull final String policyTypeName) throws PfModelException {
206 return new AuthorativeToscaProvider().getPolicyList4PolicyType(pfDao, policyTypeName);
210 public ToscaServiceTemplate getLatestPolicies(final String name) throws PfModelException {
212 return new AuthorativeToscaProvider().getLatestPolicies(pfDao, name);
216 public List<ToscaPolicy> getLatestPolicyList(final String name) throws PfModelException {
218 return new AuthorativeToscaProvider().getLatestPolicyList(pfDao, name);
222 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
223 throws PfModelException {
225 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
229 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
230 throws PfModelException {
232 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
236 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
237 throws PfModelException {
239 return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
243 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
245 return new LegacyProvider().getOperationalPolicy(pfDao, policyId);
249 public LegacyOperationalPolicy createOperationalPolicy(
250 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
252 return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
256 public LegacyOperationalPolicy updateOperationalPolicy(
257 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
259 return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
263 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
265 return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId);
269 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
271 return new LegacyProvider().getGuardPolicy(pfDao, policyId);
275 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
276 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
278 return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
282 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
283 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
285 return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
289 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
290 throws PfModelException {
292 return new LegacyProvider().deleteGuardPolicy(pfDao, policyId);
296 public PdpGroups getPdpGroups(final String name, final String version) throws PfModelException {
298 return new PdpProvider().getPdpGroups(pfDao, name, version);
302 public PdpGroups getLatestPdpGroups(final String name) throws PfModelException {
304 return new PdpProvider().getLatestPdpGroups(pfDao, name);
308 public PdpGroups getFilteredPdpGroups(@NonNull final String pdpType,
309 @NonNull final List<Pair<String, String>> supportedPolicyTypes) {
311 return new PdpProvider().getFilteredPdpGroups(pfDao, pdpType, supportedPolicyTypes);
315 public PdpGroups createPdpGroups(@NonNull final PdpGroups pdpGroups) throws PfModelException {
317 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
321 public PdpGroups updatePdpGroups(@NonNull final PdpGroups pdpGroups) throws PfModelException {
323 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
327 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
328 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
330 new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup);
334 public PdpGroup deletePdpGroup(@NonNull final String name, @NonNull final String version) throws PfModelException {
336 return new PdpProvider().deletePdpGroup(pfDao, name, version);
340 public List<PdpStatistics> getPdpStatistics(final String name, final String version) throws PfModelException {
342 return new PdpProvider().getPdpStatistics(pfDao, name, version);
346 public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
347 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
348 @NonNull final PdpStatistics pdppStatistics) throws PfModelException {
350 new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpGroupVersion, pdpType, pdpInstanceId,
355 public Map<PdpGroup, List<ToscaPolicy>> getDeployedPolicyList(final String name) throws PfModelException {
357 return new PdpProvider().getDeployedPolicyList(pfDao, name);
361 * Check if the model provider is initialized.
363 private void assertInitilized() {
365 String errorMessage = "policy models provider is not initilaized";
366 LOGGER.warn(errorMessage);
367 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
372 * Hook for unit test mocking of database connection.
374 * @param client the mocked client
376 protected void setConnection(final Connection connection) {
377 this.connection = connection;