2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation.
4 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.provider.impl;
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.pdp.concepts.Pdp;
34 import org.onap.policy.models.pdp.concepts.PdpGroup;
35 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
36 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
37 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
38 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
39 import org.onap.policy.models.provider.PolicyModelsProvider;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
49 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
51 public abstract class AbstractPolicyModelsProvider implements PolicyModelsProvider {
53 protected abstract PfDao getPfDao();
56 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
57 throws PfModelException {
59 return new AuthorativeToscaProvider().getServiceTemplateList(getPfDao(), name, version);
64 public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
65 @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
67 return new AuthorativeToscaProvider().getFilteredServiceTemplateList(getPfDao(), filter);
71 public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
72 throws PfModelException {
74 return new AuthorativeToscaProvider().createServiceTemplate(getPfDao(), serviceTemplate);
78 public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
79 throws PfModelException {
81 return new AuthorativeToscaProvider().updateServiceTemplate(getPfDao(), serviceTemplate);
85 public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
86 throws PfModelException {
89 return new AuthorativeToscaProvider().deleteServiceTemplate(getPfDao(), name, version);
93 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
95 return new AuthorativeToscaProvider().getPolicyTypes(getPfDao(), name, version);
99 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
101 return new AuthorativeToscaProvider().getPolicyTypeList(getPfDao(), name, version);
105 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
106 throws PfModelException {
108 return new AuthorativeToscaProvider().getFilteredPolicyTypes(getPfDao(), filter);
112 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
113 throws PfModelException {
115 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(getPfDao(), filter);
119 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
120 throws PfModelException {
122 return new AuthorativeToscaProvider().createPolicyTypes(getPfDao(), serviceTemplate);
126 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
127 throws PfModelException {
129 return new AuthorativeToscaProvider().updatePolicyTypes(getPfDao(), serviceTemplate);
133 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
134 throws PfModelException {
137 var policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
138 assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
140 return new AuthorativeToscaProvider().deletePolicyType(getPfDao(), name, version);
144 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
146 return new AuthorativeToscaProvider().getPolicies(getPfDao(), name, version);
150 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
152 return new AuthorativeToscaProvider().getPolicyList(getPfDao(), name, version);
156 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
157 throws PfModelException {
159 return new AuthorativeToscaProvider().getFilteredPolicies(getPfDao(), filter);
163 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
164 throws PfModelException {
166 return new AuthorativeToscaProvider().getFilteredPolicyList(getPfDao(), filter);
170 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
171 throws PfModelException {
173 return new AuthorativeToscaProvider().createPolicies(getPfDao(), serviceTemplate);
177 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
178 throws PfModelException {
180 return new AuthorativeToscaProvider().updatePolicies(getPfDao(), serviceTemplate);
184 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
185 throws PfModelException {
188 var policyIdentifier = new ToscaConceptIdentifier(name, version);
189 assertPolicyNotDeployedInPdpGroup(policyIdentifier);
191 return new AuthorativeToscaProvider().deletePolicy(getPfDao(), name, version);
195 public ToscaServiceTemplate createToscaNodeTemplates(@NonNull final ToscaServiceTemplate serviceTemplate)
196 throws PfModelException {
198 return new AuthorativeToscaProvider().createToscaNodeTemplates(getPfDao(), serviceTemplate);
202 public ToscaServiceTemplate updateToscaNodeTemplates(@NonNull final ToscaServiceTemplate serviceTemplate)
203 throws PfModelRuntimeException, PfModelException {
205 return new AuthorativeToscaProvider().updateToscaNodeTemplates(getPfDao(), serviceTemplate);
209 public ToscaServiceTemplate deleteToscaNodeTemplate(@NonNull final String name, @NonNull final String version)
210 throws PfModelException {
212 return new AuthorativeToscaProvider().deleteToscaNodeTemplate(getPfDao(), name, version);
216 public List<Map<ToscaEntityKey, Map<String, Object>>> getNodeTemplateMetadataSets(final String name,
217 final String version)
218 throws PfModelException {
220 return new AuthorativeToscaProvider().getNodeTemplateMetadataSet(getPfDao(), name, version);
224 public List<ToscaNodeTemplate> getToscaNodeTemplate(final String name, final String version)
225 throws PfModelException {
227 return new AuthorativeToscaProvider().getToscaNodeTemplate(getPfDao(), name, version);
233 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
235 return new PdpProvider().getPdpGroups(getPfDao(), name);
239 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
241 return new PdpProvider().getFilteredPdpGroups(getPfDao(), filter);
245 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
247 return new PdpProvider().createPdpGroups(getPfDao(), pdpGroups);
251 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
253 return new PdpProvider().updatePdpGroups(getPfDao(), pdpGroups);
257 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
258 throws PfModelException {
260 new PdpProvider().updatePdpSubGroup(getPfDao(), pdpGroupName, pdpSubGroup);
264 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
265 throws PfModelException {
266 new PdpProvider().updatePdp(getPfDao(), pdpGroupName, pdpSubGroup, pdp);
270 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
272 return new PdpProvider().deletePdpGroup(getPfDao(), name);
276 public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
278 return new PdpProvider().getAllPolicyStatus(getPfDao());
282 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
283 throws PfModelException {
285 return new PdpProvider().getAllPolicyStatus(getPfDao(), policy);
289 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
291 return new PdpProvider().getGroupPolicyStatus(getPfDao(), groupName);
295 public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
296 Collection<PdpPolicyStatus> deleteObjs) {
298 new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs);
302 * Check if the model provider is initialized.
304 private void assertInitialized() {
305 if (getPfDao() == null) {
306 var errorMessage = "policy models provider is not initilaized";
307 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
312 * Assert that the policy type is not supported in any PDP group.
314 * @param policyTypeIdentifier the policy type identifier
315 * @throws PfModelException if the policy type is supported in a PDP group
317 private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
318 throws PfModelException {
319 for (PdpGroup pdpGroup : getPdpGroups(null)) {
320 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
321 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
322 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
323 "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
324 + pdpSubGroup.getPdpType());
331 * Assert that the policy is not deployed in a PDP group.
333 * @param policyIdentifier the identifier of the policy
334 * @throws PfModelException thrown if the policy is deployed in a PDP group
336 private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
337 throws PfModelException {
338 for (PdpGroup pdpGroup : getPdpGroups(null)) {
339 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
340 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
341 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
342 "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
343 + pdpSubGroup.getPdpType());