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.PdpStatistics;
42 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
43 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
44 import org.onap.policy.models.provider.PolicyModelsProvider;
45 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
50 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
51 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
52 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
53 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
59 * a relational database.
61 * @author Liam Fallon (liam.fallon@est.tech)
63 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
64 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
66 private final PolicyModelsProviderParameters parameters;
68 // Database connection and the DAO for reading and writing Policy Framework concepts
69 private Connection connection;
73 * Constructor that takes the parameters.
75 * @param parameters the parameters for the provider
77 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
78 this.parameters = parameters;
82 public void init() throws PfModelException {
83 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
84 parameters.getPersistenceUnit());
86 if (connection != null || pfDao != null) {
87 String errorMessage = "provider is already initialized";
88 LOGGER.warn(errorMessage);
89 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
92 // Decode the password using Base64
93 String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
95 // Connect to the database, call does not implement AutoCloseable for try-with-resources
97 connection = DriverManager.getConnection(parameters.getDatabaseUrl(), parameters.getDatabaseUser(),
99 } catch (Exception exc) {
100 String errorMessage = "could not connect to database with URL \"" + parameters.getDatabaseUrl() + "\"";
101 LOGGER.warn(errorMessage, exc);
102 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
105 // Parameters for the DAO
106 final DaoParameters daoParameters = new DaoParameters();
107 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
108 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
111 pfDao = new PfDaoFactory().createPfDao(daoParameters);
112 pfDao.init(daoParameters);
113 } catch (Exception exc) {
114 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
115 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
116 LOGGER.warn(errorMessage, exc);
119 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
124 public void close() throws PfModelException {
125 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
126 parameters.getPersistenceUnit());
133 if (connection != null) {
136 } catch (Exception exc) {
138 String errorMessage =
139 "could not close connection to database with URL \"" + parameters.getDatabaseUrl() + "\"";
140 LOGGER.warn(errorMessage, exc);
141 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
147 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
148 parameters.getPersistenceUnit());
152 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
154 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
158 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
160 return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
164 public ToscaServiceTemplate getLatestPolicyTypes(final String name) throws PfModelException {
166 return new AuthorativeToscaProvider().getLatestPolicyTypes(pfDao, name);
170 public List<ToscaPolicyType> getLatestPolicyTypeList(final String name) throws PfModelException {
172 return new AuthorativeToscaProvider().getLatestPolicyTypeList(pfDao, name);
176 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
177 throws PfModelException {
179 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
183 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
184 throws PfModelException {
186 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
190 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
191 throws PfModelException {
193 return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
197 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
199 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
203 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
205 return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
209 public List<ToscaPolicy> getPolicyList4PolicyType(@NonNull final String policyTypeName,
210 final String policyTypeVersion) throws PfModelException {
212 return new AuthorativeToscaProvider().getPolicyList4PolicyType(pfDao, policyTypeName, policyTypeVersion);
216 public ToscaServiceTemplate getLatestPolicies(final String name) throws PfModelException {
218 return new AuthorativeToscaProvider().getLatestPolicies(pfDao, name);
222 public List<ToscaPolicy> getLatestPolicyList(final String name) throws PfModelException {
224 return new AuthorativeToscaProvider().getLatestPolicyList(pfDao, name);
228 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
229 throws PfModelException {
231 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
235 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
236 throws PfModelException {
238 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
242 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
243 throws PfModelException {
245 return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
249 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
251 return new LegacyProvider().getOperationalPolicy(pfDao, policyId);
255 public LegacyOperationalPolicy createOperationalPolicy(
256 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
258 return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
262 public LegacyOperationalPolicy updateOperationalPolicy(
263 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
265 return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
269 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
271 return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId);
275 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
277 return new LegacyProvider().getGuardPolicy(pfDao, policyId);
281 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
282 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
284 return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
288 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
289 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
291 return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
295 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
296 throws PfModelException {
298 return new LegacyProvider().deleteGuardPolicy(pfDao, policyId);
302 public List<PdpGroup> getPdpGroups(final String name, final String version) throws PfModelException {
304 return new PdpProvider().getPdpGroups(pfDao, name, version);
308 public List<PdpGroup> getLatestPdpGroups(final String name) throws PfModelException {
310 return new PdpProvider().getLatestPdpGroups(pfDao, name);
314 public List<PdpGroup> getFilteredPdpGroups(final String pdpType,
315 @NonNull final List<Pair<String, String>> supportedPolicyTypes) {
317 return new PdpProvider().getFilteredPdpGroups(pfDao, pdpType, supportedPolicyTypes);
321 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
323 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
327 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
329 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
333 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
334 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
336 new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup);
340 public PdpGroup deletePdpGroup(@NonNull final String name, @NonNull final String version) throws PfModelException {
342 return new PdpProvider().deletePdpGroup(pfDao, name, version);
346 public List<PdpStatistics> getPdpStatistics(final String name, final String version) throws PfModelException {
348 return new PdpProvider().getPdpStatistics(pfDao, name, version);
352 public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
353 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
354 @NonNull final PdpStatistics pdppStatistics) throws PfModelException {
356 new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpGroupVersion, pdpType, pdpInstanceId,
361 public Map<Pair<String, String>, List<ToscaPolicy>> getDeployedPolicyList(final String name)
362 throws PfModelException {
364 return new PdpProvider().getDeployedPolicyList(pfDao, name);
368 * Check if the model provider is initialized.
370 private void assertInitilized() {
372 String errorMessage = "policy models provider is not initilaized";
373 LOGGER.warn(errorMessage);
374 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
379 * Hook for unit test mocking of database connection.
381 * @param client the mocked client
383 protected void setConnection(final Connection connection) {
384 this.connection = connection;