Add Service Template TOSCA handling
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 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.Date;
26 import java.util.List;
27 import java.util.Properties;
28 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
30 import org.eclipse.persistence.config.PersistenceUnitProperties;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.dao.DaoParameters;
34 import org.onap.policy.models.dao.PfDao;
35 import org.onap.policy.models.dao.PfDaoFactory;
36 import org.onap.policy.models.dao.impl.DefaultPfDao;
37 import org.onap.policy.models.pdp.concepts.Pdp;
38 import org.onap.policy.models.pdp.concepts.PdpGroup;
39 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
40 import org.onap.policy.models.pdp.concepts.PdpStatistics;
41 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
42 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
43 import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
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.ToscaPolicyIdentifier;
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.ToscaPolicyTypeIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter;
54 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 /**
59  * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
60  * a relational database.
61  *
62  * @author Liam Fallon (liam.fallon@est.tech)
63  */
64 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
65
66     private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class);
67
68     private final PolicyModelsProviderParameters parameters;
69
70     // Database connection and the DAO for reading and writing Policy Framework concepts
71     private PfDao pfDao;
72
73     /**
74      * Constructor that takes the parameters.
75      *
76      * @param parameters the parameters for the provider
77      */
78     public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
79         this.parameters = parameters;
80     }
81
82     @Override
83     public void init() throws PfModelException {
84         LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
85                 parameters.getPersistenceUnit());
86
87         if (pfDao != null) {
88             String errorMessage = "provider is already initialized";
89             LOGGER.warn(errorMessage);
90             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
91         }
92
93         // Parameters for the DAO
94         final DaoParameters daoParameters = new DaoParameters();
95         daoParameters.setPluginClass(DefaultPfDao.class.getName());
96         daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
97
98         // @formatter:off
99         Properties jdbcProperties = new Properties();
100         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER,   parameters.getDatabaseDriver());
101         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL,      parameters.getDatabaseUrl());
102         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER,     parameters.getDatabaseUser());
103         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
104         // @formatter:on
105
106         daoParameters.setJdbcProperties(jdbcProperties);
107
108         try {
109             pfDao = new PfDaoFactory().createPfDao(daoParameters);
110             pfDao.init(daoParameters);
111         } catch (Exception exc) {
112             String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
113                     + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
114
115             this.close();
116             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
117         }
118     }
119
120     @Override
121     public void close() throws PfModelException {
122         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
123                 parameters.getPersistenceUnit());
124
125         if (pfDao != null) {
126             pfDao.close();
127             pfDao = null;
128         }
129
130         LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
131                 parameters.getPersistenceUnit());
132     }
133
134     @Override
135     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
136             throws PfModelException {
137         assertInitialized();
138         return new AuthorativeToscaProvider().getServiceTemplateList(pfDao, name, version);
139     }
140
141
142     @Override
143     public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull ToscaServiceTemplateFilter filter)
144             throws PfModelException {
145         assertInitialized();
146         return new AuthorativeToscaProvider().getFilteredServiceTemplateList(pfDao, filter);
147     }
148
149     @Override
150     public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
151             throws PfModelException {
152         assertInitialized();
153         return new AuthorativeToscaProvider().createServiceTemplate(pfDao, serviceTemplate);
154     }
155
156     @Override
157     public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
158             throws PfModelException {
159         assertInitialized();
160         return new AuthorativeToscaProvider().updateServiceTemplate(pfDao, serviceTemplate);
161     }
162
163     @Override
164     public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
165             throws PfModelException {
166         assertInitialized();
167
168         return new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, name, version);
169     }
170
171     @Override
172     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
173         assertInitialized();
174         return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version);
175     }
176
177     @Override
178     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
179         assertInitialized();
180         return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version);
181     }
182
183     @Override
184     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException {
185         assertInitialized();
186         return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter);
187     }
188
189     @Override
190     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter)
191             throws PfModelException {
192         assertInitialized();
193         return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter);
194     }
195
196     @Override
197     public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
198             throws PfModelException {
199         assertInitialized();
200         return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
201     }
202
203     @Override
204     public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
205             throws PfModelException {
206         assertInitialized();
207         return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
208     }
209
210     @Override
211     public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
212             throws PfModelException {
213         assertInitialized();
214
215         ToscaPolicyTypeIdentifier policyTypeIdentifier = new ToscaPolicyTypeIdentifier(name, version);
216         assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
217
218         return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
219     }
220
221     @Override
222     public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
223         assertInitialized();
224         return new AuthorativeToscaProvider().getPolicies(pfDao, name, version);
225     }
226
227     @Override
228     public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
229         assertInitialized();
230         return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version);
231     }
232
233     @Override
234     public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException {
235         assertInitialized();
236         return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter);
237     }
238
239     @Override
240     public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException {
241         assertInitialized();
242         return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
243     }
244
245     @Override
246     public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
247             throws PfModelException {
248         assertInitialized();
249         return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate);
250     }
251
252     @Override
253     public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
254             throws PfModelException {
255         assertInitialized();
256         return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate);
257     }
258
259     @Override
260     public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
261             throws PfModelException {
262         assertInitialized();
263
264         ToscaPolicyIdentifier policyIdentifier = new ToscaPolicyIdentifier(name, version);
265         assertPolicyNotDeployedInPdpGroup(policyIdentifier);
266
267         return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
268     }
269
270     @Override
271     public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
272         assertInitialized();
273         return new PdpProvider().getPdpGroups(pfDao, name);
274     }
275
276     @Override
277     public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
278         assertInitialized();
279         return new PdpProvider().getFilteredPdpGroups(pfDao, filter);
280     }
281
282     @Override
283     public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
284         assertInitialized();
285         return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
286     }
287
288     @Override
289     public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
290         assertInitialized();
291         return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
292     }
293
294     @Override
295     public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
296             throws PfModelException {
297         assertInitialized();
298         new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup);
299     }
300
301     @Override
302     public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
303             throws PfModelException {
304         new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpSubGroup, pdp);
305     }
306
307     @Override
308     public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
309         assertInitialized();
310         return new PdpProvider().deletePdpGroup(pfDao, name);
311     }
312
313     @Override
314     public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException {
315         assertInitialized();
316         return new PdpStatisticsProvider().getPdpStatistics(pfDao, name, timestamp);
317     }
318
319     @Override
320     public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName,
321             final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp, final String sortOrder,
322             final int getRecordNum) throws PfModelException {
323         assertInitialized();
324         return new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, name, pdpGroupName, pdpSubGroup,
325                 startTimeStamp, endTimeStamp, sortOrder, getRecordNum);
326     }
327
328     @Override
329     public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
330             throws PfModelException {
331         assertInitialized();
332         return new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsList);
333     }
334
335     @Override
336     public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
337             throws PfModelException {
338         assertInitialized();
339         return new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsList);
340     }
341
342     @Override
343     public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Date timestamp)
344             throws PfModelException {
345         assertInitialized();
346         return new PdpStatisticsProvider().deletePdpStatistics(pfDao, name, timestamp);
347     }
348
349     /**
350      * Check if the model provider is initialized.
351      */
352     private void assertInitialized() {
353         if (pfDao == null) {
354             String errorMessage = "policy models provider is not initilaized";
355             LOGGER.warn(errorMessage);
356             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
357         }
358     }
359
360     /**
361      * Assert that the policy type is not supported in any PDP group.
362      *
363      * @param policyTypeIdentifier the policy type identifier
364      * @throws PfModelException if the policy type is supported in a PDP group
365      */
366     private void assertPolicyTypeNotSupportedInPdpGroup(ToscaPolicyTypeIdentifier policyTypeIdentifier)
367             throws PfModelException {
368         for (PdpGroup pdpGroup : getPdpGroups(null)) {
369             for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
370                 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
371                     throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
372                             "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
373                                     + pdpSubGroup.getPdpType());
374                 }
375             }
376         }
377     }
378
379     /**
380      * Assert that the policy is not deployed in a PDP group.
381      *
382      * @param policyIdentifier the identifier of the policy
383      * @throws PfModelException thrown if the policy is deployed in a PDP group
384      */
385     private void assertPolicyNotDeployedInPdpGroup(final ToscaPolicyIdentifier policyIdentifier)
386             throws PfModelException {
387         for (PdpGroup pdpGroup : getPdpGroups(null)) {
388             for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
389                 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
390                     throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
391                             "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
392                                     + pdpSubGroup.getPdpType());
393                 }
394             }
395         }
396     }
397 }