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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.provider.impl;
25 import java.time.Instant;
26 import java.util.Collection;
27 import java.util.List;
28 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.pap.concepts.PolicyAudit;
33 import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider;
34 import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter;
35 import org.onap.policy.models.pdp.concepts.Pdp;
36 import org.onap.policy.models.pdp.concepts.PdpGroup;
37 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
38 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
41 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
42 import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
43 import org.onap.policy.models.provider.PolicyModelsProvider;
44 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
52 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
55 * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
56 * a relational database.
58 * @author Liam Fallon (liam.fallon@est.tech)
60 public class DatabasePolicyModelsProviderImpl extends AbstractModelsProvider implements PolicyModelsProvider {
62 * Constructor that takes the parameters.
64 * @param parameters the parameters for the provider
66 public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
71 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
72 throws PfModelException {
74 return new AuthorativeToscaProvider().getServiceTemplateList(getPfDao(), name, version);
79 public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
80 @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
82 return new AuthorativeToscaProvider().getFilteredServiceTemplateList(getPfDao(), filter);
86 public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
87 throws PfModelException {
89 return new AuthorativeToscaProvider().createServiceTemplate(getPfDao(), serviceTemplate);
93 public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
94 throws PfModelException {
96 return new AuthorativeToscaProvider().updateServiceTemplate(getPfDao(), serviceTemplate);
100 public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
101 throws PfModelException {
104 return new AuthorativeToscaProvider().deleteServiceTemplate(getPfDao(), name, version);
108 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
110 return new AuthorativeToscaProvider().getPolicyTypes(getPfDao(), name, version);
114 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
116 return new AuthorativeToscaProvider().getPolicyTypeList(getPfDao(), name, version);
120 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
121 throws PfModelException {
123 return new AuthorativeToscaProvider().getFilteredPolicyTypes(getPfDao(), filter);
127 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
128 throws PfModelException {
130 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(getPfDao(), filter);
134 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
135 throws PfModelException {
137 return new AuthorativeToscaProvider().createPolicyTypes(getPfDao(), serviceTemplate);
141 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
142 throws PfModelException {
144 return new AuthorativeToscaProvider().updatePolicyTypes(getPfDao(), serviceTemplate);
148 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
149 throws PfModelException {
152 var policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
153 assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
155 return new AuthorativeToscaProvider().deletePolicyType(getPfDao(), name, version);
159 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
161 return new AuthorativeToscaProvider().getPolicies(getPfDao(), name, version);
165 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
167 return new AuthorativeToscaProvider().getPolicyList(getPfDao(), name, version);
171 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
172 throws PfModelException {
174 return new AuthorativeToscaProvider().getFilteredPolicies(getPfDao(), filter);
178 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
179 throws PfModelException {
181 return new AuthorativeToscaProvider().getFilteredPolicyList(getPfDao(), filter);
185 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
186 throws PfModelException {
188 return new AuthorativeToscaProvider().createPolicies(getPfDao(), serviceTemplate);
192 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
193 throws PfModelException {
195 return new AuthorativeToscaProvider().updatePolicies(getPfDao(), serviceTemplate);
199 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
200 throws PfModelException {
203 var policyIdentifier = new ToscaConceptIdentifier(name, version);
204 assertPolicyNotDeployedInPdpGroup(policyIdentifier);
206 return new AuthorativeToscaProvider().deletePolicy(getPfDao(), name, version);
210 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
212 return new PdpProvider().getPdpGroups(getPfDao(), name);
216 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
218 return new PdpProvider().getFilteredPdpGroups(getPfDao(), filter);
222 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
224 return new PdpProvider().createPdpGroups(getPfDao(), pdpGroups);
228 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
230 return new PdpProvider().updatePdpGroups(getPfDao(), pdpGroups);
234 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
235 throws PfModelException {
237 new PdpProvider().updatePdpSubGroup(getPfDao(), pdpGroupName, pdpSubGroup);
241 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
242 throws PfModelException {
243 new PdpProvider().updatePdp(getPfDao(), pdpGroupName, pdpSubGroup, pdp);
247 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
249 return new PdpProvider().deletePdpGroup(getPfDao(), name);
253 public List<PdpStatistics> getPdpStatistics(final String name, final Instant timestamp) throws PfModelException {
255 return new PdpStatisticsProvider().getPdpStatistics(getPfDao(), name, timestamp);
259 public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName,
260 final String pdpSubGroup, final Instant startTimeStamp,
261 final Instant endTimeStamp, final String sortOrder,
262 final int getRecordNum) throws PfModelException {
264 return new PdpStatisticsProvider().getFilteredPdpStatistics(getPfDao(), name, pdpGroupName, pdpSubGroup,
265 startTimeStamp, endTimeStamp, sortOrder, getRecordNum);
269 public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
270 throws PfModelException {
272 return new PdpStatisticsProvider().createPdpStatistics(getPfDao(), pdpStatisticsList);
276 public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
277 throws PfModelException {
279 return new PdpStatisticsProvider().updatePdpStatistics(getPfDao(), pdpStatisticsList);
283 public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Instant timestamp)
284 throws PfModelException {
286 return new PdpStatisticsProvider().deletePdpStatistics(getPfDao(), name, timestamp);
290 public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
292 return new PdpProvider().getAllPolicyStatus(getPfDao());
296 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
297 throws PfModelException {
299 return new PdpProvider().getAllPolicyStatus(getPfDao(), policy);
303 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
305 return new PdpProvider().getGroupPolicyStatus(getPfDao(), groupName);
309 public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
310 Collection<PdpPolicyStatus> deleteObjs) {
312 new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs);
316 public void createAuditRecords(List<PolicyAudit> auditRecords) {
318 new PolicyAuditProvider().createAuditRecords(getPfDao(), auditRecords);
322 public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter, @NonNull Integer numRecords) {
324 if (auditFilter == null || auditFilter.isEmpty()) {
325 return new PolicyAuditProvider().getAuditRecords(getPfDao(), numRecords);
327 return new PolicyAuditProvider().getAuditRecords(getPfDao(), auditFilter, numRecords);
332 * Check if the model provider is initialized.
334 private void assertInitialized() {
335 if (getPfDao() == null) {
336 var errorMessage = "policy models provider is not initilaized";
337 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
342 * Assert that the policy type is not supported in any PDP group.
344 * @param policyTypeIdentifier the policy type identifier
345 * @throws PfModelException if the policy type is supported in a PDP group
347 private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
348 throws PfModelException {
349 for (PdpGroup pdpGroup : getPdpGroups(null)) {
350 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
351 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
352 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
353 "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
354 + pdpSubGroup.getPdpType());
361 * Assert that the policy is not deployed in a PDP group.
363 * @param policyIdentifier the identifier of the policy
364 * @throws PfModelException thrown if the policy is deployed in a PDP group
366 private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
367 throws PfModelException {
368 for (PdpGroup pdpGroup : getPdpGroups(null)) {
369 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
370 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
371 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
372 "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
373 + pdpSubGroup.getPdpType());