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.PdpFilterParameters;
 
  42 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
 
  43 import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
 
  44 import org.onap.policy.models.provider.PolicyModelsProvider;
 
  45 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
 
  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.ToscaPolicy;
 
  50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 
  51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  52 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
 
  53 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
 
  56  * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
 
  57  * a relational database.
 
  59  * @author Liam Fallon (liam.fallon@est.tech)
 
  61 public class DatabasePolicyModelsProviderImpl extends AbstractModelsProvider implements PolicyModelsProvider {
 
  63      * Constructor that takes the parameters.
 
  65      * @param parameters the parameters for the provider
 
  67     public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
 
  72     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
 
  73             throws PfModelException {
 
  75         return new AuthorativeToscaProvider().getServiceTemplateList(getPfDao(), name, version);
 
  80     public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
 
  81             @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
 
  83         return new AuthorativeToscaProvider().getFilteredServiceTemplateList(getPfDao(), filter);
 
  87     public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
 
  88             throws PfModelException {
 
  90         return new AuthorativeToscaProvider().createServiceTemplate(getPfDao(), serviceTemplate);
 
  94     public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
 
  95             throws PfModelException {
 
  97         return new AuthorativeToscaProvider().updateServiceTemplate(getPfDao(), serviceTemplate);
 
 101     public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
 
 102             throws PfModelException {
 
 105         return new AuthorativeToscaProvider().deleteServiceTemplate(getPfDao(), name, version);
 
 109     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
 
 111         return new AuthorativeToscaProvider().getPolicyTypes(getPfDao(), name, version);
 
 115     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
 
 117         return new AuthorativeToscaProvider().getPolicyTypeList(getPfDao(), name, version);
 
 121     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
 
 122             throws PfModelException {
 
 124         return new AuthorativeToscaProvider().getFilteredPolicyTypes(getPfDao(), filter);
 
 128     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
 
 129             throws PfModelException {
 
 131         return new AuthorativeToscaProvider().getFilteredPolicyTypeList(getPfDao(), filter);
 
 135     public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
 
 136             throws PfModelException {
 
 138         return new AuthorativeToscaProvider().createPolicyTypes(getPfDao(), serviceTemplate);
 
 142     public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
 
 143             throws PfModelException {
 
 145         return new AuthorativeToscaProvider().updatePolicyTypes(getPfDao(), serviceTemplate);
 
 149     public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
 
 150             throws PfModelException {
 
 153         var policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
 
 154         assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
 
 156         return new AuthorativeToscaProvider().deletePolicyType(getPfDao(), name, version);
 
 160     public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
 
 162         return new AuthorativeToscaProvider().getPolicies(getPfDao(), name, version);
 
 166     public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
 
 168         return new AuthorativeToscaProvider().getPolicyList(getPfDao(), name, version);
 
 172     public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
 
 173             throws PfModelException {
 
 175         return new AuthorativeToscaProvider().getFilteredPolicies(getPfDao(), filter);
 
 179     public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
 
 180             throws PfModelException {
 
 182         return new AuthorativeToscaProvider().getFilteredPolicyList(getPfDao(), filter);
 
 186     public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
 
 187             throws PfModelException {
 
 189         return new AuthorativeToscaProvider().createPolicies(getPfDao(), serviceTemplate);
 
 193     public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
 
 194             throws PfModelException {
 
 196         return new AuthorativeToscaProvider().updatePolicies(getPfDao(), serviceTemplate);
 
 200     public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
 
 201             throws PfModelException {
 
 204         var policyIdentifier = new ToscaConceptIdentifier(name, version);
 
 205         assertPolicyNotDeployedInPdpGroup(policyIdentifier);
 
 207         return new AuthorativeToscaProvider().deletePolicy(getPfDao(), name, version);
 
 211     public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
 
 213         return new PdpProvider().getPdpGroups(getPfDao(), name);
 
 217     public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
 
 219         return new PdpProvider().getFilteredPdpGroups(getPfDao(), filter);
 
 223     public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
 
 225         return new PdpProvider().createPdpGroups(getPfDao(), pdpGroups);
 
 229     public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
 
 231         return new PdpProvider().updatePdpGroups(getPfDao(), pdpGroups);
 
 235     public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
 
 236             throws PfModelException {
 
 238         new PdpProvider().updatePdpSubGroup(getPfDao(), pdpGroupName, pdpSubGroup);
 
 242     public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
 
 243             throws PfModelException {
 
 244         new PdpProvider().updatePdp(getPfDao(), pdpGroupName, pdpSubGroup, pdp);
 
 248     public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
 
 250         return new PdpProvider().deletePdpGroup(getPfDao(), name);
 
 254     public List<PdpStatistics> getPdpStatistics(final String name, final Instant timestamp) throws PfModelException {
 
 256         return new PdpStatisticsProvider().getPdpStatistics(getPfDao(), name, timestamp);
 
 260     public List<PdpStatistics> getFilteredPdpStatistics(PdpFilterParameters filterParams) throws PfModelException {
 
 262         return new PdpStatisticsProvider().getFilteredPdpStatistics(getPfDao(), filterParams);
 
 266     public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
 
 267             throws PfModelException {
 
 269         return new PdpStatisticsProvider().createPdpStatistics(getPfDao(), pdpStatisticsList);
 
 273     public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
 
 274             throws PfModelException {
 
 276         return new PdpStatisticsProvider().updatePdpStatistics(getPfDao(), pdpStatisticsList);
 
 280     public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Instant timestamp)
 
 281             throws PfModelException {
 
 283         return new PdpStatisticsProvider().deletePdpStatistics(getPfDao(), name, timestamp);
 
 287     public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
 
 289         return new PdpProvider().getAllPolicyStatus(getPfDao());
 
 293     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
 
 294             throws PfModelException {
 
 296         return new PdpProvider().getAllPolicyStatus(getPfDao(), policy);
 
 300     public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
 
 302         return new PdpProvider().getGroupPolicyStatus(getPfDao(), groupName);
 
 306     public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
 
 307             Collection<PdpPolicyStatus> deleteObjs) {
 
 309         new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs);
 
 313     public void createAuditRecords(List<PolicyAudit> auditRecords) {
 
 315         new PolicyAuditProvider().createAuditRecords(getPfDao(), auditRecords);
 
 319     public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter) {
 
 321         return new PolicyAuditProvider().getAuditRecords(getPfDao(), auditFilter);
 
 325      * Check if the model provider is initialized.
 
 327     private void assertInitialized() {
 
 328         if (getPfDao() == null) {
 
 329             var errorMessage = "policy models provider is not initilaized";
 
 330             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
 
 335      * Assert that the policy type is not supported in any PDP group.
 
 337      * @param policyTypeIdentifier the policy type identifier
 
 338      * @throws PfModelException if the policy type is supported in a PDP group
 
 340     private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
 
 341             throws PfModelException {
 
 342         for (PdpGroup pdpGroup : getPdpGroups(null)) {
 
 343             for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
 
 344                 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
 
 345                     throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
 
 346                             "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
 
 347                                     + pdpSubGroup.getPdpType());
 
 354      * Assert that the policy is not deployed in a PDP group.
 
 356      * @param policyIdentifier the identifier of the policy
 
 357      * @throws PfModelException thrown if the policy is deployed in a PDP group
 
 359     private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
 
 360             throws PfModelException {
 
 361         for (PdpGroup pdpGroup : getPdpGroups(null)) {
 
 362             for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
 
 363                 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
 
 364                     throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
 
 365                             "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
 
 366                                     + pdpSubGroup.getPdpType());