2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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;
27 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
29 import org.onap.policy.models.base.PfConceptKey;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.dao.PfDao;
33 import org.onap.policy.models.pap.concepts.PolicyAudit;
34 import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider;
35 import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter;
36 import org.onap.policy.models.pdp.concepts.Pdp;
37 import org.onap.policy.models.pdp.concepts.PdpGroup;
38 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
39 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
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.PdpFilterParameters;
43 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
44 import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
45 import org.onap.policy.models.provider.PolicyModelsProvider;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
55 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
57 public abstract class AbstractPolicyModelsProvider implements PolicyModelsProvider {
59 protected abstract PfDao getPfDao();
62 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
63 throws PfModelException {
65 return new AuthorativeToscaProvider().getServiceTemplateList(getPfDao(), name, version);
70 public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
71 @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
73 return new AuthorativeToscaProvider().getFilteredServiceTemplateList(getPfDao(), filter);
77 public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
78 throws PfModelException {
80 return new AuthorativeToscaProvider().createServiceTemplate(getPfDao(), serviceTemplate);
84 public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
85 throws PfModelException {
87 return new AuthorativeToscaProvider().updateServiceTemplate(getPfDao(), serviceTemplate);
91 public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
92 throws PfModelException {
95 return new AuthorativeToscaProvider().deleteServiceTemplate(getPfDao(), name, version);
99 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
101 return new AuthorativeToscaProvider().getPolicyTypes(getPfDao(), name, version);
105 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
107 return new AuthorativeToscaProvider().getPolicyTypeList(getPfDao(), name, version);
111 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
112 throws PfModelException {
114 return new AuthorativeToscaProvider().getFilteredPolicyTypes(getPfDao(), filter);
118 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
119 throws PfModelException {
121 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(getPfDao(), filter);
125 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
126 throws PfModelException {
128 return new AuthorativeToscaProvider().createPolicyTypes(getPfDao(), serviceTemplate);
132 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
133 throws PfModelException {
135 return new AuthorativeToscaProvider().updatePolicyTypes(getPfDao(), serviceTemplate);
139 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
140 throws PfModelException {
143 var policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
144 assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
146 return new AuthorativeToscaProvider().deletePolicyType(getPfDao(), name, version);
150 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
152 return new AuthorativeToscaProvider().getPolicies(getPfDao(), name, version);
156 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
158 return new AuthorativeToscaProvider().getPolicyList(getPfDao(), name, version);
162 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
163 throws PfModelException {
165 return new AuthorativeToscaProvider().getFilteredPolicies(getPfDao(), filter);
169 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
170 throws PfModelException {
172 return new AuthorativeToscaProvider().getFilteredPolicyList(getPfDao(), filter);
176 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
177 throws PfModelException {
179 return new AuthorativeToscaProvider().createPolicies(getPfDao(), serviceTemplate);
183 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
184 throws PfModelException {
186 return new AuthorativeToscaProvider().updatePolicies(getPfDao(), serviceTemplate);
190 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
191 throws PfModelException {
194 var policyIdentifier = new ToscaConceptIdentifier(name, version);
195 assertPolicyNotDeployedInPdpGroup(policyIdentifier);
197 return new AuthorativeToscaProvider().deletePolicy(getPfDao(), name, version);
201 public ToscaServiceTemplate createToscaNodeTemplates(@NonNull final ToscaServiceTemplate serviceTemplate)
202 throws PfModelException {
204 return new AuthorativeToscaProvider().createToscaNodeTemplates(getPfDao(), serviceTemplate);
208 public ToscaServiceTemplate updateToscaNodeTemplates(@NonNull final ToscaServiceTemplate serviceTemplate)
209 throws PfModelRuntimeException, PfModelException {
211 return new AuthorativeToscaProvider().updateToscaNodeTemplates(getPfDao(), serviceTemplate);
215 public ToscaServiceTemplate deleteToscaNodeTemplate(@NonNull final String name, @NonNull final String version)
216 throws PfModelException {
218 return new AuthorativeToscaProvider().deleteToscaNodeTemplate(getPfDao(), name, version);
222 public List<Map<ToscaEntityKey, Map<String, Object>>> getNodeTemplateMetadataSets(final String name,
223 final String version)
224 throws PfModelException {
226 return new AuthorativeToscaProvider().getNodeTemplateMetadataSet(getPfDao(), name, version);
230 public List<Map<PfConceptKey, ToscaNodeTemplate>> getToscaNodeTemplate(final String name,
231 final String version)
232 throws PfModelException {
234 return new AuthorativeToscaProvider().getToscaNodeTemplate(getPfDao(), name, version);
240 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
242 return new PdpProvider().getPdpGroups(getPfDao(), name);
246 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
248 return new PdpProvider().getFilteredPdpGroups(getPfDao(), filter);
252 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
254 return new PdpProvider().createPdpGroups(getPfDao(), pdpGroups);
258 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
260 return new PdpProvider().updatePdpGroups(getPfDao(), pdpGroups);
264 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
265 throws PfModelException {
267 new PdpProvider().updatePdpSubGroup(getPfDao(), pdpGroupName, pdpSubGroup);
271 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
272 throws PfModelException {
273 new PdpProvider().updatePdp(getPfDao(), pdpGroupName, pdpSubGroup, pdp);
277 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
279 return new PdpProvider().deletePdpGroup(getPfDao(), name);
283 public List<PdpStatistics> getFilteredPdpStatistics(PdpFilterParameters filterParams) throws PfModelException {
285 return new PdpStatisticsProvider().getFilteredPdpStatistics(getPfDao(), filterParams);
289 public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
290 throws PfModelException {
292 return new PdpStatisticsProvider().createPdpStatistics(getPfDao(), pdpStatisticsList);
296 public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
297 throws PfModelException {
299 return new PdpStatisticsProvider().updatePdpStatistics(getPfDao(), pdpStatisticsList);
303 public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Instant timestamp)
304 throws PfModelException {
306 return new PdpStatisticsProvider().deletePdpStatistics(getPfDao(), name, timestamp);
310 public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
312 return new PdpProvider().getAllPolicyStatus(getPfDao());
316 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
317 throws PfModelException {
319 return new PdpProvider().getAllPolicyStatus(getPfDao(), policy);
323 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
325 return new PdpProvider().getGroupPolicyStatus(getPfDao(), groupName);
329 public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
330 Collection<PdpPolicyStatus> deleteObjs) {
332 new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs);
336 public void createAuditRecords(List<PolicyAudit> auditRecords) {
338 new PolicyAuditProvider().createAuditRecords(getPfDao(), auditRecords);
342 public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter) {
344 return new PolicyAuditProvider().getAuditRecords(getPfDao(), auditFilter);
348 * Check if the model provider is initialized.
350 private void assertInitialized() {
351 if (getPfDao() == null) {
352 var errorMessage = "policy models provider is not initilaized";
353 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
358 * Assert that the policy type is not supported in any PDP group.
360 * @param policyTypeIdentifier the policy type identifier
361 * @throws PfModelException if the policy type is supported in a PDP group
363 private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
364 throws PfModelException {
365 for (PdpGroup pdpGroup : getPdpGroups(null)) {
366 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
367 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
368 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
369 "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
370 + pdpSubGroup.getPdpType());
377 * Assert that the policy is not deployed in a PDP group.
379 * @param policyIdentifier the identifier of the policy
380 * @throws PfModelException thrown if the policy is deployed in a PDP group
382 private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
383 throws PfModelException {
384 for (PdpGroup pdpGroup : getPdpGroups(null)) {
385 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
386 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
387 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
388 "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
389 + pdpSubGroup.getPdpType());