6b54a1c24edf92db985d2587e3a719f4590f0deb
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.provider.impl;
24
25 import java.util.Collection;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.Properties;
29 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
31 import org.eclipse.persistence.config.PersistenceUnitProperties;
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.PdpPolicyStatus;
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.pdp.persistence.provider.PdpStatisticsProvider;
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.ToscaConceptIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter;
56 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 /**
61  * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
62  * a relational database.
63  *
64  * @author Liam Fallon (liam.fallon@est.tech)
65  */
66 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
67
68     private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class);
69
70     private final PolicyModelsProviderParameters parameters;
71
72     // Database connection and the DAO for reading and writing Policy Framework concepts
73     private PfDao pfDao;
74
75     /**
76      * Constructor that takes the parameters.
77      *
78      * @param parameters the parameters for the provider
79      */
80     public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
81         this.parameters = parameters;
82     }
83
84     @Override
85     public void init() throws PfModelException {
86         LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
87                 parameters.getPersistenceUnit());
88
89         if (pfDao != null) {
90             String errorMessage = "provider is already initialized";
91             LOGGER.warn(errorMessage);
92             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
93         }
94
95         // Parameters for the DAO
96         final DaoParameters daoParameters = new DaoParameters();
97         daoParameters.setPluginClass(DefaultPfDao.class.getName());
98         daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
99
100         // @formatter:off
101         Properties jdbcProperties = new Properties();
102         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER,   parameters.getDatabaseDriver());
103         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL,      parameters.getDatabaseUrl());
104         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER,     parameters.getDatabaseUser());
105         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
106         // @formatter:on
107
108         daoParameters.setJdbcProperties(jdbcProperties);
109
110         try {
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
117             this.close();
118             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
119         }
120     }
121
122     @Override
123     public void close() throws PfModelException {
124         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
125                 parameters.getPersistenceUnit());
126
127         if (pfDao != null) {
128             pfDao.close();
129             pfDao = null;
130         }
131
132         LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
133                 parameters.getPersistenceUnit());
134     }
135
136     @Override
137     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
138             throws PfModelException {
139         assertInitialized();
140         return new AuthorativeToscaProvider().getServiceTemplateList(pfDao, name, version);
141     }
142
143
144     @Override
145     public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull ToscaServiceTemplateFilter filter)
146             throws PfModelException {
147         assertInitialized();
148         return new AuthorativeToscaProvider().getFilteredServiceTemplateList(pfDao, filter);
149     }
150
151     @Override
152     public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
153             throws PfModelException {
154         assertInitialized();
155         return new AuthorativeToscaProvider().createServiceTemplate(pfDao, serviceTemplate);
156     }
157
158     @Override
159     public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
160             throws PfModelException {
161         assertInitialized();
162         return new AuthorativeToscaProvider().updateServiceTemplate(pfDao, serviceTemplate);
163     }
164
165     @Override
166     public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
167             throws PfModelException {
168         assertInitialized();
169
170         return new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, name, version);
171     }
172
173     @Override
174     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
175         assertInitialized();
176         return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
177     }
178
179     @Override
180     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
181         assertInitialized();
182         return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
183     }
184
185     @Override
186     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
187         assertInitialized();
188         return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
189     }
190
191     @Override
192     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
193             throws PfModelException {
194         assertInitialized();
195         return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
196     }
197
198     @Override
199     public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
200             throws PfModelException {
201         assertInitialized();
202         return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
203     }
204
205     @Override
206     public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
207             throws PfModelException {
208         assertInitialized();
209         return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
210     }
211
212     @Override
213     public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
214             throws PfModelException {
215         assertInitialized();
216
217         ToscaConceptIdentifier policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
218         assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
219
220         return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
221     }
222
223     @Override
224     public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
225         assertInitialized();
226         return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
227     }
228
229     @Override
230     public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
231         assertInitialized();
232         return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
233     }
234
235     @Override
236     public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
237         assertInitialized();
238         return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
239     }
240
241     @Override
242     public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
243         assertInitialized();
244         return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
245     }
246
247     @Override
248     public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
249             throws PfModelException {
250         assertInitialized();
251         return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
252     }
253
254     @Override
255     public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
256             throws PfModelException {
257         assertInitialized();
258         return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
259     }
260
261     @Override
262     public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
263             throws PfModelException {
264         assertInitialized();
265
266         ToscaConceptIdentifier policyIdentifier = new ToscaConceptIdentifier(name, version);
267         assertPolicyNotDeployedInPdpGroup(policyIdentifier);
268
269         return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
270     }
271
272     @Override
273     public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
274         assertInitialized();
275         return new PdpProvider().getPdpGroups(pfDao, name);
276     }
277
278     @Override
279     public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
280         assertInitialized();
281         return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
282     }
283
284     @Override
285     public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
286         assertInitialized();
287         return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
288     }
289
290     @Override
291     public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
292         assertInitialized();
293         return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
294     }
295
296     @Override
297     public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
298             throws PfModelException {
299         assertInitialized();
300         new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup);
301     }
302
303     @Override
304     public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
305             throws PfModelException {
306         new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpSubGroup, pdp);
307     }
308
309     @Override
310     public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
311         assertInitialized();
312         return new PdpProvider().deletePdpGroup(pfDao, name);
313     }
314
315     @Override
316     public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException {
317         assertInitialized();
318         return new PdpStatisticsProvider().getPdpStatistics(pfDao, name, timestamp);
319     }
320
321     @Override
322     public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName,
323             final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp, final String sortOrder,
324             final int getRecordNum) throws PfModelException {
325         assertInitialized();
326         return new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, name, pdpGroupName, pdpSubGroup,
327                 startTimeStamp, endTimeStamp, sortOrder, getRecordNum);
328     }
329
330     @Override
331     public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
332             throws PfModelException {
333         assertInitialized();
334         return new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsList);
335     }
336
337     @Override
338     public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
339             throws PfModelException {
340         assertInitialized();
341         return new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsList);
342     }
343
344     @Override
345     public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Date timestamp)
346             throws PfModelException {
347         assertInitialized();
348         return new PdpStatisticsProvider().deletePdpStatistics(pfDao, name, timestamp);
349     }
350
351     @Override
352     public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
353         assertInitialized();
354         return new PdpProvider().getAllPolicyStatus(pfDao);
355     }
356
357     @Override
358     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
359                     throws PfModelException {
360         assertInitialized();
361         return new PdpProvider().getAllPolicyStatus(pfDao, policy);
362     }
363
364     @Override
365     public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName)
366                     throws PfModelException {
367         assertInitialized();
368         return new PdpProvider().getGroupPolicyStatus(pfDao, groupName);
369     }
370
371     @Override
372     public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs,
373                     Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
374         assertInitialized();
375         new PdpProvider().cudPolicyStatus(pfDao, createObjs, updateObjs, deleteObjs);
376     }
377
378     /**
379      * Check if the model provider is initialized.
380      */
381     private void assertInitialized() {
382         if (pfDao == null) {
383             String errorMessage = "policy models provider is not initilaized";
384             LOGGER.warn(errorMessage);
385             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
386         }
387     }
388
389     /**
390      * Assert that the policy type is not supported in any PDP group.
391      *
392      * @param policyTypeIdentifier the policy type identifier
393      * @throws PfModelException if the policy type is supported in a PDP group
394      */
395     private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
396             throws PfModelException {
397         for (PdpGroup pdpGroup : getPdpGroups(null)) {
398             for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
399                 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
400                     throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
401                             "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
402                                     + pdpSubGroup.getPdpType());
403                 }
404             }
405         }
406     }
407
408     /**
409      * Assert that the policy is not deployed in a PDP group.
410      *
411      * @param policyIdentifier the identifier of the policy
412      * @throws PfModelException thrown if the policy is deployed in a PDP group
413      */
414     private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
415             throws PfModelException {
416         for (PdpGroup pdpGroup : getPdpGroups(null)) {
417             for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
418                 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
419                     throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
420                             "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
421                                     + pdpSubGroup.getPdpType());
422                 }
423             }
424         }
425     }
426 }