TOSCA Compliant Guard Policies
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderImpl.java
index 1ac2458..195b882 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,10 +24,12 @@ package org.onap.policy.models.provider.impl;
 import java.util.Base64;
 import java.util.Date;
 import java.util.List;
-import java.util.Map;
 import java.util.Properties;
+
 import javax.ws.rs.core.Response;
+
 import lombok.NonNull;
+
 import org.eclipse.persistence.config.PersistenceUnitProperties;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.base.PfModelRuntimeException;
@@ -46,12 +48,12 @@ import org.onap.policy.models.provider.PolicyModelsProvider;
 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
 import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
 import org.slf4j.Logger;
@@ -65,7 +67,7 @@ import org.slf4j.LoggerFactory;
  */
 public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class);
 
     private final PolicyModelsProviderParameters parameters;
 
@@ -98,7 +100,7 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
         daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
 
         // Decode the password using Base64
-        String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
+        String decodedPassword = new String(Base64.getDecoder().decode(getValue(parameters.getDatabasePassword())));
 
         // @formatter:off
         Properties jdbcProperties = new Properties();
@@ -116,13 +118,19 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
         } catch (Exception exc) {
             String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
                     + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
-            LOGGER.warn(errorMessage, exc);
 
             this.close();
             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
         }
     }
 
+    private String getValue(final String value) {
+        if (value != null && value.matches("[$][{].*[}]$")) {
+            return System.getenv(value.substring(2, value.length() - 1));
+        }
+        return value;
+    }
+
     @Override
     public void close() throws PfModelException {
         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
@@ -180,6 +188,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
     public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
             throws PfModelException {
         assertInitialized();
+
+        ToscaPolicyTypeIdentifier policyTypeIdentifier = new ToscaPolicyTypeIdentifier(name, version);
+        assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
+
         return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
     }
 
@@ -207,7 +219,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
         return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter);
     }
 
-
     @Override
     public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
             throws PfModelException {
@@ -226,6 +237,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
     public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
             throws PfModelException {
         assertInitialized();
+
+        ToscaPolicyIdentifier policyIdentifier = new ToscaPolicyIdentifier(name, version);
+        assertPolicyNotDeployedInPdpGroup(policyIdentifier);
+
         return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
     }
 
@@ -254,35 +269,11 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId,
             @NonNull final String policyVersion) throws PfModelException {
         assertInitialized();
-        return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId, policyVersion);
-    }
-
-    @Override
-    public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId,
-            final String policyVersion) throws PfModelException {
-        assertInitialized();
-        return new LegacyProvider().getGuardPolicy(pfDao, policyId, policyVersion);
-    }
 
-    @Override
-    public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
-            @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
-        assertInitialized();
-        return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
-    }
-
-    @Override
-    public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
-            @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
-        assertInitialized();
-        return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
-    }
+        assertPolicyNotDeployedInPdpGroup(
+                new ToscaPolicyIdentifier(policyId, policyVersion + LegacyProvider.LEGACY_MINOR_PATCH_SUFFIX));
 
-    @Override
-    public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId,
-            @NonNull final String policyVersion) throws PfModelException {
-        assertInitialized();
-        return new LegacyProvider().deleteGuardPolicy(pfDao, policyId, policyVersion);
+        return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId, policyVersion);
     }
 
     @Override
@@ -291,7 +282,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
         return new PdpProvider().getPdpGroups(pfDao, name);
     }
 
-
     @Override
     public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
         assertInitialized();
@@ -337,10 +327,11 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
 
     @Override
     public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName,
-            final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp) throws PfModelException {
+            final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp, final String sortOrder,
+            final int getRecordNum) throws PfModelException {
         assertInitialized();
         return new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, name, pdpGroupName, pdpSubGroup,
-                startTimeStamp, endTimeStamp);
+                startTimeStamp, endTimeStamp, sortOrder, getRecordNum);
     }
 
     @Override
@@ -357,7 +348,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
         return new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsList);
     }
 
-
     @Override
     public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Date timestamp)
             throws PfModelException {
@@ -375,4 +365,42 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
         }
     }
+
+    /**
+     * Assert that the policy type is not supported in any PDP group.
+     *
+     * @param policyTypeIdentifier the policy type identifier
+     * @throws PfModelException if the policy type is supported in a PDP group
+     */
+    private void assertPolicyTypeNotSupportedInPdpGroup(ToscaPolicyTypeIdentifier policyTypeIdentifier)
+            throws PfModelException {
+        for (PdpGroup pdpGroup : getPdpGroups(null)) {
+            for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+                if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
+                    throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+                            "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
+                                    + pdpSubGroup.getPdpType());
+                }
+            }
+        }
+    }
+
+    /**
+     * Assert that the policy is not deployed in a PDP group.
+     *
+     * @param policyIdentifier the identifier of the policy
+     * @throws PfModelException thrown if the policy is deployed in a PDP group
+     */
+    private void assertPolicyNotDeployedInPdpGroup(final ToscaPolicyIdentifier policyIdentifier)
+            throws PfModelException {
+        for (PdpGroup pdpGroup : getPdpGroups(null)) {
+            for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+                if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
+                    throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+                            "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
+                                    + pdpSubGroup.getPdpType());
+                }
+            }
+        }
+    }
 }