2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.time.Instant;
24 import java.util.Collection;
25 import java.util.List;
26 import javax.ws.rs.core.Response;
27 import lombok.NonNull;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.dao.PfDao;
31 import org.onap.policy.models.pap.concepts.PolicyAudit;
32 import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider;
33 import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter;
34 import org.onap.policy.models.pdp.concepts.Pdp;
35 import org.onap.policy.models.pdp.concepts.PdpGroup;
36 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
37 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
38 import org.onap.policy.models.pdp.concepts.PdpStatistics;
39 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
40 import org.onap.policy.models.pdp.persistence.provider.PdpFilterParameters;
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.tosca.authorative.concepts.ToscaConceptIdentifier;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
51 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
53 public abstract class AbstractPolicyModelsProvider implements PolicyModelsProvider {
55 protected abstract PfDao getPfDao();
58 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
59 throws PfModelException {
61 return new AuthorativeToscaProvider().getServiceTemplateList(getPfDao(), name, version);
66 public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
67 @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
69 return new AuthorativeToscaProvider().getFilteredServiceTemplateList(getPfDao(), filter);
73 public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
74 throws PfModelException {
76 return new AuthorativeToscaProvider().createServiceTemplate(getPfDao(), serviceTemplate);
80 public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
81 throws PfModelException {
83 return new AuthorativeToscaProvider().updateServiceTemplate(getPfDao(), serviceTemplate);
87 public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
88 throws PfModelException {
91 return new AuthorativeToscaProvider().deleteServiceTemplate(getPfDao(), name, version);
95 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
97 return new AuthorativeToscaProvider().getPolicyTypes(getPfDao(), name, version);
101 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
103 return new AuthorativeToscaProvider().getPolicyTypeList(getPfDao(), name, version);
107 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
108 throws PfModelException {
110 return new AuthorativeToscaProvider().getFilteredPolicyTypes(getPfDao(), filter);
114 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
115 throws PfModelException {
117 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(getPfDao(), filter);
121 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
122 throws PfModelException {
124 return new AuthorativeToscaProvider().createPolicyTypes(getPfDao(), serviceTemplate);
128 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
129 throws PfModelException {
131 return new AuthorativeToscaProvider().updatePolicyTypes(getPfDao(), serviceTemplate);
135 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
136 throws PfModelException {
139 var policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
140 assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
142 return new AuthorativeToscaProvider().deletePolicyType(getPfDao(), name, version);
146 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
148 return new AuthorativeToscaProvider().getPolicies(getPfDao(), name, version);
152 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
154 return new AuthorativeToscaProvider().getPolicyList(getPfDao(), name, version);
158 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
159 throws PfModelException {
161 return new AuthorativeToscaProvider().getFilteredPolicies(getPfDao(), filter);
165 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
166 throws PfModelException {
168 return new AuthorativeToscaProvider().getFilteredPolicyList(getPfDao(), filter);
172 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
173 throws PfModelException {
175 return new AuthorativeToscaProvider().createPolicies(getPfDao(), serviceTemplate);
179 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
180 throws PfModelException {
182 return new AuthorativeToscaProvider().updatePolicies(getPfDao(), serviceTemplate);
186 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
187 throws PfModelException {
190 var policyIdentifier = new ToscaConceptIdentifier(name, version);
191 assertPolicyNotDeployedInPdpGroup(policyIdentifier);
193 return new AuthorativeToscaProvider().deletePolicy(getPfDao(), name, version);
197 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
199 return new PdpProvider().getPdpGroups(getPfDao(), name);
203 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
205 return new PdpProvider().getFilteredPdpGroups(getPfDao(), filter);
209 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
211 return new PdpProvider().createPdpGroups(getPfDao(), pdpGroups);
215 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
217 return new PdpProvider().updatePdpGroups(getPfDao(), pdpGroups);
221 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
222 throws PfModelException {
224 new PdpProvider().updatePdpSubGroup(getPfDao(), pdpGroupName, pdpSubGroup);
228 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
229 throws PfModelException {
230 new PdpProvider().updatePdp(getPfDao(), pdpGroupName, pdpSubGroup, pdp);
234 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
236 return new PdpProvider().deletePdpGroup(getPfDao(), name);
240 public List<PdpStatistics> getFilteredPdpStatistics(PdpFilterParameters filterParams) throws PfModelException {
242 return new PdpStatisticsProvider().getFilteredPdpStatistics(getPfDao(), filterParams);
246 public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
247 throws PfModelException {
249 return new PdpStatisticsProvider().createPdpStatistics(getPfDao(), pdpStatisticsList);
253 public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
254 throws PfModelException {
256 return new PdpStatisticsProvider().updatePdpStatistics(getPfDao(), pdpStatisticsList);
260 public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Instant timestamp)
261 throws PfModelException {
263 return new PdpStatisticsProvider().deletePdpStatistics(getPfDao(), name, timestamp);
267 public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
269 return new PdpProvider().getAllPolicyStatus(getPfDao());
273 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
274 throws PfModelException {
276 return new PdpProvider().getAllPolicyStatus(getPfDao(), policy);
280 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
282 return new PdpProvider().getGroupPolicyStatus(getPfDao(), groupName);
286 public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
287 Collection<PdpPolicyStatus> deleteObjs) {
289 new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs);
293 public void createAuditRecords(List<PolicyAudit> auditRecords) {
295 new PolicyAuditProvider().createAuditRecords(getPfDao(), auditRecords);
299 public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter) {
301 return new PolicyAuditProvider().getAuditRecords(getPfDao(), auditFilter);
305 * Check if the model provider is initialized.
307 private void assertInitialized() {
308 if (getPfDao() == null) {
309 var errorMessage = "policy models provider is not initilaized";
310 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
315 * Assert that the policy type is not supported in any PDP group.
317 * @param policyTypeIdentifier the policy type identifier
318 * @throws PfModelException if the policy type is supported in a PDP group
320 private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
321 throws PfModelException {
322 for (PdpGroup pdpGroup : getPdpGroups(null)) {
323 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
324 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
325 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
326 "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
327 + pdpSubGroup.getPdpType());
334 * Assert that the policy is not deployed in a PDP group.
336 * @param policyIdentifier the identifier of the policy
337 * @throws PfModelException thrown if the policy is deployed in a PDP group
339 private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
340 throws PfModelException {
341 for (PdpGroup pdpGroup : getPdpGroups(null)) {
342 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
343 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
344 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
345 "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
346 + pdpSubGroup.getPdpType());