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.util.Base64;
24 import java.util.List;
26 import java.util.Properties;
28 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
32 import org.eclipse.persistence.config.PersistenceUnitProperties;
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.Pdp;
40 import org.onap.policy.models.pdp.concepts.PdpGroup;
41 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
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.ToscaPolicyFilter;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
53 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
54 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
55 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
56 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
62 * a relational database.
64 * @author Liam Fallon (liam.fallon@est.tech)
66 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
68 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
70 private final PolicyModelsProviderParameters parameters;
72 // Database connection and the DAO for reading and writing Policy Framework concepts
76 * Constructor that takes the parameters.
78 * @param parameters the parameters for the provider
80 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
81 this.parameters = parameters;
85 public void init() throws PfModelException {
86 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
87 parameters.getPersistenceUnit());
90 String errorMessage = "provider is already initialized";
91 LOGGER.warn(errorMessage);
92 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
95 // Parameters for the DAO
96 final DaoParameters daoParameters = new DaoParameters();
97 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
98 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
100 // Decode the password using Base64
101 String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
104 Properties jdbcProperties = new Properties();
105 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver());
106 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl());
107 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser());
108 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, decodedPassword);
111 daoParameters.setJdbcProperties(jdbcProperties);
113 pfDao = new PfDaoFactory().createPfDao(daoParameters);
115 pfDao = new PfDaoFactory().createPfDao(daoParameters);
116 pfDao.init(daoParameters);
117 } catch (Exception exc) {
118 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
119 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
120 LOGGER.warn(errorMessage, exc);
123 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
128 public void close() throws PfModelException {
129 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
130 parameters.getPersistenceUnit());
137 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
138 parameters.getPersistenceUnit());
142 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
144 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
148 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
150 return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
154 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
156 return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
160 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
161 throws PfModelException {
163 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
167 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
168 throws PfModelException {
170 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
174 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
175 throws PfModelException {
177 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
181 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
182 throws PfModelException {
184 return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
188 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
190 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
194 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
196 return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
200 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
202 return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
206 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
208 return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
213 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
214 throws PfModelException {
216 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
220 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
221 throws PfModelException {
223 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
227 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
228 throws PfModelException {
230 return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
234 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
236 return new LegacyProvider().getOperationalPolicy(pfDao, policyId);
240 public LegacyOperationalPolicy createOperationalPolicy(
241 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
243 return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
247 public LegacyOperationalPolicy updateOperationalPolicy(
248 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
250 return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
254 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
256 return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId);
260 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
262 return new LegacyProvider().getGuardPolicy(pfDao, policyId);
266 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
267 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
269 return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
273 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
274 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
276 return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
280 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
281 throws PfModelException {
283 return new LegacyProvider().deleteGuardPolicy(pfDao, policyId);
287 public List<PdpGroup> getPdpGroups(final String name, final String version) throws PfModelException {
289 return new PdpProvider().getPdpGroups(pfDao, name, version);
293 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
295 return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
299 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
301 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
305 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
307 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
311 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
312 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
314 new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup);
318 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion, @NonNull String pdpSubGroup,
319 @NonNull Pdp pdp) throws PfModelException {
320 new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp);
324 public PdpGroup deletePdpGroup(@NonNull final String name, @NonNull final String version) throws PfModelException {
326 return new PdpProvider().deletePdpGroup(pfDao, name, version);
330 public List<PdpStatistics> getPdpStatistics(final String name, final String version) throws PfModelException {
332 return new PdpProvider().getPdpStatistics(pfDao, name, version);
336 public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
337 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
338 @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
340 new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpGroupVersion, pdpType, pdpInstanceId,
345 * Check if the model provider is initialized.
347 private void assertInitilized() {
349 String errorMessage = "policy models provider is not initilaized";
350 LOGGER.warn(errorMessage);
351 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);