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.PfModelException;
30 import org.onap.policy.models.base.PfModelRuntimeException;
31 import org.onap.policy.models.dao.PfDao;
32 import org.onap.policy.models.pdp.concepts.Pdp;
33 import org.onap.policy.models.pdp.concepts.PdpGroup;
34 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
35 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
36 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
37 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
38 import org.onap.policy.models.provider.PolicyModelsProvider;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
48 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
50 public abstract class AbstractPolicyModelsProvider implements PolicyModelsProvider {
52 protected abstract PfDao getPfDao();
55 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
56 throws PfModelException {
58 return new AuthorativeToscaProvider().getServiceTemplateList(getPfDao(), name, version);
63 public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
64 @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
66 return new AuthorativeToscaProvider().getFilteredServiceTemplateList(getPfDao(), filter);
70 public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
71 throws PfModelException {
73 return new AuthorativeToscaProvider().createServiceTemplate(getPfDao(), serviceTemplate);
77 public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
78 throws PfModelException {
80 return new AuthorativeToscaProvider().updateServiceTemplate(getPfDao(), serviceTemplate);
84 public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
85 throws PfModelException {
88 return new AuthorativeToscaProvider().deleteServiceTemplate(getPfDao(), name, version);
92 public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
94 return new AuthorativeToscaProvider().getPolicyTypes(getPfDao(), name, version);
98 public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
100 return new AuthorativeToscaProvider().getPolicyTypeList(getPfDao(), name, version);
104 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
105 throws PfModelException {
107 return new AuthorativeToscaProvider().getFilteredPolicyTypes(getPfDao(), filter);
111 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
112 throws PfModelException {
114 return new AuthorativeToscaProvider().getFilteredPolicyTypeList(getPfDao(), filter);
118 public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
119 throws PfModelException {
121 return new AuthorativeToscaProvider().createPolicyTypes(getPfDao(), serviceTemplate);
125 public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
126 throws PfModelException {
128 return new AuthorativeToscaProvider().updatePolicyTypes(getPfDao(), serviceTemplate);
132 public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
133 throws PfModelException {
136 var policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
137 assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
139 return new AuthorativeToscaProvider().deletePolicyType(getPfDao(), name, version);
143 public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
145 return new AuthorativeToscaProvider().getPolicies(getPfDao(), name, version);
149 public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
151 return new AuthorativeToscaProvider().getPolicyList(getPfDao(), name, version);
155 public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
156 throws PfModelException {
158 return new AuthorativeToscaProvider().getFilteredPolicies(getPfDao(), filter);
162 public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
163 throws PfModelException {
165 return new AuthorativeToscaProvider().getFilteredPolicyList(getPfDao(), filter);
169 public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
170 throws PfModelException {
172 return new AuthorativeToscaProvider().createPolicies(getPfDao(), serviceTemplate);
176 public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
177 throws PfModelException {
179 return new AuthorativeToscaProvider().updatePolicies(getPfDao(), serviceTemplate);
183 public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
184 throws PfModelException {
187 var policyIdentifier = new ToscaConceptIdentifier(name, version);
188 assertPolicyNotDeployedInPdpGroup(policyIdentifier);
190 return new AuthorativeToscaProvider().deletePolicy(getPfDao(), name, version);
194 public ToscaServiceTemplate createToscaNodeTemplates(@NonNull final ToscaServiceTemplate serviceTemplate)
195 throws PfModelException {
197 return new AuthorativeToscaProvider().createToscaNodeTemplates(getPfDao(), serviceTemplate);
201 public ToscaServiceTemplate updateToscaNodeTemplates(@NonNull final ToscaServiceTemplate serviceTemplate)
202 throws PfModelRuntimeException, PfModelException {
204 return new AuthorativeToscaProvider().updateToscaNodeTemplates(getPfDao(), serviceTemplate);
208 public ToscaServiceTemplate deleteToscaNodeTemplate(@NonNull final String name, @NonNull final String version)
209 throws PfModelException {
211 return new AuthorativeToscaProvider().deleteToscaNodeTemplate(getPfDao(), name, version);
215 public List<Map<ToscaEntityKey, Map<String, Object>>> getNodeTemplateMetadataSets(final String name,
216 final String version)
217 throws PfModelException {
219 return new AuthorativeToscaProvider().getNodeTemplateMetadataSet(getPfDao(), name, version);
223 public List<ToscaNodeTemplate> getToscaNodeTemplate(final String name, final String version)
224 throws PfModelException {
226 return new AuthorativeToscaProvider().getToscaNodeTemplate(getPfDao(), name, version);
232 public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
234 return new PdpProvider().getPdpGroups(getPfDao(), name);
238 public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
240 return new PdpProvider().getFilteredPdpGroups(getPfDao(), filter);
244 public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
246 return new PdpProvider().createPdpGroups(getPfDao(), pdpGroups);
250 public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
252 return new PdpProvider().updatePdpGroups(getPfDao(), pdpGroups);
256 public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
257 throws PfModelException {
259 new PdpProvider().updatePdpSubGroup(getPfDao(), pdpGroupName, pdpSubGroup);
263 public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
264 throws PfModelException {
265 new PdpProvider().updatePdp(getPfDao(), pdpGroupName, pdpSubGroup, pdp);
269 public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
271 return new PdpProvider().deletePdpGroup(getPfDao(), name);
275 public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
277 return new PdpProvider().getAllPolicyStatus(getPfDao());
281 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
282 throws PfModelException {
284 return new PdpProvider().getAllPolicyStatus(getPfDao(), policy);
288 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
290 return new PdpProvider().getGroupPolicyStatus(getPfDao(), groupName);
294 public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
295 Collection<PdpPolicyStatus> deleteObjs) {
297 new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs);
301 * Check if the model provider is initialized.
303 private void assertInitialized() {
304 if (getPfDao() == null) {
305 var errorMessage = "policy models provider is not initilaized";
306 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
311 * Assert that the policy type is not supported in any PDP group.
313 * @param policyTypeIdentifier the policy type identifier
314 * @throws PfModelException if the policy type is supported in a PDP group
316 private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
317 throws PfModelException {
318 for (PdpGroup pdpGroup : getPdpGroups(null)) {
319 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
320 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
321 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
322 "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
323 + pdpSubGroup.getPdpType());
330 * Assert that the policy is not deployed in a PDP group.
332 * @param policyIdentifier the identifier of the policy
333 * @throws PfModelException thrown if the policy is deployed in a PDP group
335 private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
336 throws PfModelException {
337 for (PdpGroup pdpGroup : getPdpGroups(null)) {
338 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
339 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
340 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
341 "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
342 + pdpSubGroup.getPdpType());