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.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.Pdp;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
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.ToscaPolicyFilter;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
52 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
53 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
54 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
55 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
61 * a relational database.
63 * @author Liam Fallon (liam.fallon@est.tech)
65 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
67 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
69 // Constants for persistence properties
71 private static final String JAVAX_PERSISTENCE_JDBC_DRIVER = "javax.persistence.jdbc.driver";
72 private static final String JAVAX_PERSISTENCE_JDBC_URL = "javax.persistence.jdbc.url";
73 private static final String JAVAX_PERSISTENCE_JDBC_USER = "javax.persistence.jdbc.user";
74 private static final String JAVAX_PERSISTENCE_JDBC_PWORD = "javax.persistence.jdbc.password";
77 private final PolicyModelsProviderParameters parameters;
79 // Database connection and the DAO for reading and writing Policy Framework concepts
83 * Constructor that takes the parameters.
85 * @param parameters the parameters for the provider
87 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
88 this.parameters = parameters;
92 public void init() throws PfModelException {
93 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
94 parameters.getPersistenceUnit());
97 String errorMessage = "provider is already initialized";
98 LOGGER.warn(errorMessage);
99 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
102 // Parameters for the DAO
103 final DaoParameters daoParameters = new DaoParameters();
104 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
105 daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
107 // Decode the password using Base64
108 String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
111 Properties jdbcProperties = new Properties();
112 jdbcProperties.setProperty(JAVAX_PERSISTENCE_JDBC_DRIVER, parameters.getDatabaseDriver());
113 jdbcProperties.setProperty(JAVAX_PERSISTENCE_JDBC_URL, parameters.getDatabaseUrl());
114 jdbcProperties.setProperty(JAVAX_PERSISTENCE_JDBC_USER, parameters.getDatabaseUser());
115 jdbcProperties.setProperty(JAVAX_PERSISTENCE_JDBC_PWORD, decodedPassword);
118 daoParameters.setJdbcProperties(jdbcProperties);
120 pfDao = new PfDaoFactory().createPfDao(daoParameters);
122 pfDao = new PfDaoFactory().createPfDao(daoParameters);
123 pfDao.init(daoParameters);
124 } catch (Exception exc) {
125 String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
126 + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
127 LOGGER.warn(errorMessage, exc);
130 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
135 public void close() throws PfModelException {
136 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
137 parameters.getPersistenceUnit());
144 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
145 parameters.getPersistenceUnit());
149 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
151 return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
155 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
157 return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
161 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
163 return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
167 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
168 throws PfModelException {
170 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
174 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
175 throws PfModelException {
177 return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
181 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
182 throws PfModelException {
184 return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
188 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
189 throws PfModelException {
191 return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
195 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
197 return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
201 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
203 return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
207 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
209 return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
213 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
215 return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
220 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
221 throws PfModelException {
223 return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
227 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
228 throws PfModelException {
230 return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
234 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
235 throws PfModelException {
237 return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
241 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
243 return new LegacyProvider().getOperationalPolicy(pfDao, policyId);
247 public LegacyOperationalPolicy createOperationalPolicy(
248 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
250 return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
254 public LegacyOperationalPolicy updateOperationalPolicy(
255 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
257 return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
261 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
263 return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId);
267 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
269 return new LegacyProvider().getGuardPolicy(pfDao, policyId);
273 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
274 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
276 return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
280 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
281 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
283 return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
287 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
288 throws PfModelException {
290 return new LegacyProvider().deleteGuardPolicy(pfDao, policyId);
294 public List<PdpGroup> getPdpGroups(final String name, final String version) throws PfModelException {
296 return new PdpProvider().getPdpGroups(pfDao, name, version);
300 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
302 return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
306 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
308 return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
312 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
314 return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
318 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
319 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
321 new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup);
325 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion, @NonNull String pdpSubGroup,
326 @NonNull Pdp pdp) throws PfModelException {
327 new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp);
331 public PdpGroup deletePdpGroup(@NonNull final String name, @NonNull final String version) throws PfModelException {
333 return new PdpProvider().deletePdpGroup(pfDao, name, version);
337 public List<PdpStatistics> getPdpStatistics(final String name, final String version) throws PfModelException {
339 return new PdpProvider().getPdpStatistics(pfDao, name, version);
343 public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
344 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
345 @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
347 new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpGroupVersion, pdpType, pdpInstanceId,
352 * Check if the model provider is initialized.
354 private void assertInitilized() {
356 String errorMessage = "policy models provider is not initilaized";
357 LOGGER.warn(errorMessage);
358 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);