Merge "Removing Named Query."
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.provider.impl;
23
24 import java.util.Base64;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import javax.ws.rs.core.Response;
30
31 import lombok.NonNull;
32
33 import org.eclipse.persistence.config.PersistenceUnitProperties;
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.Pdp;
41 import org.onap.policy.models.pdp.concepts.PdpGroup;
42 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
43 import org.onap.policy.models.pdp.concepts.PdpStatistics;
44 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
45 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
46 import org.onap.policy.models.provider.PolicyModelsProvider;
47 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
53 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
54 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
55 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
56 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
57 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 /**
62  * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
63  * a relational database.
64  *
65  * @author Liam Fallon (liam.fallon@est.tech)
66  */
67 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
68
69     private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
70
71     private final PolicyModelsProviderParameters parameters;
72
73     // Database connection and the DAO for reading and writing Policy Framework concepts
74     private PfDao pfDao;
75
76     /**
77      * Constructor that takes the parameters.
78      *
79      * @param parameters the parameters for the provider
80      */
81     public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
82         this.parameters = parameters;
83     }
84
85     @Override
86     public void init() throws PfModelException {
87         LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
88                 parameters.getPersistenceUnit());
89
90         if (pfDao != null) {
91             String errorMessage = "provider is already initialized";
92             LOGGER.warn(errorMessage);
93             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
94         }
95
96         // Parameters for the DAO
97         final DaoParameters daoParameters = new DaoParameters();
98         daoParameters.setPluginClass(DefaultPfDao.class.getName());
99         daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
100
101         // Decode the password using Base64
102         String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
103
104         // @formatter:off
105         Properties jdbcProperties = new Properties();
106         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER,   parameters.getDatabaseDriver());
107         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL,      parameters.getDatabaseUrl());
108         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER,     parameters.getDatabaseUser());
109         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, decodedPassword);
110         // @formatter:on
111
112         daoParameters.setJdbcProperties(jdbcProperties);
113
114         try {
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);
121
122             this.close();
123             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
124         }
125     }
126
127     @Override
128     public void close() throws PfModelException {
129         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
130                 parameters.getPersistenceUnit());
131
132         if (pfDao != null) {
133             pfDao.close();
134             pfDao = null;
135         }
136
137         LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
138                 parameters.getPersistenceUnit());
139     }
140
141     @Override
142     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
143         assertInitilized();
144         return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
145     }
146
147     @Override
148     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
149         assertInitilized();
150         return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
151     }
152
153     @Override
154     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
155         assertInitilized();
156         return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
157     }
158
159     @Override
160     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
161             throws PfModelException {
162         assertInitilized();
163         return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
164     }
165
166     @Override
167     public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
168             throws PfModelException {
169         assertInitilized();
170         return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
171     }
172
173     @Override
174     public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
175             throws PfModelException {
176         assertInitilized();
177         return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
178     }
179
180     @Override
181     public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
182             throws PfModelException {
183         assertInitilized();
184         return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
185     }
186
187     @Override
188     public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
189         assertInitilized();
190         return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
191     }
192
193     @Override
194     public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
195         assertInitilized();
196         return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
197     }
198
199     @Override
200     public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
201         assertInitilized();
202         return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
203     }
204
205     @Override
206     public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
207         assertInitilized();
208         return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
209     }
210
211
212     @Override
213     public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
214             throws PfModelException {
215         assertInitilized();
216         return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
217     }
218
219     @Override
220     public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
221             throws PfModelException {
222         assertInitilized();
223         return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
224     }
225
226     @Override
227     public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
228             throws PfModelException {
229         assertInitilized();
230         return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
231     }
232
233     @Override
234     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId, final String policyVersion)
235             throws PfModelException {
236         assertInitilized();
237         return new LegacyProvider().getOperationalPolicy(pfDao, policyId, policyVersion);
238     }
239
240     @Override
241     public LegacyOperationalPolicy createOperationalPolicy(
242             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
243         assertInitilized();
244         return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
245     }
246
247     @Override
248     public LegacyOperationalPolicy updateOperationalPolicy(
249             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
250         assertInitilized();
251         return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
252     }
253
254     @Override
255     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId,
256             @NonNull final String policyVersion) throws PfModelException {
257         assertInitilized();
258         return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId, policyVersion);
259     }
260
261     @Override
262     public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId,
263             final String policyVersion) throws PfModelException {
264         assertInitilized();
265         return new LegacyProvider().getGuardPolicy(pfDao, policyId, policyVersion);
266     }
267
268     @Override
269     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
270             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
271         assertInitilized();
272         return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
273     }
274
275     @Override
276     public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
277             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
278         assertInitilized();
279         return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
280     }
281
282     @Override
283     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId,
284             @NonNull final String policyVersion) throws PfModelException {
285         assertInitilized();
286         return new LegacyProvider().deleteGuardPolicy(pfDao, policyId, policyVersion);
287     }
288
289     @Override
290     public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
291         assertInitilized();
292         return new PdpProvider().getPdpGroups(pfDao, name);
293     }
294
295     @Override
296     public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
297         assertInitilized();
298         return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
299     }
300
301     @Override
302     public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
303         assertInitilized();
304         return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
305     }
306
307     @Override
308     public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
309         assertInitilized();
310         return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
311     }
312
313     @Override
314     public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
315             throws PfModelException {
316         assertInitilized();
317         new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup);
318     }
319
320     @Override
321     public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
322             throws PfModelException {
323         new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpSubGroup, pdp);
324     }
325
326     @Override
327     public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
328         assertInitilized();
329         return new PdpProvider().deletePdpGroup(pfDao, name);
330     }
331
332     @Override
333     public List<PdpStatistics> getPdpStatistics(final String name) throws PfModelException {
334         assertInitilized();
335         return new PdpProvider().getPdpStatistics(pfDao, name);
336     }
337
338     @Override
339     public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpType,
340             @NonNull final String pdpInstanceId, @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
341         assertInitilized();
342         new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpType, pdpInstanceId, pdpStatistics);
343     }
344
345     /**
346      * Check if the model provider is initialized.
347      */
348     private void assertInitilized() {
349         if (pfDao == null) {
350             String errorMessage = "policy models provider is not initilaized";
351             LOGGER.warn(errorMessage);
352             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
353         }
354     }
355 }